blob: ccae1306376575d0ebbe8df09056e0148147249b [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file contains codegen for the X86 ISA */
18
19#include "codegen_x86.h"
20#include "dex/quick/mir_to_lir-inl.h"
21#include "mirror/array.h"
22#include "x86_lir.h"
23
24namespace art {
25
26/*
27 * Perform register memory operation.
28 */
29LIR* X86Mir2Lir::GenRegMemCheck(ConditionCode c_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070030 int reg1, int base, int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070031 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind,
32 current_dalvik_offset_, reg1, base, offset);
33 OpRegMem(kOpCmp, reg1, base, offset);
34 LIR* branch = OpCondBranch(c_code, tgt);
35 // Remember branch target - will process later
36 throw_launchpads_.Insert(tgt);
37 return branch;
38}
39
40/*
Mark Mendell343adb52013-12-18 06:02:17 -080041 * Perform a compare of memory to immediate value
42 */
43LIR* X86Mir2Lir::GenMemImmedCheck(ConditionCode c_code,
44 int base, int offset, int check_value, ThrowKind kind) {
45 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind,
46 current_dalvik_offset_, base, check_value, 0);
47 NewLIR3(IS_SIMM8(check_value) ? kX86Cmp32MI8 : kX86Cmp32MI, base, offset, check_value);
48 LIR* branch = OpCondBranch(c_code, tgt);
49 // Remember branch target - will process later
50 throw_launchpads_.Insert(tgt);
51 return branch;
52}
53
54/*
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 * Compare two 64-bit values
56 * x = y return 0
57 * x < y return -1
58 * x > y return 1
59 */
60void X86Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070061 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 FlushAllRegs();
63 LockCallTemps(); // Prepare for explicit register usage
64 LoadValueDirectWideFixed(rl_src1, r0, r1);
65 LoadValueDirectWideFixed(rl_src2, r2, r3);
66 // Compute (r1:r0) = (r1:r0) - (r3:r2)
67 OpRegReg(kOpSub, r0, r2); // r0 = r0 - r2
68 OpRegReg(kOpSbc, r1, r3); // r1 = r1 - r3 - CF
69 NewLIR2(kX86Set8R, r2, kX86CondL); // r2 = (r1:r0) < (r3:r2) ? 1 : 0
70 NewLIR2(kX86Movzx8RR, r2, r2);
71 OpReg(kOpNeg, r2); // r2 = -r2
72 OpRegReg(kOpOr, r0, r1); // r0 = high | low - sets ZF
73 NewLIR2(kX86Set8R, r0, kX86CondNz); // r0 = (r1:r0) != (r3:r2) ? 1 : 0
74 NewLIR2(kX86Movzx8RR, r0, r0);
75 OpRegReg(kOpOr, r0, r2); // r0 = r0 | r2
76 RegLocation rl_result = LocCReturn();
77 StoreValue(rl_dest, rl_result);
78}
79
80X86ConditionCode X86ConditionEncoding(ConditionCode cond) {
81 switch (cond) {
82 case kCondEq: return kX86CondEq;
83 case kCondNe: return kX86CondNe;
84 case kCondCs: return kX86CondC;
85 case kCondCc: return kX86CondNc;
Vladimir Marko58af1f92013-12-19 13:31:15 +000086 case kCondUlt: return kX86CondC;
87 case kCondUge: return kX86CondNc;
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 case kCondMi: return kX86CondS;
89 case kCondPl: return kX86CondNs;
90 case kCondVs: return kX86CondO;
91 case kCondVc: return kX86CondNo;
92 case kCondHi: return kX86CondA;
93 case kCondLs: return kX86CondBe;
94 case kCondGe: return kX86CondGe;
95 case kCondLt: return kX86CondL;
96 case kCondGt: return kX86CondG;
97 case kCondLe: return kX86CondLe;
98 case kCondAl:
99 case kCondNv: LOG(FATAL) << "Should not reach here";
100 }
101 return kX86CondO;
102}
103
104LIR* X86Mir2Lir::OpCmpBranch(ConditionCode cond, int src1, int src2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700105 LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 NewLIR2(kX86Cmp32RR, src1, src2);
107 X86ConditionCode cc = X86ConditionEncoding(cond);
108 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ ,
109 cc);
110 branch->target = target;
111 return branch;
112}
113
114LIR* X86Mir2Lir::OpCmpImmBranch(ConditionCode cond, int reg,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700115 int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 if ((check_value == 0) && (cond == kCondEq || cond == kCondNe)) {
117 // TODO: when check_value == 0 and reg is rCX, use the jcxz/nz opcode
118 NewLIR2(kX86Test32RR, reg, reg);
119 } else {
120 NewLIR2(IS_SIMM8(check_value) ? kX86Cmp32RI8 : kX86Cmp32RI, reg, check_value);
121 }
122 X86ConditionCode cc = X86ConditionEncoding(cond);
123 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ , cc);
124 branch->target = target;
125 return branch;
126}
127
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700128LIR* X86Mir2Lir::OpRegCopyNoInsert(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 if (X86_FPREG(r_dest) || X86_FPREG(r_src))
130 return OpFpRegCopy(r_dest, r_src);
131 LIR* res = RawLIR(current_dalvik_offset_, kX86Mov32RR,
132 r_dest, r_src);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800133 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 res->flags.is_nop = true;
135 }
136 return res;
137}
138
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700139LIR* X86Mir2Lir::OpRegCopy(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 LIR *res = OpRegCopyNoInsert(r_dest, r_src);
141 AppendLIR(res);
142 return res;
143}
144
145void X86Mir2Lir::OpRegCopyWide(int dest_lo, int dest_hi,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700146 int src_lo, int src_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 bool dest_fp = X86_FPREG(dest_lo) && X86_FPREG(dest_hi);
148 bool src_fp = X86_FPREG(src_lo) && X86_FPREG(src_hi);
149 assert(X86_FPREG(src_lo) == X86_FPREG(src_hi));
150 assert(X86_FPREG(dest_lo) == X86_FPREG(dest_hi));
151 if (dest_fp) {
152 if (src_fp) {
153 OpRegCopy(S2d(dest_lo, dest_hi), S2d(src_lo, src_hi));
154 } else {
155 // TODO: Prevent this from happening in the code. The result is often
156 // unused or could have been loaded more easily from memory.
157 NewLIR2(kX86MovdxrRR, dest_lo, src_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000158 dest_hi = AllocTempDouble();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 NewLIR2(kX86MovdxrRR, dest_hi, src_hi);
160 NewLIR2(kX86PsllqRI, dest_hi, 32);
161 NewLIR2(kX86OrpsRR, dest_lo, dest_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000162 FreeTemp(dest_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 }
164 } else {
165 if (src_fp) {
166 NewLIR2(kX86MovdrxRR, dest_lo, src_lo);
167 NewLIR2(kX86PsrlqRI, src_lo, 32);
168 NewLIR2(kX86MovdrxRR, dest_hi, src_lo);
169 } else {
170 // Handle overlap
171 if (src_hi == dest_lo) {
172 OpRegCopy(dest_hi, src_hi);
173 OpRegCopy(dest_lo, src_lo);
174 } else {
175 OpRegCopy(dest_lo, src_lo);
176 OpRegCopy(dest_hi, src_hi);
177 }
178 }
179 }
180}
181
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700182void X86Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 UNIMPLEMENTED(FATAL) << "Need codegen for GenSelect";
184}
185
186void X86Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
buzbee0d829482013-10-11 15:24:55 -0700187 LIR* taken = &block_label_list_[bb->taken];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
189 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Vladimir Markoa8946072014-01-22 10:30:44 +0000190 ConditionCode ccode = mir->meta.ccode;
Mark Mendell412d4f82013-12-18 13:32:36 -0800191
192 if (rl_src1.is_const) {
193 std::swap(rl_src1, rl_src2);
194 ccode = FlipComparisonOrder(ccode);
195 }
196 if (rl_src2.is_const) {
197 // Do special compare/branch against simple const operand
198 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
199 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
200 return;
201 }
202
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 FlushAllRegs();
204 LockCallTemps(); // Prepare for explicit register usage
205 LoadValueDirectWideFixed(rl_src1, r0, r1);
206 LoadValueDirectWideFixed(rl_src2, r2, r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 // Swap operands and condition code to prevent use of zero flag.
208 if (ccode == kCondLe || ccode == kCondGt) {
209 // Compute (r3:r2) = (r3:r2) - (r1:r0)
210 OpRegReg(kOpSub, r2, r0); // r2 = r2 - r0
211 OpRegReg(kOpSbc, r3, r1); // r3 = r3 - r1 - CF
212 } else {
213 // Compute (r1:r0) = (r1:r0) - (r3:r2)
214 OpRegReg(kOpSub, r0, r2); // r0 = r0 - r2
215 OpRegReg(kOpSbc, r1, r3); // r1 = r1 - r3 - CF
216 }
217 switch (ccode) {
218 case kCondEq:
219 case kCondNe:
220 OpRegReg(kOpOr, r0, r1); // r0 = r0 | r1
221 break;
222 case kCondLe:
223 ccode = kCondGe;
224 break;
225 case kCondGt:
226 ccode = kCondLt;
227 break;
228 case kCondLt:
229 case kCondGe:
230 break;
231 default:
232 LOG(FATAL) << "Unexpected ccode: " << ccode;
233 }
234 OpCondBranch(ccode, taken);
235}
236
Mark Mendell412d4f82013-12-18 13:32:36 -0800237void X86Mir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
238 int64_t val, ConditionCode ccode) {
239 int32_t val_lo = Low32Bits(val);
240 int32_t val_hi = High32Bits(val);
241 LIR* taken = &block_label_list_[bb->taken];
242 LIR* not_taken = &block_label_list_[bb->fall_through];
243 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
244 int32_t low_reg = rl_src1.low_reg;
245 int32_t high_reg = rl_src1.high_reg;
246
247 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
248 int t_reg = AllocTemp();
249 OpRegRegReg(kOpOr, t_reg, low_reg, high_reg);
250 FreeTemp(t_reg);
251 OpCondBranch(ccode, taken);
252 return;
253 }
254
255 OpRegImm(kOpCmp, high_reg, val_hi);
256 switch (ccode) {
257 case kCondEq:
258 case kCondNe:
259 OpCondBranch(kCondNe, (ccode == kCondEq) ? not_taken : taken);
260 break;
261 case kCondLt:
262 OpCondBranch(kCondLt, taken);
263 OpCondBranch(kCondGt, not_taken);
264 ccode = kCondUlt;
265 break;
266 case kCondLe:
267 OpCondBranch(kCondLt, taken);
268 OpCondBranch(kCondGt, not_taken);
269 ccode = kCondLs;
270 break;
271 case kCondGt:
272 OpCondBranch(kCondGt, taken);
273 OpCondBranch(kCondLt, not_taken);
274 ccode = kCondHi;
275 break;
276 case kCondGe:
277 OpCondBranch(kCondGt, taken);
278 OpCondBranch(kCondLt, not_taken);
279 ccode = kCondUge;
280 break;
281 default:
282 LOG(FATAL) << "Unexpected ccode: " << ccode;
283 }
284 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
285}
286
Mark Mendell2bf31e62014-01-23 12:13:40 -0800287void X86Mir2Lir::CalculateMagicAndShift(int divisor, int& magic, int& shift) {
288 // It does not make sense to calculate magic and shift for zero divisor.
289 DCHECK_NE(divisor, 0);
290
291 /* According to H.S.Warren's Hacker's Delight Chapter 10 and
292 * T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
293 * The magic number M and shift S can be calculated in the following way:
294 * Let nc be the most positive value of numerator(n) such that nc = kd - 1,
295 * where divisor(d) >=2.
296 * Let nc be the most negative value of numerator(n) such that nc = kd + 1,
297 * where divisor(d) <= -2.
298 * Thus nc can be calculated like:
299 * nc = 2^31 + 2^31 % d - 1, where d >= 2
300 * nc = -2^31 + (2^31 + 1) % d, where d >= 2.
301 *
302 * So the shift p is the smallest p satisfying
303 * 2^p > nc * (d - 2^p % d), where d >= 2
304 * 2^p > nc * (d + 2^p % d), where d <= -2.
305 *
306 * the magic number M is calcuated by
307 * M = (2^p + d - 2^p % d) / d, where d >= 2
308 * M = (2^p - d - 2^p % d) / d, where d <= -2.
309 *
310 * Notice that p is always bigger than or equal to 32, so we just return 32-p as
311 * the shift number S.
312 */
313
314 int32_t p = 31;
315 const uint32_t two31 = 0x80000000U;
316
317 // Initialize the computations.
318 uint32_t abs_d = (divisor >= 0) ? divisor : -divisor;
319 uint32_t tmp = two31 + (static_cast<uint32_t>(divisor) >> 31);
320 uint32_t abs_nc = tmp - 1 - tmp % abs_d;
321 uint32_t quotient1 = two31 / abs_nc;
322 uint32_t remainder1 = two31 % abs_nc;
323 uint32_t quotient2 = two31 / abs_d;
324 uint32_t remainder2 = two31 % abs_d;
325
326 /*
327 * To avoid handling both positive and negative divisor, Hacker's Delight
328 * introduces a method to handle these 2 cases together to avoid duplication.
329 */
330 uint32_t delta;
331 do {
332 p++;
333 quotient1 = 2 * quotient1;
334 remainder1 = 2 * remainder1;
335 if (remainder1 >= abs_nc) {
336 quotient1++;
337 remainder1 = remainder1 - abs_nc;
338 }
339 quotient2 = 2 * quotient2;
340 remainder2 = 2 * remainder2;
341 if (remainder2 >= abs_d) {
342 quotient2++;
343 remainder2 = remainder2 - abs_d;
344 }
345 delta = abs_d - remainder2;
346 } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
347
348 magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1);
349 shift = p - 32;
350}
351
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, int reg_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700353 int lit, bool is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 LOG(FATAL) << "Unexpected use of GenDivRemLit for x86";
355 return rl_dest;
356}
357
Mark Mendell2bf31e62014-01-23 12:13:40 -0800358RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src,
359 int imm, bool is_div) {
360 // Use a multiply (and fixup) to perform an int div/rem by a constant.
361
362 // We have to use fixed registers, so flush all the temps.
363 FlushAllRegs();
364 LockCallTemps(); // Prepare for explicit register usage.
365
366 // Assume that the result will be in EDX.
367 RegLocation rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, kVectorNotUsed,
368 r2, INVALID_REG, INVALID_SREG, INVALID_SREG};
369
370 // handle 0x80000000 / -1 special case.
371 LIR *minint_branch = 0;
372 if (imm == -1) {
373 if (is_div) {
374 LoadValueDirectFixed(rl_src, r0);
375 OpRegImm(kOpCmp, r0, 0x80000000);
376 minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
377
378 // for x != MIN_INT, x / -1 == -x.
379 NewLIR1(kX86Neg32R, r0);
380
381 LIR* branch_around = NewLIR1(kX86Jmp8, 0);
382 // The target for cmp/jmp above.
383 minint_branch->target = NewLIR0(kPseudoTargetLabel);
384 // EAX already contains the right value (0x80000000),
385 branch_around->target = NewLIR0(kPseudoTargetLabel);
386 } else {
387 // x % -1 == 0.
388 LoadConstantNoClobber(r0, 0);
389 }
390 // For this case, return the result in EAX.
391 rl_result.low_reg = r0;
392 } else {
393 DCHECK(imm <= -2 || imm >= 2);
394 // Use H.S.Warren's Hacker's Delight Chapter 10 and
395 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
396 int magic, shift;
397 CalculateMagicAndShift(imm, magic, shift);
398
399 /*
400 * For imm >= 2,
401 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
402 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
403 * For imm <= -2,
404 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
405 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
406 * We implement this algorithm in the following way:
407 * 1. multiply magic number m and numerator n, get the higher 32bit result in EDX
408 * 2. if imm > 0 and magic < 0, add numerator to EDX
409 * if imm < 0 and magic > 0, sub numerator from EDX
410 * 3. if S !=0, SAR S bits for EDX
411 * 4. add 1 to EDX if EDX < 0
412 * 5. Thus, EDX is the quotient
413 */
414
415 // Numerator into EAX.
416 int numerator_reg = -1;
417 if (!is_div || (imm > 0 && magic < 0) || (imm < 0 && magic > 0)) {
418 // We will need the value later.
419 if (rl_src.location == kLocPhysReg) {
420 // We can use it directly.
421 DCHECK(rl_src.low_reg != r0 && rl_src.low_reg != r2);
422 numerator_reg = rl_src.low_reg;
423 } else {
424 LoadValueDirectFixed(rl_src, r1);
425 numerator_reg = r1;
426 }
427 OpRegCopy(r0, numerator_reg);
428 } else {
429 // Only need this once. Just put it into EAX.
430 LoadValueDirectFixed(rl_src, r0);
431 }
432
433 // EDX = magic.
434 LoadConstantNoClobber(r2, magic);
435
436 // EDX:EAX = magic & dividend.
437 NewLIR1(kX86Imul32DaR, r2);
438
439 if (imm > 0 && magic < 0) {
440 // Add numerator to EDX.
441 DCHECK_NE(numerator_reg, -1);
442 NewLIR2(kX86Add32RR, r2, numerator_reg);
443 } else if (imm < 0 && magic > 0) {
444 DCHECK_NE(numerator_reg, -1);
445 NewLIR2(kX86Sub32RR, r2, numerator_reg);
446 }
447
448 // Do we need the shift?
449 if (shift != 0) {
450 // Shift EDX by 'shift' bits.
451 NewLIR2(kX86Sar32RI, r2, shift);
452 }
453
454 // Add 1 to EDX if EDX < 0.
455
456 // Move EDX to EAX.
457 OpRegCopy(r0, r2);
458
459 // Move sign bit to bit 0, zeroing the rest.
460 NewLIR2(kX86Shr32RI, r2, 31);
461
462 // EDX = EDX + EAX.
463 NewLIR2(kX86Add32RR, r2, r0);
464
465 // Quotient is in EDX.
466 if (!is_div) {
467 // We need to compute the remainder.
468 // Remainder is divisor - (quotient * imm).
469 DCHECK_NE(numerator_reg, -1);
470 OpRegCopy(r0, numerator_reg);
471
472 // EAX = numerator * imm.
473 OpRegRegImm(kOpMul, r2, r2, imm);
474
475 // EDX -= EAX.
476 NewLIR2(kX86Sub32RR, r0, r2);
477
478 // For this case, return the result in EAX.
479 rl_result.low_reg = r0;
480 }
481 }
482
483 return rl_result;
484}
485
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, int reg_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700487 int reg_hi, bool is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 LOG(FATAL) << "Unexpected use of GenDivRem for x86";
489 return rl_dest;
490}
491
Mark Mendell2bf31e62014-01-23 12:13:40 -0800492RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
493 RegLocation rl_src2, bool is_div, bool check_zero) {
494 // We have to use fixed registers, so flush all the temps.
495 FlushAllRegs();
496 LockCallTemps(); // Prepare for explicit register usage.
497
498 // Load LHS into EAX.
499 LoadValueDirectFixed(rl_src1, r0);
500
501 // Load RHS into EBX.
502 LoadValueDirectFixed(rl_src2, r1);
503
504 // Copy LHS sign bit into EDX.
505 NewLIR0(kx86Cdq32Da);
506
507 if (check_zero) {
508 // Handle division by zero case.
509 GenImmedCheck(kCondEq, r1, 0, kThrowDivZero);
510 }
511
512 // Have to catch 0x80000000/-1 case, or we will get an exception!
513 OpRegImm(kOpCmp, r1, -1);
514 LIR *minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
515
516 // RHS is -1.
517 OpRegImm(kOpCmp, r0, 0x80000000);
518 LIR * minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
519
520 // In 0x80000000/-1 case.
521 if (!is_div) {
522 // For DIV, EAX is already right. For REM, we need EDX 0.
523 LoadConstantNoClobber(r2, 0);
524 }
525 LIR* done = NewLIR1(kX86Jmp8, 0);
526
527 // Expected case.
528 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
529 minint_branch->target = minus_one_branch->target;
530 NewLIR1(kX86Idivmod32DaR, r1);
531 done->target = NewLIR0(kPseudoTargetLabel);
532
533 // Result is in EAX for div and EDX for rem.
534 RegLocation rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, kVectorNotUsed,
535 r0, INVALID_REG, INVALID_SREG, INVALID_SREG};
536 if (!is_div) {
537 rl_result.low_reg = r2;
538 }
539 return rl_result;
540}
541
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700542bool X86Mir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 DCHECK_EQ(cu_->instruction_set, kX86);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800544
545 // Get the two arguments to the invoke and place them in GP registers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 RegLocation rl_src1 = info->args[0];
547 RegLocation rl_src2 = info->args[1];
548 rl_src1 = LoadValue(rl_src1, kCoreReg);
549 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800550
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 RegLocation rl_dest = InlineTarget(info);
552 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800553
554 /*
555 * If the result register is the same as the second element, then we need to be careful.
556 * The reason is that the first copy will inadvertently clobber the second element with
557 * the first one thus yielding the wrong result. Thus we do a swap in that case.
558 */
559 if (rl_result.low_reg == rl_src2.low_reg) {
560 std::swap(rl_src1, rl_src2);
561 }
562
563 // Pick the first integer as min/max.
564 OpRegCopy(rl_result.low_reg, rl_src1.low_reg);
565
566 // If the integers are both in the same register, then there is nothing else to do
567 // because they are equal and we have already moved one into the result.
568 if (rl_src1.low_reg != rl_src2.low_reg) {
569 // It is possible we didn't pick correctly so do the actual comparison now.
570 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
571
572 // Conditionally move the other integer into the destination register.
573 ConditionCode condition_code = is_min ? kCondGt : kCondLt;
574 OpCondRegReg(kOpCmov, condition_code, rl_result.low_reg, rl_src2.low_reg);
575 }
576
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 StoreValue(rl_dest, rl_result);
578 return true;
579}
580
Vladimir Markoe508a202013-11-04 15:24:22 +0000581bool X86Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
582 RegLocation rl_src_address = info->args[0]; // long address
583 rl_src_address.wide = 0; // ignore high half in info->args[1]
584 RegLocation rl_dest = InlineTarget(info);
585 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
586 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
587 if (size == kLong) {
588 // Unaligned access is allowed on x86.
589 LoadBaseDispWide(rl_address.low_reg, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
590 StoreValueWide(rl_dest, rl_result);
591 } else {
592 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
593 // Unaligned access is allowed on x86.
594 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, size, INVALID_SREG);
595 StoreValue(rl_dest, rl_result);
596 }
597 return true;
598}
599
600bool X86Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
601 RegLocation rl_src_address = info->args[0]; // long address
602 rl_src_address.wide = 0; // ignore high half in info->args[1]
603 RegLocation rl_src_value = info->args[2]; // [size] value
604 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
605 if (size == kLong) {
606 // Unaligned access is allowed on x86.
607 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
608 StoreBaseDispWide(rl_address.low_reg, 0, rl_value.low_reg, rl_value.high_reg);
609 } else {
610 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
611 // Unaligned access is allowed on x86.
612 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
613 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, size);
614 }
615 return true;
616}
617
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700618void X86Mir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 NewLIR5(kX86Lea32RA, rBase, reg1, reg2, scale, offset);
620}
621
Ian Rogers468532e2013-08-05 10:56:33 -0700622void X86Mir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
623 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624}
625
Vladimir Marko1c282e22013-11-21 14:49:47 +0000626bool X86Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Vladimir Markoc29bb612013-11-27 16:47:25 +0000627 DCHECK_EQ(cu_->instruction_set, kX86);
628 // Unused - RegLocation rl_src_unsafe = info->args[0];
629 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
630 RegLocation rl_src_offset = info->args[2]; // long low
631 rl_src_offset.wide = 0; // ignore high half in info->args[3]
632 RegLocation rl_src_expected = info->args[4]; // int, long or Object
633 // If is_long, high half is in info->args[5]
634 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
635 // If is_long, high half is in info->args[7]
636
637 if (is_long) {
Vladimir Marko70b797d2013-12-03 15:25:24 +0000638 FlushAllRegs();
639 LockCallTemps();
Vladimir Markoa6fd8ba2013-12-13 10:53:49 +0000640 LoadValueDirectWideFixed(rl_src_expected, rAX, rDX);
641 LoadValueDirectWideFixed(rl_src_new_value, rBX, rCX);
Vladimir Marko70b797d2013-12-03 15:25:24 +0000642 NewLIR1(kX86Push32R, rDI);
643 MarkTemp(rDI);
644 LockTemp(rDI);
645 NewLIR1(kX86Push32R, rSI);
646 MarkTemp(rSI);
647 LockTemp(rSI);
Vladimir Markoa6fd8ba2013-12-13 10:53:49 +0000648 const int push_offset = 4 /* push edi */ + 4 /* push esi */;
649 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src_obj.s_reg_low) + push_offset, rDI);
650 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src_offset.s_reg_low) + push_offset, rSI);
Vladimir Marko70b797d2013-12-03 15:25:24 +0000651 NewLIR4(kX86LockCmpxchg8bA, rDI, rSI, 0, 0);
652 FreeTemp(rSI);
653 UnmarkTemp(rSI);
654 NewLIR1(kX86Pop32R, rSI);
655 FreeTemp(rDI);
656 UnmarkTemp(rDI);
657 NewLIR1(kX86Pop32R, rDI);
658 FreeCallTemps();
Vladimir Markoc29bb612013-11-27 16:47:25 +0000659 } else {
660 // EAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in EAX.
661 FlushReg(r0);
662 LockTemp(r0);
663
664 // Release store semantics, get the barrier out of the way. TODO: revisit
665 GenMemBarrier(kStoreLoad);
666
667 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
668 RegLocation rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
669
670 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
671 // Mark card for object assuming new value is stored.
672 FreeTemp(r0); // Temporarily release EAX for MarkGCCard().
673 MarkGCCard(rl_new_value.low_reg, rl_object.low_reg);
674 LockTemp(r0);
675 }
676
677 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
678 LoadValueDirect(rl_src_expected, r0);
679 NewLIR5(kX86LockCmpxchgAR, rl_object.low_reg, rl_offset.low_reg, 0, 0, rl_new_value.low_reg);
680
681 FreeTemp(r0);
682 }
683
684 // Convert ZF to boolean
685 RegLocation rl_dest = InlineTarget(info); // boolean place for result
686 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
687 NewLIR2(kX86Set8R, rl_result.low_reg, kX86CondZ);
688 NewLIR2(kX86Movzx8RR, rl_result.low_reg, rl_result.low_reg);
689 StoreValue(rl_dest, rl_result);
690 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691}
692
693LIR* X86Mir2Lir::OpPcRelLoad(int reg, LIR* target) {
694 LOG(FATAL) << "Unexpected use of OpPcRelLoad for x86";
695 return NULL;
696}
697
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700698LIR* X86Mir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 LOG(FATAL) << "Unexpected use of OpVldm for x86";
700 return NULL;
701}
702
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700703LIR* X86Mir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 LOG(FATAL) << "Unexpected use of OpVstm for x86";
705 return NULL;
706}
707
708void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
709 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700710 int first_bit, int second_bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 int t_reg = AllocTemp();
712 OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, second_bit - first_bit);
713 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, t_reg);
714 FreeTemp(t_reg);
715 if (first_bit != 0) {
716 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_result.low_reg, first_bit);
717 }
718}
719
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700720void X86Mir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800721 // We are not supposed to clobber either of the provided registers, so allocate
722 // a temporary to use for the check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 int t_reg = AllocTemp();
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800724
725 // Doing an OR is a quick way to check if both registers are zero. This will set the flags.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 OpRegRegReg(kOpOr, t_reg, reg_lo, reg_hi);
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800727
728 // In case of zero, throw ArithmeticException.
729 GenCheck(kCondEq, kThrowDivZero);
730
731 // The temp is no longer needed so free it at this time.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 FreeTemp(t_reg);
733}
734
735// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700736LIR* X86Mir2Lir::OpTestSuspend(LIR* target) {
Ian Rogers468532e2013-08-05 10:56:33 -0700737 OpTlsCmp(Thread::ThreadFlagsOffset(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 return OpCondBranch((target == NULL) ? kCondNe : kCondEq, target);
739}
740
741// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700742LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 OpRegImm(kOpSub, reg, 1);
744 return OpCmpImmBranch(c_code, reg, 0, target);
745}
746
buzbee11b63d12013-08-27 07:34:17 -0700747bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700748 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 LOG(FATAL) << "Unexpected use of smallLiteralDive in x86";
750 return false;
751}
752
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700753LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 LOG(FATAL) << "Unexpected use of OpIT in x86";
755 return NULL;
756}
757
Mark Mendelle02d48f2014-01-15 11:19:23 -0800758void X86Mir2Lir::GenMulLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700759 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 LOG(FATAL) << "Unexpected use of GenX86Long for x86";
761}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800762
763void X86Mir2Lir::GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src,
764 Instruction::Code op) {
765 DCHECK_EQ(rl_dest.location, kLocPhysReg);
766 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
767 if (rl_src.location == kLocPhysReg) {
768 // Both operands are in registers.
769 if (rl_dest.low_reg == rl_src.high_reg) {
770 // The registers are the same, so we would clobber it before the use.
771 int temp_reg = AllocTemp();
772 OpRegCopy(temp_reg, rl_dest.low_reg);
773 rl_src.high_reg = temp_reg;
774 }
775 NewLIR2(x86op, rl_dest.low_reg, rl_src.low_reg);
776
777 x86op = GetOpcode(op, rl_dest, rl_src, true);
778 NewLIR2(x86op, rl_dest.high_reg, rl_src.high_reg);
779 FreeTemp(rl_src.low_reg);
780 FreeTemp(rl_src.high_reg);
781 return;
782 }
783
784 // RHS is in memory.
785 DCHECK((rl_src.location == kLocDalvikFrame) ||
786 (rl_src.location == kLocCompilerTemp));
787 int rBase = TargetReg(kSp);
788 int displacement = SRegOffset(rl_src.s_reg_low);
789
790 LIR *lir = NewLIR3(x86op, rl_dest.low_reg, rBase, displacement + LOWORD_OFFSET);
791 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
792 true /* is_load */, true /* is64bit */);
793 x86op = GetOpcode(op, rl_dest, rl_src, true);
794 lir = NewLIR3(x86op, rl_dest.high_reg, rBase, displacement + HIWORD_OFFSET);
795 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
796 true /* is_load */, true /* is64bit */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797}
798
Mark Mendelle02d48f2014-01-15 11:19:23 -0800799void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
800 rl_dest = UpdateLocWide(rl_dest);
801 if (rl_dest.location == kLocPhysReg) {
802 // Ensure we are in a register pair
803 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
804
805 rl_src = UpdateLocWide(rl_src);
806 GenLongRegOrMemOp(rl_result, rl_src, op);
807 StoreFinalValueWide(rl_dest, rl_result);
808 return;
809 }
810
811 // It wasn't in registers, so it better be in memory.
812 DCHECK((rl_dest.location == kLocDalvikFrame) ||
813 (rl_dest.location == kLocCompilerTemp));
814 rl_src = LoadValueWide(rl_src, kCoreReg);
815
816 // Operate directly into memory.
817 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
818 int rBase = TargetReg(kSp);
819 int displacement = SRegOffset(rl_dest.s_reg_low);
820
821 LIR *lir = NewLIR3(x86op, rBase, displacement + LOWORD_OFFSET, rl_src.low_reg);
822 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
823 false /* is_load */, true /* is64bit */);
824 x86op = GetOpcode(op, rl_dest, rl_src, true);
825 lir = NewLIR3(x86op, rBase, displacement + HIWORD_OFFSET, rl_src.high_reg);
826 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
827 false /* is_load */, true /* is64bit */);
828 FreeTemp(rl_src.low_reg);
829 FreeTemp(rl_src.high_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830}
831
Mark Mendelle02d48f2014-01-15 11:19:23 -0800832void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src1,
833 RegLocation rl_src2, Instruction::Code op,
834 bool is_commutative) {
835 // Is this really a 2 operand operation?
836 switch (op) {
837 case Instruction::ADD_LONG_2ADDR:
838 case Instruction::SUB_LONG_2ADDR:
839 case Instruction::AND_LONG_2ADDR:
840 case Instruction::OR_LONG_2ADDR:
841 case Instruction::XOR_LONG_2ADDR:
842 GenLongArith(rl_dest, rl_src2, op);
843 return;
844 default:
845 break;
846 }
847
848 if (rl_dest.location == kLocPhysReg) {
849 RegLocation rl_result = LoadValueWide(rl_src1, kCoreReg);
850
851 // We are about to clobber the LHS, so it needs to be a temp.
852 rl_result = ForceTempWide(rl_result);
853
854 // Perform the operation using the RHS.
855 rl_src2 = UpdateLocWide(rl_src2);
856 GenLongRegOrMemOp(rl_result, rl_src2, op);
857
858 // And now record that the result is in the temp.
859 StoreFinalValueWide(rl_dest, rl_result);
860 return;
861 }
862
863 // It wasn't in registers, so it better be in memory.
864 DCHECK((rl_dest.location == kLocDalvikFrame) ||
865 (rl_dest.location == kLocCompilerTemp));
866 rl_src1 = UpdateLocWide(rl_src1);
867 rl_src2 = UpdateLocWide(rl_src2);
868
869 // Get one of the source operands into temporary register.
870 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
871 if (IsTemp(rl_src1.low_reg) && IsTemp(rl_src1.high_reg)) {
872 GenLongRegOrMemOp(rl_src1, rl_src2, op);
873 } else if (is_commutative) {
874 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
875 // We need at least one of them to be a temporary.
876 if (!(IsTemp(rl_src2.low_reg) && IsTemp(rl_src2.high_reg))) {
877 rl_src1 = ForceTempWide(rl_src1);
878 }
879 GenLongRegOrMemOp(rl_src1, rl_src2, op);
880 } else {
881 // Need LHS to be the temp.
882 rl_src1 = ForceTempWide(rl_src1);
883 GenLongRegOrMemOp(rl_src1, rl_src2, op);
884 }
885
886 StoreFinalValueWide(rl_dest, rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887}
888
Mark Mendelle02d48f2014-01-15 11:19:23 -0800889void X86Mir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700890 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -0800891 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
892}
893
894void X86Mir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest,
895 RegLocation rl_src1, RegLocation rl_src2) {
896 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, false);
897}
898
899void X86Mir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest,
900 RegLocation rl_src1, RegLocation rl_src2) {
901 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
902}
903
904void X86Mir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest,
905 RegLocation rl_src1, RegLocation rl_src2) {
906 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
907}
908
909void X86Mir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest,
910 RegLocation rl_src1, RegLocation rl_src2) {
911 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912}
913
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700914void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Mark Mendelle02d48f2014-01-15 11:19:23 -0800915 rl_src = LoadValueWide(rl_src, kCoreReg);
916 RegLocation rl_result = ForceTempWide(rl_src);
917 if (rl_dest.low_reg == rl_src.high_reg) {
918 // The registers are the same, so we would clobber it before the use.
919 int temp_reg = AllocTemp();
920 OpRegCopy(temp_reg, rl_result.low_reg);
921 rl_result.high_reg = temp_reg;
922 }
923 OpRegReg(kOpNeg, rl_result.low_reg, rl_result.low_reg); // rLow = -rLow
924 OpRegImm(kOpAdc, rl_result.high_reg, 0); // rHigh = rHigh + CF
925 OpRegReg(kOpNeg, rl_result.high_reg, rl_result.high_reg); // rHigh = -rHigh
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 StoreValueWide(rl_dest, rl_result);
927}
928
Ian Rogers468532e2013-08-05 10:56:33 -0700929void X86Mir2Lir::OpRegThreadMem(OpKind op, int r_dest, ThreadOffset thread_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700930 X86OpCode opcode = kX86Bkpt;
931 switch (op) {
932 case kOpCmp: opcode = kX86Cmp32RT; break;
933 case kOpMov: opcode = kX86Mov32RT; break;
934 default:
935 LOG(FATAL) << "Bad opcode: " << op;
936 break;
937 }
Ian Rogers468532e2013-08-05 10:56:33 -0700938 NewLIR2(opcode, r_dest, thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939}
940
941/*
942 * Generate array load
943 */
944void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700945 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 RegisterClass reg_class = oat_reg_class_by_size(size);
947 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948 RegLocation rl_result;
949 rl_array = LoadValue(rl_array, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950
Mark Mendell343adb52013-12-18 06:02:17 -0800951 int data_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700952 if (size == kLong || size == kDouble) {
953 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
954 } else {
955 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
956 }
957
Mark Mendell343adb52013-12-18 06:02:17 -0800958 bool constant_index = rl_index.is_const;
959 int32_t constant_index_value = 0;
960 if (!constant_index) {
961 rl_index = LoadValue(rl_index, kCoreReg);
962 } else {
963 constant_index_value = mir_graph_->ConstantValue(rl_index);
964 // If index is constant, just fold it into the data offset
965 data_offset += constant_index_value << scale;
966 // treat as non array below
967 rl_index.low_reg = INVALID_REG;
968 }
969
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 /* null object? */
971 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
972
973 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -0800974 if (constant_index) {
975 GenMemImmedCheck(kCondLs, rl_array.low_reg, len_offset,
976 constant_index_value, kThrowConstantArrayBounds);
977 } else {
978 GenRegMemCheck(kCondUge, rl_index.low_reg, rl_array.low_reg,
979 len_offset, kThrowArrayBounds);
980 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 }
Mark Mendell343adb52013-12-18 06:02:17 -0800982 rl_result = EvalLoc(rl_dest, reg_class, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 if ((size == kLong) || (size == kDouble)) {
Mark Mendell343adb52013-12-18 06:02:17 -0800984 LoadBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, rl_result.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985 rl_result.high_reg, size, INVALID_SREG);
986 StoreValueWide(rl_dest, rl_result);
987 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 LoadBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale,
989 data_offset, rl_result.low_reg, INVALID_REG, size,
990 INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 StoreValue(rl_dest, rl_result);
992 }
993}
994
995/*
996 * Generate array store
997 *
998 */
999void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001000 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 RegisterClass reg_class = oat_reg_class_by_size(size);
1002 int len_offset = mirror::Array::LengthOffset().Int32Value();
1003 int data_offset;
1004
1005 if (size == kLong || size == kDouble) {
1006 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1007 } else {
1008 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1009 }
1010
1011 rl_array = LoadValue(rl_array, kCoreReg);
Mark Mendell343adb52013-12-18 06:02:17 -08001012 bool constant_index = rl_index.is_const;
1013 int32_t constant_index_value = 0;
1014 if (!constant_index) {
1015 rl_index = LoadValue(rl_index, kCoreReg);
1016 } else {
1017 // If index is constant, just fold it into the data offset
1018 constant_index_value = mir_graph_->ConstantValue(rl_index);
1019 data_offset += constant_index_value << scale;
1020 // treat as non array below
1021 rl_index.low_reg = INVALID_REG;
1022 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023
1024 /* null object? */
1025 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
1026
1027 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08001028 if (constant_index) {
1029 GenMemImmedCheck(kCondLs, rl_array.low_reg, len_offset,
1030 constant_index_value, kThrowConstantArrayBounds);
1031 } else {
1032 GenRegMemCheck(kCondUge, rl_index.low_reg, rl_array.low_reg,
1033 len_offset, kThrowArrayBounds);
1034 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 }
1036 if ((size == kLong) || (size == kDouble)) {
1037 rl_src = LoadValueWide(rl_src, reg_class);
1038 } else {
1039 rl_src = LoadValue(rl_src, reg_class);
1040 }
1041 // If the src reg can't be byte accessed, move it to a temp first.
1042 if ((size == kSignedByte || size == kUnsignedByte) && rl_src.low_reg >= 4) {
1043 int temp = AllocTemp();
1044 OpRegCopy(temp, rl_src.low_reg);
1045 StoreBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, temp,
1046 INVALID_REG, size, INVALID_SREG);
1047 } else {
1048 StoreBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, rl_src.low_reg,
1049 rl_src.high_reg, size, INVALID_SREG);
1050 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001051 if (card_mark) {
Ian Rogers773aab12013-10-14 13:50:10 -07001052 // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
Mark Mendell343adb52013-12-18 06:02:17 -08001053 if (!constant_index) {
1054 FreeTemp(rl_index.low_reg);
1055 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001056 MarkGCCard(rl_src.low_reg, rl_array.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 }
1058}
1059
1060void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001061 RegLocation rl_src1, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 // Default implementation is just to ignore the constant case.
1063 GenShiftOpLong(opcode, rl_dest, rl_src1, rl_shift);
1064}
1065
1066void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001067 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001068 switch (opcode) {
1069 case Instruction::ADD_LONG:
1070 case Instruction::AND_LONG:
1071 case Instruction::OR_LONG:
1072 case Instruction::XOR_LONG:
1073 if (rl_src2.is_const) {
1074 GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
1075 } else {
1076 DCHECK(rl_src1.is_const);
1077 GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
1078 }
1079 break;
1080 case Instruction::SUB_LONG:
1081 case Instruction::SUB_LONG_2ADDR:
1082 if (rl_src2.is_const) {
1083 GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
1084 } else {
1085 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
1086 }
1087 break;
1088 case Instruction::ADD_LONG_2ADDR:
1089 case Instruction::OR_LONG_2ADDR:
1090 case Instruction::XOR_LONG_2ADDR:
1091 case Instruction::AND_LONG_2ADDR:
1092 if (rl_src2.is_const) {
1093 GenLongImm(rl_dest, rl_src2, opcode);
1094 } else {
1095 DCHECK(rl_src1.is_const);
1096 GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
1097 }
1098 break;
1099 default:
1100 // Default - bail to non-const handler.
1101 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1102 break;
1103 }
1104}
1105
1106bool X86Mir2Lir::IsNoOp(Instruction::Code op, int32_t value) {
1107 switch (op) {
1108 case Instruction::AND_LONG_2ADDR:
1109 case Instruction::AND_LONG:
1110 return value == -1;
1111 case Instruction::OR_LONG:
1112 case Instruction::OR_LONG_2ADDR:
1113 case Instruction::XOR_LONG:
1114 case Instruction::XOR_LONG_2ADDR:
1115 return value == 0;
1116 default:
1117 return false;
1118 }
1119}
1120
1121X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
1122 bool is_high_op) {
1123 bool rhs_in_mem = rhs.location != kLocPhysReg;
1124 bool dest_in_mem = dest.location != kLocPhysReg;
1125 DCHECK(!rhs_in_mem || !dest_in_mem);
1126 switch (op) {
1127 case Instruction::ADD_LONG:
1128 case Instruction::ADD_LONG_2ADDR:
1129 if (dest_in_mem) {
1130 return is_high_op ? kX86Adc32MR : kX86Add32MR;
1131 } else if (rhs_in_mem) {
1132 return is_high_op ? kX86Adc32RM : kX86Add32RM;
1133 }
1134 return is_high_op ? kX86Adc32RR : kX86Add32RR;
1135 case Instruction::SUB_LONG:
1136 case Instruction::SUB_LONG_2ADDR:
1137 if (dest_in_mem) {
1138 return is_high_op ? kX86Sbb32MR : kX86Sub32MR;
1139 } else if (rhs_in_mem) {
1140 return is_high_op ? kX86Sbb32RM : kX86Sub32RM;
1141 }
1142 return is_high_op ? kX86Sbb32RR : kX86Sub32RR;
1143 case Instruction::AND_LONG_2ADDR:
1144 case Instruction::AND_LONG:
1145 if (dest_in_mem) {
1146 return kX86And32MR;
1147 }
1148 return rhs_in_mem ? kX86And32RM : kX86And32RR;
1149 case Instruction::OR_LONG:
1150 case Instruction::OR_LONG_2ADDR:
1151 if (dest_in_mem) {
1152 return kX86Or32MR;
1153 }
1154 return rhs_in_mem ? kX86Or32RM : kX86Or32RR;
1155 case Instruction::XOR_LONG:
1156 case Instruction::XOR_LONG_2ADDR:
1157 if (dest_in_mem) {
1158 return kX86Xor32MR;
1159 }
1160 return rhs_in_mem ? kX86Xor32RM : kX86Xor32RR;
1161 default:
1162 LOG(FATAL) << "Unexpected opcode: " << op;
1163 return kX86Add32RR;
1164 }
1165}
1166
1167X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op,
1168 int32_t value) {
1169 bool in_mem = loc.location != kLocPhysReg;
1170 bool byte_imm = IS_SIMM8(value);
1171 DCHECK(in_mem || !IsFpReg(loc.low_reg));
1172 switch (op) {
1173 case Instruction::ADD_LONG:
1174 case Instruction::ADD_LONG_2ADDR:
1175 if (byte_imm) {
1176 if (in_mem) {
1177 return is_high_op ? kX86Adc32MI8 : kX86Add32MI8;
1178 }
1179 return is_high_op ? kX86Adc32RI8 : kX86Add32RI8;
1180 }
1181 if (in_mem) {
1182 return is_high_op ? kX86Adc32MI : kX86Add32MI;
1183 }
1184 return is_high_op ? kX86Adc32RI : kX86Add32RI;
1185 case Instruction::SUB_LONG:
1186 case Instruction::SUB_LONG_2ADDR:
1187 if (byte_imm) {
1188 if (in_mem) {
1189 return is_high_op ? kX86Sbb32MI8 : kX86Sub32MI8;
1190 }
1191 return is_high_op ? kX86Sbb32RI8 : kX86Sub32RI8;
1192 }
1193 if (in_mem) {
1194 return is_high_op ? kX86Sbb32MI : kX86Sub32MI;
1195 }
1196 return is_high_op ? kX86Sbb32RI : kX86Sub32RI;
1197 case Instruction::AND_LONG_2ADDR:
1198 case Instruction::AND_LONG:
1199 if (byte_imm) {
1200 return in_mem ? kX86And32MI8 : kX86And32RI8;
1201 }
1202 return in_mem ? kX86And32MI : kX86And32RI;
1203 case Instruction::OR_LONG:
1204 case Instruction::OR_LONG_2ADDR:
1205 if (byte_imm) {
1206 return in_mem ? kX86Or32MI8 : kX86Or32RI8;
1207 }
1208 return in_mem ? kX86Or32MI : kX86Or32RI;
1209 case Instruction::XOR_LONG:
1210 case Instruction::XOR_LONG_2ADDR:
1211 if (byte_imm) {
1212 return in_mem ? kX86Xor32MI8 : kX86Xor32RI8;
1213 }
1214 return in_mem ? kX86Xor32MI : kX86Xor32RI;
1215 default:
1216 LOG(FATAL) << "Unexpected opcode: " << op;
1217 return kX86Add32MI;
1218 }
1219}
1220
1221void X86Mir2Lir::GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
1222 DCHECK(rl_src.is_const);
1223 int64_t val = mir_graph_->ConstantValueWide(rl_src);
1224 int32_t val_lo = Low32Bits(val);
1225 int32_t val_hi = High32Bits(val);
1226 rl_dest = UpdateLocWide(rl_dest);
1227
1228 // Can we just do this into memory?
1229 if ((rl_dest.location == kLocDalvikFrame) ||
1230 (rl_dest.location == kLocCompilerTemp)) {
1231 int rBase = TargetReg(kSp);
1232 int displacement = SRegOffset(rl_dest.s_reg_low);
1233
1234 if (!IsNoOp(op, val_lo)) {
1235 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
1236 LIR *lir = NewLIR3(x86op, rBase, displacement + LOWORD_OFFSET, val_lo);
1237 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
1238 false /* is_load */, true /* is64bit */);
1239 }
1240 if (!IsNoOp(op, val_hi)) {
1241 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
1242 LIR *lir = NewLIR3(x86op, rBase, displacement + HIWORD_OFFSET, val_hi);
1243 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1244 false /* is_load */, true /* is64bit */);
1245 }
1246 return;
1247 }
1248
1249 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1250 DCHECK_EQ(rl_result.location, kLocPhysReg);
1251 DCHECK(!IsFpReg(rl_result.low_reg));
1252
1253 if (!IsNoOp(op, val_lo)) {
1254 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
1255 NewLIR2(x86op, rl_result.low_reg, val_lo);
1256 }
1257 if (!IsNoOp(op, val_hi)) {
1258 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
1259 NewLIR2(x86op, rl_result.high_reg, val_hi);
1260 }
1261 StoreValueWide(rl_dest, rl_result);
1262}
1263
1264void X86Mir2Lir::GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1,
1265 RegLocation rl_src2, Instruction::Code op) {
1266 DCHECK(rl_src2.is_const);
1267 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1268 int32_t val_lo = Low32Bits(val);
1269 int32_t val_hi = High32Bits(val);
1270 rl_dest = UpdateLocWide(rl_dest);
1271 rl_src1 = UpdateLocWide(rl_src1);
1272
1273 // Can we do this directly into the destination registers?
1274 if (rl_dest.location == kLocPhysReg && rl_src1.location == kLocPhysReg &&
1275 rl_dest.low_reg == rl_src1.low_reg && rl_dest.high_reg == rl_src1.high_reg &&
1276 !IsFpReg(rl_dest.low_reg)) {
1277 if (!IsNoOp(op, val_lo)) {
1278 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
1279 NewLIR2(x86op, rl_dest.low_reg, val_lo);
1280 }
1281 if (!IsNoOp(op, val_hi)) {
1282 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
1283 NewLIR2(x86op, rl_dest.high_reg, val_hi);
1284 }
1285 return;
1286 }
1287
1288 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1289 DCHECK_EQ(rl_src1.location, kLocPhysReg);
1290
1291 // We need the values to be in a temporary
1292 RegLocation rl_result = ForceTempWide(rl_src1);
1293 if (!IsNoOp(op, val_lo)) {
1294 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
1295 NewLIR2(x86op, rl_result.low_reg, val_lo);
1296 }
1297 if (!IsNoOp(op, val_hi)) {
1298 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
1299 NewLIR2(x86op, rl_result.high_reg, val_hi);
1300 }
1301
1302 StoreFinalValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303}
1304
1305} // namespace art