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

Unified Diff: runtime/vm/intrinsifier_x64.cc

Issue 12545067: On x64 use 32bit idiv when possible instead of 64bit one. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rename movsxl to movsxd Created 7 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/intermediate_language_x64.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/intrinsifier_x64.cc
diff --git a/runtime/vm/intrinsifier_x64.cc b/runtime/vm/intrinsifier_x64.cc
index 3119fdb7594390d8729ab5b641aeaa325ab02eb4..b6f446f9ad065e3d9bfc7e8c86829c6d675e60d6 100644
--- a/runtime/vm/intrinsifier_x64.cc
+++ b/runtime/vm/intrinsifier_x64.cc
@@ -721,7 +721,7 @@ bool Intrinsifier::Integer_mul(Assembler* assembler) {
bool Intrinsifier::Integer_modulo(Assembler* assembler) {
- Label fall_through, return_zero, try_modulo;
+ Label fall_through, return_zero, try_modulo, not_32bit;
TestBothArgumentsSmis(assembler, &fall_through);
// RAX: right argument (divisor)
// Check if modulo by zero -> exception thrown in main function.
@@ -735,35 +735,84 @@ bool Intrinsifier::Integer_modulo(Assembler* assembler) {
__ j(GREATER, &try_modulo, Assembler::kNearJump);
__ movq(RAX, RCX); // Return dividend as it is smaller than divisor.
__ ret();
+
__ Bind(&return_zero);
__ xorq(RAX, RAX); // Return zero.
__ ret();
+
__ Bind(&try_modulo);
// RAX: right (non-null divisor).
__ movq(RCX, RAX);
- __ SmiUntag(RCX);
__ movq(RAX, Address(RSP, + 2 * kWordSize)); // Left argument (dividend).
+
+ // Check if both operands fit into 32bits as idiv with 64bit operands
+ // requires twice as many cycles and has much higher latency. We are checking
+ // this before untagging them to avoid corner case dividing INT_MAX by -1 that
+ // raises exception because quotient is too large for 32bit register.
+ __ movsxd(RBX, RAX);
+ __ cmpq(RBX, RAX);
+ __ j(NOT_EQUAL, &not_32bit);
+ __ movsxd(RBX, RCX);
+ __ cmpq(RBX, RCX);
+ __ j(NOT_EQUAL, &not_32bit);
+
+ // Both operands are 31bit smis. Divide using 32bit idiv.
+ __ SmiUntag(RAX);
+ __ SmiUntag(RCX);
+ __ cdq();
+ __ idivl(RCX);
+ __ movsxd(RAX, RDX);
+ __ SmiTag(RAX);
+ __ ret();
+
+ // Divide using 64bit idiv.
+ __ Bind(&not_32bit);
__ SmiUntag(RAX);
+ __ SmiUntag(RCX);
__ cqo();
__ idivq(RCX);
__ movq(RAX, RDX);
__ SmiTag(RAX);
__ ret();
+
__ Bind(&fall_through);
return false;
}
bool Intrinsifier::Integer_truncDivide(Assembler* assembler) {
- Label fall_through;
+ Label fall_through, not_32bit;
TestBothArgumentsSmis(assembler, &fall_through);
// RAX: right argument (divisor)
__ cmpq(RAX, Immediate(0));
__ j(EQUAL, &fall_through, Assembler::kNearJump);
__ movq(RCX, RAX);
- __ SmiUntag(RCX);
__ movq(RAX, Address(RSP, + 2 * kWordSize)); // Left argument (dividend).
+
+ // Check if both operands fit into 32bits as idiv with 64bit operands
+ // requires twice as many cycles and has much higher latency. We are checking
+ // this before untagging them to avoid corner case dividing INT_MAX by -1 that
+ // raises exception because quotient is too large for 32bit register.
+ __ movsxd(RBX, RAX);
+ __ cmpq(RBX, RAX);
+ __ j(NOT_EQUAL, &not_32bit);
+ __ movsxd(RBX, RCX);
+ __ cmpq(RBX, RCX);
+ __ j(NOT_EQUAL, &not_32bit);
+
+ // Both operands are 31bit smis. Divide using 32bit idiv.
__ SmiUntag(RAX);
+ __ SmiUntag(RCX);
+ __ cdq();
+ __ idivl(RCX);
+ __ movsxd(RAX, RAX);
+ __ SmiTag(RAX); // Result is guaranteed to fit into a smi.
+ __ ret();
+
+ // Divide using 64bit idiv.
+ __ Bind(&not_32bit);
+ __ SmiUntag(RAX);
+ __ SmiUntag(RCX);
__ pushq(RDX); // Preserve RDX in case of 'fall_through'.
__ cqo();
__ idivq(RCX);
« no previous file with comments | « runtime/vm/intermediate_language_x64.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698