OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1027 DeoptimizeIf(mi, instr->environment()); | 1027 DeoptimizeIf(mi, instr->environment()); |
1028 __ bind(&ok); | 1028 __ bind(&ok); |
1029 // Load the result and we are done. | 1029 // Load the result and we are done. |
1030 __ mov(result, scratch2); | 1030 __ mov(result, scratch2); |
1031 } | 1031 } |
1032 | 1032 |
1033 __ bind(&done); | 1033 __ bind(&done); |
1034 } | 1034 } |
1035 | 1035 |
1036 | 1036 |
| 1037 void LCodeGen::EmitSignedIntegerDivisionByConstant( |
| 1038 Register result, |
| 1039 Register dividend, |
| 1040 int32_t divisor, |
| 1041 Register remainder, |
| 1042 Register scratch, |
| 1043 LEnvironment* environment) { |
| 1044 ASSERT(!AreAliased(dividend, scratch, ip)); |
| 1045 ASSERT(LChunkBuilder::HasMagicNumberForDivisor(divisor)); |
| 1046 |
| 1047 uint32_t divisor_abs = abs(divisor); |
| 1048 |
| 1049 int32_t power_of_2_factor = |
| 1050 CompilerIntrinsics::CountTrailingZeros(divisor_abs); |
| 1051 |
| 1052 switch (divisor_abs) { |
| 1053 case 0: |
| 1054 DeoptimizeIf(al, environment); |
| 1055 return; |
| 1056 |
| 1057 case 1: |
| 1058 if (divisor > 0) { |
| 1059 __ Move(result, dividend); |
| 1060 } else { |
| 1061 __ rsb(result, dividend, Operand(0)); |
| 1062 } |
| 1063 // Compute the remainder. |
| 1064 __ mov(remainder, Operand(0)); |
| 1065 return; |
| 1066 |
| 1067 default: |
| 1068 if (IsPowerOf2(divisor_abs)) { |
| 1069 // Branch and condition free code for integer division by a power |
| 1070 // of two. |
| 1071 int32_t power = WhichPowerOf2(divisor_abs); |
| 1072 if (power > 1) { |
| 1073 __ mov(scratch, Operand(dividend, ASR, power - 1)); |
| 1074 } |
| 1075 __ add(scratch, dividend, Operand(scratch, LSR, 32 - power)); |
| 1076 __ mov(result, Operand(scratch, ASR, power)); |
| 1077 // Negate if necessary. |
| 1078 // We don't need to check for overflow because the case '-1' is |
| 1079 // handled separately. |
| 1080 if (divisor < 0) { |
| 1081 ASSERT(divisor != -1); |
| 1082 __ rsb(result, result, Operand(0)); |
| 1083 } |
| 1084 // Compute the remainder. |
| 1085 if (divisor > 0) { |
| 1086 __ sub(remainder, dividend, Operand(result, LSL, power)); |
| 1087 } else { |
| 1088 __ add(remainder, dividend, Operand(result, LSL, power)); |
| 1089 } |
| 1090 return; |
| 1091 } else { |
| 1092 // Use magic numbers for a few specific divisors. |
| 1093 // Details and proofs can be found in: |
| 1094 // - Hacker's Delight, Henry S. Warren, Jr. |
| 1095 // - The PowerPC Compiler Writer’s Guide |
| 1096 // and probably many others. |
| 1097 // |
| 1098 // We handle |
| 1099 // <divisor with magic numbers> * <power of 2> |
| 1100 // but not |
| 1101 // <divisor with magic numbers> * <other divisor with magic numbers> |
| 1102 DivMagicNumbers magic_numbers = |
| 1103 DivMagicNumberFor(divisor_abs >> power_of_2_factor); |
| 1104 // Branch and condition free code for integer division by a power |
| 1105 // of two. |
| 1106 const int32_t M = magic_numbers.M; |
| 1107 const int32_t s = magic_numbers.s + power_of_2_factor; |
| 1108 |
| 1109 __ mov(ip, Operand(M)); |
| 1110 __ smull(ip, scratch, dividend, ip); |
| 1111 if (M < 0) { |
| 1112 __ add(scratch, scratch, Operand(dividend)); |
| 1113 } |
| 1114 if (s > 0) { |
| 1115 __ mov(scratch, Operand(scratch, ASR, s)); |
| 1116 } |
| 1117 __ add(result, scratch, Operand(dividend, LSR, 31)); |
| 1118 if (divisor < 0) __ rsb(result, result, Operand(0)); |
| 1119 // Compute the remainder. |
| 1120 __ mov(ip, Operand(divisor)); |
| 1121 // This sequence could be replaced with 'mls' when |
| 1122 // it gets implemented. |
| 1123 __ mul(scratch, result, ip); |
| 1124 __ sub(remainder, dividend, scratch); |
| 1125 } |
| 1126 } |
| 1127 } |
| 1128 |
| 1129 |
1037 void LCodeGen::DoDivI(LDivI* instr) { | 1130 void LCodeGen::DoDivI(LDivI* instr) { |
1038 class DeferredDivI: public LDeferredCode { | 1131 class DeferredDivI: public LDeferredCode { |
1039 public: | 1132 public: |
1040 DeferredDivI(LCodeGen* codegen, LDivI* instr) | 1133 DeferredDivI(LCodeGen* codegen, LDivI* instr) |
1041 : LDeferredCode(codegen), instr_(instr) { } | 1134 : LDeferredCode(codegen), instr_(instr) { } |
1042 virtual void Generate() { | 1135 virtual void Generate() { |
1043 codegen()->DoDeferredBinaryOpStub(instr_, Token::DIV); | 1136 codegen()->DoDeferredBinaryOpStub(instr_, Token::DIV); |
1044 } | 1137 } |
1045 virtual LInstruction* instr() { return instr_; } | 1138 virtual LInstruction* instr() { return instr_; } |
1046 private: | 1139 private: |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1108 __ JumpIfNotSmi(result, &deoptimize); | 1201 __ JumpIfNotSmi(result, &deoptimize); |
1109 __ SmiUntag(result); | 1202 __ SmiUntag(result); |
1110 __ b(&done); | 1203 __ b(&done); |
1111 | 1204 |
1112 __ bind(&deoptimize); | 1205 __ bind(&deoptimize); |
1113 DeoptimizeIf(al, instr->environment()); | 1206 DeoptimizeIf(al, instr->environment()); |
1114 __ bind(&done); | 1207 __ bind(&done); |
1115 } | 1208 } |
1116 | 1209 |
1117 | 1210 |
| 1211 void LCodeGen::DoMathFloorOfDiv(LMathFloorOfDiv* instr) { |
| 1212 const Register result = ToRegister(instr->result()); |
| 1213 const Register left = ToRegister(instr->InputAt(0)); |
| 1214 const Register remainder = ToRegister(instr->TempAt(0)); |
| 1215 const Register scratch = scratch0(); |
| 1216 |
| 1217 // We only optimize this for division by constants, because the standard |
| 1218 // integer division routine is usually slower than transitionning to VFP. |
| 1219 // This could be optimized on processors with SDIV available. |
| 1220 ASSERT(instr->InputAt(1)->IsConstantOperand()); |
| 1221 int32_t divisor = ToInteger32(LConstantOperand::cast(instr->InputAt(1))); |
| 1222 EmitSignedIntegerDivisionByConstant(result, |
| 1223 left, |
| 1224 divisor, |
| 1225 remainder, |
| 1226 scratch, |
| 1227 instr->environment()); |
| 1228 // We operated a truncating division. Correct the result if necessary. |
| 1229 __ cmp(remainder, Operand(0)); |
| 1230 __ teq(remainder, Operand(divisor), ne); |
| 1231 __ sub(result, result, Operand(1), LeaveCC, mi); |
| 1232 } |
| 1233 |
| 1234 |
1118 template<int T> | 1235 template<int T> |
1119 void LCodeGen::DoDeferredBinaryOpStub(LTemplateInstruction<1, 2, T>* instr, | 1236 void LCodeGen::DoDeferredBinaryOpStub(LTemplateInstruction<1, 2, T>* instr, |
1120 Token::Value op) { | 1237 Token::Value op) { |
1121 Register left = ToRegister(instr->InputAt(0)); | 1238 Register left = ToRegister(instr->InputAt(0)); |
1122 Register right = ToRegister(instr->InputAt(1)); | 1239 Register right = ToRegister(instr->InputAt(1)); |
1123 | 1240 |
1124 PushSafepointRegistersScope scope(this, Safepoint::kWithRegistersAndDoubles); | 1241 PushSafepointRegistersScope scope(this, Safepoint::kWithRegistersAndDoubles); |
1125 // Move left to r1 and right to r0 for the stub call. | 1242 // Move left to r1 and right to r0 for the stub call. |
1126 if (left.is(r1)) { | 1243 if (left.is(r1)) { |
1127 __ Move(r0, right); | 1244 __ Move(r0, right); |
(...skipping 4009 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5137 __ sub(scratch, result, Operand(index, LSL, kPointerSizeLog2 - kSmiTagSize)); | 5254 __ sub(scratch, result, Operand(index, LSL, kPointerSizeLog2 - kSmiTagSize)); |
5138 __ ldr(result, FieldMemOperand(scratch, | 5255 __ ldr(result, FieldMemOperand(scratch, |
5139 FixedArray::kHeaderSize - kPointerSize)); | 5256 FixedArray::kHeaderSize - kPointerSize)); |
5140 __ bind(&done); | 5257 __ bind(&done); |
5141 } | 5258 } |
5142 | 5259 |
5143 | 5260 |
5144 #undef __ | 5261 #undef __ |
5145 | 5262 |
5146 } } // namespace v8::internal | 5263 } } // namespace v8::internal |
OLD | NEW |