Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: src/compiler/machine-operator-reducer.cc

Issue 649083005: [turbofan] Optimize Int32Mod by power-of-two. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/compiler/machine-operator-reducer.h ('k') | test/mjsunit/asm/int32-tmod.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/compiler/machine-operator-reducer.h" 5 #include "src/compiler/machine-operator-reducer.h"
6 6
7 #include "src/base/bits.h" 7 #include "src/base/bits.h"
8 #include "src/codegen.h" 8 #include "src/codegen.h"
9 #include "src/compiler/generic-node-inl.h" 9 #include "src/compiler/generic-node-inl.h"
10 #include "src/compiler/graph.h" 10 #include "src/compiler/graph.h"
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 if (m.IsFoldable() && !m.right().Is(0)) { // K / K => K 255 if (m.IsFoldable() && !m.right().Is(0)) { // K / K => K
256 return ReplaceInt32(m.left().Value() / m.right().Value()); 256 return ReplaceInt32(m.left().Value() / m.right().Value());
257 } 257 }
258 if (m.right().IsPowerOf2()) { // x / 2^n => x >> n 258 if (m.right().IsPowerOf2()) { // x / 2^n => x >> n
259 node->set_op(machine()->Word32Shr()); 259 node->set_op(machine()->Word32Shr());
260 node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value()))); 260 node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value())));
261 return Changed(node); 261 return Changed(node);
262 } 262 }
263 break; 263 break;
264 } 264 }
265 case IrOpcode::kInt32Mod: { 265 case IrOpcode::kInt32Mod:
266 Int32BinopMatcher m(node); 266 return ReduceInt32Mod(node);
267 if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0
268 if (m.right().Is(-1)) return ReplaceInt32(0); // x % -1 => 0
269 // TODO(turbofan): if (m.left().Is(0))
270 // TODO(turbofan): if (m.right().IsPowerOf2())
271 // TODO(turbofan): if (m.right().Is(0))
272 // TODO(turbofan): if (m.LeftEqualsRight())
273 if (m.IsFoldable() && !m.right().Is(0)) { // K % K => K
274 return ReplaceInt32(m.left().Value() % m.right().Value());
275 }
276 break;
277 }
278 case IrOpcode::kUint32Mod: { 267 case IrOpcode::kUint32Mod: {
279 Uint32BinopMatcher m(node); 268 Uint32BinopMatcher m(node);
280 if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0 269 if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0
281 // TODO(turbofan): if (m.left().Is(0)) 270 // TODO(turbofan): if (m.left().Is(0))
282 // TODO(turbofan): if (m.right().Is(0)) 271 // TODO(turbofan): if (m.right().Is(0))
283 // TODO(turbofan): if (m.LeftEqualsRight()) 272 // TODO(turbofan): if (m.LeftEqualsRight())
284 if (m.IsFoldable() && !m.right().Is(0)) { // K % K => K 273 if (m.IsFoldable() && !m.right().Is(0)) { // K % K => K
285 return ReplaceInt32(m.left().Value() % m.right().Value()); 274 return ReplaceInt32(m.left().Value() % m.right().Value());
286 } 275 }
287 if (m.right().IsPowerOf2()) { // x % 2^n => x & 2^n-1 276 if (m.right().IsPowerOf2()) { // x % 2^n => x & 2^n-1
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 } 492 }
504 break; 493 break;
505 } 494 }
506 default: 495 default:
507 break; 496 break;
508 } 497 }
509 return NoChange(); 498 return NoChange();
510 } 499 }
511 500
512 501
502 Reduction MachineOperatorReducer::ReduceInt32Mod(Node* const node) {
503 Int32BinopMatcher m(node);
504 if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0
505 if (m.right().Is(-1)) return ReplaceInt32(0); // x % -1 => 0
506 // TODO(turbofan): if (m.left().Is(0))
507 // TODO(turbofan): if (m.right().Is(0))
508 // TODO(turbofan): if (m.LeftEqualsRight())
509 if (m.IsFoldable() && !m.right().Is(0)) { // K % K => K
510 return ReplaceInt32(m.left().Value() % m.right().Value());
511 }
512 if (m.right().IsPowerOf2()) {
513 int32_t const divisor = m.right().Value();
514 Node* zero = Int32Constant(0);
515 Node* mask = Int32Constant(divisor - 1);
516 Node* dividend = m.left().node();
517
518 Node* check = graph()->NewNode(machine()->Int32LessThan(), dividend, zero);
519 Node* branch =
520 graph()->NewNode(common()->Branch(), check, graph()->start());
521
522 Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
523 Node* neg = graph()->NewNode(
524 machine()->Int32Sub(), zero,
525 graph()->NewNode(
526 machine()->Word32And(),
527 graph()->NewNode(machine()->Int32Sub(), zero, dividend), mask));
528
529 Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
530 Node* pos = graph()->NewNode(machine()->Word32And(), dividend, mask);
531
532 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
533 Node* phi = graph()->NewNode(common()->Phi(kMachInt32, 2), neg, pos, merge);
534 return Replace(phi);
535 }
536 return NoChange();
537 }
538
539
513 Reduction MachineOperatorReducer::ReduceProjection(size_t index, Node* node) { 540 Reduction MachineOperatorReducer::ReduceProjection(size_t index, Node* node) {
514 switch (node->opcode()) { 541 switch (node->opcode()) {
515 case IrOpcode::kInt32AddWithOverflow: { 542 case IrOpcode::kInt32AddWithOverflow: {
516 DCHECK(index == 0 || index == 1); 543 DCHECK(index == 0 || index == 1);
517 Int32BinopMatcher m(node); 544 Int32BinopMatcher m(node);
518 if (m.IsFoldable()) { 545 if (m.IsFoldable()) {
519 int32_t val; 546 int32_t val;
520 bool ovf = base::bits::SignedAddOverflow32(m.left().Value(), 547 bool ovf = base::bits::SignedAddOverflow32(m.left().Value(),
521 m.right().Value(), &val); 548 m.right().Value(), &val);
522 return ReplaceInt32((index == 0) ? val : ovf); 549 return ReplaceInt32((index == 0) ? val : ovf);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 MachineOperatorBuilder* MachineOperatorReducer::machine() const { 582 MachineOperatorBuilder* MachineOperatorReducer::machine() const {
556 return jsgraph()->machine(); 583 return jsgraph()->machine();
557 } 584 }
558 585
559 586
560 Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); } 587 Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); }
561 588
562 } // namespace compiler 589 } // namespace compiler
563 } // namespace internal 590 } // namespace internal
564 } // namespace v8 591 } // namespace v8
OLDNEW
« no previous file with comments | « src/compiler/machine-operator-reducer.h ('k') | test/mjsunit/asm/int32-tmod.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698