| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved.7 | 1 // Copyright 2012 the V8 project authors. All rights reserved.7 |
| 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 1114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1125 __ Branch(USE_DELAY_SLOT, &done, ge, left_reg, Operand(zero_reg)); | 1125 __ Branch(USE_DELAY_SLOT, &done, ge, left_reg, Operand(zero_reg)); |
| 1126 __ mfhi(result_reg); | 1126 __ mfhi(result_reg); |
| 1127 | 1127 |
| 1128 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { | 1128 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1129 DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg)); | 1129 DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg)); |
| 1130 } | 1130 } |
| 1131 __ bind(&done); | 1131 __ bind(&done); |
| 1132 } | 1132 } |
| 1133 | 1133 |
| 1134 | 1134 |
| 1135 void LCodeGen::EmitSignedIntegerDivisionByConstant( | |
| 1136 Register result, | |
| 1137 Register dividend, | |
| 1138 int32_t divisor, | |
| 1139 Register remainder, | |
| 1140 Register scratch, | |
| 1141 LEnvironment* environment) { | |
| 1142 ASSERT(!AreAliased(dividend, scratch, at, no_reg)); | |
| 1143 | |
| 1144 uint32_t divisor_abs = abs(divisor); | |
| 1145 | |
| 1146 int32_t power_of_2_factor = | |
| 1147 CompilerIntrinsics::CountTrailingZeros(divisor_abs); | |
| 1148 | |
| 1149 switch (divisor_abs) { | |
| 1150 case 0: | |
| 1151 DeoptimizeIf(al, environment); | |
| 1152 return; | |
| 1153 | |
| 1154 case 1: | |
| 1155 if (divisor > 0) { | |
| 1156 __ Move(result, dividend); | |
| 1157 } else { | |
| 1158 __ SubuAndCheckForOverflow(result, zero_reg, dividend, scratch); | |
| 1159 DeoptimizeIf(lt, environment, scratch, Operand(zero_reg)); | |
| 1160 } | |
| 1161 // Compute the remainder. | |
| 1162 __ Move(remainder, zero_reg); | |
| 1163 return; | |
| 1164 | |
| 1165 default: | |
| 1166 if (IsPowerOf2(divisor_abs)) { | |
| 1167 // Branch and condition free code for integer division by a power | |
| 1168 // of two. | |
| 1169 int32_t power = WhichPowerOf2(divisor_abs); | |
| 1170 if (power > 1) { | |
| 1171 __ sra(scratch, dividend, power - 1); | |
| 1172 } | |
| 1173 __ srl(scratch, scratch, 32 - power); | |
| 1174 __ Addu(scratch, dividend, Operand(scratch)); | |
| 1175 __ sra(result, scratch, power); | |
| 1176 // Negate if necessary. | |
| 1177 // We don't need to check for overflow because the case '-1' is | |
| 1178 // handled separately. | |
| 1179 if (divisor < 0) { | |
| 1180 ASSERT(divisor != -1); | |
| 1181 __ Subu(result, zero_reg, Operand(result)); | |
| 1182 } | |
| 1183 // Compute the remainder. | |
| 1184 if (divisor > 0) { | |
| 1185 __ sll(scratch, result, power); | |
| 1186 __ Subu(remainder, dividend, Operand(scratch)); | |
| 1187 } else { | |
| 1188 __ sll(scratch, result, power); | |
| 1189 __ Addu(remainder, dividend, Operand(scratch)); | |
| 1190 } | |
| 1191 return; | |
| 1192 } else if (LChunkBuilder::HasMagicNumberForDivisor(divisor)) { | |
| 1193 // Use magic numbers for a few specific divisors. | |
| 1194 // Details and proofs can be found in: | |
| 1195 // - Hacker's Delight, Henry S. Warren, Jr. | |
| 1196 // - The PowerPC Compiler Writer's Guide | |
| 1197 // and probably many others. | |
| 1198 // | |
| 1199 // We handle | |
| 1200 // <divisor with magic numbers> * <power of 2> | |
| 1201 // but not | |
| 1202 // <divisor with magic numbers> * <other divisor with magic numbers> | |
| 1203 DivMagicNumbers magic_numbers = | |
| 1204 DivMagicNumberFor(divisor_abs >> power_of_2_factor); | |
| 1205 // Branch and condition free code for integer division by a power | |
| 1206 // of two. | |
| 1207 const int32_t M = magic_numbers.M; | |
| 1208 const int32_t s = magic_numbers.s + power_of_2_factor; | |
| 1209 | |
| 1210 __ li(scratch, Operand(M)); | |
| 1211 __ mult(dividend, scratch); | |
| 1212 __ mfhi(scratch); | |
| 1213 if (M < 0) { | |
| 1214 __ Addu(scratch, scratch, Operand(dividend)); | |
| 1215 } | |
| 1216 if (s > 0) { | |
| 1217 __ sra(scratch, scratch, s); | |
| 1218 __ mov(scratch, scratch); | |
| 1219 } | |
| 1220 __ srl(at, dividend, 31); | |
| 1221 __ Addu(result, scratch, Operand(at)); | |
| 1222 if (divisor < 0) __ Subu(result, zero_reg, Operand(result)); | |
| 1223 // Compute the remainder. | |
| 1224 __ li(scratch, Operand(divisor)); | |
| 1225 __ Mul(scratch, result, Operand(scratch)); | |
| 1226 __ Subu(remainder, dividend, Operand(scratch)); | |
| 1227 } else { | |
| 1228 __ li(scratch, Operand(divisor)); | |
| 1229 __ div(dividend, scratch); | |
| 1230 __ mfhi(remainder); | |
| 1231 __ mflo(result); | |
| 1232 } | |
| 1233 } | |
| 1234 } | |
| 1235 | |
| 1236 | |
| 1237 void LCodeGen::DoDivI(LDivI* instr) { | 1135 void LCodeGen::DoDivI(LDivI* instr) { |
| 1238 const Register left = ToRegister(instr->left()); | 1136 const Register left = ToRegister(instr->left()); |
| 1239 const Register right = ToRegister(instr->right()); | 1137 const Register right = ToRegister(instr->right()); |
| 1240 const Register result = ToRegister(instr->result()); | 1138 const Register result = ToRegister(instr->result()); |
| 1241 | 1139 |
| 1242 // On MIPS div is asynchronous - it will run in the background while we | 1140 // On MIPS div is asynchronous - it will run in the background while we |
| 1243 // check for special cases. | 1141 // check for special cases. |
| 1244 __ div(left, right); | 1142 __ div(left, right); |
| 1245 | 1143 |
| 1246 // Check for x / 0. | 1144 // Check for x / 0. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1277 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier()); | 1175 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier()); |
| 1278 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand()); | 1176 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand()); |
| 1279 | 1177 |
| 1280 // This is computed in-place. | 1178 // This is computed in-place. |
| 1281 ASSERT(addend.is(ToDoubleRegister(instr->result()))); | 1179 ASSERT(addend.is(ToDoubleRegister(instr->result()))); |
| 1282 | 1180 |
| 1283 __ madd_d(addend, addend, multiplier, multiplicand); | 1181 __ madd_d(addend, addend, multiplier, multiplicand); |
| 1284 } | 1182 } |
| 1285 | 1183 |
| 1286 | 1184 |
| 1185 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) { |
| 1186 Register dividend = ToRegister(instr->dividend()); |
| 1187 int32_t divisor = instr->divisor(); |
| 1188 ASSERT(dividend.is(ToRegister(instr->result()))); |
| 1189 Register scratch = scratch0(); |
| 1190 |
| 1191 // If the divisor is positive, things are easy: There can be no deopts and we |
| 1192 // can simply do an arithmetic right shift. |
| 1193 if (divisor == 1) return; |
| 1194 uint16_t shift = WhichPowerOf2Abs(divisor); |
| 1195 if (divisor > 1) { |
| 1196 __ sra(dividend, dividend, shift); |
| 1197 return; |
| 1198 } |
| 1199 |
| 1200 // If the divisor is negative, we have to negate and handle edge cases. |
| 1201 Label not_kmin_int, done; |
| 1202 __ Subu(scratch, zero_reg, dividend); |
| 1203 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1204 DeoptimizeIf(eq, instr->environment(), scratch, Operand(zero_reg)); |
| 1205 } |
| 1206 if (instr->hydrogen()->left()->RangeCanInclude(kMinInt)) { |
| 1207 // Note that we could emit branch-free code, but that would need one more |
| 1208 // register. |
| 1209 __ Branch(¬_kmin_int, ne, dividend, Operand(kMinInt)); |
| 1210 if (divisor == -1) { |
| 1211 DeoptimizeIf(al, instr->environment()); |
| 1212 } else { |
| 1213 __ li(dividend, Operand(kMinInt / divisor)); |
| 1214 __ Branch(&done); |
| 1215 } |
| 1216 } |
| 1217 __ bind(¬_kmin_int); |
| 1218 __ sra(dividend, scratch, shift); |
| 1219 __ bind(&done); |
| 1220 } |
| 1221 |
| 1222 |
| 1287 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) { | 1223 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) { |
| 1288 Register left = ToRegister(instr->dividend()); | 1224 Register dividend = ToRegister(instr->dividend()); |
| 1289 Register remainder = ToRegister(instr->temp()); | 1225 int32_t divisor = instr->divisor(); |
| 1290 Register scratch = scratch0(); | |
| 1291 Register result = ToRegister(instr->result()); | 1226 Register result = ToRegister(instr->result()); |
| 1227 ASSERT(!dividend.is(result)); |
| 1292 | 1228 |
| 1293 ASSERT(instr->divisor()->IsConstantOperand()); | 1229 if (divisor == 0) { |
| 1294 Label done; | 1230 DeoptimizeIf(al, instr->environment()); |
| 1295 int32_t divisor = ToInteger32(LConstantOperand::cast(instr->divisor())); | 1231 return; |
| 1296 if (divisor < 0) { | |
| 1297 DeoptimizeIf(eq, instr->environment(), left, Operand(zero_reg)); | |
| 1298 } | 1232 } |
| 1299 EmitSignedIntegerDivisionByConstant(result, | 1233 |
| 1300 left, | 1234 // Check for (0 / -x) that will produce negative zero. |
| 1301 divisor, | 1235 HMathFloorOfDiv* hdiv = instr->hydrogen(); |
| 1302 remainder, | 1236 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && |
| 1303 scratch, | 1237 hdiv->left()->RangeCanInclude(0) && divisor < 0) { |
| 1304 instr->environment()); | 1238 DeoptimizeIf(eq, instr->environment(), dividend, Operand(zero_reg)); |
| 1305 // We performed a truncating division. Correct the result if necessary. | 1239 } |
| 1306 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT); | 1240 |
| 1307 __ Xor(scratch , remainder, Operand(divisor)); | 1241 __ FlooringDiv(result, dividend, divisor); |
| 1308 __ Branch(&done, ge, scratch, Operand(zero_reg)); | |
| 1309 __ Subu(result, result, Operand(1)); | |
| 1310 __ bind(&done); | |
| 1311 } | 1242 } |
| 1312 | 1243 |
| 1313 | 1244 |
| 1314 void LCodeGen::DoMathFloorOfDiv(LMathFloorOfDiv* instr) { | 1245 void LCodeGen::DoMathFloorOfDiv(LMathFloorOfDiv* instr) { |
| 1315 const Register result = ToRegister(instr->result()); | 1246 const Register result = ToRegister(instr->result()); |
| 1316 const Register left = ToRegister(instr->left()); | 1247 const Register left = ToRegister(instr->left()); |
| 1317 const Register remainder = ToRegister(instr->temp()); | 1248 const Register remainder = ToRegister(instr->temp()); |
| 1318 const Register scratch = scratch0(); | 1249 const Register scratch = scratch0(); |
| 1319 | 1250 |
| 1320 Label done; | 1251 Label done; |
| (...skipping 4464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5785 __ Subu(scratch, result, scratch); | 5716 __ Subu(scratch, result, scratch); |
| 5786 __ lw(result, FieldMemOperand(scratch, | 5717 __ lw(result, FieldMemOperand(scratch, |
| 5787 FixedArray::kHeaderSize - kPointerSize)); | 5718 FixedArray::kHeaderSize - kPointerSize)); |
| 5788 __ bind(&done); | 5719 __ bind(&done); |
| 5789 } | 5720 } |
| 5790 | 5721 |
| 5791 | 5722 |
| 5792 #undef __ | 5723 #undef __ |
| 5793 | 5724 |
| 5794 } } // namespace v8::internal | 5725 } } // namespace v8::internal |
| OLD | NEW |