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 1351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1362 LOperand* dividend = UseRegister(instr->left()); | 1362 LOperand* dividend = UseRegister(instr->left()); |
1363 LOperand* divisor = UseRegister(instr->right()); | 1363 LOperand* divisor = UseRegister(instr->right()); |
1364 LDivI* div = new(zone()) LDivI(dividend, divisor); | 1364 LDivI* div = new(zone()) LDivI(dividend, divisor); |
1365 return AssignEnvironment(DefineAsRegister(div)); | 1365 return AssignEnvironment(DefineAsRegister(div)); |
1366 } else { | 1366 } else { |
1367 return DoArithmeticT(Token::DIV, instr); | 1367 return DoArithmeticT(Token::DIV, instr); |
1368 } | 1368 } |
1369 } | 1369 } |
1370 | 1370 |
1371 | 1371 |
| 1372 bool LChunkBuilder::HasMagicNumberForDivisor(int32_t divisor) { |
| 1373 uint32_t divisor_abs = abs(divisor); |
| 1374 // Dividing by 0, 1, and powers of 2 is easy. |
| 1375 // Note that IsPowerOf2(0) returns true; |
| 1376 ASSERT(IsPowerOf2(0) == true); |
| 1377 if (IsPowerOf2(divisor_abs)) return true; |
| 1378 |
| 1379 // We have magic numbers for a few specific divisors. |
| 1380 // Details and proofs can be found in: |
| 1381 // - Hacker's Delight, Henry S. Warren, Jr. |
| 1382 // - The PowerPC Compiler Writer's Guide |
| 1383 // and probably many others. |
| 1384 // |
| 1385 // We handle |
| 1386 // <divisor with magic numbers> * <power of 2> |
| 1387 // but not |
| 1388 // <divisor with magic numbers> * <other divisor with magic numbers> |
| 1389 int32_t power_of_2_factor = |
| 1390 CompilerIntrinsics::CountTrailingZeros(divisor_abs); |
| 1391 DivMagicNumbers magic_numbers = |
| 1392 DivMagicNumberFor(divisor_abs >> power_of_2_factor); |
| 1393 if (magic_numbers.M != InvalidDivMagicNumber.M) return true; |
| 1394 |
| 1395 return false; |
| 1396 } |
| 1397 |
| 1398 |
| 1399 HValue* LChunkBuilder::SimplifiedDivisorForMathFloorOfDiv(HValue* divisor) { |
| 1400 // Only optimize when we have magic numbers for the divisor. |
| 1401 // The standard integer division routine is usually slower than transitionning |
| 1402 // to FPU. |
| 1403 if (divisor->IsConstant() && |
| 1404 HConstant::cast(divisor)->HasInteger32Value()) { |
| 1405 HConstant* constant_val = HConstant::cast(divisor); |
| 1406 return constant_val->CopyToRepresentation(Representation::Integer32(), |
| 1407 divisor->block()->zone()); |
| 1408 } |
| 1409 return NULL; |
| 1410 } |
| 1411 |
| 1412 |
1372 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) { | 1413 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) { |
1373 UNIMPLEMENTED(); | 1414 HValue* right = instr->right(); |
1374 return NULL; | 1415 LOperand* dividend = UseRegister(instr->left()); |
| 1416 LOperand* divisor = UseRegisterOrConstant(right); |
| 1417 LOperand* remainder = TempRegister(); |
| 1418 ASSERT(right->IsConstant() && |
| 1419 HConstant::cast(right)->HasInteger32Value()); |
| 1420 return AssignEnvironment(DefineAsRegister( |
| 1421 new(zone()) LMathFloorOfDiv(dividend, divisor, remainder))); |
1375 } | 1422 } |
1376 | 1423 |
1377 | 1424 |
1378 LInstruction* LChunkBuilder::DoMod(HMod* instr) { | 1425 LInstruction* LChunkBuilder::DoMod(HMod* instr) { |
1379 HValue* left = instr->left(); | 1426 HValue* left = instr->left(); |
1380 HValue* right = instr->right(); | 1427 HValue* right = instr->right(); |
1381 if (instr->representation().IsInteger32()) { | 1428 if (instr->representation().IsInteger32()) { |
1382 ASSERT(left->representation().IsInteger32()); | 1429 ASSERT(left->representation().IsInteger32()); |
1383 ASSERT(right->representation().IsInteger32()); | 1430 ASSERT(right->representation().IsInteger32()); |
1384 if (instr->HasPowerOf2Divisor()) { | 1431 if (instr->HasPowerOf2Divisor()) { |
(...skipping 1136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2521 | 2568 |
2522 | 2569 |
2523 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) { | 2570 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) { |
2524 LOperand* object = UseRegister(instr->object()); | 2571 LOperand* object = UseRegister(instr->object()); |
2525 LOperand* index = UseRegister(instr->index()); | 2572 LOperand* index = UseRegister(instr->index()); |
2526 return DefineAsRegister(new(zone()) LLoadFieldByIndex(object, index)); | 2573 return DefineAsRegister(new(zone()) LLoadFieldByIndex(object, index)); |
2527 } | 2574 } |
2528 | 2575 |
2529 | 2576 |
2530 } } // namespace v8::internal | 2577 } } // namespace v8::internal |
OLD | NEW |