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 1293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1304 LOperand* dividend = UseFixed(instr->left(), r0); | 1304 LOperand* dividend = UseFixed(instr->left(), r0); |
1305 LOperand* divisor = UseFixed(instr->right(), r1); | 1305 LOperand* divisor = UseFixed(instr->right(), r1); |
1306 return AssignEnvironment(AssignPointerMap( | 1306 return AssignEnvironment(AssignPointerMap( |
1307 DefineFixed(new(zone()) LDivI(dividend, divisor), r0))); | 1307 DefineFixed(new(zone()) LDivI(dividend, divisor), r0))); |
1308 } else { | 1308 } else { |
1309 return DoArithmeticT(Token::DIV, instr); | 1309 return DoArithmeticT(Token::DIV, instr); |
1310 } | 1310 } |
1311 } | 1311 } |
1312 | 1312 |
1313 | 1313 |
| 1314 bool LChunkBuilder::HasMagicNumberForDivisor(int32_t divisor) { |
| 1315 uint32_t divisor_abs = abs(divisor); |
| 1316 // Dividing by 0, 1, and powers of 2 is easy. |
| 1317 // Note that IsPowerOf2(0) returns true; |
| 1318 ASSERT(IsPowerOf2(0) == true); |
| 1319 if (IsPowerOf2(divisor_abs)) return true; |
| 1320 |
| 1321 // We have magic numbers for a few specific divisors. |
| 1322 // Details and proofs can be found in: |
| 1323 // - Hacker's Delight, Henry S. Warren, Jr. |
| 1324 // - The PowerPC Compiler Writer’s Guide |
| 1325 // and probably many others. |
| 1326 // |
| 1327 // We handle |
| 1328 // <divisor with magic numbers> * <power of 2> |
| 1329 // but not |
| 1330 // <divisor with magic numbers> * <other divisor with magic numbers> |
| 1331 int32_t power_of_2_factor = |
| 1332 CompilerIntrinsics::CountTrailingZeros(divisor_abs); |
| 1333 DivMagicNumbers magic_numbers = |
| 1334 DivMagicNumberFor(divisor_abs >> power_of_2_factor); |
| 1335 if (magic_numbers.M != InvalidDivMagicNumber.M) return true; |
| 1336 |
| 1337 return false; |
| 1338 } |
| 1339 |
| 1340 |
| 1341 HValue* LChunkBuilder::SimplifiedDividendForMathFloorOfDiv(HValue* dividend) { |
| 1342 // A change from an integer32 can be replaced by the integer32 value. |
| 1343 if (dividend->IsChange() && |
| 1344 HChange::cast(dividend)->from().IsInteger32()) { |
| 1345 return HChange::cast(dividend)->value(); |
| 1346 } |
| 1347 // Change representation to integer32 if the constant can be represented as an |
| 1348 // integer32 value. |
| 1349 if (dividend->IsConstant() && |
| 1350 HConstant::cast(dividend)->HasInteger32Value()) { |
| 1351 return HConstant::cast(dividend)->CopyToRepresentation( |
| 1352 Representation::Integer32()); |
| 1353 } |
| 1354 return NULL; |
| 1355 } |
| 1356 |
| 1357 |
| 1358 HValue* LChunkBuilder::SimplifiedDivisorForMathFloorOfDiv(HValue* divisor) { |
| 1359 // Only optimize when we have magic numbers for the divisor. |
| 1360 // The standard integer division routine is usually slower than transitionning |
| 1361 // to VFP. |
| 1362 if (divisor->IsConstant() && |
| 1363 HConstant::cast(divisor)->HasInteger32Value()) { |
| 1364 HConstant* constant_val = HConstant::cast(divisor); |
| 1365 int32_t int32_val = constant_val->Integer32Value(); |
| 1366 if (LChunkBuilder::HasMagicNumberForDivisor(int32_val)) { |
| 1367 return constant_val->CopyToRepresentation(Representation::Integer32()); |
| 1368 } |
| 1369 } |
| 1370 return NULL; |
| 1371 } |
| 1372 |
| 1373 |
| 1374 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) { |
| 1375 HValue* right = instr->right(); |
| 1376 LOperand* dividend = UseRegister(instr->left()); |
| 1377 LOperand* divisor = UseRegisterOrConstant(right); |
| 1378 LOperand* remainder = TempRegister(); |
| 1379 ASSERT(right->IsConstant() && |
| 1380 HConstant::cast(right)->HasInteger32Value() && |
| 1381 HasMagicNumberForDivisor(HConstant::cast(right)->Integer32Value())); |
| 1382 return AssignEnvironment(DefineAsRegister( |
| 1383 new LMathFloorOfDiv(dividend, divisor, remainder))); |
| 1384 } |
| 1385 |
| 1386 |
1314 LInstruction* LChunkBuilder::DoMod(HMod* instr) { | 1387 LInstruction* LChunkBuilder::DoMod(HMod* instr) { |
1315 if (instr->representation().IsInteger32()) { | 1388 if (instr->representation().IsInteger32()) { |
1316 ASSERT(instr->left()->representation().IsInteger32()); | 1389 ASSERT(instr->left()->representation().IsInteger32()); |
1317 ASSERT(instr->right()->representation().IsInteger32()); | 1390 ASSERT(instr->right()->representation().IsInteger32()); |
1318 | 1391 |
1319 LModI* mod; | 1392 LModI* mod; |
1320 if (instr->HasPowerOf2Divisor()) { | 1393 if (instr->HasPowerOf2Divisor()) { |
1321 ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero)); | 1394 ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero)); |
1322 LOperand* value = UseRegisterAtStart(instr->left()); | 1395 LOperand* value = UseRegisterAtStart(instr->left()); |
1323 mod = new(zone()) LModI(value, UseOrConstant(instr->right())); | 1396 mod = new(zone()) LModI(value, UseOrConstant(instr->right())); |
(...skipping 976 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2300 | 2373 |
2301 | 2374 |
2302 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) { | 2375 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) { |
2303 LOperand* object = UseRegister(instr->object()); | 2376 LOperand* object = UseRegister(instr->object()); |
2304 LOperand* index = UseRegister(instr->index()); | 2377 LOperand* index = UseRegister(instr->index()); |
2305 return DefineAsRegister(new(zone()) LLoadFieldByIndex(object, index)); | 2378 return DefineAsRegister(new(zone()) LLoadFieldByIndex(object, index)); |
2306 } | 2379 } |
2307 | 2380 |
2308 | 2381 |
2309 } } // namespace v8::internal | 2382 } } // namespace v8::internal |
OLD | NEW |