Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: src/arm/lithium-codegen-arm.cc

Issue 15769010: Improve code for integral modulus calculation. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Rebased. ia32/x64/ARM done, MIPS restructured. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 1147 matching lines...) Expand 10 before | Expand all | Expand 10 after
1158 } 1158 }
1159 } 1159 }
1160 1160
1161 1161
1162 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) { 1162 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1163 // Nothing to do. 1163 // Nothing to do.
1164 } 1164 }
1165 1165
1166 1166
1167 void LCodeGen::DoModI(LModI* instr) { 1167 void LCodeGen::DoModI(LModI* instr) {
1168 if (instr->hydrogen()->HasPowerOf2Divisor()) { 1168 HMod* hmod = instr->hydrogen();
1169 Register dividend = ToRegister(instr->left()); 1169 HValue* left = hmod->left();
1170 Register result = ToRegister(instr->result()); 1170 HValue* right = hmod->right();
1171 if (hmod->HasPowerOf2Divisor()) {
1172 // TODO(svenpanne) We should really do the strength reduction on the
1173 // Hydrogen level.
1174 Register left_reg = ToRegister(instr->left());
1175 Register result_reg = ToRegister(instr->result());
1171 1176
1172 int32_t divisor = 1177 int32_t divisor = Abs(right->GetInteger32Constant());
1173 HConstant::cast(instr->hydrogen()->right())->Integer32Value();
1174 1178
1175 if (divisor < 0) divisor = -divisor; 1179 Label left_is_not_negative, done;
1180 if (left->CanBeNegative()) {
1181 __ cmp(left_reg, Operand::Zero());
1182 __ b(pl, &left_is_not_negative);
1183 __ rsb(result_reg, left_reg, Operand::Zero());
1184 __ and_(result_reg, result_reg, Operand(divisor - 1));
1185 __ rsb(result_reg, result_reg, Operand::Zero(), SetCC);
1186 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1187 DeoptimizeIf(eq, instr->environment());
1188 }
1189 __ b(&done);
1190 }
1176 1191
1177 Label positive_dividend, done; 1192 __ bind(&left_is_not_negative);
1178 __ cmp(dividend, Operand::Zero()); 1193 __ and_(result_reg, left_reg, Operand(divisor - 1));
1179 __ b(pl, &positive_dividend); 1194 __ bind(&done);
1180 __ rsb(result, dividend, Operand::Zero()); 1195
1181 __ and_(result, result, Operand(divisor - 1), SetCC); 1196 } else if (hmod->has_fixed_right_arg()) {
1182 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { 1197 Register left_reg = ToRegister(instr->left());
1198 Register right_reg = ToRegister(instr->right());
1199 Register result_reg = ToRegister(instr->result());
1200
1201 int32_t divisor = hmod->fixed_right_arg_value();
1202 ASSERT(IsPowerOf2(divisor));
1203
1204 // Check if our assumption of a fixed right operand still holds.
1205 __ cmp(right_reg, Operand(divisor));
1206 DeoptimizeIf(ne, instr->environment());
1207
1208 Label left_is_not_negative, done;
1209 if (left->CanBeNegative()) {
1210 __ cmp(left_reg, Operand::Zero());
1211 __ b(pl, &left_is_not_negative);
1212 __ rsb(result_reg, left_reg, Operand::Zero());
1213 __ and_(result_reg, result_reg, Operand(divisor - 1));
1214 __ rsb(result_reg, result_reg, Operand::Zero(), SetCC);
1215 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1216 DeoptimizeIf(eq, instr->environment());
1217 }
1218 __ b(&done);
1219 }
1220
1221 __ bind(&left_is_not_negative);
1222 __ and_(result_reg, left_reg, Operand(divisor - 1));
1223 __ bind(&done);
1224
1225 } else if (CpuFeatures::IsSupported(SUDIV)) {
1226 CpuFeatureScope scope(masm(), SUDIV);
1227
1228 Register left_reg = ToRegister(instr->left());
1229 Register right_reg = ToRegister(instr->right());
1230 Register result_reg = ToRegister(instr->result());
1231
1232 Label done;
1233 // Check for x % 0, sdiv might signal an exception. We have to deopt in this
1234 // case because we can't return a NaN.
1235 if (right->CanBeZero()) {
1236 __ cmp(right_reg, Operand::Zero());
1183 DeoptimizeIf(eq, instr->environment()); 1237 DeoptimizeIf(eq, instr->environment());
1184 } 1238 }
1185 __ rsb(result, result, Operand::Zero());
1186 __ b(&done);
1187 __ bind(&positive_dividend);
1188 __ and_(result, dividend, Operand(divisor - 1));
1189 __ bind(&done);
1190 return;
1191 }
1192 1239
1193 // These registers hold untagged 32 bit values. 1240 // Check for kMinInt % -1, sdiv might signal an exception. We have to deopt
Rodolph Perfetta 2013/06/07 19:55:03 sdiv kMinInt, -1 will not trigger an exception but
Sven Panne 2013/06/10 11:05:39 Adapted comment.
1194 Register left = ToRegister(instr->left()); 1241 // if we care about -0, because we can't return that.
1195 Register right = ToRegister(instr->right()); 1242 if (left->RangeCanInclude(kMinInt) && right->RangeCanInclude(-1)) {
1196 Register result = ToRegister(instr->result()); 1243 Label no_overflow_possible;
1197 Label done; 1244 __ cmp(left_reg, Operand(kMinInt));
1198 1245 __ b(ne, &no_overflow_possible);
1199 // Check for x % 0. 1246 __ cmp(right_reg, Operand(-1));
1200 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) { 1247 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1201 __ cmp(right, Operand::Zero()); 1248 DeoptimizeIf(eq, instr->environment());
1202 DeoptimizeIf(eq, instr->environment()); 1249 } else {
1203 } 1250 __ b(ne, &no_overflow_possible);
1204 1251 __ mov(result_reg, Operand::Zero());
1205 if (CpuFeatures::IsSupported(SUDIV)) { 1252 __ jmp(&done);
1206 CpuFeatureScope scope(masm(), SUDIV); 1253 }
1207 // Check for (kMinInt % -1). 1254 __ bind(&no_overflow_possible);
1208 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1209 Label left_not_min_int;
1210 __ cmp(left, Operand(kMinInt));
1211 __ b(ne, &left_not_min_int);
1212 __ cmp(right, Operand(-1));
1213 DeoptimizeIf(eq, instr->environment());
1214 __ bind(&left_not_min_int);
1215 } 1255 }
1216 1256
1217 // For r3 = r1 % r2; we can have the following ARM code 1257 // For 'r3 = r1 % r2' we can have the following ARM code:
1218 // sdiv r3, r1, r2 1258 // sdiv r3, r1, r2
1219 // mls r3, r3, r2, r1 1259 // mls r3, r3, r2, r1
1220 1260
1221 __ sdiv(result, left, right); 1261 __ sdiv(result_reg, left_reg, right_reg);
1222 __ mls(result, result, right, left); 1262 __ mls(result_reg, result_reg, right_reg, left_reg);
1223 1263
1224 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { 1264 // If we care about -0, test if the dividend is <0 and the result is 0.
1225 __ cmp(result, Operand::Zero()); 1265 if (left->CanBeNegative() &&
1266 hmod->CanBeZero() &&
1267 hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1268 __ cmp(result_reg, Operand::Zero());
1226 __ b(ne, &done); 1269 __ b(ne, &done);
1227 __ cmp(left, Operand::Zero()); 1270 __ cmp(left_reg, Operand::Zero());
1228 DeoptimizeIf(lt, instr->environment()); 1271 DeoptimizeIf(lt, instr->environment());
1229 } 1272 }
1273 __ bind(&done);
1274
1230 } else { 1275 } else {
1276 // General case, without any SDIV support.
1277 Register left_reg = ToRegister(instr->left());
1278 Register right_reg = ToRegister(instr->right());
1279 Register result_reg = ToRegister(instr->result());
1231 Register scratch = scratch0(); 1280 Register scratch = scratch0();
1281 ASSERT(!scratch.is(left_reg));
1282 ASSERT(!scratch.is(right_reg));
1283 ASSERT(!scratch.is(result_reg));
1232 Register scratch2 = ToRegister(instr->temp()); 1284 Register scratch2 = ToRegister(instr->temp());
1233 DwVfpRegister dividend = ToDoubleRegister(instr->temp2()); 1285 DwVfpRegister dividend = ToDoubleRegister(instr->temp2());
1234 DwVfpRegister divisor = ToDoubleRegister(instr->temp3()); 1286 DwVfpRegister divisor = ToDoubleRegister(instr->temp3());
1287 ASSERT(!divisor.is(dividend));
1235 DwVfpRegister quotient = double_scratch0(); 1288 DwVfpRegister quotient = double_scratch0();
1289 ASSERT(!quotient.is(dividend));
1290 ASSERT(!quotient.is(divisor));
1236 1291
1237 ASSERT(!dividend.is(divisor)); 1292 Label done;
1238 ASSERT(!dividend.is(quotient)); 1293 // Check for x % 0, we have to deopt in this case because we can't return a
1239 ASSERT(!divisor.is(quotient)); 1294 // NaN.
1240 ASSERT(!scratch.is(left)); 1295 if (right->CanBeZero()) {
1241 ASSERT(!scratch.is(right)); 1296 __ cmp(right_reg, Operand::Zero());
1242 ASSERT(!scratch.is(result)); 1297 DeoptimizeIf(eq, instr->environment());
1298 }
1243 1299
1244 Label vfp_modulo, right_negative; 1300 __ Move(result_reg, left_reg);
1245 1301 // Load the arguments in VFP registers. The divisor value is preloaded
1246 __ Move(result, left); 1302 // before. Be careful that 'right_reg' is only live on entry.
1247 1303 // TODO(svenpanne) The last comments seems to be wrong nowadays.
Jakob Kummerow 2013/06/07 16:50:07 Delete them, then! "The divisor value is preloaded
Sven Panne 2013/06/10 11:05:39 Removed the comment. "scary" = "it was a bug" ;-)
1248 // (0 % x) must yield 0 (if x is finite, which is the case here). 1304 __ vmov(dividend.low(), left_reg);
1249 __ cmp(left, Operand::Zero()); 1305 __ vmov(divisor.low(), right_reg);
1250 __ b(eq, &done);
1251 // Preload right in a vfp register.
1252 __ vmov(divisor.low(), right);
1253 __ b(lt, &vfp_modulo);
1254
1255 __ cmp(left, Operand(right));
1256 __ b(lt, &done);
1257
1258 // Check for (positive) power of two on the right hand side.
1259 __ JumpIfNotPowerOfTwoOrZeroAndNeg(right,
1260 scratch,
1261 &right_negative,
1262 &vfp_modulo);
1263 // Perform modulo operation (scratch contains right - 1).
1264 __ and_(result, scratch, Operand(left));
1265 __ b(&done);
1266
1267 __ bind(&right_negative);
1268 // Negate right. The sign of the divisor does not matter.
1269 __ rsb(right, right, Operand::Zero());
1270
1271 __ bind(&vfp_modulo);
1272 // Load the arguments in VFP registers.
1273 // The divisor value is preloaded before. Be careful that 'right'
1274 // is only live on entry.
1275 __ vmov(dividend.low(), left);
1276 // From here on don't use right as it may have been reallocated
1277 // (for example to scratch2).
1278 right = no_reg;
1279 1306
1280 __ vcvt_f64_s32(dividend, dividend.low()); 1307 __ vcvt_f64_s32(dividend, dividend.low());
1281 __ vcvt_f64_s32(divisor, divisor.low()); 1308 __ vcvt_f64_s32(divisor, divisor.low());
1282 1309
1310 // TODO(svenpanne) The kMinInt % -1 case seems to be handled implicitly. Is
1311 // this correct? If yes, a comment about this might be appropriate.
Jakob Kummerow 2013/06/07 16:50:07 IIUC, we don't need special handling for that case
Sven Panne 2013/06/10 11:05:39 I added a comment about this.
1283 // We do not care about the sign of the divisor. 1312 // We do not care about the sign of the divisor.
1284 __ vabs(divisor, divisor); 1313 __ vabs(divisor, divisor);
1285 // Compute the quotient and round it to a 32bit integer. 1314 // Compute the quotient and round it to a 32bit integer.
1286 __ vdiv(quotient, dividend, divisor); 1315 __ vdiv(quotient, dividend, divisor);
1287 __ vcvt_s32_f64(quotient.low(), quotient); 1316 __ vcvt_s32_f64(quotient.low(), quotient);
1288 __ vcvt_f64_s32(quotient, quotient.low()); 1317 __ vcvt_f64_s32(quotient, quotient.low());
1289 1318
1290 // Compute the remainder in result. 1319 // Compute the remainder in result.
1291 DwVfpRegister double_scratch = dividend; 1320 DwVfpRegister double_scratch = dividend;
1292 __ vmul(double_scratch, divisor, quotient); 1321 __ vmul(double_scratch, divisor, quotient);
1293 __ vcvt_s32_f64(double_scratch.low(), double_scratch); 1322 __ vcvt_s32_f64(double_scratch.low(), double_scratch);
1294 __ vmov(scratch, double_scratch.low()); 1323 __ vmov(scratch, double_scratch.low());
1295 1324
1296 if (!instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { 1325 // If we care about -0, test if the dividend is <0 and the result is 0.
1297 __ sub(result, left, scratch); 1326 if (left->CanBeNegative() &&
1298 } else { 1327 hmod->CanBeZero() &&
1328 hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1299 Label ok; 1329 Label ok;
1300 // Check for -0. 1330 // TODO(svenpanne) Why do we need scratch2 in addition to scratch?
Jakob Kummerow 2013/06/07 16:50:07 I don't think we do.
Rodolph Perfetta 2013/06/07 19:55:03 I don't think we do either. in fact you should be
Sven Panne 2013/06/10 11:05:39 Good idea, I did that and simplified LModI accordi
1301 __ sub(scratch2, left, scratch, SetCC); 1331 __ sub(scratch2, left_reg, scratch, SetCC);
1302 __ b(ne, &ok); 1332 __ b(ne, &ok);
1303 __ cmp(left, Operand::Zero()); 1333 __ cmp(left_reg, Operand::Zero());
1304 DeoptimizeIf(mi, instr->environment()); 1334 DeoptimizeIf(mi, instr->environment());
1305 __ bind(&ok); 1335 __ bind(&ok);
1306 // Load the result and we are done. 1336 // Load the result and we are done.
1307 __ mov(result, scratch2); 1337 __ mov(result_reg, scratch2);
1338 } else {
1339 __ sub(result_reg, left_reg, scratch);
1308 } 1340 }
1341 __ bind(&done);
1309 } 1342 }
1310 __ bind(&done);
1311 } 1343 }
1312 1344
1313 1345
1314 void LCodeGen::EmitSignedIntegerDivisionByConstant( 1346 void LCodeGen::EmitSignedIntegerDivisionByConstant(
1315 Register result, 1347 Register result,
1316 Register dividend, 1348 Register dividend,
1317 int32_t divisor, 1349 int32_t divisor,
1318 Register remainder, 1350 Register remainder,
1319 Register scratch, 1351 Register scratch,
1320 LEnvironment* environment) { 1352 LEnvironment* environment) {
(...skipping 4607 matching lines...) Expand 10 before | Expand all | Expand 10 after
5928 __ sub(scratch, result, Operand::PointerOffsetFromSmiKey(index)); 5960 __ sub(scratch, result, Operand::PointerOffsetFromSmiKey(index));
5929 __ ldr(result, FieldMemOperand(scratch, 5961 __ ldr(result, FieldMemOperand(scratch,
5930 FixedArray::kHeaderSize - kPointerSize)); 5962 FixedArray::kHeaderSize - kPointerSize));
5931 __ bind(&done); 5963 __ bind(&done);
5932 } 5964 }
5933 5965
5934 5966
5935 #undef __ 5967 #undef __
5936 5968
5937 } } // namespace v8::internal 5969 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698