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 1158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1169 __ mfhi(result_reg); | 1169 __ mfhi(result_reg); |
1170 | 1170 |
1171 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { | 1171 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) { |
1172 DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg)); | 1172 DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg)); |
1173 } | 1173 } |
1174 __ bind(&done); | 1174 __ bind(&done); |
1175 } | 1175 } |
1176 } | 1176 } |
1177 | 1177 |
1178 | 1178 |
| 1179 void LCodeGen::EmitSignedIntegerDivisionByConstant( |
| 1180 Register result, |
| 1181 Register dividend, |
| 1182 int32_t divisor, |
| 1183 Register remainder, |
| 1184 Register scratch, |
| 1185 LEnvironment* environment) { |
| 1186 ASSERT(!AreAliased(dividend, scratch, at, no_reg)); |
| 1187 ASSERT(LChunkBuilder::HasMagicNumberForDivisor(divisor)); |
| 1188 |
| 1189 uint32_t divisor_abs = abs(divisor); |
| 1190 |
| 1191 int32_t power_of_2_factor = |
| 1192 CompilerIntrinsics::CountTrailingZeros(divisor_abs); |
| 1193 |
| 1194 switch (divisor_abs) { |
| 1195 case 0: |
| 1196 DeoptimizeIf(al, environment); |
| 1197 return; |
| 1198 |
| 1199 case 1: |
| 1200 if (divisor > 0) { |
| 1201 __ Move(result, dividend); |
| 1202 } else { |
| 1203 __ SubuAndCheckForOverflow(result, zero_reg, dividend, scratch); |
| 1204 DeoptimizeIf(lt, environment, scratch, Operand(zero_reg)); |
| 1205 } |
| 1206 // Compute the remainder. |
| 1207 __ Move(remainder, zero_reg); |
| 1208 return; |
| 1209 |
| 1210 default: |
| 1211 if (IsPowerOf2(divisor_abs)) { |
| 1212 // Branch and condition free code for integer division by a power |
| 1213 // of two. |
| 1214 int32_t power = WhichPowerOf2(divisor_abs); |
| 1215 if (power > 1) { |
| 1216 __ sra(scratch, dividend, power - 1); |
| 1217 } |
| 1218 __ srl(scratch, scratch, 32 - power); |
| 1219 __ Addu(scratch, dividend, Operand(scratch)); |
| 1220 __ sra(result, scratch, power); |
| 1221 // Negate if necessary. |
| 1222 // We don't need to check for overflow because the case '-1' is |
| 1223 // handled separately. |
| 1224 if (divisor < 0) { |
| 1225 ASSERT(divisor != -1); |
| 1226 __ Subu(result, zero_reg, Operand(result)); |
| 1227 } |
| 1228 // Compute the remainder. |
| 1229 if (divisor > 0) { |
| 1230 __ sll(scratch, result, power); |
| 1231 __ Subu(remainder, dividend, Operand(scratch)); |
| 1232 } else { |
| 1233 __ sll(scratch, result, power); |
| 1234 __ Addu(remainder, dividend, Operand(scratch)); |
| 1235 } |
| 1236 return; |
| 1237 } else if (LChunkBuilder::HasMagicNumberForDivisor(divisor)) { |
| 1238 // Use magic numbers for a few specific divisors. |
| 1239 // Details and proofs can be found in: |
| 1240 // - Hacker's Delight, Henry S. Warren, Jr. |
| 1241 // - The PowerPC Compiler Writer's Guide |
| 1242 // and probably many others. |
| 1243 // |
| 1244 // We handle |
| 1245 // <divisor with magic numbers> * <power of 2> |
| 1246 // but not |
| 1247 // <divisor with magic numbers> * <other divisor with magic numbers> |
| 1248 DivMagicNumbers magic_numbers = |
| 1249 DivMagicNumberFor(divisor_abs >> power_of_2_factor); |
| 1250 // Branch and condition free code for integer division by a power |
| 1251 // of two. |
| 1252 const int32_t M = magic_numbers.M; |
| 1253 const int32_t s = magic_numbers.s + power_of_2_factor; |
| 1254 |
| 1255 __ li(scratch, Operand(M)); |
| 1256 __ mult(dividend, scratch); |
| 1257 __ mfhi(scratch); |
| 1258 if (M < 0) { |
| 1259 __ Addu(scratch, scratch, Operand(dividend)); |
| 1260 } |
| 1261 if (s > 0) { |
| 1262 __ sra(scratch, scratch, s); |
| 1263 __ mov(scratch, scratch); |
| 1264 } |
| 1265 __ srl(at, dividend, 31); |
| 1266 __ Addu(result, scratch, Operand(at)); |
| 1267 if (divisor < 0) __ Subu(result, zero_reg, Operand(result)); |
| 1268 // Compute the remainder. |
| 1269 __ li(scratch, Operand(divisor)); |
| 1270 __ Mul(scratch, result, Operand(scratch)); |
| 1271 __ Subu(remainder, dividend, Operand(scratch)); |
| 1272 } else { |
| 1273 __ li(scratch, Operand(divisor)); |
| 1274 __ div(dividend, scratch); |
| 1275 __ mfhi(remainder); |
| 1276 __ mflo(result); |
| 1277 } |
| 1278 } |
| 1279 } |
| 1280 |
| 1281 |
1179 void LCodeGen::DoDivI(LDivI* instr) { | 1282 void LCodeGen::DoDivI(LDivI* instr) { |
1180 const Register left = ToRegister(instr->left()); | 1283 const Register left = ToRegister(instr->left()); |
1181 const Register right = ToRegister(instr->right()); | 1284 const Register right = ToRegister(instr->right()); |
1182 const Register result = ToRegister(instr->result()); | 1285 const Register result = ToRegister(instr->result()); |
1183 | 1286 |
1184 // On MIPS div is asynchronous - it will run in the background while we | 1287 // On MIPS div is asynchronous - it will run in the background while we |
1185 // check for special cases. | 1288 // check for special cases. |
1186 __ div(left, right); | 1289 __ div(left, right); |
1187 | 1290 |
1188 // Check for x / 0. | 1291 // Check for x / 0. |
(...skipping 30 matching lines...) Expand all Loading... |
1219 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier()); | 1322 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier()); |
1220 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand()); | 1323 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand()); |
1221 | 1324 |
1222 // This is computed in-place. | 1325 // This is computed in-place. |
1223 ASSERT(addend.is(ToDoubleRegister(instr->result()))); | 1326 ASSERT(addend.is(ToDoubleRegister(instr->result()))); |
1224 | 1327 |
1225 __ madd_d(addend, addend, multiplier, multiplicand); | 1328 __ madd_d(addend, addend, multiplier, multiplicand); |
1226 } | 1329 } |
1227 | 1330 |
1228 | 1331 |
| 1332 void LCodeGen::DoMathFloorOfDiv(LMathFloorOfDiv* instr) { |
| 1333 const Register result = ToRegister(instr->result()); |
| 1334 const Register left = ToRegister(instr->left()); |
| 1335 const Register remainder = ToRegister(instr->temp()); |
| 1336 const Register scratch = scratch0(); |
| 1337 |
| 1338 if (instr->right()->IsConstantOperand()) { |
| 1339 Label done; |
| 1340 int32_t divisor = ToInteger32(LConstantOperand::cast(instr->right())); |
| 1341 if (divisor < 0) { |
| 1342 DeoptimizeIf(eq, instr->environment(), left, Operand(zero_reg)); |
| 1343 } |
| 1344 EmitSignedIntegerDivisionByConstant(result, |
| 1345 left, |
| 1346 divisor, |
| 1347 remainder, |
| 1348 scratch, |
| 1349 instr->environment()); |
| 1350 // We performed a truncating division. Correct the result if necessary. |
| 1351 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT); |
| 1352 __ Xor(scratch , remainder, Operand(divisor)); |
| 1353 __ Branch(&done, ge, scratch, Operand(zero_reg)); |
| 1354 __ Subu(result, result, Operand(1)); |
| 1355 __ bind(&done); |
| 1356 } else { |
| 1357 Label done; |
| 1358 const Register right = ToRegister(instr->right()); |
| 1359 |
| 1360 // On MIPS div is asynchronous - it will run in the background while we |
| 1361 // check for special cases. |
| 1362 __ div(left, right); |
| 1363 |
| 1364 // Check for x / 0. |
| 1365 DeoptimizeIf(eq, instr->environment(), right, Operand(zero_reg)); |
| 1366 |
| 1367 // Check for (0 / -x) that will produce negative zero. |
| 1368 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 1369 Label left_not_zero; |
| 1370 __ Branch(&left_not_zero, ne, left, Operand(zero_reg)); |
| 1371 DeoptimizeIf(lt, instr->environment(), right, Operand(zero_reg)); |
| 1372 __ bind(&left_not_zero); |
| 1373 } |
| 1374 |
| 1375 // Check for (kMinInt / -1). |
| 1376 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { |
| 1377 Label left_not_min_int; |
| 1378 __ Branch(&left_not_min_int, ne, left, Operand(kMinInt)); |
| 1379 DeoptimizeIf(eq, instr->environment(), right, Operand(-1)); |
| 1380 __ bind(&left_not_min_int); |
| 1381 } |
| 1382 |
| 1383 __ mfhi(remainder); |
| 1384 __ mflo(result); |
| 1385 |
| 1386 // We performed a truncating division. Correct the result if necessary. |
| 1387 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT); |
| 1388 __ Xor(scratch , remainder, Operand(right)); |
| 1389 __ Branch(&done, ge, scratch, Operand(zero_reg)); |
| 1390 __ Subu(result, result, Operand(1)); |
| 1391 __ bind(&done); |
| 1392 } |
| 1393 } |
| 1394 |
| 1395 |
1229 void LCodeGen::DoMulI(LMulI* instr) { | 1396 void LCodeGen::DoMulI(LMulI* instr) { |
1230 Register scratch = scratch0(); | 1397 Register scratch = scratch0(); |
1231 Register result = ToRegister(instr->result()); | 1398 Register result = ToRegister(instr->result()); |
1232 // Note that result may alias left. | 1399 // Note that result may alias left. |
1233 Register left = ToRegister(instr->left()); | 1400 Register left = ToRegister(instr->left()); |
1234 LOperand* right_op = instr->right(); | 1401 LOperand* right_op = instr->right(); |
1235 | 1402 |
1236 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow); | 1403 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow); |
1237 bool bailout_on_minus_zero = | 1404 bool bailout_on_minus_zero = |
1238 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero); | 1405 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero); |
(...skipping 4442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5681 __ Subu(scratch, result, scratch); | 5848 __ Subu(scratch, result, scratch); |
5682 __ lw(result, FieldMemOperand(scratch, | 5849 __ lw(result, FieldMemOperand(scratch, |
5683 FixedArray::kHeaderSize - kPointerSize)); | 5850 FixedArray::kHeaderSize - kPointerSize)); |
5684 __ bind(&done); | 5851 __ bind(&done); |
5685 } | 5852 } |
5686 | 5853 |
5687 | 5854 |
5688 #undef __ | 5855 #undef __ |
5689 | 5856 |
5690 } } // namespace v8::internal | 5857 } } // namespace v8::internal |
OLD | NEW |