Chromium Code Reviews| 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 | |
|
Paul Lind
2013/06/14 16:34:58
Is there any specific reason you removed this asse
palfia
2013/06/15 00:15:25
Added ASSERT.
| |
| 1188 uint32_t divisor_abs = abs(divisor); | |
| 1189 | |
| 1190 int32_t power_of_2_factor = | |
| 1191 CompilerIntrinsics::CountTrailingZeros(divisor_abs); | |
| 1192 | |
| 1193 switch (divisor_abs) { | |
| 1194 case 0: | |
| 1195 DeoptimizeIf(al, environment); | |
| 1196 return; | |
| 1197 | |
| 1198 case 1: | |
| 1199 if (divisor > 0) { | |
| 1200 __ Move(result, dividend); | |
| 1201 } else { | |
| 1202 __ SubuAndCheckForOverflow(result, zero_reg, dividend, scratch); | |
| 1203 DeoptimizeIf(lt, environment, scratch, Operand(zero_reg)); | |
| 1204 } | |
| 1205 // Compute the remainder. | |
| 1206 __ Move(remainder, zero_reg); | |
| 1207 return; | |
| 1208 | |
| 1209 default: | |
| 1210 if (IsPowerOf2(divisor_abs)) { | |
| 1211 // Branch and condition free code for integer division by a power | |
| 1212 // of two. | |
| 1213 int32_t power = WhichPowerOf2(divisor_abs); | |
| 1214 if (power > 1) { | |
| 1215 __ sra(scratch, dividend, power - 1); | |
| 1216 } | |
| 1217 __ srl(scratch, scratch, 32 - power); | |
| 1218 __ Addu(scratch, dividend, Operand(scratch)); | |
| 1219 __ sra(result, scratch, power); | |
| 1220 // Negate if necessary. | |
| 1221 // We don't need to check for overflow because the case '-1' is | |
| 1222 // handled separately. | |
| 1223 if (divisor < 0) { | |
| 1224 ASSERT(divisor != -1); | |
| 1225 __ Subu(result, zero_reg, Operand(result)); | |
| 1226 } | |
| 1227 // Compute the remainder. | |
| 1228 if (divisor > 0) { | |
| 1229 __ sll(scratch, result, power); | |
| 1230 __ Subu(remainder, dividend, Operand(scratch)); | |
| 1231 } else { | |
| 1232 __ sll(scratch, result, power); | |
| 1233 __ Addu(remainder, dividend, Operand(scratch)); | |
| 1234 } | |
| 1235 return; | |
| 1236 } else if (LChunkBuilder::HasMagicNumberForDivisor(divisor)) { | |
| 1237 // Use magic numbers for a few specific divisors. | |
| 1238 // Details and proofs can be found in: | |
| 1239 // - Hacker's Delight, Henry S. Warren, Jr. | |
| 1240 // - The PowerPC Compiler Writer's Guide | |
| 1241 // and probably many others. | |
| 1242 // | |
| 1243 // We handle | |
| 1244 // <divisor with magic numbers> * <power of 2> | |
| 1245 // but not | |
| 1246 // <divisor with magic numbers> * <other divisor with magic numbers> | |
| 1247 DivMagicNumbers magic_numbers = | |
| 1248 DivMagicNumberFor(divisor_abs >> power_of_2_factor); | |
| 1249 // Branch and condition free code for integer division by a power | |
| 1250 // of two. | |
| 1251 const int32_t M = magic_numbers.M; | |
| 1252 const int32_t s = magic_numbers.s + power_of_2_factor; | |
| 1253 | |
| 1254 __ li(scratch, Operand(M)); | |
| 1255 __ mult(dividend, scratch); | |
| 1256 __ mfhi(scratch); | |
| 1257 if (M < 0) { | |
| 1258 __ Addu(scratch, scratch, Operand(dividend)); | |
| 1259 } | |
| 1260 if (s > 0) { | |
| 1261 __ sra(scratch, scratch, s); | |
| 1262 __ mov(scratch, scratch); | |
| 1263 } | |
| 1264 __ srl(at, dividend, 31); | |
| 1265 __ Addu(result, scratch, Operand(at)); | |
| 1266 if (divisor < 0) __ Subu(result, zero_reg, Operand(result)); | |
| 1267 // Compute the remainder. | |
| 1268 __ li(scratch, Operand(divisor)); | |
| 1269 // This sequence could be replaced with 'mls' when | |
| 1270 // it gets implemented. | |
|
Paul Lind
2013/06/14 16:34:58
Remove this comment, 'mls' refers to arm instructi
palfia
2013/06/15 00:15:25
Done.
| |
| 1271 __ Mul(scratch, result, Operand(scratch)); | |
| 1272 __ Subu(remainder, dividend, Operand(scratch)); | |
| 1273 } else { | |
| 1274 __ li(scratch, Operand(divisor)); | |
| 1275 __ div(dividend, scratch); | |
| 1276 __ mfhi(remainder); | |
| 1277 __ mflo(result); | |
| 1278 } | |
| 1279 } | |
| 1280 } | |
| 1281 | |
| 1282 | |
| 1179 void LCodeGen::DoDivI(LDivI* instr) { | 1283 void LCodeGen::DoDivI(LDivI* instr) { |
| 1180 const Register left = ToRegister(instr->left()); | 1284 const Register left = ToRegister(instr->left()); |
| 1181 const Register right = ToRegister(instr->right()); | 1285 const Register right = ToRegister(instr->right()); |
| 1182 const Register result = ToRegister(instr->result()); | 1286 const Register result = ToRegister(instr->result()); |
| 1183 | 1287 |
| 1184 // On MIPS div is asynchronous - it will run in the background while we | 1288 // On MIPS div is asynchronous - it will run in the background while we |
| 1185 // check for special cases. | 1289 // check for special cases. |
| 1186 __ div(left, right); | 1290 __ div(left, right); |
| 1187 | 1291 |
| 1188 // Check for x / 0. | 1292 // Check for x / 0. |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 1219 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier()); | 1323 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier()); |
| 1220 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand()); | 1324 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand()); |
| 1221 | 1325 |
| 1222 // This is computed in-place. | 1326 // This is computed in-place. |
| 1223 ASSERT(addend.is(ToDoubleRegister(instr->result()))); | 1327 ASSERT(addend.is(ToDoubleRegister(instr->result()))); |
| 1224 | 1328 |
| 1225 __ madd_d(addend, addend, multiplier, multiplicand); | 1329 __ madd_d(addend, addend, multiplier, multiplicand); |
| 1226 } | 1330 } |
| 1227 | 1331 |
| 1228 | 1332 |
| 1333 void LCodeGen::DoMathFloorOfDiv(LMathFloorOfDiv* instr) { | |
| 1334 const Register result = ToRegister(instr->result()); | |
| 1335 const Register left = ToRegister(instr->left()); | |
| 1336 const Register remainder = ToRegister(instr->temp()); | |
| 1337 const Register scratch = scratch0(); | |
| 1338 | |
| 1339 Label done; | |
| 1340 // We only optimize this for division by constants, because the standard | |
| 1341 // integer division routine is usually slower than transitionning to FPU. | |
| 1342 ASSERT(instr->right()->IsConstantOperand()); | |
| 1343 int32_t divisor = ToInteger32(LConstantOperand::cast(instr->right())); | |
| 1344 if (divisor < 0) { | |
| 1345 DeoptimizeIf(eq, instr->environment(), left, Operand(zero_reg)); | |
| 1346 } | |
| 1347 EmitSignedIntegerDivisionByConstant(result, | |
| 1348 left, | |
| 1349 divisor, | |
| 1350 remainder, | |
| 1351 scratch, | |
| 1352 instr->environment()); | |
| 1353 // We operated a truncating division. Correct the result if necessary. | |
| 1354 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT); | |
| 1355 __ Xor(scratch , remainder, Operand(divisor)); | |
| 1356 __ Branch(&done, ge, scratch, Operand(zero_reg)); | |
| 1357 __ Subu(result, result, Operand(1)); | |
| 1358 __ bind(&done); | |
| 1359 } | |
| 1360 | |
| 1361 | |
| 1229 void LCodeGen::DoMulI(LMulI* instr) { | 1362 void LCodeGen::DoMulI(LMulI* instr) { |
| 1230 Register scratch = scratch0(); | 1363 Register scratch = scratch0(); |
| 1231 Register result = ToRegister(instr->result()); | 1364 Register result = ToRegister(instr->result()); |
| 1232 // Note that result may alias left. | 1365 // Note that result may alias left. |
| 1233 Register left = ToRegister(instr->left()); | 1366 Register left = ToRegister(instr->left()); |
| 1234 LOperand* right_op = instr->right(); | 1367 LOperand* right_op = instr->right(); |
| 1235 | 1368 |
| 1236 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow); | 1369 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow); |
| 1237 bool bailout_on_minus_zero = | 1370 bool bailout_on_minus_zero = |
| 1238 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero); | 1371 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero); |
| (...skipping 4442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5681 __ Subu(scratch, result, scratch); | 5814 __ Subu(scratch, result, scratch); |
| 5682 __ lw(result, FieldMemOperand(scratch, | 5815 __ lw(result, FieldMemOperand(scratch, |
| 5683 FixedArray::kHeaderSize - kPointerSize)); | 5816 FixedArray::kHeaderSize - kPointerSize)); |
| 5684 __ bind(&done); | 5817 __ bind(&done); |
| 5685 } | 5818 } |
| 5686 | 5819 |
| 5687 | 5820 |
| 5688 #undef __ | 5821 #undef __ |
| 5689 | 5822 |
| 5690 } } // namespace v8::internal | 5823 } } // namespace v8::internal |
| OLD | NEW |