blob: 3f5f33c93904de9c222c93915941b3c5b596e276 [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) {
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800183 RegLocation rl_result;
184 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
185 RegLocation rl_dest = mir_graph_->GetDest(mir);
186 rl_src = LoadValue(rl_src, kCoreReg);
187
188 // The kMirOpSelect has two variants, one for constants and one for moves.
189 const bool is_constant_case = (mir->ssa_rep->num_uses == 1);
190
191 if (is_constant_case) {
192 int true_val = mir->dalvikInsn.vB;
193 int false_val = mir->dalvikInsn.vC;
194 rl_result = EvalLoc(rl_dest, kCoreReg, true);
195
196 /*
197 * 1) When the true case is zero and result_reg is not same as src_reg:
198 * xor result_reg, result_reg
199 * cmp $0, src_reg
200 * mov t1, $false_case
201 * cmovnz result_reg, t1
202 * 2) When the false case is zero and result_reg is not same as src_reg:
203 * xor result_reg, result_reg
204 * cmp $0, src_reg
205 * mov t1, $true_case
206 * cmovz result_reg, t1
207 * 3) All other cases (we do compare first to set eflags):
208 * cmp $0, src_reg
209 * mov result_reg, $true_case
210 * mov t1, $false_case
211 * cmovnz result_reg, t1
212 */
213 const bool result_reg_same_as_src = (rl_src.location == kLocPhysReg && rl_src.low_reg == rl_result.low_reg);
214 const bool true_zero_case = (true_val == 0 && false_val != 0 && !result_reg_same_as_src);
215 const bool false_zero_case = (false_val == 0 && true_val != 0 && !result_reg_same_as_src);
216 const bool catch_all_case = !(true_zero_case || false_zero_case);
217
218 if (true_zero_case || false_zero_case) {
219 OpRegReg(kOpXor, rl_result.low_reg, rl_result.low_reg);
220 }
221
222 if (true_zero_case || false_zero_case || catch_all_case) {
223 OpRegImm(kOpCmp, rl_src.low_reg, 0);
224 }
225
226 if (catch_all_case) {
227 OpRegImm(kOpMov, rl_result.low_reg, true_val);
228 }
229
230 if (true_zero_case || false_zero_case || catch_all_case) {
231 int immediateForTemp = false_zero_case ? true_val : false_val;
232 int temp1_reg = AllocTemp();
233 OpRegImm(kOpMov, temp1_reg, immediateForTemp);
234
235 ConditionCode cc = false_zero_case ? kCondEq : kCondNe;
236 OpCondRegReg(kOpCmov, cc, rl_result.low_reg, temp1_reg);
237
238 FreeTemp(temp1_reg);
239 }
240 } else {
241 RegLocation rl_true = mir_graph_->GetSrc(mir, 1);
242 RegLocation rl_false = mir_graph_->GetSrc(mir, 2);
243 rl_true = LoadValue(rl_true, kCoreReg);
244 rl_false = LoadValue(rl_false, kCoreReg);
245 rl_result = EvalLoc(rl_dest, kCoreReg, true);
246
247 /*
248 * 1) When true case is already in place:
249 * cmp $0, src_reg
250 * cmovnz result_reg, false_reg
251 * 2) When false case is already in place:
252 * cmp $0, src_reg
253 * cmovz result_reg, true_reg
254 * 3) When neither cases are in place:
255 * cmp $0, src_reg
256 * mov result_reg, true_reg
257 * cmovnz result_reg, false_reg
258 */
259
260 // kMirOpSelect is generated just for conditional cases when comparison is done with zero.
261 OpRegImm(kOpCmp, rl_src.low_reg, 0);
262
263 if (rl_result.low_reg == rl_true.low_reg) {
264 OpCondRegReg(kOpCmov, kCondNe, rl_result.low_reg, rl_false.low_reg);
265 } else if (rl_result.low_reg == rl_false.low_reg) {
266 OpCondRegReg(kOpCmov, kCondEq, rl_result.low_reg, rl_true.low_reg);
267 } else {
268 OpRegCopy(rl_result.low_reg, rl_true.low_reg);
269 OpCondRegReg(kOpCmov, kCondNe, rl_result.low_reg, rl_false.low_reg);
270 }
271 }
272
273 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274}
275
276void X86Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
buzbee0d829482013-10-11 15:24:55 -0700277 LIR* taken = &block_label_list_[bb->taken];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
279 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Vladimir Markoa8946072014-01-22 10:30:44 +0000280 ConditionCode ccode = mir->meta.ccode;
Mark Mendell412d4f82013-12-18 13:32:36 -0800281
282 if (rl_src1.is_const) {
283 std::swap(rl_src1, rl_src2);
284 ccode = FlipComparisonOrder(ccode);
285 }
286 if (rl_src2.is_const) {
287 // Do special compare/branch against simple const operand
288 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
289 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
290 return;
291 }
292
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 FlushAllRegs();
294 LockCallTemps(); // Prepare for explicit register usage
295 LoadValueDirectWideFixed(rl_src1, r0, r1);
296 LoadValueDirectWideFixed(rl_src2, r2, r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 // Swap operands and condition code to prevent use of zero flag.
298 if (ccode == kCondLe || ccode == kCondGt) {
299 // Compute (r3:r2) = (r3:r2) - (r1:r0)
300 OpRegReg(kOpSub, r2, r0); // r2 = r2 - r0
301 OpRegReg(kOpSbc, r3, r1); // r3 = r3 - r1 - CF
302 } else {
303 // Compute (r1:r0) = (r1:r0) - (r3:r2)
304 OpRegReg(kOpSub, r0, r2); // r0 = r0 - r2
305 OpRegReg(kOpSbc, r1, r3); // r1 = r1 - r3 - CF
306 }
307 switch (ccode) {
308 case kCondEq:
309 case kCondNe:
310 OpRegReg(kOpOr, r0, r1); // r0 = r0 | r1
311 break;
312 case kCondLe:
313 ccode = kCondGe;
314 break;
315 case kCondGt:
316 ccode = kCondLt;
317 break;
318 case kCondLt:
319 case kCondGe:
320 break;
321 default:
322 LOG(FATAL) << "Unexpected ccode: " << ccode;
323 }
324 OpCondBranch(ccode, taken);
325}
326
Mark Mendell412d4f82013-12-18 13:32:36 -0800327void X86Mir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
328 int64_t val, ConditionCode ccode) {
329 int32_t val_lo = Low32Bits(val);
330 int32_t val_hi = High32Bits(val);
331 LIR* taken = &block_label_list_[bb->taken];
332 LIR* not_taken = &block_label_list_[bb->fall_through];
333 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
334 int32_t low_reg = rl_src1.low_reg;
335 int32_t high_reg = rl_src1.high_reg;
336
337 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
338 int t_reg = AllocTemp();
339 OpRegRegReg(kOpOr, t_reg, low_reg, high_reg);
340 FreeTemp(t_reg);
341 OpCondBranch(ccode, taken);
342 return;
343 }
344
345 OpRegImm(kOpCmp, high_reg, val_hi);
346 switch (ccode) {
347 case kCondEq:
348 case kCondNe:
349 OpCondBranch(kCondNe, (ccode == kCondEq) ? not_taken : taken);
350 break;
351 case kCondLt:
352 OpCondBranch(kCondLt, taken);
353 OpCondBranch(kCondGt, not_taken);
354 ccode = kCondUlt;
355 break;
356 case kCondLe:
357 OpCondBranch(kCondLt, taken);
358 OpCondBranch(kCondGt, not_taken);
359 ccode = kCondLs;
360 break;
361 case kCondGt:
362 OpCondBranch(kCondGt, taken);
363 OpCondBranch(kCondLt, not_taken);
364 ccode = kCondHi;
365 break;
366 case kCondGe:
367 OpCondBranch(kCondGt, taken);
368 OpCondBranch(kCondLt, not_taken);
369 ccode = kCondUge;
370 break;
371 default:
372 LOG(FATAL) << "Unexpected ccode: " << ccode;
373 }
374 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
375}
376
Mark Mendell2bf31e62014-01-23 12:13:40 -0800377void X86Mir2Lir::CalculateMagicAndShift(int divisor, int& magic, int& shift) {
378 // It does not make sense to calculate magic and shift for zero divisor.
379 DCHECK_NE(divisor, 0);
380
381 /* According to H.S.Warren's Hacker's Delight Chapter 10 and
382 * T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
383 * The magic number M and shift S can be calculated in the following way:
384 * Let nc be the most positive value of numerator(n) such that nc = kd - 1,
385 * where divisor(d) >=2.
386 * Let nc be the most negative value of numerator(n) such that nc = kd + 1,
387 * where divisor(d) <= -2.
388 * Thus nc can be calculated like:
389 * nc = 2^31 + 2^31 % d - 1, where d >= 2
390 * nc = -2^31 + (2^31 + 1) % d, where d >= 2.
391 *
392 * So the shift p is the smallest p satisfying
393 * 2^p > nc * (d - 2^p % d), where d >= 2
394 * 2^p > nc * (d + 2^p % d), where d <= -2.
395 *
396 * the magic number M is calcuated by
397 * M = (2^p + d - 2^p % d) / d, where d >= 2
398 * M = (2^p - d - 2^p % d) / d, where d <= -2.
399 *
400 * Notice that p is always bigger than or equal to 32, so we just return 32-p as
401 * the shift number S.
402 */
403
404 int32_t p = 31;
405 const uint32_t two31 = 0x80000000U;
406
407 // Initialize the computations.
408 uint32_t abs_d = (divisor >= 0) ? divisor : -divisor;
409 uint32_t tmp = two31 + (static_cast<uint32_t>(divisor) >> 31);
410 uint32_t abs_nc = tmp - 1 - tmp % abs_d;
411 uint32_t quotient1 = two31 / abs_nc;
412 uint32_t remainder1 = two31 % abs_nc;
413 uint32_t quotient2 = two31 / abs_d;
414 uint32_t remainder2 = two31 % abs_d;
415
416 /*
417 * To avoid handling both positive and negative divisor, Hacker's Delight
418 * introduces a method to handle these 2 cases together to avoid duplication.
419 */
420 uint32_t delta;
421 do {
422 p++;
423 quotient1 = 2 * quotient1;
424 remainder1 = 2 * remainder1;
425 if (remainder1 >= abs_nc) {
426 quotient1++;
427 remainder1 = remainder1 - abs_nc;
428 }
429 quotient2 = 2 * quotient2;
430 remainder2 = 2 * remainder2;
431 if (remainder2 >= abs_d) {
432 quotient2++;
433 remainder2 = remainder2 - abs_d;
434 }
435 delta = abs_d - remainder2;
436 } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
437
438 magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1);
439 shift = p - 32;
440}
441
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, int reg_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700443 int lit, bool is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 LOG(FATAL) << "Unexpected use of GenDivRemLit for x86";
445 return rl_dest;
446}
447
Mark Mendell2bf31e62014-01-23 12:13:40 -0800448RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src,
449 int imm, bool is_div) {
450 // Use a multiply (and fixup) to perform an int div/rem by a constant.
451
452 // We have to use fixed registers, so flush all the temps.
453 FlushAllRegs();
454 LockCallTemps(); // Prepare for explicit register usage.
455
456 // Assume that the result will be in EDX.
457 RegLocation rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, kVectorNotUsed,
458 r2, INVALID_REG, INVALID_SREG, INVALID_SREG};
459
460 // handle 0x80000000 / -1 special case.
461 LIR *minint_branch = 0;
462 if (imm == -1) {
463 if (is_div) {
464 LoadValueDirectFixed(rl_src, r0);
465 OpRegImm(kOpCmp, r0, 0x80000000);
466 minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
467
468 // for x != MIN_INT, x / -1 == -x.
469 NewLIR1(kX86Neg32R, r0);
470
471 LIR* branch_around = NewLIR1(kX86Jmp8, 0);
472 // The target for cmp/jmp above.
473 minint_branch->target = NewLIR0(kPseudoTargetLabel);
474 // EAX already contains the right value (0x80000000),
475 branch_around->target = NewLIR0(kPseudoTargetLabel);
476 } else {
477 // x % -1 == 0.
478 LoadConstantNoClobber(r0, 0);
479 }
480 // For this case, return the result in EAX.
481 rl_result.low_reg = r0;
482 } else {
483 DCHECK(imm <= -2 || imm >= 2);
484 // Use H.S.Warren's Hacker's Delight Chapter 10 and
485 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
486 int magic, shift;
487 CalculateMagicAndShift(imm, magic, shift);
488
489 /*
490 * For imm >= 2,
491 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
492 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
493 * For imm <= -2,
494 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
495 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
496 * We implement this algorithm in the following way:
497 * 1. multiply magic number m and numerator n, get the higher 32bit result in EDX
498 * 2. if imm > 0 and magic < 0, add numerator to EDX
499 * if imm < 0 and magic > 0, sub numerator from EDX
500 * 3. if S !=0, SAR S bits for EDX
501 * 4. add 1 to EDX if EDX < 0
502 * 5. Thus, EDX is the quotient
503 */
504
505 // Numerator into EAX.
506 int numerator_reg = -1;
507 if (!is_div || (imm > 0 && magic < 0) || (imm < 0 && magic > 0)) {
508 // We will need the value later.
509 if (rl_src.location == kLocPhysReg) {
510 // We can use it directly.
511 DCHECK(rl_src.low_reg != r0 && rl_src.low_reg != r2);
512 numerator_reg = rl_src.low_reg;
513 } else {
514 LoadValueDirectFixed(rl_src, r1);
515 numerator_reg = r1;
516 }
517 OpRegCopy(r0, numerator_reg);
518 } else {
519 // Only need this once. Just put it into EAX.
520 LoadValueDirectFixed(rl_src, r0);
521 }
522
523 // EDX = magic.
524 LoadConstantNoClobber(r2, magic);
525
526 // EDX:EAX = magic & dividend.
527 NewLIR1(kX86Imul32DaR, r2);
528
529 if (imm > 0 && magic < 0) {
530 // Add numerator to EDX.
531 DCHECK_NE(numerator_reg, -1);
532 NewLIR2(kX86Add32RR, r2, numerator_reg);
533 } else if (imm < 0 && magic > 0) {
534 DCHECK_NE(numerator_reg, -1);
535 NewLIR2(kX86Sub32RR, r2, numerator_reg);
536 }
537
538 // Do we need the shift?
539 if (shift != 0) {
540 // Shift EDX by 'shift' bits.
541 NewLIR2(kX86Sar32RI, r2, shift);
542 }
543
544 // Add 1 to EDX if EDX < 0.
545
546 // Move EDX to EAX.
547 OpRegCopy(r0, r2);
548
549 // Move sign bit to bit 0, zeroing the rest.
550 NewLIR2(kX86Shr32RI, r2, 31);
551
552 // EDX = EDX + EAX.
553 NewLIR2(kX86Add32RR, r2, r0);
554
555 // Quotient is in EDX.
556 if (!is_div) {
557 // We need to compute the remainder.
558 // Remainder is divisor - (quotient * imm).
559 DCHECK_NE(numerator_reg, -1);
560 OpRegCopy(r0, numerator_reg);
561
562 // EAX = numerator * imm.
563 OpRegRegImm(kOpMul, r2, r2, imm);
564
565 // EDX -= EAX.
566 NewLIR2(kX86Sub32RR, r0, r2);
567
568 // For this case, return the result in EAX.
569 rl_result.low_reg = r0;
570 }
571 }
572
573 return rl_result;
574}
575
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, int reg_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700577 int reg_hi, bool is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 LOG(FATAL) << "Unexpected use of GenDivRem for x86";
579 return rl_dest;
580}
581
Mark Mendell2bf31e62014-01-23 12:13:40 -0800582RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
583 RegLocation rl_src2, bool is_div, bool check_zero) {
584 // We have to use fixed registers, so flush all the temps.
585 FlushAllRegs();
586 LockCallTemps(); // Prepare for explicit register usage.
587
588 // Load LHS into EAX.
589 LoadValueDirectFixed(rl_src1, r0);
590
591 // Load RHS into EBX.
592 LoadValueDirectFixed(rl_src2, r1);
593
594 // Copy LHS sign bit into EDX.
595 NewLIR0(kx86Cdq32Da);
596
597 if (check_zero) {
598 // Handle division by zero case.
599 GenImmedCheck(kCondEq, r1, 0, kThrowDivZero);
600 }
601
602 // Have to catch 0x80000000/-1 case, or we will get an exception!
603 OpRegImm(kOpCmp, r1, -1);
604 LIR *minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
605
606 // RHS is -1.
607 OpRegImm(kOpCmp, r0, 0x80000000);
608 LIR * minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
609
610 // In 0x80000000/-1 case.
611 if (!is_div) {
612 // For DIV, EAX is already right. For REM, we need EDX 0.
613 LoadConstantNoClobber(r2, 0);
614 }
615 LIR* done = NewLIR1(kX86Jmp8, 0);
616
617 // Expected case.
618 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
619 minint_branch->target = minus_one_branch->target;
620 NewLIR1(kX86Idivmod32DaR, r1);
621 done->target = NewLIR0(kPseudoTargetLabel);
622
623 // Result is in EAX for div and EDX for rem.
624 RegLocation rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, kVectorNotUsed,
625 r0, INVALID_REG, INVALID_SREG, INVALID_SREG};
626 if (!is_div) {
627 rl_result.low_reg = r2;
628 }
629 return rl_result;
630}
631
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700632bool X86Mir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 DCHECK_EQ(cu_->instruction_set, kX86);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800634
635 // Get the two arguments to the invoke and place them in GP registers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 RegLocation rl_src1 = info->args[0];
637 RegLocation rl_src2 = info->args[1];
638 rl_src1 = LoadValue(rl_src1, kCoreReg);
639 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800640
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 RegLocation rl_dest = InlineTarget(info);
642 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800643
644 /*
645 * If the result register is the same as the second element, then we need to be careful.
646 * The reason is that the first copy will inadvertently clobber the second element with
647 * the first one thus yielding the wrong result. Thus we do a swap in that case.
648 */
649 if (rl_result.low_reg == rl_src2.low_reg) {
650 std::swap(rl_src1, rl_src2);
651 }
652
653 // Pick the first integer as min/max.
654 OpRegCopy(rl_result.low_reg, rl_src1.low_reg);
655
656 // If the integers are both in the same register, then there is nothing else to do
657 // because they are equal and we have already moved one into the result.
658 if (rl_src1.low_reg != rl_src2.low_reg) {
659 // It is possible we didn't pick correctly so do the actual comparison now.
660 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
661
662 // Conditionally move the other integer into the destination register.
663 ConditionCode condition_code = is_min ? kCondGt : kCondLt;
664 OpCondRegReg(kOpCmov, condition_code, rl_result.low_reg, rl_src2.low_reg);
665 }
666
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 StoreValue(rl_dest, rl_result);
668 return true;
669}
670
Vladimir Markoe508a202013-11-04 15:24:22 +0000671bool X86Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
672 RegLocation rl_src_address = info->args[0]; // long address
673 rl_src_address.wide = 0; // ignore high half in info->args[1]
674 RegLocation rl_dest = InlineTarget(info);
675 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
676 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
677 if (size == kLong) {
678 // Unaligned access is allowed on x86.
679 LoadBaseDispWide(rl_address.low_reg, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
680 StoreValueWide(rl_dest, rl_result);
681 } else {
682 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
683 // Unaligned access is allowed on x86.
684 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, size, INVALID_SREG);
685 StoreValue(rl_dest, rl_result);
686 }
687 return true;
688}
689
690bool X86Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
691 RegLocation rl_src_address = info->args[0]; // long address
692 rl_src_address.wide = 0; // ignore high half in info->args[1]
693 RegLocation rl_src_value = info->args[2]; // [size] value
694 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
695 if (size == kLong) {
696 // Unaligned access is allowed on x86.
697 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
698 StoreBaseDispWide(rl_address.low_reg, 0, rl_value.low_reg, rl_value.high_reg);
699 } else {
700 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
701 // Unaligned access is allowed on x86.
702 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
703 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, size);
704 }
705 return true;
706}
707
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700708void X86Mir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 NewLIR5(kX86Lea32RA, rBase, reg1, reg2, scale, offset);
710}
711
Ian Rogers468532e2013-08-05 10:56:33 -0700712void X86Mir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
713 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714}
715
Vladimir Marko1c282e22013-11-21 14:49:47 +0000716bool X86Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Vladimir Markoc29bb612013-11-27 16:47:25 +0000717 DCHECK_EQ(cu_->instruction_set, kX86);
718 // Unused - RegLocation rl_src_unsafe = info->args[0];
719 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
720 RegLocation rl_src_offset = info->args[2]; // long low
721 rl_src_offset.wide = 0; // ignore high half in info->args[3]
722 RegLocation rl_src_expected = info->args[4]; // int, long or Object
723 // If is_long, high half is in info->args[5]
724 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
725 // If is_long, high half is in info->args[7]
726
727 if (is_long) {
Vladimir Marko70b797d2013-12-03 15:25:24 +0000728 FlushAllRegs();
729 LockCallTemps();
Vladimir Markoa6fd8ba2013-12-13 10:53:49 +0000730 LoadValueDirectWideFixed(rl_src_expected, rAX, rDX);
731 LoadValueDirectWideFixed(rl_src_new_value, rBX, rCX);
Vladimir Marko70b797d2013-12-03 15:25:24 +0000732 NewLIR1(kX86Push32R, rDI);
733 MarkTemp(rDI);
734 LockTemp(rDI);
735 NewLIR1(kX86Push32R, rSI);
736 MarkTemp(rSI);
737 LockTemp(rSI);
Vladimir Markoa6fd8ba2013-12-13 10:53:49 +0000738 const int push_offset = 4 /* push edi */ + 4 /* push esi */;
739 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src_obj.s_reg_low) + push_offset, rDI);
740 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src_offset.s_reg_low) + push_offset, rSI);
Vladimir Marko70b797d2013-12-03 15:25:24 +0000741 NewLIR4(kX86LockCmpxchg8bA, rDI, rSI, 0, 0);
742 FreeTemp(rSI);
743 UnmarkTemp(rSI);
744 NewLIR1(kX86Pop32R, rSI);
745 FreeTemp(rDI);
746 UnmarkTemp(rDI);
747 NewLIR1(kX86Pop32R, rDI);
748 FreeCallTemps();
Vladimir Markoc29bb612013-11-27 16:47:25 +0000749 } else {
750 // EAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in EAX.
751 FlushReg(r0);
752 LockTemp(r0);
753
754 // Release store semantics, get the barrier out of the way. TODO: revisit
755 GenMemBarrier(kStoreLoad);
756
757 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
758 RegLocation rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
759
760 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
761 // Mark card for object assuming new value is stored.
762 FreeTemp(r0); // Temporarily release EAX for MarkGCCard().
763 MarkGCCard(rl_new_value.low_reg, rl_object.low_reg);
764 LockTemp(r0);
765 }
766
767 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
768 LoadValueDirect(rl_src_expected, r0);
769 NewLIR5(kX86LockCmpxchgAR, rl_object.low_reg, rl_offset.low_reg, 0, 0, rl_new_value.low_reg);
770
771 FreeTemp(r0);
772 }
773
774 // Convert ZF to boolean
775 RegLocation rl_dest = InlineTarget(info); // boolean place for result
776 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
777 NewLIR2(kX86Set8R, rl_result.low_reg, kX86CondZ);
778 NewLIR2(kX86Movzx8RR, rl_result.low_reg, rl_result.low_reg);
779 StoreValue(rl_dest, rl_result);
780 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781}
782
783LIR* X86Mir2Lir::OpPcRelLoad(int reg, LIR* target) {
784 LOG(FATAL) << "Unexpected use of OpPcRelLoad for x86";
785 return NULL;
786}
787
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700788LIR* X86Mir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 LOG(FATAL) << "Unexpected use of OpVldm for x86";
790 return NULL;
791}
792
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700793LIR* X86Mir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 LOG(FATAL) << "Unexpected use of OpVstm for x86";
795 return NULL;
796}
797
798void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
799 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700800 int first_bit, int second_bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 int t_reg = AllocTemp();
802 OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, second_bit - first_bit);
803 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, t_reg);
804 FreeTemp(t_reg);
805 if (first_bit != 0) {
806 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_result.low_reg, first_bit);
807 }
808}
809
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700810void X86Mir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800811 // We are not supposed to clobber either of the provided registers, so allocate
812 // a temporary to use for the check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813 int t_reg = AllocTemp();
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800814
815 // 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 -0700816 OpRegRegReg(kOpOr, t_reg, reg_lo, reg_hi);
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800817
818 // In case of zero, throw ArithmeticException.
819 GenCheck(kCondEq, kThrowDivZero);
820
821 // The temp is no longer needed so free it at this time.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 FreeTemp(t_reg);
823}
824
825// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700826LIR* X86Mir2Lir::OpTestSuspend(LIR* target) {
Ian Rogers468532e2013-08-05 10:56:33 -0700827 OpTlsCmp(Thread::ThreadFlagsOffset(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700828 return OpCondBranch((target == NULL) ? kCondNe : kCondEq, target);
829}
830
831// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700832LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 OpRegImm(kOpSub, reg, 1);
834 return OpCmpImmBranch(c_code, reg, 0, target);
835}
836
buzbee11b63d12013-08-27 07:34:17 -0700837bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700838 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 LOG(FATAL) << "Unexpected use of smallLiteralDive in x86";
840 return false;
841}
842
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700843LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 LOG(FATAL) << "Unexpected use of OpIT in x86";
845 return NULL;
846}
847
Mark Mendelle02d48f2014-01-15 11:19:23 -0800848void X86Mir2Lir::GenMulLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700849 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 LOG(FATAL) << "Unexpected use of GenX86Long for x86";
851}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800852
853void X86Mir2Lir::GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src,
854 Instruction::Code op) {
855 DCHECK_EQ(rl_dest.location, kLocPhysReg);
856 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
857 if (rl_src.location == kLocPhysReg) {
858 // Both operands are in registers.
859 if (rl_dest.low_reg == rl_src.high_reg) {
860 // The registers are the same, so we would clobber it before the use.
861 int temp_reg = AllocTemp();
862 OpRegCopy(temp_reg, rl_dest.low_reg);
863 rl_src.high_reg = temp_reg;
864 }
865 NewLIR2(x86op, rl_dest.low_reg, rl_src.low_reg);
866
867 x86op = GetOpcode(op, rl_dest, rl_src, true);
868 NewLIR2(x86op, rl_dest.high_reg, rl_src.high_reg);
869 FreeTemp(rl_src.low_reg);
870 FreeTemp(rl_src.high_reg);
871 return;
872 }
873
874 // RHS is in memory.
875 DCHECK((rl_src.location == kLocDalvikFrame) ||
876 (rl_src.location == kLocCompilerTemp));
877 int rBase = TargetReg(kSp);
878 int displacement = SRegOffset(rl_src.s_reg_low);
879
880 LIR *lir = NewLIR3(x86op, rl_dest.low_reg, rBase, displacement + LOWORD_OFFSET);
881 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
882 true /* is_load */, true /* is64bit */);
883 x86op = GetOpcode(op, rl_dest, rl_src, true);
884 lir = NewLIR3(x86op, rl_dest.high_reg, rBase, displacement + HIWORD_OFFSET);
885 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
886 true /* is_load */, true /* is64bit */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887}
888
Mark Mendelle02d48f2014-01-15 11:19:23 -0800889void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
890 rl_dest = UpdateLocWide(rl_dest);
891 if (rl_dest.location == kLocPhysReg) {
892 // Ensure we are in a register pair
893 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
894
895 rl_src = UpdateLocWide(rl_src);
896 GenLongRegOrMemOp(rl_result, rl_src, op);
897 StoreFinalValueWide(rl_dest, rl_result);
898 return;
899 }
900
901 // It wasn't in registers, so it better be in memory.
902 DCHECK((rl_dest.location == kLocDalvikFrame) ||
903 (rl_dest.location == kLocCompilerTemp));
904 rl_src = LoadValueWide(rl_src, kCoreReg);
905
906 // Operate directly into memory.
907 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
908 int rBase = TargetReg(kSp);
909 int displacement = SRegOffset(rl_dest.s_reg_low);
910
911 LIR *lir = NewLIR3(x86op, rBase, displacement + LOWORD_OFFSET, rl_src.low_reg);
912 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
913 false /* is_load */, true /* is64bit */);
914 x86op = GetOpcode(op, rl_dest, rl_src, true);
915 lir = NewLIR3(x86op, rBase, displacement + HIWORD_OFFSET, rl_src.high_reg);
916 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
917 false /* is_load */, true /* is64bit */);
918 FreeTemp(rl_src.low_reg);
919 FreeTemp(rl_src.high_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920}
921
Mark Mendelle02d48f2014-01-15 11:19:23 -0800922void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src1,
923 RegLocation rl_src2, Instruction::Code op,
924 bool is_commutative) {
925 // Is this really a 2 operand operation?
926 switch (op) {
927 case Instruction::ADD_LONG_2ADDR:
928 case Instruction::SUB_LONG_2ADDR:
929 case Instruction::AND_LONG_2ADDR:
930 case Instruction::OR_LONG_2ADDR:
931 case Instruction::XOR_LONG_2ADDR:
932 GenLongArith(rl_dest, rl_src2, op);
933 return;
934 default:
935 break;
936 }
937
938 if (rl_dest.location == kLocPhysReg) {
939 RegLocation rl_result = LoadValueWide(rl_src1, kCoreReg);
940
941 // We are about to clobber the LHS, so it needs to be a temp.
942 rl_result = ForceTempWide(rl_result);
943
944 // Perform the operation using the RHS.
945 rl_src2 = UpdateLocWide(rl_src2);
946 GenLongRegOrMemOp(rl_result, rl_src2, op);
947
948 // And now record that the result is in the temp.
949 StoreFinalValueWide(rl_dest, rl_result);
950 return;
951 }
952
953 // It wasn't in registers, so it better be in memory.
954 DCHECK((rl_dest.location == kLocDalvikFrame) ||
955 (rl_dest.location == kLocCompilerTemp));
956 rl_src1 = UpdateLocWide(rl_src1);
957 rl_src2 = UpdateLocWide(rl_src2);
958
959 // Get one of the source operands into temporary register.
960 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
961 if (IsTemp(rl_src1.low_reg) && IsTemp(rl_src1.high_reg)) {
962 GenLongRegOrMemOp(rl_src1, rl_src2, op);
963 } else if (is_commutative) {
964 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
965 // We need at least one of them to be a temporary.
966 if (!(IsTemp(rl_src2.low_reg) && IsTemp(rl_src2.high_reg))) {
967 rl_src1 = ForceTempWide(rl_src1);
968 }
969 GenLongRegOrMemOp(rl_src1, rl_src2, op);
970 } else {
971 // Need LHS to be the temp.
972 rl_src1 = ForceTempWide(rl_src1);
973 GenLongRegOrMemOp(rl_src1, rl_src2, op);
974 }
975
976 StoreFinalValueWide(rl_dest, rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977}
978
Mark Mendelle02d48f2014-01-15 11:19:23 -0800979void X86Mir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700980 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -0800981 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
982}
983
984void X86Mir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest,
985 RegLocation rl_src1, RegLocation rl_src2) {
986 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, false);
987}
988
989void X86Mir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest,
990 RegLocation rl_src1, RegLocation rl_src2) {
991 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
992}
993
994void X86Mir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest,
995 RegLocation rl_src1, RegLocation rl_src2) {
996 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
997}
998
999void X86Mir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest,
1000 RegLocation rl_src1, RegLocation rl_src2) {
1001 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002}
1003
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001004void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001005 rl_src = LoadValueWide(rl_src, kCoreReg);
1006 RegLocation rl_result = ForceTempWide(rl_src);
1007 if (rl_dest.low_reg == rl_src.high_reg) {
1008 // The registers are the same, so we would clobber it before the use.
1009 int temp_reg = AllocTemp();
1010 OpRegCopy(temp_reg, rl_result.low_reg);
1011 rl_result.high_reg = temp_reg;
1012 }
1013 OpRegReg(kOpNeg, rl_result.low_reg, rl_result.low_reg); // rLow = -rLow
1014 OpRegImm(kOpAdc, rl_result.high_reg, 0); // rHigh = rHigh + CF
1015 OpRegReg(kOpNeg, rl_result.high_reg, rl_result.high_reg); // rHigh = -rHigh
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 StoreValueWide(rl_dest, rl_result);
1017}
1018
Ian Rogers468532e2013-08-05 10:56:33 -07001019void X86Mir2Lir::OpRegThreadMem(OpKind op, int r_dest, ThreadOffset thread_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 X86OpCode opcode = kX86Bkpt;
1021 switch (op) {
1022 case kOpCmp: opcode = kX86Cmp32RT; break;
1023 case kOpMov: opcode = kX86Mov32RT; break;
1024 default:
1025 LOG(FATAL) << "Bad opcode: " << op;
1026 break;
1027 }
Ian Rogers468532e2013-08-05 10:56:33 -07001028 NewLIR2(opcode, r_dest, thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029}
1030
1031/*
1032 * Generate array load
1033 */
1034void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001035 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 RegisterClass reg_class = oat_reg_class_by_size(size);
1037 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 RegLocation rl_result;
1039 rl_array = LoadValue(rl_array, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040
Mark Mendell343adb52013-12-18 06:02:17 -08001041 int data_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 if (size == kLong || size == kDouble) {
1043 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1044 } else {
1045 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1046 }
1047
Mark Mendell343adb52013-12-18 06:02:17 -08001048 bool constant_index = rl_index.is_const;
1049 int32_t constant_index_value = 0;
1050 if (!constant_index) {
1051 rl_index = LoadValue(rl_index, kCoreReg);
1052 } else {
1053 constant_index_value = mir_graph_->ConstantValue(rl_index);
1054 // If index is constant, just fold it into the data offset
1055 data_offset += constant_index_value << scale;
1056 // treat as non array below
1057 rl_index.low_reg = INVALID_REG;
1058 }
1059
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 /* null object? */
1061 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
1062
1063 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08001064 if (constant_index) {
1065 GenMemImmedCheck(kCondLs, rl_array.low_reg, len_offset,
1066 constant_index_value, kThrowConstantArrayBounds);
1067 } else {
1068 GenRegMemCheck(kCondUge, rl_index.low_reg, rl_array.low_reg,
1069 len_offset, kThrowArrayBounds);
1070 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 }
Mark Mendell343adb52013-12-18 06:02:17 -08001072 rl_result = EvalLoc(rl_dest, reg_class, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 if ((size == kLong) || (size == kDouble)) {
Mark Mendell343adb52013-12-18 06:02:17 -08001074 LoadBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, rl_result.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 rl_result.high_reg, size, INVALID_SREG);
1076 StoreValueWide(rl_dest, rl_result);
1077 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 LoadBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale,
1079 data_offset, rl_result.low_reg, INVALID_REG, size,
1080 INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 StoreValue(rl_dest, rl_result);
1082 }
1083}
1084
1085/*
1086 * Generate array store
1087 *
1088 */
1089void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001090 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 RegisterClass reg_class = oat_reg_class_by_size(size);
1092 int len_offset = mirror::Array::LengthOffset().Int32Value();
1093 int data_offset;
1094
1095 if (size == kLong || size == kDouble) {
1096 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1097 } else {
1098 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1099 }
1100
1101 rl_array = LoadValue(rl_array, kCoreReg);
Mark Mendell343adb52013-12-18 06:02:17 -08001102 bool constant_index = rl_index.is_const;
1103 int32_t constant_index_value = 0;
1104 if (!constant_index) {
1105 rl_index = LoadValue(rl_index, kCoreReg);
1106 } else {
1107 // If index is constant, just fold it into the data offset
1108 constant_index_value = mir_graph_->ConstantValue(rl_index);
1109 data_offset += constant_index_value << scale;
1110 // treat as non array below
1111 rl_index.low_reg = INVALID_REG;
1112 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113
1114 /* null object? */
1115 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
1116
1117 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08001118 if (constant_index) {
1119 GenMemImmedCheck(kCondLs, rl_array.low_reg, len_offset,
1120 constant_index_value, kThrowConstantArrayBounds);
1121 } else {
1122 GenRegMemCheck(kCondUge, rl_index.low_reg, rl_array.low_reg,
1123 len_offset, kThrowArrayBounds);
1124 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 }
1126 if ((size == kLong) || (size == kDouble)) {
1127 rl_src = LoadValueWide(rl_src, reg_class);
1128 } else {
1129 rl_src = LoadValue(rl_src, reg_class);
1130 }
1131 // If the src reg can't be byte accessed, move it to a temp first.
1132 if ((size == kSignedByte || size == kUnsignedByte) && rl_src.low_reg >= 4) {
1133 int temp = AllocTemp();
1134 OpRegCopy(temp, rl_src.low_reg);
1135 StoreBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, temp,
1136 INVALID_REG, size, INVALID_SREG);
1137 } else {
1138 StoreBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, rl_src.low_reg,
1139 rl_src.high_reg, size, INVALID_SREG);
1140 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001141 if (card_mark) {
Ian Rogers773aab12013-10-14 13:50:10 -07001142 // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
Mark Mendell343adb52013-12-18 06:02:17 -08001143 if (!constant_index) {
1144 FreeTemp(rl_index.low_reg);
1145 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001146 MarkGCCard(rl_src.low_reg, rl_array.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 }
1148}
1149
1150void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001151 RegLocation rl_src1, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 // Default implementation is just to ignore the constant case.
1153 GenShiftOpLong(opcode, rl_dest, rl_src1, rl_shift);
1154}
1155
1156void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001158 switch (opcode) {
1159 case Instruction::ADD_LONG:
1160 case Instruction::AND_LONG:
1161 case Instruction::OR_LONG:
1162 case Instruction::XOR_LONG:
1163 if (rl_src2.is_const) {
1164 GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
1165 } else {
1166 DCHECK(rl_src1.is_const);
1167 GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
1168 }
1169 break;
1170 case Instruction::SUB_LONG:
1171 case Instruction::SUB_LONG_2ADDR:
1172 if (rl_src2.is_const) {
1173 GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
1174 } else {
1175 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
1176 }
1177 break;
1178 case Instruction::ADD_LONG_2ADDR:
1179 case Instruction::OR_LONG_2ADDR:
1180 case Instruction::XOR_LONG_2ADDR:
1181 case Instruction::AND_LONG_2ADDR:
1182 if (rl_src2.is_const) {
1183 GenLongImm(rl_dest, rl_src2, opcode);
1184 } else {
1185 DCHECK(rl_src1.is_const);
1186 GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
1187 }
1188 break;
1189 default:
1190 // Default - bail to non-const handler.
1191 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1192 break;
1193 }
1194}
1195
1196bool X86Mir2Lir::IsNoOp(Instruction::Code op, int32_t value) {
1197 switch (op) {
1198 case Instruction::AND_LONG_2ADDR:
1199 case Instruction::AND_LONG:
1200 return value == -1;
1201 case Instruction::OR_LONG:
1202 case Instruction::OR_LONG_2ADDR:
1203 case Instruction::XOR_LONG:
1204 case Instruction::XOR_LONG_2ADDR:
1205 return value == 0;
1206 default:
1207 return false;
1208 }
1209}
1210
1211X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
1212 bool is_high_op) {
1213 bool rhs_in_mem = rhs.location != kLocPhysReg;
1214 bool dest_in_mem = dest.location != kLocPhysReg;
1215 DCHECK(!rhs_in_mem || !dest_in_mem);
1216 switch (op) {
1217 case Instruction::ADD_LONG:
1218 case Instruction::ADD_LONG_2ADDR:
1219 if (dest_in_mem) {
1220 return is_high_op ? kX86Adc32MR : kX86Add32MR;
1221 } else if (rhs_in_mem) {
1222 return is_high_op ? kX86Adc32RM : kX86Add32RM;
1223 }
1224 return is_high_op ? kX86Adc32RR : kX86Add32RR;
1225 case Instruction::SUB_LONG:
1226 case Instruction::SUB_LONG_2ADDR:
1227 if (dest_in_mem) {
1228 return is_high_op ? kX86Sbb32MR : kX86Sub32MR;
1229 } else if (rhs_in_mem) {
1230 return is_high_op ? kX86Sbb32RM : kX86Sub32RM;
1231 }
1232 return is_high_op ? kX86Sbb32RR : kX86Sub32RR;
1233 case Instruction::AND_LONG_2ADDR:
1234 case Instruction::AND_LONG:
1235 if (dest_in_mem) {
1236 return kX86And32MR;
1237 }
1238 return rhs_in_mem ? kX86And32RM : kX86And32RR;
1239 case Instruction::OR_LONG:
1240 case Instruction::OR_LONG_2ADDR:
1241 if (dest_in_mem) {
1242 return kX86Or32MR;
1243 }
1244 return rhs_in_mem ? kX86Or32RM : kX86Or32RR;
1245 case Instruction::XOR_LONG:
1246 case Instruction::XOR_LONG_2ADDR:
1247 if (dest_in_mem) {
1248 return kX86Xor32MR;
1249 }
1250 return rhs_in_mem ? kX86Xor32RM : kX86Xor32RR;
1251 default:
1252 LOG(FATAL) << "Unexpected opcode: " << op;
1253 return kX86Add32RR;
1254 }
1255}
1256
1257X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op,
1258 int32_t value) {
1259 bool in_mem = loc.location != kLocPhysReg;
1260 bool byte_imm = IS_SIMM8(value);
1261 DCHECK(in_mem || !IsFpReg(loc.low_reg));
1262 switch (op) {
1263 case Instruction::ADD_LONG:
1264 case Instruction::ADD_LONG_2ADDR:
1265 if (byte_imm) {
1266 if (in_mem) {
1267 return is_high_op ? kX86Adc32MI8 : kX86Add32MI8;
1268 }
1269 return is_high_op ? kX86Adc32RI8 : kX86Add32RI8;
1270 }
1271 if (in_mem) {
1272 return is_high_op ? kX86Adc32MI : kX86Add32MI;
1273 }
1274 return is_high_op ? kX86Adc32RI : kX86Add32RI;
1275 case Instruction::SUB_LONG:
1276 case Instruction::SUB_LONG_2ADDR:
1277 if (byte_imm) {
1278 if (in_mem) {
1279 return is_high_op ? kX86Sbb32MI8 : kX86Sub32MI8;
1280 }
1281 return is_high_op ? kX86Sbb32RI8 : kX86Sub32RI8;
1282 }
1283 if (in_mem) {
1284 return is_high_op ? kX86Sbb32MI : kX86Sub32MI;
1285 }
1286 return is_high_op ? kX86Sbb32RI : kX86Sub32RI;
1287 case Instruction::AND_LONG_2ADDR:
1288 case Instruction::AND_LONG:
1289 if (byte_imm) {
1290 return in_mem ? kX86And32MI8 : kX86And32RI8;
1291 }
1292 return in_mem ? kX86And32MI : kX86And32RI;
1293 case Instruction::OR_LONG:
1294 case Instruction::OR_LONG_2ADDR:
1295 if (byte_imm) {
1296 return in_mem ? kX86Or32MI8 : kX86Or32RI8;
1297 }
1298 return in_mem ? kX86Or32MI : kX86Or32RI;
1299 case Instruction::XOR_LONG:
1300 case Instruction::XOR_LONG_2ADDR:
1301 if (byte_imm) {
1302 return in_mem ? kX86Xor32MI8 : kX86Xor32RI8;
1303 }
1304 return in_mem ? kX86Xor32MI : kX86Xor32RI;
1305 default:
1306 LOG(FATAL) << "Unexpected opcode: " << op;
1307 return kX86Add32MI;
1308 }
1309}
1310
1311void X86Mir2Lir::GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
1312 DCHECK(rl_src.is_const);
1313 int64_t val = mir_graph_->ConstantValueWide(rl_src);
1314 int32_t val_lo = Low32Bits(val);
1315 int32_t val_hi = High32Bits(val);
1316 rl_dest = UpdateLocWide(rl_dest);
1317
1318 // Can we just do this into memory?
1319 if ((rl_dest.location == kLocDalvikFrame) ||
1320 (rl_dest.location == kLocCompilerTemp)) {
1321 int rBase = TargetReg(kSp);
1322 int displacement = SRegOffset(rl_dest.s_reg_low);
1323
1324 if (!IsNoOp(op, val_lo)) {
1325 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
1326 LIR *lir = NewLIR3(x86op, rBase, displacement + LOWORD_OFFSET, val_lo);
1327 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
1328 false /* is_load */, true /* is64bit */);
1329 }
1330 if (!IsNoOp(op, val_hi)) {
1331 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
1332 LIR *lir = NewLIR3(x86op, rBase, displacement + HIWORD_OFFSET, val_hi);
1333 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1334 false /* is_load */, true /* is64bit */);
1335 }
1336 return;
1337 }
1338
1339 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1340 DCHECK_EQ(rl_result.location, kLocPhysReg);
1341 DCHECK(!IsFpReg(rl_result.low_reg));
1342
1343 if (!IsNoOp(op, val_lo)) {
1344 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
1345 NewLIR2(x86op, rl_result.low_reg, val_lo);
1346 }
1347 if (!IsNoOp(op, val_hi)) {
1348 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
1349 NewLIR2(x86op, rl_result.high_reg, val_hi);
1350 }
1351 StoreValueWide(rl_dest, rl_result);
1352}
1353
1354void X86Mir2Lir::GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1,
1355 RegLocation rl_src2, Instruction::Code op) {
1356 DCHECK(rl_src2.is_const);
1357 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1358 int32_t val_lo = Low32Bits(val);
1359 int32_t val_hi = High32Bits(val);
1360 rl_dest = UpdateLocWide(rl_dest);
1361 rl_src1 = UpdateLocWide(rl_src1);
1362
1363 // Can we do this directly into the destination registers?
1364 if (rl_dest.location == kLocPhysReg && rl_src1.location == kLocPhysReg &&
1365 rl_dest.low_reg == rl_src1.low_reg && rl_dest.high_reg == rl_src1.high_reg &&
1366 !IsFpReg(rl_dest.low_reg)) {
1367 if (!IsNoOp(op, val_lo)) {
1368 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
1369 NewLIR2(x86op, rl_dest.low_reg, val_lo);
1370 }
1371 if (!IsNoOp(op, val_hi)) {
1372 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
1373 NewLIR2(x86op, rl_dest.high_reg, val_hi);
1374 }
1375 return;
1376 }
1377
1378 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1379 DCHECK_EQ(rl_src1.location, kLocPhysReg);
1380
1381 // We need the values to be in a temporary
1382 RegLocation rl_result = ForceTempWide(rl_src1);
1383 if (!IsNoOp(op, val_lo)) {
1384 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
1385 NewLIR2(x86op, rl_result.low_reg, val_lo);
1386 }
1387 if (!IsNoOp(op, val_hi)) {
1388 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
1389 NewLIR2(x86op, rl_result.high_reg, val_hi);
1390 }
1391
1392 StoreFinalValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393}
1394
1395} // namespace art