blob: d993d934a5f7cef7645a88a12ce57d82422f1433 [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"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "base/logging.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070025#include "dex/reg_storage_eq.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "x86_lir.h"
28
29namespace art {
30
31/*
Brian Carlstrom7940e442013-07-12 13:46:57 -070032 * Compare two 64-bit values
33 * x = y return 0
34 * x < y return -1
35 * x > y return 1
36 */
37void X86Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070038 RegLocation rl_src2) {
Elena Sayapinadd644502014-07-01 18:39:52 +070039 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -070040 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
41 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
42 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Chao-ying Fua0147762014-06-06 18:38:49 -070043 RegStorage temp_reg = AllocTemp();
Serguei Katkov1c557032014-06-23 13:23:38 +070044 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
45 NewLIR2(kX86Set8R, rl_result.reg.GetReg(), kX86CondG); // result = (src1 > src2) ? 1 : 0
46 NewLIR2(kX86Set8R, temp_reg.GetReg(), kX86CondL); // temp = (src1 >= src2) ? 0 : 1
47 NewLIR2(kX86Sub8RR, rl_result.reg.GetReg(), temp_reg.GetReg());
48 NewLIR2(kX86Movsx8qRR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
Serguei Katkov04982232014-06-20 18:17:16 +070049
Chao-ying Fua0147762014-06-06 18:38:49 -070050 StoreValue(rl_dest, rl_result);
51 FreeTemp(temp_reg);
52 return;
53 }
54
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +070055 // Prepare for explicit register usage
56 ExplicitTempRegisterLock(this, 4, &rs_r0, &rs_r1, &rs_r2, &rs_r3);
buzbee091cc402014-03-31 10:14:40 -070057 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_r0, rs_r1);
58 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_r2, rs_r3);
buzbee2700f7e2014-03-07 09:46:20 -080059 LoadValueDirectWideFixed(rl_src1, r_tmp1);
60 LoadValueDirectWideFixed(rl_src2, r_tmp2);
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 // Compute (r1:r0) = (r1:r0) - (r3:r2)
buzbee2700f7e2014-03-07 09:46:20 -080062 OpRegReg(kOpSub, rs_r0, rs_r2); // r0 = r0 - r2
63 OpRegReg(kOpSbc, rs_r1, rs_r3); // r1 = r1 - r3 - CF
buzbee091cc402014-03-31 10:14:40 -070064 NewLIR2(kX86Set8R, rs_r2.GetReg(), kX86CondL); // r2 = (r1:r0) < (r3:r2) ? 1 : 0
65 NewLIR2(kX86Movzx8RR, rs_r2.GetReg(), rs_r2.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -080066 OpReg(kOpNeg, rs_r2); // r2 = -r2
67 OpRegReg(kOpOr, rs_r0, rs_r1); // r0 = high | low - sets ZF
buzbee091cc402014-03-31 10:14:40 -070068 NewLIR2(kX86Set8R, rs_r0.GetReg(), kX86CondNz); // r0 = (r1:r0) != (r3:r2) ? 1 : 0
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 NewLIR2(kX86Movzx8RR, r0, r0);
buzbee2700f7e2014-03-07 09:46:20 -080070 OpRegReg(kOpOr, rs_r0, rs_r2); // r0 = r0 | r2
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 RegLocation rl_result = LocCReturn();
72 StoreValue(rl_dest, rl_result);
73}
74
75X86ConditionCode X86ConditionEncoding(ConditionCode cond) {
76 switch (cond) {
77 case kCondEq: return kX86CondEq;
78 case kCondNe: return kX86CondNe;
79 case kCondCs: return kX86CondC;
80 case kCondCc: return kX86CondNc;
Vladimir Marko58af1f92013-12-19 13:31:15 +000081 case kCondUlt: return kX86CondC;
82 case kCondUge: return kX86CondNc;
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 case kCondMi: return kX86CondS;
84 case kCondPl: return kX86CondNs;
85 case kCondVs: return kX86CondO;
86 case kCondVc: return kX86CondNo;
87 case kCondHi: return kX86CondA;
88 case kCondLs: return kX86CondBe;
89 case kCondGe: return kX86CondGe;
90 case kCondLt: return kX86CondL;
91 case kCondGt: return kX86CondG;
92 case kCondLe: return kX86CondLe;
93 case kCondAl:
94 case kCondNv: LOG(FATAL) << "Should not reach here";
95 }
96 return kX86CondO;
97}
98
buzbee2700f7e2014-03-07 09:46:20 -080099LIR* X86Mir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700100 NewLIR2(src1.Is64Bit() ? kX86Cmp64RR : kX86Cmp32RR, src1.GetReg(), src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 X86ConditionCode cc = X86ConditionEncoding(cond);
102 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ ,
103 cc);
104 branch->target = target;
105 return branch;
106}
107
buzbee2700f7e2014-03-07 09:46:20 -0800108LIR* X86Mir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700109 int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 if ((check_value == 0) && (cond == kCondEq || cond == kCondNe)) {
111 // TODO: when check_value == 0 and reg is rCX, use the jcxz/nz opcode
Chao-ying Fua77ee512014-07-01 17:43:41 -0700112 NewLIR2(reg.Is64Bit() ? kX86Test64RR: kX86Test32RR, reg.GetReg(), reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700114 if (reg.Is64Bit()) {
115 NewLIR2(IS_SIMM8(check_value) ? kX86Cmp64RI8 : kX86Cmp64RI, reg.GetReg(), check_value);
116 } else {
117 NewLIR2(IS_SIMM8(check_value) ? kX86Cmp32RI8 : kX86Cmp32RI, reg.GetReg(), check_value);
118 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 }
120 X86ConditionCode cc = X86ConditionEncoding(cond);
121 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ , cc);
122 branch->target = target;
123 return branch;
124}
125
buzbee2700f7e2014-03-07 09:46:20 -0800126LIR* X86Mir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
127 // If src or dest is a pair, we'll be using low reg.
128 if (r_dest.IsPair()) {
129 r_dest = r_dest.GetLow();
130 }
131 if (r_src.IsPair()) {
132 r_src = r_src.GetLow();
133 }
buzbee091cc402014-03-31 10:14:40 -0700134 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 return OpFpRegCopy(r_dest, r_src);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700136 LIR* res = RawLIR(current_dalvik_offset_, r_dest.Is64Bit() ? kX86Mov64RR : kX86Mov32RR,
buzbee2700f7e2014-03-07 09:46:20 -0800137 r_dest.GetReg(), r_src.GetReg());
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800138 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 res->flags.is_nop = true;
140 }
141 return res;
142}
143
buzbee7a11ab02014-04-28 20:02:38 -0700144void X86Mir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
145 if (r_dest != r_src) {
146 LIR *res = OpRegCopyNoInsert(r_dest, r_src);
147 AppendLIR(res);
148 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149}
150
buzbee2700f7e2014-03-07 09:46:20 -0800151void X86Mir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700152 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700153 bool dest_fp = r_dest.IsFloat();
154 bool src_fp = r_src.IsFloat();
buzbee7a11ab02014-04-28 20:02:38 -0700155 if (dest_fp) {
156 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700157 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 } else {
buzbee7a11ab02014-04-28 20:02:38 -0700159 // TODO: Prevent this from happening in the code. The result is often
160 // unused or could have been loaded more easily from memory.
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700161 if (!r_src.IsPair()) {
162 DCHECK(!r_dest.IsPair());
163 NewLIR2(kX86MovqxrRR, r_dest.GetReg(), r_src.GetReg());
164 } else {
165 NewLIR2(kX86MovdxrRR, r_dest.GetReg(), r_src.GetLowReg());
166 RegStorage r_tmp = AllocTempDouble();
167 NewLIR2(kX86MovdxrRR, r_tmp.GetReg(), r_src.GetHighReg());
168 NewLIR2(kX86PunpckldqRR, r_dest.GetReg(), r_tmp.GetReg());
169 FreeTemp(r_tmp);
170 }
buzbee7a11ab02014-04-28 20:02:38 -0700171 }
172 } else {
173 if (src_fp) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700174 if (!r_dest.IsPair()) {
175 DCHECK(!r_src.IsPair());
176 NewLIR2(kX86MovqrxRR, r_dest.GetReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700177 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700178 NewLIR2(kX86MovdrxRR, r_dest.GetLowReg(), r_src.GetReg());
179 RegStorage temp_reg = AllocTempDouble();
180 NewLIR2(kX86MovsdRR, temp_reg.GetReg(), r_src.GetReg());
181 NewLIR2(kX86PsrlqRI, temp_reg.GetReg(), 32);
182 NewLIR2(kX86MovdrxRR, r_dest.GetHighReg(), temp_reg.GetReg());
183 }
184 } else {
185 DCHECK_EQ(r_dest.IsPair(), r_src.IsPair());
186 if (!r_src.IsPair()) {
187 // Just copy the register directly.
188 OpRegCopy(r_dest, r_src);
189 } else {
190 // Handle overlap
191 if (r_src.GetHighReg() == r_dest.GetLowReg() &&
192 r_src.GetLowReg() == r_dest.GetHighReg()) {
193 // Deal with cycles.
194 RegStorage temp_reg = AllocTemp();
195 OpRegCopy(temp_reg, r_dest.GetHigh());
196 OpRegCopy(r_dest.GetHigh(), r_dest.GetLow());
197 OpRegCopy(r_dest.GetLow(), temp_reg);
198 FreeTemp(temp_reg);
199 } else if (r_src.GetHighReg() == r_dest.GetLowReg()) {
200 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
201 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
202 } else {
203 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
204 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
205 }
buzbee7a11ab02014-04-28 20:02:38 -0700206 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 }
208 }
209 }
210}
211
Andreas Gampe90969af2014-07-15 23:02:11 -0700212void X86Mir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
213 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700214 RegisterClass dest_reg_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700215 DCHECK(!left_op.IsPair() && !right_op.IsPair() && !rs_dest.IsPair());
216 DCHECK(!left_op.IsFloat() && !right_op.IsFloat() && !rs_dest.IsFloat());
217
218 // We really need this check for correctness, otherwise we will need to do more checks in
219 // non zero/one case
220 if (true_val == false_val) {
221 LoadConstantNoClobber(rs_dest, true_val);
222 return;
Andreas Gampe90969af2014-07-15 23:02:11 -0700223 }
224
Serguei Katkov9ee45192014-07-17 14:39:03 +0700225 const bool dest_intersect = IsSameReg(rs_dest, left_op) || IsSameReg(rs_dest, right_op);
226
227 const bool zero_one_case = (true_val == 0 && false_val == 1) || (true_val == 1 && false_val == 0);
228 if (zero_one_case && IsByteRegister(rs_dest)) {
229 if (!dest_intersect) {
230 LoadConstantNoClobber(rs_dest, 0);
231 }
232 OpRegReg(kOpCmp, left_op, right_op);
233 // Set the low byte of the result to 0 or 1 from the compare condition code.
234 NewLIR2(kX86Set8R, rs_dest.GetReg(),
235 X86ConditionEncoding(true_val == 1 ? code : FlipComparisonOrder(code)));
236 if (dest_intersect) {
237 NewLIR2(rs_dest.Is64Bit() ? kX86Movzx8qRR : kX86Movzx8RR, rs_dest.GetReg(), rs_dest.GetReg());
238 }
239 } else {
240 // Be careful rs_dest can be changed only after cmp because it can be the same as one of ops
241 // and it cannot use xor because it makes cc flags to be dirty
242 RegStorage temp_reg = AllocTypedTemp(false, dest_reg_class, false);
243 if (temp_reg.Valid()) {
244 if (false_val == 0 && dest_intersect) {
245 code = FlipComparisonOrder(code);
246 std::swap(true_val, false_val);
247 }
248 if (!dest_intersect) {
249 LoadConstantNoClobber(rs_dest, false_val);
250 }
251 LoadConstantNoClobber(temp_reg, true_val);
252 OpRegReg(kOpCmp, left_op, right_op);
253 if (dest_intersect) {
254 LoadConstantNoClobber(rs_dest, false_val);
255 DCHECK(!last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
256 }
257 OpCondRegReg(kOpCmov, code, rs_dest, temp_reg);
258 FreeTemp(temp_reg);
259 } else {
260 // slow path
261 LIR* cmp_branch = OpCmpBranch(code, left_op, right_op, nullptr);
262 LoadConstantNoClobber(rs_dest, false_val);
263 LIR* that_is_it = NewLIR1(kX86Jmp8, 0);
264 LIR* true_case = NewLIR0(kPseudoTargetLabel);
265 cmp_branch->target = true_case;
266 LoadConstantNoClobber(rs_dest, true_val);
267 LIR* end = NewLIR0(kPseudoTargetLabel);
268 that_is_it->target = end;
269 }
270 }
Andreas Gampe90969af2014-07-15 23:02:11 -0700271}
272
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700273void X86Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700274 UNUSED(bb);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800275 RegLocation rl_result;
276 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
277 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700278 // Avoid using float regs here.
279 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
280 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
Vladimir Markoa1a70742014-03-03 10:28:05 +0000281 ConditionCode ccode = mir->meta.ccode;
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800282
283 // The kMirOpSelect has two variants, one for constants and one for moves.
284 const bool is_constant_case = (mir->ssa_rep->num_uses == 1);
285
286 if (is_constant_case) {
287 int true_val = mir->dalvikInsn.vB;
288 int false_val = mir->dalvikInsn.vC;
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800289
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700290 // simplest strange case
291 if (true_val == false_val) {
292 rl_result = EvalLoc(rl_dest, result_reg_class, true);
293 LoadConstantNoClobber(rl_result.reg, true_val);
294 } else {
295 // TODO: use GenSelectConst32 and handle additional opcode patterns such as
296 // "cmp; setcc; movzx" or "cmp; sbb r0,r0; and r0,$mask; add r0,$literal".
297 rl_src = LoadValue(rl_src, src_reg_class);
298 rl_result = EvalLoc(rl_dest, result_reg_class, true);
299 /*
300 * For ccode == kCondEq:
301 *
302 * 1) When the true case is zero and result_reg is not same as src_reg:
303 * xor result_reg, result_reg
304 * cmp $0, src_reg
305 * mov t1, $false_case
306 * cmovnz result_reg, t1
307 * 2) When the false case is zero and result_reg is not same as src_reg:
308 * xor result_reg, result_reg
309 * cmp $0, src_reg
310 * mov t1, $true_case
311 * cmovz result_reg, t1
312 * 3) All other cases (we do compare first to set eflags):
313 * cmp $0, src_reg
314 * mov result_reg, $false_case
315 * mov t1, $true_case
316 * cmovz result_reg, t1
317 */
318 // FIXME: depending on how you use registers you could get a false != mismatch when dealing
319 // with different views of the same underlying physical resource (i.e. solo32 vs. solo64).
320 const bool result_reg_same_as_src =
321 (rl_src.location == kLocPhysReg && rl_src.reg.GetRegNum() == rl_result.reg.GetRegNum());
322 const bool true_zero_case = (true_val == 0 && false_val != 0 && !result_reg_same_as_src);
323 const bool false_zero_case = (false_val == 0 && true_val != 0 && !result_reg_same_as_src);
324 const bool catch_all_case = !(true_zero_case || false_zero_case);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800325
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700326 if (true_zero_case || false_zero_case) {
327 OpRegReg(kOpXor, rl_result.reg, rl_result.reg);
328 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800329
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700330 if (true_zero_case || false_zero_case || catch_all_case) {
331 OpRegImm(kOpCmp, rl_src.reg, 0);
332 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800333
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700334 if (catch_all_case) {
335 OpRegImm(kOpMov, rl_result.reg, false_val);
336 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800337
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700338 if (true_zero_case || false_zero_case || catch_all_case) {
339 ConditionCode cc = true_zero_case ? NegateComparison(ccode) : ccode;
340 int immediateForTemp = true_zero_case ? false_val : true_val;
341 RegStorage temp1_reg = AllocTypedTemp(false, result_reg_class);
342 OpRegImm(kOpMov, temp1_reg, immediateForTemp);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800343
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700344 OpCondRegReg(kOpCmov, cc, rl_result.reg, temp1_reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800345
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700346 FreeTemp(temp1_reg);
347 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800348 }
349 } else {
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700350 rl_src = LoadValue(rl_src, src_reg_class);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800351 RegLocation rl_true = mir_graph_->GetSrc(mir, 1);
352 RegLocation rl_false = mir_graph_->GetSrc(mir, 2);
buzbeea0cd2d72014-06-01 09:33:49 -0700353 rl_true = LoadValue(rl_true, result_reg_class);
354 rl_false = LoadValue(rl_false, result_reg_class);
355 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800356
357 /*
Vladimir Markoa1a70742014-03-03 10:28:05 +0000358 * For ccode == kCondEq:
359 *
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800360 * 1) When true case is already in place:
361 * cmp $0, src_reg
362 * cmovnz result_reg, false_reg
363 * 2) When false case is already in place:
364 * cmp $0, src_reg
365 * cmovz result_reg, true_reg
366 * 3) When neither cases are in place:
367 * cmp $0, src_reg
Vladimir Markoa1a70742014-03-03 10:28:05 +0000368 * mov result_reg, false_reg
369 * cmovz result_reg, true_reg
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800370 */
371
372 // kMirOpSelect is generated just for conditional cases when comparison is done with zero.
buzbee2700f7e2014-03-07 09:46:20 -0800373 OpRegImm(kOpCmp, rl_src.reg, 0);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800374
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000375 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) {
buzbee2700f7e2014-03-07 09:46:20 -0800376 OpCondRegReg(kOpCmov, NegateComparison(ccode), rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000377 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) {
buzbee2700f7e2014-03-07 09:46:20 -0800378 OpCondRegReg(kOpCmov, ccode, rl_result.reg, rl_true.reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800379 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800380 OpRegCopy(rl_result.reg, rl_false.reg);
381 OpCondRegReg(kOpCmov, ccode, rl_result.reg, rl_true.reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800382 }
383 }
384
385 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386}
387
388void X86Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
buzbee0d829482013-10-11 15:24:55 -0700389 LIR* taken = &block_label_list_[bb->taken];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
391 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Vladimir Markoa8946072014-01-22 10:30:44 +0000392 ConditionCode ccode = mir->meta.ccode;
Mark Mendell412d4f82013-12-18 13:32:36 -0800393
394 if (rl_src1.is_const) {
395 std::swap(rl_src1, rl_src2);
396 ccode = FlipComparisonOrder(ccode);
397 }
398 if (rl_src2.is_const) {
399 // Do special compare/branch against simple const operand
400 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
401 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
402 return;
403 }
404
Elena Sayapinadd644502014-07-01 18:39:52 +0700405 if (cu_->target64) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700406 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
407 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
408
409 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
410 OpCondBranch(ccode, taken);
411 return;
412 }
413
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700414 // Prepare for explicit register usage
415 ExplicitTempRegisterLock(this, 4, &rs_r0, &rs_r1, &rs_r2, &rs_r3);
buzbee091cc402014-03-31 10:14:40 -0700416 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_r0, rs_r1);
417 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_r2, rs_r3);
buzbee2700f7e2014-03-07 09:46:20 -0800418 LoadValueDirectWideFixed(rl_src1, r_tmp1);
419 LoadValueDirectWideFixed(rl_src2, r_tmp2);
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700420
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 // Swap operands and condition code to prevent use of zero flag.
422 if (ccode == kCondLe || ccode == kCondGt) {
423 // Compute (r3:r2) = (r3:r2) - (r1:r0)
buzbee2700f7e2014-03-07 09:46:20 -0800424 OpRegReg(kOpSub, rs_r2, rs_r0); // r2 = r2 - r0
425 OpRegReg(kOpSbc, rs_r3, rs_r1); // r3 = r3 - r1 - CF
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 } else {
427 // Compute (r1:r0) = (r1:r0) - (r3:r2)
buzbee2700f7e2014-03-07 09:46:20 -0800428 OpRegReg(kOpSub, rs_r0, rs_r2); // r0 = r0 - r2
429 OpRegReg(kOpSbc, rs_r1, rs_r3); // r1 = r1 - r3 - CF
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 }
431 switch (ccode) {
432 case kCondEq:
433 case kCondNe:
buzbee2700f7e2014-03-07 09:46:20 -0800434 OpRegReg(kOpOr, rs_r0, rs_r1); // r0 = r0 | r1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 break;
436 case kCondLe:
437 ccode = kCondGe;
438 break;
439 case kCondGt:
440 ccode = kCondLt;
441 break;
442 case kCondLt:
443 case kCondGe:
444 break;
445 default:
446 LOG(FATAL) << "Unexpected ccode: " << ccode;
447 }
448 OpCondBranch(ccode, taken);
449}
450
Mark Mendell412d4f82013-12-18 13:32:36 -0800451void X86Mir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
452 int64_t val, ConditionCode ccode) {
453 int32_t val_lo = Low32Bits(val);
454 int32_t val_hi = High32Bits(val);
455 LIR* taken = &block_label_list_[bb->taken];
Mark Mendell412d4f82013-12-18 13:32:36 -0800456 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Mark Mendell752e2052014-05-01 10:19:04 -0400457 bool is_equality_test = ccode == kCondEq || ccode == kCondNe;
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700458
Elena Sayapinadd644502014-07-01 18:39:52 +0700459 if (cu_->target64) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700460 if (is_equality_test && val == 0) {
461 // We can simplify of comparing for ==, != to 0.
462 NewLIR2(kX86Test64RR, rl_src1.reg.GetReg(), rl_src1.reg.GetReg());
463 } else if (is_equality_test && val_hi == 0 && val_lo > 0) {
464 OpRegImm(kOpCmp, rl_src1.reg, val_lo);
465 } else {
466 RegStorage tmp = AllocTypedTempWide(false, kCoreReg);
467 LoadConstantWide(tmp, val);
468 OpRegReg(kOpCmp, rl_src1.reg, tmp);
469 FreeTemp(tmp);
470 }
471 OpCondBranch(ccode, taken);
472 return;
473 }
474
Mark Mendell752e2052014-05-01 10:19:04 -0400475 if (is_equality_test && val != 0) {
476 rl_src1 = ForceTempWide(rl_src1);
477 }
buzbee2700f7e2014-03-07 09:46:20 -0800478 RegStorage low_reg = rl_src1.reg.GetLow();
479 RegStorage high_reg = rl_src1.reg.GetHigh();
Mark Mendell412d4f82013-12-18 13:32:36 -0800480
Mark Mendell752e2052014-05-01 10:19:04 -0400481 if (is_equality_test) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700482 // We can simplify of comparing for ==, != to 0.
Mark Mendell752e2052014-05-01 10:19:04 -0400483 if (val == 0) {
484 if (IsTemp(low_reg)) {
485 OpRegReg(kOpOr, low_reg, high_reg);
486 // We have now changed it; ignore the old values.
487 Clobber(rl_src1.reg);
488 } else {
489 RegStorage t_reg = AllocTemp();
490 OpRegRegReg(kOpOr, t_reg, low_reg, high_reg);
491 FreeTemp(t_reg);
492 }
493 OpCondBranch(ccode, taken);
494 return;
495 }
496
497 // Need to compute the actual value for ==, !=.
498 OpRegImm(kOpSub, low_reg, val_lo);
499 NewLIR2(kX86Sbb32RI, high_reg.GetReg(), val_hi);
500 OpRegReg(kOpOr, high_reg, low_reg);
501 Clobber(rl_src1.reg);
502 } else if (ccode == kCondLe || ccode == kCondGt) {
503 // Swap operands and condition code to prevent use of zero flag.
504 RegStorage tmp = AllocTypedTempWide(false, kCoreReg);
505 LoadConstantWide(tmp, val);
506 OpRegReg(kOpSub, tmp.GetLow(), low_reg);
507 OpRegReg(kOpSbc, tmp.GetHigh(), high_reg);
508 ccode = (ccode == kCondLe) ? kCondGe : kCondLt;
509 FreeTemp(tmp);
510 } else {
511 // We can use a compare for the low word to set CF.
512 OpRegImm(kOpCmp, low_reg, val_lo);
513 if (IsTemp(high_reg)) {
514 NewLIR2(kX86Sbb32RI, high_reg.GetReg(), val_hi);
515 // We have now changed it; ignore the old values.
516 Clobber(rl_src1.reg);
517 } else {
518 // mov temp_reg, high_reg; sbb temp_reg, high_constant
519 RegStorage t_reg = AllocTemp();
520 OpRegCopy(t_reg, high_reg);
521 NewLIR2(kX86Sbb32RI, t_reg.GetReg(), val_hi);
522 FreeTemp(t_reg);
523 }
Mark Mendell412d4f82013-12-18 13:32:36 -0800524 }
525
Mark Mendell752e2052014-05-01 10:19:04 -0400526 OpCondBranch(ccode, taken);
Mark Mendell412d4f82013-12-18 13:32:36 -0800527}
528
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700529void X86Mir2Lir::CalculateMagicAndShift(int64_t divisor, int64_t& magic, int& shift, bool is_long) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800530 // It does not make sense to calculate magic and shift for zero divisor.
531 DCHECK_NE(divisor, 0);
532
533 /* According to H.S.Warren's Hacker's Delight Chapter 10 and
534 * T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
535 * The magic number M and shift S can be calculated in the following way:
536 * Let nc be the most positive value of numerator(n) such that nc = kd - 1,
537 * where divisor(d) >=2.
538 * Let nc be the most negative value of numerator(n) such that nc = kd + 1,
539 * where divisor(d) <= -2.
540 * Thus nc can be calculated like:
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700541 * nc = exp + exp % d - 1, where d >= 2 and exp = 2^31 for int or 2^63 for long
542 * nc = -exp + (exp + 1) % d, where d >= 2 and exp = 2^31 for int or 2^63 for long
Mark Mendell2bf31e62014-01-23 12:13:40 -0800543 *
544 * So the shift p is the smallest p satisfying
545 * 2^p > nc * (d - 2^p % d), where d >= 2
546 * 2^p > nc * (d + 2^p % d), where d <= -2.
547 *
548 * the magic number M is calcuated by
549 * M = (2^p + d - 2^p % d) / d, where d >= 2
550 * M = (2^p - d - 2^p % d) / d, where d <= -2.
551 *
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700552 * Notice that p is always bigger than or equal to 32/64, so we just return 32-p/64-p as
Mark Mendell2bf31e62014-01-23 12:13:40 -0800553 * the shift number S.
554 */
555
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700556 int64_t p = (is_long) ? 63 : 31;
557 const uint64_t exp = (is_long) ? 0x8000000000000000ULL : 0x80000000U;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800558
559 // Initialize the computations.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700560 uint64_t abs_d = (divisor >= 0) ? divisor : -divisor;
561 uint64_t tmp = exp + ((is_long) ? static_cast<uint64_t>(divisor) >> 63 :
562 static_cast<uint32_t>(divisor) >> 31);
563 uint64_t abs_nc = tmp - 1 - tmp % abs_d;
564 uint64_t quotient1 = exp / abs_nc;
565 uint64_t remainder1 = exp % abs_nc;
566 uint64_t quotient2 = exp / abs_d;
567 uint64_t remainder2 = exp % abs_d;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800568
569 /*
570 * To avoid handling both positive and negative divisor, Hacker's Delight
571 * introduces a method to handle these 2 cases together to avoid duplication.
572 */
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700573 uint64_t delta;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800574 do {
575 p++;
576 quotient1 = 2 * quotient1;
577 remainder1 = 2 * remainder1;
578 if (remainder1 >= abs_nc) {
579 quotient1++;
580 remainder1 = remainder1 - abs_nc;
581 }
582 quotient2 = 2 * quotient2;
583 remainder2 = 2 * remainder2;
584 if (remainder2 >= abs_d) {
585 quotient2++;
586 remainder2 = remainder2 - abs_d;
587 }
588 delta = abs_d - remainder2;
589 } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
590
591 magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700592
593 if (!is_long) {
594 magic = static_cast<int>(magic);
595 }
596
597 shift = (is_long) ? p - 64 : p - 32;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800598}
599
buzbee2700f7e2014-03-07 09:46:20 -0800600RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg_lo, int lit, bool is_div) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700601 UNUSED(rl_dest, reg_lo, lit, is_div);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 LOG(FATAL) << "Unexpected use of GenDivRemLit for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700603 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604}
605
Mark Mendell2bf31e62014-01-23 12:13:40 -0800606RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src,
607 int imm, bool is_div) {
608 // Use a multiply (and fixup) to perform an int div/rem by a constant.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700609 RegLocation rl_result;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800610
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700611 if (imm == 1) {
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700612 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800613 if (is_div) {
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700614 // x / 1 == x.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700615 LoadValueDirectFixed(rl_src, rl_result.reg);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700616 } else {
617 // x % 1 == 0.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700618 LoadConstantNoClobber(rl_result.reg, 0);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700619 }
620 } else if (imm == -1) { // handle 0x80000000 / -1 special case.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700621 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700622 if (is_div) {
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700623 LoadValueDirectFixed(rl_src, rl_result.reg);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400624
625 // Check if numerator is 0
626 OpRegImm(kOpCmp, rl_result.reg, 0);
627 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
628
629 // handle 0x80000000 / -1
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700630 OpRegImm(kOpCmp, rl_result.reg, 0x80000000);
631 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800632
633 // for x != MIN_INT, x / -1 == -x.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700634 NewLIR1(kX86Neg32R, rl_result.reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800635
Mark Mendell2bf31e62014-01-23 12:13:40 -0800636 // EAX already contains the right value (0x80000000),
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700637 minint_branch->target = NewLIR0(kPseudoTargetLabel);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400638 branch->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800639 } else {
640 // x % -1 == 0.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700641 LoadConstantNoClobber(rl_result.reg, 0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800642 }
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700643 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
644 // Division using shifting.
645 rl_src = LoadValue(rl_src, kCoreReg);
646 rl_result = EvalLoc(rl_dest, kCoreReg, true);
647 if (IsSameReg(rl_result.reg, rl_src.reg)) {
648 RegStorage rs_temp = AllocTypedTemp(false, kCoreReg);
649 rl_result.reg.SetReg(rs_temp.GetReg());
650 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400651
652 // Check if numerator is 0
653 OpRegImm(kOpCmp, rl_src.reg, 0);
654 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
655 LoadConstantNoClobber(rl_result.reg, 0);
656 LIR* done = NewLIR1(kX86Jmp8, 0);
657 branch->target = NewLIR0(kPseudoTargetLabel);
658
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700659 NewLIR3(kX86Lea32RM, rl_result.reg.GetReg(), rl_src.reg.GetReg(), std::abs(imm) - 1);
660 NewLIR2(kX86Test32RR, rl_src.reg.GetReg(), rl_src.reg.GetReg());
661 OpCondRegReg(kOpCmov, kCondPl, rl_result.reg, rl_src.reg);
Andreas Gampe7e499922015-01-06 08:28:12 -0800662 int shift_amount = CTZ(imm);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700663 OpRegImm(kOpAsr, rl_result.reg, shift_amount);
664 if (imm < 0) {
665 OpReg(kOpNeg, rl_result.reg);
666 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400667 done->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800668 } else {
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700669 CHECK(imm <= -2 || imm >= 2);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700670
Mark Mendell2bf31e62014-01-23 12:13:40 -0800671 // Use H.S.Warren's Hacker's Delight Chapter 10 and
672 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700673 int64_t magic;
674 int shift;
675 CalculateMagicAndShift((int64_t)imm, magic, shift, false /* is_long */);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800676
677 /*
678 * For imm >= 2,
679 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
680 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
681 * For imm <= -2,
682 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
683 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
684 * We implement this algorithm in the following way:
685 * 1. multiply magic number m and numerator n, get the higher 32bit result in EDX
686 * 2. if imm > 0 and magic < 0, add numerator to EDX
687 * if imm < 0 and magic > 0, sub numerator from EDX
688 * 3. if S !=0, SAR S bits for EDX
689 * 4. add 1 to EDX if EDX < 0
690 * 5. Thus, EDX is the quotient
691 */
692
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700693 FlushReg(rs_r0);
694 Clobber(rs_r0);
695 LockTemp(rs_r0);
696 FlushReg(rs_r2);
697 Clobber(rs_r2);
698 LockTemp(rs_r2);
699
Mark Mendell3a91f442014-09-02 12:44:24 -0400700 // Assume that the result will be in EDX for divide, and EAX for remainder.
701 rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, is_div ? rs_r2 : rs_r0,
702 INVALID_SREG, INVALID_SREG};
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700703
Mark Mendell3a91f442014-09-02 12:44:24 -0400704 // We need the value at least twice. Load into a temp.
705 rl_src = LoadValue(rl_src, kCoreReg);
706 RegStorage numerator_reg = rl_src.reg;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800707
Mark Mendell3a91f442014-09-02 12:44:24 -0400708 // Check if numerator is 0.
709 OpRegImm(kOpCmp, numerator_reg, 0);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400710 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell3a91f442014-09-02 12:44:24 -0400711 // Return result 0 if numerator was 0.
712 LoadConstantNoClobber(rl_result.reg, 0);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400713 LIR* done = NewLIR1(kX86Jmp8, 0);
714 branch->target = NewLIR0(kPseudoTargetLabel);
715
Mark Mendell3a91f442014-09-02 12:44:24 -0400716 // EAX = magic.
717 LoadConstant(rs_r0, magic);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800718
Mark Mendell3a91f442014-09-02 12:44:24 -0400719 // EDX:EAX = magic * numerator.
720 NewLIR1(kX86Imul32DaR, numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800721
722 if (imm > 0 && magic < 0) {
723 // Add numerator to EDX.
buzbee2700f7e2014-03-07 09:46:20 -0800724 DCHECK(numerator_reg.Valid());
buzbee091cc402014-03-31 10:14:40 -0700725 NewLIR2(kX86Add32RR, rs_r2.GetReg(), numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800726 } else if (imm < 0 && magic > 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800727 DCHECK(numerator_reg.Valid());
buzbee091cc402014-03-31 10:14:40 -0700728 NewLIR2(kX86Sub32RR, rs_r2.GetReg(), numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800729 }
730
731 // Do we need the shift?
732 if (shift != 0) {
733 // Shift EDX by 'shift' bits.
buzbee091cc402014-03-31 10:14:40 -0700734 NewLIR2(kX86Sar32RI, rs_r2.GetReg(), shift);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800735 }
736
737 // Add 1 to EDX if EDX < 0.
738
739 // Move EDX to EAX.
buzbee2700f7e2014-03-07 09:46:20 -0800740 OpRegCopy(rs_r0, rs_r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800741
742 // Move sign bit to bit 0, zeroing the rest.
buzbee091cc402014-03-31 10:14:40 -0700743 NewLIR2(kX86Shr32RI, rs_r2.GetReg(), 31);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800744
745 // EDX = EDX + EAX.
buzbee091cc402014-03-31 10:14:40 -0700746 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r0.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800747
748 // Quotient is in EDX.
749 if (!is_div) {
750 // We need to compute the remainder.
751 // Remainder is divisor - (quotient * imm).
buzbee2700f7e2014-03-07 09:46:20 -0800752 DCHECK(numerator_reg.Valid());
753 OpRegCopy(rs_r0, numerator_reg);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800754
755 // EAX = numerator * imm.
buzbee2700f7e2014-03-07 09:46:20 -0800756 OpRegRegImm(kOpMul, rs_r2, rs_r2, imm);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800757
Mark Mendell3a91f442014-09-02 12:44:24 -0400758 // EAX -= EDX.
buzbee091cc402014-03-31 10:14:40 -0700759 NewLIR2(kX86Sub32RR, rs_r0.GetReg(), rs_r2.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800760
761 // For this case, return the result in EAX.
Mark Mendell2bf31e62014-01-23 12:13:40 -0800762 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400763 done->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800764 }
765
766 return rl_result;
767}
768
buzbee2700f7e2014-03-07 09:46:20 -0800769RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg_lo, RegStorage reg_hi,
770 bool is_div) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700771 UNUSED(rl_dest, reg_lo, reg_hi, is_div);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 LOG(FATAL) << "Unexpected use of GenDivRem for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700773 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774}
775
Mark Mendell2bf31e62014-01-23 12:13:40 -0800776RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700777 RegLocation rl_src2, bool is_div, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700778 UNUSED(rl_dest);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800779 // We have to use fixed registers, so flush all the temps.
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700780
781 // Prepare for explicit register usage.
782 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800783
784 // Load LHS into EAX.
buzbee2700f7e2014-03-07 09:46:20 -0800785 LoadValueDirectFixed(rl_src1, rs_r0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800786
787 // Load RHS into EBX.
buzbee2700f7e2014-03-07 09:46:20 -0800788 LoadValueDirectFixed(rl_src2, rs_r1);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800789
790 // Copy LHS sign bit into EDX.
791 NewLIR0(kx86Cdq32Da);
792
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700793 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800794 // Handle division by zero case.
Mingyao Yange643a172014-04-08 11:02:52 -0700795 GenDivZeroCheck(rs_r1);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800796 }
797
Yixin Shou2ddd1752014-08-26 15:15:13 -0400798 // Check if numerator is 0
799 OpRegImm(kOpCmp, rs_r0, 0);
800 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
801
Mark Mendell2bf31e62014-01-23 12:13:40 -0800802 // Have to catch 0x80000000/-1 case, or we will get an exception!
buzbee2700f7e2014-03-07 09:46:20 -0800803 OpRegImm(kOpCmp, rs_r1, -1);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700804 LIR* minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800805
806 // RHS is -1.
buzbee2700f7e2014-03-07 09:46:20 -0800807 OpRegImm(kOpCmp, rs_r0, 0x80000000);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700808 LIR* minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800809
Yixin Shou2ddd1752014-08-26 15:15:13 -0400810 branch->target = NewLIR0(kPseudoTargetLabel);
811
Mark Mendell2bf31e62014-01-23 12:13:40 -0800812 // In 0x80000000/-1 case.
813 if (!is_div) {
814 // For DIV, EAX is already right. For REM, we need EDX 0.
buzbee2700f7e2014-03-07 09:46:20 -0800815 LoadConstantNoClobber(rs_r2, 0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800816 }
817 LIR* done = NewLIR1(kX86Jmp8, 0);
818
819 // Expected case.
820 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
821 minint_branch->target = minus_one_branch->target;
buzbee091cc402014-03-31 10:14:40 -0700822 NewLIR1(kX86Idivmod32DaR, rs_r1.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800823 done->target = NewLIR0(kPseudoTargetLabel);
824
825 // Result is in EAX for div and EDX for rem.
buzbee091cc402014-03-31 10:14:40 -0700826 RegLocation rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, rs_r0, INVALID_SREG, INVALID_SREG};
Mark Mendell2bf31e62014-01-23 12:13:40 -0800827 if (!is_div) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000828 rl_result.reg.SetReg(r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800829 }
830 return rl_result;
831}
832
David Srbecky1109fb32015-04-07 20:21:06 +0100833static dwarf::Reg DwarfCoreReg(bool is_x86_64, int num) {
834 return is_x86_64 ? dwarf::Reg::X86_64Core(num) : dwarf::Reg::X86Core(num);
835}
836
Serban Constantinescu23abec92014-07-02 16:13:38 +0100837bool X86Mir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700838 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800839
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700840 if (is_long && !cu_->target64) {
841 /*
842 * We want to implement the following algorithm
843 * mov eax, low part of arg1
844 * mov edx, high part of arg1
845 * mov ebx, low part of arg2
846 * mov ecx, high part of arg2
847 * mov edi, eax
848 * sub edi, ebx
849 * mov edi, edx
850 * sbb edi, ecx
851 * is_min ? "cmovgel eax, ebx" : "cmovll eax, ebx"
852 * is_min ? "cmovgel edx, ecx" : "cmovll edx, ecx"
853 *
854 * The algorithm above needs 5 registers: a pair for the first operand
855 * (which later will be used as result), a pair for the second operand
856 * and a temp register (e.g. 'edi') for intermediate calculations.
857 * Ideally we have 6 GP caller-save registers in 32-bit mode. They are:
858 * 'eax', 'ebx', 'ecx', 'edx', 'esi' and 'edi'. So there should be
859 * always enough registers to operate on. Practically, there is a pair
860 * of registers 'edi' and 'esi' which holds promoted values and
861 * sometimes should be treated as 'callee save'. If one of the operands
862 * is in the promoted registers then we have enough register to
863 * operate on. Otherwise there is lack of resources and we have to
864 * save 'edi' before calculations and restore after.
865 */
866
867 RegLocation rl_src1 = info->args[0];
868 RegLocation rl_src2 = info->args[2];
869 RegLocation rl_dest = InlineTargetWide(info);
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700870
Mark Mendella65c1db2014-10-21 17:44:32 -0400871 if (rl_dest.s_reg_low == INVALID_SREG) {
872 // Result is unused, the code is dead. Inlining successful, no code generated.
873 return true;
874 }
875
nikolay serdjuk55693282015-01-20 17:03:02 +0600876 if (PartiallyIntersects(rl_src1, rl_dest) &&
877 PartiallyIntersects(rl_src2, rl_dest)) {
878 // A special case which we don't want to handle.
879 // This is when src1 is mapped on v0 and v1,
880 // src2 is mapped on v2, v3,
881 // result is mapped on v1, v2
882 return false;
883 }
884
885
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700886 /*
887 * If the result register is the same as the second element, then we
888 * need to be careful. The reason is that the first copy will
889 * inadvertently clobber the second element with the first one thus
890 * yielding the wrong result. Thus we do a swap in that case.
891 */
nikolay serdjuk55693282015-01-20 17:03:02 +0600892 if (Intersects(rl_src2, rl_dest)) {
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700893 std::swap(rl_src1, rl_src2);
894 }
895
896 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
897 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
898
899 // Pick the first integer as min/max.
900 OpRegCopyWide(rl_result.reg, rl_src1.reg);
901
902 /*
903 * If the integers are both in the same register, then there is
904 * nothing else to do because they are equal and we have already
905 * moved one into the result.
906 */
nikolay serdjuk55693282015-01-20 17:03:02 +0600907 if (mir_graph_->SRegToVReg(rl_src1.s_reg_low) ==
908 mir_graph_->SRegToVReg(rl_src2.s_reg_low)) {
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700909 StoreValueWide(rl_dest, rl_result);
910 return true;
911 }
912
913 // Free registers to make some room for the second operand.
nikolay serdjuk55693282015-01-20 17:03:02 +0600914 // But don't try to free part of a source which intersects
915 // part of result or promoted registers.
916
917 if (IsTemp(rl_src1.reg.GetLow()) &&
918 (rl_src1.reg.GetLowReg() != rl_result.reg.GetHighReg()) &&
919 (rl_src1.reg.GetLowReg() != rl_result.reg.GetLowReg())) {
920 // Is low part temporary and doesn't intersect any parts of result?
921 FreeTemp(rl_src1.reg.GetLow());
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700922 }
nikolay serdjuk55693282015-01-20 17:03:02 +0600923
924 if (IsTemp(rl_src1.reg.GetHigh()) &&
925 (rl_src1.reg.GetHighReg() != rl_result.reg.GetLowReg()) &&
926 (rl_src1.reg.GetHighReg() != rl_result.reg.GetHighReg())) {
927 // Is high part temporary and doesn't intersect any parts of result?
928 FreeTemp(rl_src1.reg.GetHigh());
929 }
930
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700931 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
932
933 // Do we have a free register for intermediate calculations?
934 RegStorage tmp = AllocTemp(false);
David Srbecky1109fb32015-04-07 20:21:06 +0100935 const int kRegSize = cu_->target64 ? 8 : 4;
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700936 if (tmp == RegStorage::InvalidReg()) {
937 /*
938 * No, will use 'edi'.
939 *
940 * As mentioned above we have 4 temporary and 2 promotable
941 * caller-save registers. Therefore, we assume that a free
942 * register can be allocated only if 'esi' and 'edi' are
943 * already used as operands. If number of promotable registers
944 * increases from 2 to 4 then our assumption fails and operand
945 * data is corrupted.
946 * Let's DCHECK it.
947 */
948 DCHECK(IsTemp(rl_src2.reg.GetLow()) &&
949 IsTemp(rl_src2.reg.GetHigh()) &&
950 IsTemp(rl_result.reg.GetLow()) &&
951 IsTemp(rl_result.reg.GetHigh()));
952 tmp = rs_rDI;
953 NewLIR1(kX86Push32R, tmp.GetReg());
David Srbecky1109fb32015-04-07 20:21:06 +0100954 cfi_.AdjustCFAOffset(kRegSize);
955 // Record cfi only if it is not already spilled.
956 if (!CoreSpillMaskContains(tmp.GetReg())) {
957 cfi_.RelOffset(DwarfCoreReg(cu_->target64, tmp.GetReg()), 0);
958 }
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700959 }
960
961 // Now we are ready to do calculations.
962 OpRegReg(kOpMov, tmp, rl_result.reg.GetLow());
963 OpRegReg(kOpSub, tmp, rl_src2.reg.GetLow());
964 OpRegReg(kOpMov, tmp, rl_result.reg.GetHigh());
965 OpRegReg(kOpSbc, tmp, rl_src2.reg.GetHigh());
966
967 // Let's put pop 'edi' here to break a bit the dependency chain.
968 if (tmp == rs_rDI) {
969 NewLIR1(kX86Pop32R, tmp.GetReg());
David Srbecky1109fb32015-04-07 20:21:06 +0100970 cfi_.AdjustCFAOffset(-kRegSize);
971 if (!CoreSpillMaskContains(tmp.GetReg())) {
972 cfi_.Restore(DwarfCoreReg(cu_->target64, tmp.GetReg()));
973 }
nikolay serdjuk55693282015-01-20 17:03:02 +0600974 } else {
975 FreeTemp(tmp);
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700976 }
977
978 // Conditionally move the other integer into the destination register.
979 ConditionCode cc = is_min ? kCondGe : kCondLt;
980 OpCondRegReg(kOpCmov, cc, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
981 OpCondRegReg(kOpCmov, cc, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
nikolay serdjuk55693282015-01-20 17:03:02 +0600982 FreeTemp(rl_src2.reg);
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700983 StoreValueWide(rl_dest, rl_result);
984 return true;
Serban Constantinescu23abec92014-07-02 16:13:38 +0100985 }
986
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800987 // Get the two arguments to the invoke and place them in GP registers.
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800988 RegLocation rl_dest = (is_long) ? InlineTargetWide(info) : InlineTarget(info);
989 if (rl_dest.s_reg_low == INVALID_SREG) {
990 // Result is unused, the code is dead. Inlining successful, no code generated.
991 return true;
992 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 RegLocation rl_src1 = info->args[0];
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700994 RegLocation rl_src2 = (is_long) ? info->args[2] : info->args[1];
995 rl_src1 = (is_long) ? LoadValueWide(rl_src1, kCoreReg) : LoadValue(rl_src1, kCoreReg);
996 rl_src2 = (is_long) ? LoadValueWide(rl_src2, kCoreReg) : LoadValue(rl_src2, kCoreReg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800997
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800999
1000 /*
1001 * If the result register is the same as the second element, then we need to be careful.
1002 * The reason is that the first copy will inadvertently clobber the second element with
1003 * the first one thus yielding the wrong result. Thus we do a swap in that case.
1004 */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001005 if (rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001006 std::swap(rl_src1, rl_src2);
1007 }
1008
1009 // Pick the first integer as min/max.
buzbee2700f7e2014-03-07 09:46:20 -08001010 OpRegCopy(rl_result.reg, rl_src1.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001011
1012 // If the integers are both in the same register, then there is nothing else to do
1013 // because they are equal and we have already moved one into the result.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001014 if (rl_src1.reg.GetReg() != rl_src2.reg.GetReg()) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001015 // It is possible we didn't pick correctly so do the actual comparison now.
buzbee2700f7e2014-03-07 09:46:20 -08001016 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001017
1018 // Conditionally move the other integer into the destination register.
1019 ConditionCode condition_code = is_min ? kCondGt : kCondLt;
buzbee2700f7e2014-03-07 09:46:20 -08001020 OpCondRegReg(kOpCmov, condition_code, rl_result.reg, rl_src2.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001021 }
1022
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001023 if (is_long) {
Vladimir Markoe508a202013-11-04 15:24:22 +00001024 StoreValueWide(rl_dest, rl_result);
1025 } else {
Vladimir Markoe508a202013-11-04 15:24:22 +00001026 StoreValue(rl_dest, rl_result);
1027 }
1028 return true;
1029}
1030
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001031bool X86Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001032 RegLocation rl_dest = size == k64 ? InlineTargetWide(info) : InlineTarget(info);
1033 if (rl_dest.s_reg_low == INVALID_SREG) {
1034 // Result is unused, the code is dead. Inlining successful, no code generated.
1035 return true;
1036 }
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001037 RegLocation rl_src_address = info->args[0]; // long address
1038 RegLocation rl_address;
1039 if (!cu_->target64) {
1040 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[0]
1041 rl_address = LoadValue(rl_src_address, kCoreReg);
1042 } else {
1043 rl_address = LoadValueWide(rl_src_address, kCoreReg);
1044 }
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001045 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1046 // Unaligned access is allowed on x86.
1047 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
1048 if (size == k64) {
1049 StoreValueWide(rl_dest, rl_result);
1050 } else {
1051 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
1052 StoreValue(rl_dest, rl_result);
1053 }
1054 return true;
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001055}
1056
Vladimir Markoe508a202013-11-04 15:24:22 +00001057bool X86Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001058 RegLocation rl_src_address = info->args[0]; // long address
1059 RegLocation rl_address;
1060 if (!cu_->target64) {
1061 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[0]
1062 rl_address = LoadValue(rl_src_address, kCoreReg);
1063 } else {
1064 rl_address = LoadValueWide(rl_src_address, kCoreReg);
1065 }
1066 RegLocation rl_src_value = info->args[2]; // [size] value
1067 RegLocation rl_value;
1068 if (size == k64) {
1069 // Unaligned access is allowed on x86.
1070 rl_value = LoadValueWide(rl_src_value, kCoreReg);
1071 } else {
1072 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
1073 // In 32-bit mode the only EAX..EDX registers can be used with Mov8MR.
1074 if (!cu_->target64 && size == kSignedByte) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001075 rl_src_value = UpdateLocTyped(rl_src_value);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001076 if (rl_src_value.location == kLocPhysReg && !IsByteRegister(rl_src_value.reg)) {
1077 RegStorage temp = AllocateByteRegister();
1078 OpRegCopy(temp, rl_src_value.reg);
1079 rl_value.reg = temp;
1080 } else {
1081 rl_value = LoadValue(rl_src_value, kCoreReg);
1082 }
1083 } else {
1084 rl_value = LoadValue(rl_src_value, kCoreReg);
1085 }
1086 }
1087 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
1088 return true;
Vladimir Markoe508a202013-11-04 15:24:22 +00001089}
1090
buzbee2700f7e2014-03-07 09:46:20 -08001091void X86Mir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
1092 NewLIR5(kX86Lea32RA, r_base.GetReg(), reg1.GetReg(), reg2.GetReg(), scale, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093}
1094
Ian Rogersdd7624d2014-03-14 17:43:00 -07001095void X86Mir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001096 DCHECK_EQ(kX86, cu_->instruction_set);
1097 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
1098}
1099
1100void X86Mir2Lir::OpTlsCmp(ThreadOffset<8> offset, int val) {
1101 DCHECK_EQ(kX86_64, cu_->instruction_set);
Ian Rogers468532e2013-08-05 10:56:33 -07001102 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103}
1104
buzbee2700f7e2014-03-07 09:46:20 -08001105static bool IsInReg(X86Mir2Lir *pMir2Lir, const RegLocation &rl, RegStorage reg) {
1106 return rl.reg.Valid() && rl.reg.GetReg() == reg.GetReg() && (pMir2Lir->IsLive(reg) || rl.home);
Yevgeny Rouband3a2dfa2014-03-18 15:55:16 +07001107}
1108
Vladimir Marko1c282e22013-11-21 14:49:47 +00001109bool X86Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001110 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001111 // Unused - RegLocation rl_src_unsafe = info->args[0];
1112 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
1113 RegLocation rl_src_offset = info->args[2]; // long low
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001114 if (!cu_->target64) {
1115 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
1116 }
Vladimir Markoc29bb612013-11-27 16:47:25 +00001117 RegLocation rl_src_expected = info->args[4]; // int, long or Object
1118 // If is_long, high half is in info->args[5]
1119 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
1120 // If is_long, high half is in info->args[7]
David Srbecky1109fb32015-04-07 20:21:06 +01001121 const int kRegSize = cu_->target64 ? 8 : 4;
Vladimir Markoc29bb612013-11-27 16:47:25 +00001122
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001123 if (is_long && cu_->target64) {
1124 // RAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in RAX.
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001125 FlushReg(rs_r0q);
1126 Clobber(rs_r0q);
1127 LockTemp(rs_r0q);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001128
1129 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
1130 RegLocation rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001131 RegLocation rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
1132 LoadValueDirectWide(rl_src_expected, rs_r0q);
Andreas Gampeccc60262014-07-04 18:02:38 -07001133 NewLIR5(kX86LockCmpxchg64AR, rl_object.reg.GetReg(), rl_offset.reg.GetReg(), 0, 0,
1134 rl_new_value.reg.GetReg());
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001135
1136 // After a store we need to insert barrier in case of potential load. Since the
1137 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated.
Hans Boehm48f5c472014-06-27 14:50:10 -07001138 GenMemBarrier(kAnyAny);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001139
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001140 FreeTemp(rs_r0q);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001141 } else if (is_long) {
Yevgeny Rouband3a2dfa2014-03-18 15:55:16 +07001142 // TODO: avoid unnecessary loads of SI and DI when the values are in registers.
Vladimir Marko70b797d2013-12-03 15:25:24 +00001143 FlushAllRegs();
1144 LockCallTemps();
buzbee091cc402014-03-31 10:14:40 -07001145 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
1146 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_rBX, rs_rCX);
buzbee2700f7e2014-03-07 09:46:20 -08001147 LoadValueDirectWideFixed(rl_src_expected, r_tmp1);
1148 LoadValueDirectWideFixed(rl_src_new_value, r_tmp2);
buzbee695d13a2014-04-19 13:32:20 -07001149 // FIXME: needs 64-bit update.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001150 const bool obj_in_di = IsInReg(this, rl_src_obj, rs_rDI);
1151 const bool obj_in_si = IsInReg(this, rl_src_obj, rs_rSI);
1152 DCHECK(!obj_in_si || !obj_in_di);
1153 const bool off_in_di = IsInReg(this, rl_src_offset, rs_rDI);
1154 const bool off_in_si = IsInReg(this, rl_src_offset, rs_rSI);
1155 DCHECK(!off_in_si || !off_in_di);
1156 // If obj/offset is in a reg, use that reg. Otherwise, use the empty reg.
1157 RegStorage rs_obj = obj_in_di ? rs_rDI : obj_in_si ? rs_rSI : !off_in_di ? rs_rDI : rs_rSI;
1158 RegStorage rs_off = off_in_si ? rs_rSI : off_in_di ? rs_rDI : !obj_in_si ? rs_rSI : rs_rDI;
1159 bool push_di = (!obj_in_di && !off_in_di) && (rs_obj == rs_rDI || rs_off == rs_rDI);
1160 bool push_si = (!obj_in_si && !off_in_si) && (rs_obj == rs_rSI || rs_off == rs_rSI);
1161 if (push_di) {
1162 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1163 MarkTemp(rs_rDI);
1164 LockTemp(rs_rDI);
David Srbecky1109fb32015-04-07 20:21:06 +01001165 cfi_.AdjustCFAOffset(kRegSize);
1166 // Record cfi only if it is not already spilled.
1167 if (!CoreSpillMaskContains(rs_rDI.GetReg())) {
1168 cfi_.RelOffset(DwarfCoreReg(cu_->target64, rs_rDI.GetReg()), 0);
1169 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001170 }
1171 if (push_si) {
1172 NewLIR1(kX86Push32R, rs_rSI.GetReg());
1173 MarkTemp(rs_rSI);
1174 LockTemp(rs_rSI);
David Srbecky1109fb32015-04-07 20:21:06 +01001175 cfi_.AdjustCFAOffset(kRegSize);
1176 // Record cfi only if it is not already spilled.
1177 if (!CoreSpillMaskContains(rs_rSI.GetReg())) {
1178 cfi_.RelOffset(DwarfCoreReg(cu_->target64, rs_rSI.GetReg()), 0);
1179 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001180 }
1181 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1182 const size_t push_offset = (push_si ? 4u : 0u) + (push_di ? 4u : 0u);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001183 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001184 if (!obj_in_si && !obj_in_di) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001185 LoadWordDisp(rs_rSP, SRegOffset(rl_src_obj.s_reg_low) + push_offset, rs_obj);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001186 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1187 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1188 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - push_offset / 4u;
1189 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
1190 }
1191 if (!off_in_si && !off_in_di) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001192 LoadWordDisp(rs_rSP, SRegOffset(rl_src_offset.s_reg_low) + push_offset, rs_off);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001193 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1194 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1195 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - push_offset / 4u;
1196 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
1197 }
1198 NewLIR4(kX86LockCmpxchg64A, rs_obj.GetReg(), rs_off.GetReg(), 0, 0);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001199
Hans Boehm48f5c472014-06-27 14:50:10 -07001200 // After a store we need to insert barrier to prevent reordering with either
1201 // earlier or later memory accesses. Since
1202 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated,
1203 // and it will be associated with the cmpxchg instruction, preventing both.
1204 GenMemBarrier(kAnyAny);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001205
1206 if (push_si) {
1207 FreeTemp(rs_rSI);
1208 UnmarkTemp(rs_rSI);
1209 NewLIR1(kX86Pop32R, rs_rSI.GetReg());
David Srbecky1109fb32015-04-07 20:21:06 +01001210 cfi_.AdjustCFAOffset(-kRegSize);
1211 if (!CoreSpillMaskContains(rs_rSI.GetReg())) {
1212 cfi_.Restore(DwarfCoreReg(cu_->target64, rs_rSI.GetRegNum()));
1213 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001214 }
1215 if (push_di) {
1216 FreeTemp(rs_rDI);
1217 UnmarkTemp(rs_rDI);
1218 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
David Srbecky1109fb32015-04-07 20:21:06 +01001219 cfi_.AdjustCFAOffset(-kRegSize);
1220 if (!CoreSpillMaskContains(rs_rDI.GetReg())) {
1221 cfi_.Restore(DwarfCoreReg(cu_->target64, rs_rDI.GetRegNum()));
1222 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001223 }
Vladimir Marko70b797d2013-12-03 15:25:24 +00001224 FreeCallTemps();
Vladimir Markoc29bb612013-11-27 16:47:25 +00001225 } else {
1226 // EAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in EAX.
buzbee2700f7e2014-03-07 09:46:20 -08001227 FlushReg(rs_r0);
buzbee091cc402014-03-31 10:14:40 -07001228 Clobber(rs_r0);
buzbee2700f7e2014-03-07 09:46:20 -08001229 LockTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001230
buzbeea0cd2d72014-06-01 09:33:49 -07001231 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Markofac10702015-04-22 11:51:52 +01001232 RegLocation rl_new_value = LoadValue(rl_src_new_value, is_object ? kRefReg : kCoreReg);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001233
1234 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
1235 // Mark card for object assuming new value is stored.
buzbee091cc402014-03-31 10:14:40 -07001236 FreeTemp(rs_r0); // Temporarily release EAX for MarkGCCard().
Vladimir Marko743b98c2014-11-24 19:45:41 +00001237 MarkGCCard(0, rl_new_value.reg, rl_object.reg);
buzbee091cc402014-03-31 10:14:40 -07001238 LockTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001239 }
1240
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001241 RegLocation rl_offset;
1242 if (cu_->target64) {
1243 rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
1244 } else {
1245 rl_offset = LoadValue(rl_src_offset, kCoreReg);
1246 }
buzbee2700f7e2014-03-07 09:46:20 -08001247 LoadValueDirect(rl_src_expected, rs_r0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001248 NewLIR5(kX86LockCmpxchgAR, rl_object.reg.GetReg(), rl_offset.reg.GetReg(), 0, 0,
1249 rl_new_value.reg.GetReg());
Vladimir Markoc29bb612013-11-27 16:47:25 +00001250
Hans Boehm48f5c472014-06-27 14:50:10 -07001251 // After a store we need to insert barrier to prevent reordering with either
1252 // earlier or later memory accesses. Since
1253 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated,
1254 // and it will be associated with the cmpxchg instruction, preventing both.
1255 GenMemBarrier(kAnyAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001256
buzbee091cc402014-03-31 10:14:40 -07001257 FreeTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001258 }
1259
1260 // Convert ZF to boolean
1261 RegLocation rl_dest = InlineTarget(info); // boolean place for result
1262 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001263 RegStorage result_reg = rl_result.reg;
1264
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001265 // For 32-bit, SETcc only works with EAX..EDX.
1266 if (!IsByteRegister(result_reg)) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001267 result_reg = AllocateByteRegister();
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001268 }
1269 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondZ);
1270 NewLIR2(kX86Movzx8RR, rl_result.reg.GetReg(), result_reg.GetReg());
1271 if (IsTemp(result_reg)) {
1272 FreeTemp(result_reg);
1273 }
Vladimir Markoc29bb612013-11-27 16:47:25 +00001274 StoreValue(rl_dest, rl_result);
1275 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276}
1277
Yixin Shou8c914c02014-07-28 14:17:09 -04001278void X86Mir2Lir::SwapBits(RegStorage result_reg, int shift, int32_t value) {
1279 RegStorage r_temp = AllocTemp();
1280 OpRegCopy(r_temp, result_reg);
1281 OpRegImm(kOpLsr, result_reg, shift);
1282 OpRegImm(kOpAnd, r_temp, value);
1283 OpRegImm(kOpAnd, result_reg, value);
1284 OpRegImm(kOpLsl, r_temp, shift);
1285 OpRegReg(kOpOr, result_reg, r_temp);
1286 FreeTemp(r_temp);
1287}
1288
1289void X86Mir2Lir::SwapBits64(RegStorage result_reg, int shift, int64_t value) {
1290 RegStorage r_temp = AllocTempWide();
1291 OpRegCopy(r_temp, result_reg);
1292 OpRegImm(kOpLsr, result_reg, shift);
1293 RegStorage r_value = AllocTempWide();
1294 LoadConstantWide(r_value, value);
1295 OpRegReg(kOpAnd, r_temp, r_value);
1296 OpRegReg(kOpAnd, result_reg, r_value);
1297 OpRegImm(kOpLsl, r_temp, shift);
1298 OpRegReg(kOpOr, result_reg, r_temp);
1299 FreeTemp(r_temp);
1300 FreeTemp(r_value);
1301}
1302
1303bool X86Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001304 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info);
1305 if (rl_dest.s_reg_low == INVALID_SREG) {
1306 // Result is unused, the code is dead. Inlining successful, no code generated.
1307 return true;
1308 }
Yixin Shou8c914c02014-07-28 14:17:09 -04001309 RegLocation rl_src_i = info->args[0];
1310 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg)
1311 : LoadValue(rl_src_i, kCoreReg);
Yixin Shou8c914c02014-07-28 14:17:09 -04001312 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1313 if (size == k64) {
1314 if (cu_->instruction_set == kX86_64) {
1315 /* Use one bswap instruction to reverse byte order first and then use 3 rounds of
1316 swapping bits to reverse bits in a long number x. Using bswap to save instructions
1317 compared to generic luni implementation which has 5 rounds of swapping bits.
1318 x = bswap x
1319 x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
1320 x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
1321 x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
1322 */
1323 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1324 SwapBits64(rl_result.reg, 1, 0x5555555555555555);
1325 SwapBits64(rl_result.reg, 2, 0x3333333333333333);
1326 SwapBits64(rl_result.reg, 4, 0x0f0f0f0f0f0f0f0f);
1327 StoreValueWide(rl_dest, rl_result);
1328 return true;
1329 }
1330 RegStorage r_i_low = rl_i.reg.GetLow();
1331 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
1332 // First REV shall clobber rl_result.reg.GetLowReg(), save the value in a temp for the second
1333 // REV.
1334 r_i_low = AllocTemp();
1335 OpRegCopy(r_i_low, rl_i.reg);
1336 }
1337 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1338 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1339 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
1340 FreeTemp(r_i_low);
1341 }
1342 SwapBits(rl_result.reg.GetLow(), 1, 0x55555555);
1343 SwapBits(rl_result.reg.GetLow(), 2, 0x33333333);
1344 SwapBits(rl_result.reg.GetLow(), 4, 0x0f0f0f0f);
1345 SwapBits(rl_result.reg.GetHigh(), 1, 0x55555555);
1346 SwapBits(rl_result.reg.GetHigh(), 2, 0x33333333);
1347 SwapBits(rl_result.reg.GetHigh(), 4, 0x0f0f0f0f);
1348 StoreValueWide(rl_dest, rl_result);
1349 } else {
1350 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1351 SwapBits(rl_result.reg, 1, 0x55555555);
1352 SwapBits(rl_result.reg, 2, 0x33333333);
1353 SwapBits(rl_result.reg, 4, 0x0f0f0f0f);
1354 StoreValue(rl_dest, rl_result);
1355 }
1356 return true;
1357}
1358
Vladimir Markof6737f72015-03-23 17:05:14 +00001359void X86Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Mark Mendell27dee8b2014-12-01 19:06:12 -05001360 if (cu_->target64) {
1361 // We can do this directly using RIP addressing.
Mark Mendell27dee8b2014-12-01 19:06:12 -05001362 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Vladimir Markodc56cc52015-03-27 18:18:36 +00001363 LIR* res = NewLIR3(kX86Mov32RM, reg.GetReg(), kRIPReg, kDummy32BitOffset);
Mark Mendell27dee8b2014-12-01 19:06:12 -05001364 res->target = target;
1365 res->flags.fixup = kFixupLoad;
Vladimir Markof6737f72015-03-23 17:05:14 +00001366 return;
Mark Mendell27dee8b2014-12-01 19:06:12 -05001367 }
1368
Vladimir Marko1961b602015-04-08 20:51:48 +01001369 // Get the PC to a register and get the anchor.
1370 LIR* anchor;
1371 RegStorage r_pc = GetPcAndAnchor(&anchor);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001372
1373 // Load the proper value from the literal area.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001374 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Vladimir Marko1961b602015-04-08 20:51:48 +01001375 LIR* res = NewLIR3(kX86Mov32RM, reg.GetReg(), r_pc.GetReg(), kDummy32BitOffset);
1376 res->operands[4] = WrapPointer(anchor);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001377 res->target = target;
1378 res->flags.fixup = kFixupLoad;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001379}
1380
Vladimir Markodc56cc52015-03-27 18:18:36 +00001381bool X86Mir2Lir::CanUseOpPcRelDexCacheArrayLoad() const {
Vladimir Marko1961b602015-04-08 20:51:48 +01001382 return dex_cache_arrays_layout_.Valid();
1383}
1384
1385LIR* X86Mir2Lir::OpLoadPc(RegStorage r_dest) {
1386 DCHECK(!cu_->target64);
1387 LIR* call = NewLIR1(kX86CallI, 0);
1388 call->flags.fixup = kFixupLabel;
1389 LIR* pop = NewLIR1(kX86Pop32R, r_dest.GetReg());
1390 pop->flags.fixup = kFixupLabel;
1391 DCHECK(NEXT_LIR(call) == pop);
1392 return call;
1393}
1394
1395RegStorage X86Mir2Lir::GetPcAndAnchor(LIR** anchor, RegStorage r_tmp) {
1396 if (pc_rel_base_reg_.Valid()) {
1397 DCHECK(setup_pc_rel_base_reg_ != nullptr);
1398 *anchor = NEXT_LIR(setup_pc_rel_base_reg_);
1399 DCHECK(*anchor != nullptr);
1400 DCHECK_EQ((*anchor)->opcode, kX86Pop32R);
1401 pc_rel_base_reg_used_ = true;
1402 return pc_rel_base_reg_;
1403 } else {
1404 RegStorage r_pc = r_tmp.Valid() ? r_tmp : AllocTempRef();
1405 LIR* load_pc = OpLoadPc(r_pc);
1406 *anchor = NEXT_LIR(load_pc);
1407 DCHECK(*anchor != nullptr);
1408 DCHECK_EQ((*anchor)->opcode, kX86Pop32R);
1409 return r_pc;
1410 }
Vladimir Markodc56cc52015-03-27 18:18:36 +00001411}
1412
Mathieu Chartiere401d142015-04-22 13:56:20 -07001413void X86Mir2Lir::OpPcRelDexCacheArrayLoad(const DexFile* dex_file, int offset, RegStorage r_dest,
1414 bool wide) {
Vladimir Markodc56cc52015-03-27 18:18:36 +00001415 if (cu_->target64) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001416 LIR* mov = NewLIR3(wide ? kX86Mov64RM : kX86Mov32RM, r_dest.GetReg(), kRIPReg,
1417 kDummy32BitOffset);
Vladimir Markodc56cc52015-03-27 18:18:36 +00001418 mov->flags.fixup = kFixupLabel;
1419 mov->operands[3] = WrapPointer(dex_file);
1420 mov->operands[4] = offset;
Vladimir Marko1961b602015-04-08 20:51:48 +01001421 mov->target = mov; // Used for pc_insn_offset (not used by x86-64 relative patcher).
Vladimir Markodc56cc52015-03-27 18:18:36 +00001422 dex_cache_access_insns_.push_back(mov);
1423 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001424 CHECK(!wide) << "Unsupported";
Vladimir Marko1961b602015-04-08 20:51:48 +01001425 // Get the PC to a register and get the anchor. Use r_dest for the temp if needed.
1426 LIR* anchor;
1427 RegStorage r_pc = GetPcAndAnchor(&anchor, r_dest);
1428 LIR* mov = NewLIR3(kX86Mov32RM, r_dest.GetReg(), r_pc.GetReg(), kDummy32BitOffset);
1429 mov->flags.fixup = kFixupLabel;
1430 mov->operands[3] = WrapPointer(dex_file);
1431 mov->operands[4] = offset;
1432 mov->target = anchor; // Used for pc_insn_offset.
1433 dex_cache_access_insns_.push_back(mov);
Vladimir Markodc56cc52015-03-27 18:18:36 +00001434 }
1435}
1436
buzbee2700f7e2014-03-07 09:46:20 -08001437LIR* X86Mir2Lir::OpVldm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001438 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001439 LOG(FATAL) << "Unexpected use of OpVldm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001440 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441}
1442
buzbee2700f7e2014-03-07 09:46:20 -08001443LIR* X86Mir2Lir::OpVstm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001444 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 LOG(FATAL) << "Unexpected use of OpVstm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001446 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447}
1448
1449void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1450 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001451 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001452 UNUSED(lit);
buzbee2700f7e2014-03-07 09:46:20 -08001453 RegStorage t_reg = AllocTemp();
1454 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, second_bit - first_bit);
1455 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 FreeTemp(t_reg);
1457 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001458 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 }
1460}
1461
Mingyao Yange643a172014-04-08 11:02:52 -07001462void X86Mir2Lir::GenDivZeroCheckWide(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001463 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001464 DCHECK(reg.Is64Bit());
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001465
Chao-ying Fua0147762014-06-06 18:38:49 -07001466 NewLIR2(kX86Cmp64RI8, reg.GetReg(), 0);
1467 } else {
1468 DCHECK(reg.IsPair());
1469
1470 // We are not supposed to clobber the incoming storage, so allocate a temporary.
1471 RegStorage t_reg = AllocTemp();
1472 // Doing an OR is a quick way to check if both registers are zero. This will set the flags.
1473 OpRegRegReg(kOpOr, t_reg, reg.GetLow(), reg.GetHigh());
1474 // The temp is no longer needed so free it at this time.
1475 FreeTemp(t_reg);
1476 }
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001477
1478 // In case of zero, throw ArithmeticException.
Mingyao Yange643a172014-04-08 11:02:52 -07001479 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001480}
1481
Mingyao Yang80365d92014-04-18 12:10:58 -07001482void X86Mir2Lir::GenArrayBoundsCheck(RegStorage index,
1483 RegStorage array_base,
1484 int len_offset) {
1485 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1486 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001487 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1488 RegStorage index_in, RegStorage array_base_in, int32_t len_offset_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001489 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001490 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001491 }
1492
1493 void Compile() OVERRIDE {
1494 m2l_->ResetRegPool();
1495 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001496 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001497
1498 RegStorage new_index = index_;
1499 // Move index out of kArg1, either directly to kArg0, or to kArg2.
Serguei Katkov4c7cc152014-06-24 00:50:02 +07001500 // TODO: clean-up to check not a number but with type
Andreas Gampeccc60262014-07-04 18:02:38 -07001501 if (index_ == m2l_->TargetReg(kArg1, kNotWide)) {
1502 if (array_base_ == m2l_->TargetReg(kArg0, kRef)) {
1503 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kNotWide), index_);
1504 new_index = m2l_->TargetReg(kArg2, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001505 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001506 m2l_->OpRegCopy(m2l_->TargetReg(kArg0, kNotWide), index_);
1507 new_index = m2l_->TargetReg(kArg0, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001508 }
1509 }
1510 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001511 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1512 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1513 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, new_index,
1514 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001515 }
1516
1517 private:
1518 const RegStorage index_;
1519 const RegStorage array_base_;
1520 const int32_t len_offset_;
1521 };
1522
1523 OpRegMem(kOpCmp, index, array_base, len_offset);
Dave Allison69dfe512014-07-11 17:11:58 +00001524 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001525 LIR* branch = OpCondBranch(kCondUge, nullptr);
1526 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1527 index, array_base, len_offset));
1528}
1529
1530void X86Mir2Lir::GenArrayBoundsCheck(int32_t index,
1531 RegStorage array_base,
1532 int32_t len_offset) {
1533 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1534 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001535 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1536 int32_t index_in, RegStorage array_base_in, int32_t len_offset_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001537 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001538 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001539 }
1540
1541 void Compile() OVERRIDE {
1542 m2l_->ResetRegPool();
1543 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001544 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001545
1546 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001547 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1548 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1549 x86_m2l->LoadConstant(m2l_->TargetReg(kArg0, kNotWide), index_);
1550 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, m2l_->TargetReg(kArg0, kNotWide),
1551 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001552 }
1553
1554 private:
1555 const int32_t index_;
1556 const RegStorage array_base_;
1557 const int32_t len_offset_;
1558 };
1559
1560 NewLIR3(IS_SIMM8(index) ? kX86Cmp32MI8 : kX86Cmp32MI, array_base.GetReg(), len_offset, index);
Dave Allison69dfe512014-07-11 17:11:58 +00001561 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001562 LIR* branch = OpCondBranch(kCondLs, nullptr);
1563 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1564 index, array_base, len_offset));
1565}
1566
Brian Carlstrom7940e442013-07-12 13:46:57 -07001567// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001568LIR* X86Mir2Lir::OpTestSuspend(LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -07001569 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001570 OpTlsCmp(Thread::ThreadFlagsOffset<8>(), 0);
1571 } else {
1572 OpTlsCmp(Thread::ThreadFlagsOffset<4>(), 0);
1573 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001574 return OpCondBranch((target == nullptr) ? kCondNe : kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575}
1576
1577// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001578LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 OpRegImm(kOpSub, reg, 1);
Yixin Shoua0dac3e2014-01-23 05:01:22 -08001580 return OpCondBranch(c_code, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581}
1582
buzbee11b63d12013-08-27 07:34:17 -07001583bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001584 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001585 UNUSED(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001586 LOG(FATAL) << "Unexpected use of smallLiteralDive in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001587 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588}
1589
Ian Rogerse2143c02014-03-28 08:47:16 -07001590bool X86Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001591 UNUSED(rl_src, rl_dest, lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001592 LOG(FATAL) << "Unexpected use of easyMultiply in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001593 UNREACHABLE();
Ian Rogerse2143c02014-03-28 08:47:16 -07001594}
1595
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001596LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001597 UNUSED(cond, guide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001598 LOG(FATAL) << "Unexpected use of OpIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001599 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600}
1601
Dave Allison3da67a52014-04-02 17:03:45 -07001602void X86Mir2Lir::OpEndIT(LIR* it) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001603 UNUSED(it);
Dave Allison3da67a52014-04-02 17:03:45 -07001604 LOG(FATAL) << "Unexpected use of OpEndIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001605 UNREACHABLE();
Dave Allison3da67a52014-04-02 17:03:45 -07001606}
1607
buzbee2700f7e2014-03-07 09:46:20 -08001608void X86Mir2Lir::GenImulRegImm(RegStorage dest, RegStorage src, int val) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001609 switch (val) {
1610 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001611 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001612 break;
1613 case 1:
1614 OpRegCopy(dest, src);
1615 break;
1616 default:
1617 OpRegRegImm(kOpMul, dest, src, val);
1618 break;
1619 }
1620}
1621
buzbee2700f7e2014-03-07 09:46:20 -08001622void X86Mir2Lir::GenImulMemImm(RegStorage dest, int sreg, int displacement, int val) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001623 UNUSED(sreg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001624 // All memory accesses below reference dalvik regs.
1625 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1626
Mark Mendell4708dcd2014-01-22 09:05:18 -08001627 LIR *m;
1628 switch (val) {
1629 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001630 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001631 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001632 case 1: {
1633 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
1634 LoadBaseDisp(rs_rSP, displacement, dest, k32, kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001635 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001636 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001637 default:
buzbee091cc402014-03-31 10:14:40 -07001638 m = NewLIR4(IS_SIMM8(val) ? kX86Imul32RMI8 : kX86Imul32RMI, dest.GetReg(),
Ian Rogersb28c1c02014-11-08 11:21:21 -08001639 rs_rX86_SP_32.GetReg(), displacement, val);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001640 AnnotateDalvikRegAccess(m, displacement >> 2, true /* is_load */, true /* is_64bit */);
1641 break;
1642 }
1643}
1644
Andreas Gampec76c6142014-08-04 16:30:03 -07001645void X86Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001646 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001647 if (!cu_->target64) {
1648 // Some x86 32b ops are fallback.
1649 switch (opcode) {
1650 case Instruction::NOT_LONG:
1651 case Instruction::DIV_LONG:
1652 case Instruction::DIV_LONG_2ADDR:
1653 case Instruction::REM_LONG:
1654 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001655 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001656 return;
1657
1658 default:
1659 // Everything else we can handle.
1660 break;
1661 }
1662 }
1663
1664 switch (opcode) {
1665 case Instruction::NOT_LONG:
1666 GenNotLong(rl_dest, rl_src2);
1667 return;
1668
1669 case Instruction::ADD_LONG:
1670 case Instruction::ADD_LONG_2ADDR:
1671 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1672 return;
1673
1674 case Instruction::SUB_LONG:
1675 case Instruction::SUB_LONG_2ADDR:
1676 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, false);
1677 return;
1678
1679 case Instruction::MUL_LONG:
1680 case Instruction::MUL_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001681 GenMulLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001682 return;
1683
1684 case Instruction::DIV_LONG:
1685 case Instruction::DIV_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001686 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001687 return;
1688
1689 case Instruction::REM_LONG:
1690 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001691 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001692 return;
1693
1694 case Instruction::AND_LONG_2ADDR:
1695 case Instruction::AND_LONG:
1696 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1697 return;
1698
1699 case Instruction::OR_LONG:
1700 case Instruction::OR_LONG_2ADDR:
1701 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1702 return;
1703
1704 case Instruction::XOR_LONG:
1705 case Instruction::XOR_LONG_2ADDR:
1706 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1707 return;
1708
1709 case Instruction::NEG_LONG:
1710 GenNegLong(rl_dest, rl_src2);
1711 return;
1712
1713 default:
1714 LOG(FATAL) << "Invalid long arith op";
1715 return;
1716 }
1717}
1718
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001719bool X86Mir2Lir::GenMulLongConst(RegLocation rl_dest, RegLocation rl_src1, int64_t val, int flags) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001720 // All memory accesses below reference dalvik regs.
1721 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1722
Andreas Gampec76c6142014-08-04 16:30:03 -07001723 if (val == 0) {
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001724 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampec76c6142014-08-04 16:30:03 -07001725 if (cu_->target64) {
1726 OpRegReg(kOpXor, rl_result.reg, rl_result.reg);
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001727 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001728 OpRegReg(kOpXor, rl_result.reg.GetLow(), rl_result.reg.GetLow());
1729 OpRegReg(kOpXor, rl_result.reg.GetHigh(), rl_result.reg.GetHigh());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001730 }
Andreas Gampec76c6142014-08-04 16:30:03 -07001731 StoreValueWide(rl_dest, rl_result);
1732 return true;
1733 } else if (val == 1) {
1734 StoreValueWide(rl_dest, rl_src1);
1735 return true;
1736 } else if (val == 2) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001737 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src1, rl_src1, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001738 return true;
1739 } else if (IsPowerOfTwo(val)) {
Andreas Gampe7e499922015-01-06 08:28:12 -08001740 int shift_amount = CTZ(val);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001741 if (!PartiallyIntersects(rl_src1, rl_dest)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001742 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1743 RegLocation rl_result = GenShiftImmOpLong(Instruction::SHL_LONG, rl_dest, rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001744 shift_amount, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001745 StoreValueWide(rl_dest, rl_result);
1746 return true;
1747 }
1748 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001749
Andreas Gampec76c6142014-08-04 16:30:03 -07001750 // Okay, on 32b just bite the bullet and do it, still better than the general case.
1751 if (!cu_->target64) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001752 int32_t val_lo = Low32Bits(val);
1753 int32_t val_hi = High32Bits(val);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001754 // Prepare for explicit register usage.
1755 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001756 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001757 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1758 int displacement = SRegOffset(rl_src1.s_reg_low);
1759
1760 // ECX <- 1H * 2L
1761 // EAX <- 1L * 2H
1762 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001763 GenImulRegImm(rs_r1, rl_src1.reg.GetHigh(), val_lo);
1764 GenImulRegImm(rs_r0, rl_src1.reg.GetLow(), val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001765 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001766 GenImulMemImm(rs_r1, GetSRegHi(rl_src1.s_reg_low), displacement + HIWORD_OFFSET, val_lo);
1767 GenImulMemImm(rs_r0, rl_src1.s_reg_low, displacement + LOWORD_OFFSET, val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001768 }
1769
1770 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001771 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001772
1773 // EAX <- 2L
buzbee2700f7e2014-03-07 09:46:20 -08001774 LoadConstantNoClobber(rs_r0, val_lo);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001775
1776 // EDX:EAX <- 2L * 1L (double precision)
1777 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001778 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001779 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001780 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001781 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1782 true /* is_load */, true /* is_64bit */);
1783 }
1784
1785 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001786 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001787
1788 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001789 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
1790 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001791 StoreValueWide(rl_dest, rl_result);
Andreas Gampec76c6142014-08-04 16:30:03 -07001792 return true;
1793 }
1794 return false;
1795}
1796
1797void X86Mir2Lir::GenMulLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001798 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001799 if (rl_src1.is_const) {
1800 std::swap(rl_src1, rl_src2);
1801 }
1802
1803 if (rl_src2.is_const) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001804 if (GenMulLongConst(rl_dest, rl_src1, mir_graph_->ConstantValueWide(rl_src2), flags)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001805 return;
1806 }
1807 }
1808
1809 // All memory accesses below reference dalvik regs.
1810 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1811
1812 if (cu_->target64) {
1813 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1814 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1815 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1816 if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1817 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1818 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
1819 } else if (rl_result.reg.GetReg() != rl_src1.reg.GetReg() &&
1820 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1821 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
1822 } else if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1823 rl_result.reg.GetReg() != rl_src2.reg.GetReg()) {
1824 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1825 } else {
1826 OpRegCopy(rl_result.reg, rl_src1.reg);
1827 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1828 }
1829 StoreValueWide(rl_dest, rl_result);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001830 return;
1831 }
1832
Andreas Gampec76c6142014-08-04 16:30:03 -07001833 // Not multiplying by a constant. Do it the hard way
Mark Mendellde99bba2014-02-14 12:15:02 -08001834 // Check for V*V. We can eliminate a multiply in that case, as 2L*1H == 2H*1L.
1835 bool is_square = mir_graph_->SRegToVReg(rl_src1.s_reg_low) ==
1836 mir_graph_->SRegToVReg(rl_src2.s_reg_low);
1837
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001838 // Prepare for explicit register usage.
1839 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001840 rl_src1 = UpdateLocWideTyped(rl_src1);
1841 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001842
1843 // At this point, the VRs are in their home locations.
1844 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1845 bool src2_in_reg = rl_src2.location == kLocPhysReg;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001846 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001847
1848 // ECX <- 1H
1849 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001850 NewLIR2(kX86Mov32RR, rs_r1.GetReg(), rl_src1.reg.GetHighReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001851 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001852 LoadBaseDisp(rs_rSP, SRegOffset(rl_src1.s_reg_low) + HIWORD_OFFSET, rs_r1, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001853 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001854 }
1855
Mark Mendellde99bba2014-02-14 12:15:02 -08001856 if (is_square) {
1857 // Take advantage of the fact that the values are the same.
1858 // ECX <- ECX * 2L (1H * 2L)
1859 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001860 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001861 } else {
1862 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001863 LIR* m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001864 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001865 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1866 true /* is_load */, true /* is_64bit */);
1867 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001868
Mark Mendellde99bba2014-02-14 12:15:02 -08001869 // ECX <- 2*ECX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001870 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001871 } else {
Mark Mendellde99bba2014-02-14 12:15:02 -08001872 // EAX <- 2H
1873 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001874 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetHighReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001875 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001876 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + HIWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001877 kNotVolatile);
Mark Mendellde99bba2014-02-14 12:15:02 -08001878 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001879
Mark Mendellde99bba2014-02-14 12:15:02 -08001880 // EAX <- EAX * 1L (2H * 1L)
1881 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001882 NewLIR2(kX86Imul32RR, rs_r0.GetReg(), rl_src1.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001883 } else {
1884 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001885 LIR *m = NewLIR3(kX86Imul32RM, rs_r0.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001886 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001887 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1888 true /* is_load */, true /* is_64bit */);
1889 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001890
Mark Mendellde99bba2014-02-14 12:15:02 -08001891 // ECX <- ECX * 2L (1H * 2L)
1892 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001893 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001894 } else {
1895 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001896 LIR *m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001897 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001898 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1899 true /* is_load */, true /* is_64bit */);
1900 }
1901
1902 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001903 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001904 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001905
1906 // EAX <- 2L
1907 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001908 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001909 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001910 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + LOWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001911 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001912 }
1913
1914 // EDX:EAX <- 2L * 1L (double precision)
1915 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001916 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001917 } else {
1918 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001919 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001920 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1921 true /* is_load */, true /* is_64bit */);
1922 }
1923
1924 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001925 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001926
1927 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001928 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
buzbee2700f7e2014-03-07 09:46:20 -08001929 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001930 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001931}
Mark Mendelle02d48f2014-01-15 11:19:23 -08001932
1933void X86Mir2Lir::GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src,
1934 Instruction::Code op) {
1935 DCHECK_EQ(rl_dest.location, kLocPhysReg);
1936 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
1937 if (rl_src.location == kLocPhysReg) {
1938 // Both operands are in registers.
Serguei Katkovab5545f2014-03-25 10:51:15 +07001939 // But we must ensure that rl_src is in pair
Elena Sayapinadd644502014-07-01 18:39:52 +07001940 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001941 NewLIR2(x86op, rl_dest.reg.GetReg(), rl_src.reg.GetReg());
1942 } else {
1943 rl_src = LoadValueWide(rl_src, kCoreReg);
1944 if (rl_dest.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1945 // The registers are the same, so we would clobber it before the use.
1946 RegStorage temp_reg = AllocTemp();
1947 OpRegCopy(temp_reg, rl_dest.reg);
1948 rl_src.reg.SetHighReg(temp_reg.GetReg());
1949 }
1950 NewLIR2(x86op, rl_dest.reg.GetLowReg(), rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08001951
Chao-ying Fua0147762014-06-06 18:38:49 -07001952 x86op = GetOpcode(op, rl_dest, rl_src, true);
1953 NewLIR2(x86op, rl_dest.reg.GetHighReg(), rl_src.reg.GetHighReg());
Chao-ying Fua0147762014-06-06 18:38:49 -07001954 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08001955 return;
1956 }
1957
1958 // RHS is in memory.
1959 DCHECK((rl_src.location == kLocDalvikFrame) ||
1960 (rl_src.location == kLocCompilerTemp));
Ian Rogersb28c1c02014-11-08 11:21:21 -08001961 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08001962 int displacement = SRegOffset(rl_src.s_reg_low);
1963
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001964 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -07001965 LIR *lir = NewLIR3(x86op, cu_->target64 ? rl_dest.reg.GetReg() : rl_dest.reg.GetLowReg(),
1966 r_base, displacement + LOWORD_OFFSET);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001967 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
1968 true /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07001969 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001970 x86op = GetOpcode(op, rl_dest, rl_src, true);
1971 lir = NewLIR3(x86op, rl_dest.reg.GetHighReg(), r_base, displacement + HIWORD_OFFSET);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001972 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1973 true /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07001974 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975}
1976
Mark Mendelle02d48f2014-01-15 11:19:23 -08001977void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001978 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001979 if (rl_dest.location == kLocPhysReg) {
1980 // Ensure we are in a register pair
1981 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1982
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001983 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001984 GenLongRegOrMemOp(rl_result, rl_src, op);
1985 StoreFinalValueWide(rl_dest, rl_result);
1986 return;
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001987 } else if (!cu_->target64 && Intersects(rl_src, rl_dest)) {
1988 // Handle the case when src and dest are intersect.
1989 rl_src = LoadValueWide(rl_src, kCoreReg);
1990 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001991 rl_src = UpdateLocWideTyped(rl_src);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001992 GenLongRegOrMemOp(rl_result, rl_src, op);
1993 StoreFinalValueWide(rl_dest, rl_result);
1994 return;
Mark Mendelle02d48f2014-01-15 11:19:23 -08001995 }
1996
1997 // It wasn't in registers, so it better be in memory.
1998 DCHECK((rl_dest.location == kLocDalvikFrame) ||
1999 (rl_dest.location == kLocCompilerTemp));
2000 rl_src = LoadValueWide(rl_src, kCoreReg);
2001
2002 // Operate directly into memory.
2003 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
Ian Rogersb28c1c02014-11-08 11:21:21 -08002004 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002005 int displacement = SRegOffset(rl_dest.s_reg_low);
2006
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002007 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002008 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET,
Elena Sayapinadd644502014-07-01 18:39:52 +07002009 cu_->target64 ? rl_src.reg.GetReg() : rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002010 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002011 true /* is_load */, true /* is64bit */);
2012 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002013 false /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07002014 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002015 x86op = GetOpcode(op, rl_dest, rl_src, true);
2016 lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, rl_src.reg.GetHighReg());
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002017 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
2018 true /* is_load */, true /* is64bit */);
2019 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
2020 false /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07002021 }
nikolay serdjuk6b9356c2014-11-13 18:15:23 +06002022
2023 int v_src_reg = mir_graph_->SRegToVReg(rl_src.s_reg_low);
2024 int v_dst_reg = mir_graph_->SRegToVReg(rl_dest.s_reg_low);
2025
2026 // If the left operand is in memory and the right operand is in a register
2027 // and both belong to the same dalvik register then we should clobber the
2028 // right one because it doesn't hold valid data anymore.
2029 if (v_src_reg == v_dst_reg) {
2030 Clobber(rl_src.reg);
2031 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032}
2033
Mark Mendelle02d48f2014-01-15 11:19:23 -08002034void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src1,
2035 RegLocation rl_src2, Instruction::Code op,
2036 bool is_commutative) {
2037 // Is this really a 2 operand operation?
2038 switch (op) {
2039 case Instruction::ADD_LONG_2ADDR:
2040 case Instruction::SUB_LONG_2ADDR:
2041 case Instruction::AND_LONG_2ADDR:
2042 case Instruction::OR_LONG_2ADDR:
2043 case Instruction::XOR_LONG_2ADDR:
Mark Mendelle87f9b52014-04-30 14:13:18 -04002044 if (GenerateTwoOperandInstructions()) {
2045 GenLongArith(rl_dest, rl_src2, op);
2046 return;
2047 }
2048 break;
2049
Mark Mendelle02d48f2014-01-15 11:19:23 -08002050 default:
2051 break;
2052 }
2053
2054 if (rl_dest.location == kLocPhysReg) {
2055 RegLocation rl_result = LoadValueWide(rl_src1, kCoreReg);
2056
2057 // We are about to clobber the LHS, so it needs to be a temp.
2058 rl_result = ForceTempWide(rl_result);
2059
2060 // Perform the operation using the RHS.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002061 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002062 GenLongRegOrMemOp(rl_result, rl_src2, op);
2063
2064 // And now record that the result is in the temp.
2065 StoreFinalValueWide(rl_dest, rl_result);
2066 return;
2067 }
2068
2069 // It wasn't in registers, so it better be in memory.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002070 DCHECK((rl_dest.location == kLocDalvikFrame) || (rl_dest.location == kLocCompilerTemp));
2071 rl_src1 = UpdateLocWideTyped(rl_src1);
2072 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002073
2074 // Get one of the source operands into temporary register.
2075 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +07002076 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002077 if (IsTemp(rl_src1.reg)) {
2078 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2079 } else if (is_commutative) {
2080 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2081 // We need at least one of them to be a temporary.
2082 if (!IsTemp(rl_src2.reg)) {
2083 rl_src1 = ForceTempWide(rl_src1);
2084 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2085 } else {
2086 GenLongRegOrMemOp(rl_src2, rl_src1, op);
2087 StoreFinalValueWide(rl_dest, rl_src2);
2088 return;
2089 }
2090 } else {
2091 // Need LHS to be the temp.
Mark Mendelle02d48f2014-01-15 11:19:23 -08002092 rl_src1 = ForceTempWide(rl_src1);
Yevgeny Rouban91b6ffa2014-03-07 14:35:44 +07002093 GenLongRegOrMemOp(rl_src1, rl_src2, op);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002094 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002095 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002096 if (IsTemp(rl_src1.reg.GetLow()) && IsTemp(rl_src1.reg.GetHigh())) {
2097 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2098 } else if (is_commutative) {
2099 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2100 // We need at least one of them to be a temporary.
2101 if (!(IsTemp(rl_src2.reg.GetLow()) && IsTemp(rl_src2.reg.GetHigh()))) {
2102 rl_src1 = ForceTempWide(rl_src1);
2103 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2104 } else {
2105 GenLongRegOrMemOp(rl_src2, rl_src1, op);
2106 StoreFinalValueWide(rl_dest, rl_src2);
2107 return;
2108 }
2109 } else {
2110 // Need LHS to be the temp.
2111 rl_src1 = ForceTempWide(rl_src1);
2112 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2113 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002114 }
2115
2116 StoreFinalValueWide(rl_dest, rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002117}
2118
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002119void X86Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002120 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002121 rl_src = LoadValueWide(rl_src, kCoreReg);
2122 RegLocation rl_result;
2123 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2124 OpRegCopy(rl_result.reg, rl_src.reg);
2125 OpReg(kOpNot, rl_result.reg);
2126 StoreValueWide(rl_dest, rl_result);
2127 } else {
2128 LOG(FATAL) << "Unexpected use GenNotLong()";
2129 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002130}
2131
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002132void X86Mir2Lir::GenDivRemLongLit(RegLocation rl_dest, RegLocation rl_src,
2133 int64_t imm, bool is_div) {
2134 if (imm == 0) {
2135 GenDivZeroException();
2136 } else if (imm == 1) {
2137 if (is_div) {
2138 // x / 1 == x.
2139 StoreValueWide(rl_dest, rl_src);
2140 } else {
2141 // x % 1 == 0.
2142 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2143 LoadConstantWide(rl_result.reg, 0);
2144 StoreValueWide(rl_dest, rl_result);
2145 }
2146 } else if (imm == -1) { // handle 0x8000000000000000 / -1 special case.
2147 if (is_div) {
2148 rl_src = LoadValueWide(rl_src, kCoreReg);
2149 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2150 RegStorage rs_temp = AllocTempWide();
2151
2152 OpRegCopy(rl_result.reg, rl_src.reg);
2153 LoadConstantWide(rs_temp, 0x8000000000000000);
2154
2155 // If x == MIN_LONG, return MIN_LONG.
2156 OpRegReg(kOpCmp, rl_src.reg, rs_temp);
2157 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
2158
2159 // For x != MIN_LONG, x / -1 == -x.
2160 OpReg(kOpNeg, rl_result.reg);
2161
2162 minint_branch->target = NewLIR0(kPseudoTargetLabel);
2163 FreeTemp(rs_temp);
2164 StoreValueWide(rl_dest, rl_result);
2165 } else {
2166 // x % -1 == 0.
2167 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2168 LoadConstantWide(rl_result.reg, 0);
2169 StoreValueWide(rl_dest, rl_result);
2170 }
2171 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
2172 // Division using shifting.
2173 rl_src = LoadValueWide(rl_src, kCoreReg);
2174 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2175 if (IsSameReg(rl_result.reg, rl_src.reg)) {
2176 RegStorage rs_temp = AllocTypedTempWide(false, kCoreReg);
2177 rl_result.reg.SetReg(rs_temp.GetReg());
2178 }
2179 LoadConstantWide(rl_result.reg, std::abs(imm) - 1);
2180 OpRegReg(kOpAdd, rl_result.reg, rl_src.reg);
2181 NewLIR2(kX86Test64RR, rl_src.reg.GetReg(), rl_src.reg.GetReg());
2182 OpCondRegReg(kOpCmov, kCondPl, rl_result.reg, rl_src.reg);
Andreas Gampe7e499922015-01-06 08:28:12 -08002183 int shift_amount = CTZ(imm);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002184 OpRegImm(kOpAsr, rl_result.reg, shift_amount);
2185 if (imm < 0) {
2186 OpReg(kOpNeg, rl_result.reg);
2187 }
2188 StoreValueWide(rl_dest, rl_result);
2189 } else {
2190 CHECK(imm <= -2 || imm >= 2);
2191
2192 FlushReg(rs_r0q);
2193 Clobber(rs_r0q);
2194 LockTemp(rs_r0q);
2195 FlushReg(rs_r2q);
2196 Clobber(rs_r2q);
2197 LockTemp(rs_r2q);
2198
Mark Mendell3a91f442014-09-02 12:44:24 -04002199 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
2200 is_div ? rs_r2q : rs_r0q, INVALID_SREG, INVALID_SREG};
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002201
2202 // Use H.S.Warren's Hacker's Delight Chapter 10 and
2203 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
2204 int64_t magic;
2205 int shift;
2206 CalculateMagicAndShift(imm, magic, shift, true /* is_long */);
2207
2208 /*
2209 * For imm >= 2,
2210 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
2211 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
2212 * For imm <= -2,
2213 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
2214 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
2215 * We implement this algorithm in the following way:
2216 * 1. multiply magic number m and numerator n, get the higher 64bit result in RDX
2217 * 2. if imm > 0 and magic < 0, add numerator to RDX
2218 * if imm < 0 and magic > 0, sub numerator from RDX
2219 * 3. if S !=0, SAR S bits for RDX
2220 * 4. add 1 to RDX if RDX < 0
2221 * 5. Thus, RDX is the quotient
2222 */
2223
Mark Mendell3a91f442014-09-02 12:44:24 -04002224 // RAX = magic.
2225 LoadConstantWide(rs_r0q, magic);
2226
2227 // Multiply by numerator.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002228 RegStorage numerator_reg;
2229 if (!is_div || (imm > 0 && magic < 0) || (imm < 0 && magic > 0)) {
2230 // We will need the value later.
2231 rl_src = LoadValueWide(rl_src, kCoreReg);
2232 numerator_reg = rl_src.reg;
Mark Mendell3a91f442014-09-02 12:44:24 -04002233
2234 // RDX:RAX = magic * numerator.
2235 NewLIR1(kX86Imul64DaR, numerator_reg.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002236 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002237 // Only need this once. Multiply directly from the value.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002238 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendell3a91f442014-09-02 12:44:24 -04002239 if (rl_src.location != kLocPhysReg) {
2240 // Okay, we can do this from memory.
2241 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2242 int displacement = SRegOffset(rl_src.s_reg_low);
2243 // RDX:RAX = magic * numerator.
Ian Rogersb28c1c02014-11-08 11:21:21 -08002244 LIR *m = NewLIR2(kX86Imul64DaM, rs_rX86_SP_32.GetReg(), displacement);
Mark Mendell3a91f442014-09-02 12:44:24 -04002245 AnnotateDalvikRegAccess(m, displacement >> 2,
2246 true /* is_load */, true /* is_64bit */);
2247 } else {
2248 // RDX:RAX = magic * numerator.
2249 NewLIR1(kX86Imul64DaR, rl_src.reg.GetReg());
2250 }
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002251 }
2252
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002253 if (imm > 0 && magic < 0) {
2254 // Add numerator to RDX.
2255 DCHECK(numerator_reg.Valid());
2256 OpRegReg(kOpAdd, rs_r2q, numerator_reg);
2257 } else if (imm < 0 && magic > 0) {
2258 DCHECK(numerator_reg.Valid());
2259 OpRegReg(kOpSub, rs_r2q, numerator_reg);
2260 }
2261
2262 // Do we need the shift?
2263 if (shift != 0) {
2264 // Shift RDX by 'shift' bits.
2265 OpRegImm(kOpAsr, rs_r2q, shift);
2266 }
2267
2268 // Move RDX to RAX.
2269 OpRegCopyWide(rs_r0q, rs_r2q);
2270
2271 // Move sign bit to bit 0, zeroing the rest.
2272 OpRegImm(kOpLsr, rs_r2q, 63);
2273
2274 // RDX = RDX + RAX.
2275 OpRegReg(kOpAdd, rs_r2q, rs_r0q);
2276
2277 // Quotient is in RDX.
2278 if (!is_div) {
2279 // We need to compute the remainder.
2280 // Remainder is divisor - (quotient * imm).
2281 DCHECK(numerator_reg.Valid());
2282 OpRegCopyWide(rs_r0q, numerator_reg);
2283
2284 // Imul doesn't support 64-bit imms.
2285 if (imm > std::numeric_limits<int32_t>::max() ||
2286 imm < std::numeric_limits<int32_t>::min()) {
2287 RegStorage rs_temp = AllocTempWide();
2288 LoadConstantWide(rs_temp, imm);
2289
2290 // RAX = numerator * imm.
2291 NewLIR2(kX86Imul64RR, rs_r2q.GetReg(), rs_temp.GetReg());
2292
2293 FreeTemp(rs_temp);
2294 } else {
2295 // RAX = numerator * imm.
2296 int short_imm = static_cast<int>(imm);
2297 NewLIR3(kX86Imul64RRI, rs_r2q.GetReg(), rs_r2q.GetReg(), short_imm);
2298 }
2299
Mark Mendell3a91f442014-09-02 12:44:24 -04002300 // RAX -= RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002301 OpRegReg(kOpSub, rs_r0q, rs_r2q);
2302
Mark Mendell3a91f442014-09-02 12:44:24 -04002303 // Result in RAX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002304 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002305 // Result in RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002306 }
2307 StoreValueWide(rl_dest, rl_result);
2308 FreeTemp(rs_r0q);
2309 FreeTemp(rs_r2q);
2310 }
2311}
2312
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002313void X86Mir2Lir::GenDivRemLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002314 RegLocation rl_src2, bool is_div, int flags) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002315 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002316 LOG(FATAL) << "Unexpected use GenDivRemLong()";
2317 return;
2318 }
2319
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002320 if (rl_src2.is_const) {
2321 DCHECK(rl_src2.wide);
2322 int64_t imm = mir_graph_->ConstantValueWide(rl_src2);
2323 GenDivRemLongLit(rl_dest, rl_src1, imm, is_div);
2324 return;
2325 }
2326
Chao-ying Fua0147762014-06-06 18:38:49 -07002327 // We have to use fixed registers, so flush all the temps.
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002328 // Prepare for explicit register usage.
2329 ExplicitTempRegisterLock(this, 4, &rs_r0q, &rs_r1q, &rs_r2q, &rs_r6q);
Chao-ying Fua0147762014-06-06 18:38:49 -07002330
2331 // Load LHS into RAX.
2332 LoadValueDirectWideFixed(rl_src1, rs_r0q);
2333
2334 // Load RHS into RCX.
2335 LoadValueDirectWideFixed(rl_src2, rs_r1q);
2336
2337 // Copy LHS sign bit into RDX.
2338 NewLIR0(kx86Cqo64Da);
2339
2340 // Handle division by zero case.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002341 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2342 GenDivZeroCheckWide(rs_r1q);
2343 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002344
2345 // Have to catch 0x8000000000000000/-1 case, or we will get an exception!
2346 NewLIR2(kX86Cmp64RI8, rs_r1q.GetReg(), -1);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002347 LIR* minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002348
2349 // RHS is -1.
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002350 LoadConstantWide(rs_r6q, 0x8000000000000000);
2351 NewLIR2(kX86Cmp64RR, rs_r0q.GetReg(), rs_r6q.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002352 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002353
2354 // In 0x8000000000000000/-1 case.
2355 if (!is_div) {
2356 // For DIV, RAX is already right. For REM, we need RDX 0.
2357 NewLIR2(kX86Xor64RR, rs_r2q.GetReg(), rs_r2q.GetReg());
2358 }
2359 LIR* done = NewLIR1(kX86Jmp8, 0);
2360
2361 // Expected case.
2362 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
2363 minint_branch->target = minus_one_branch->target;
2364 NewLIR1(kX86Idivmod64DaR, rs_r1q.GetReg());
2365 done->target = NewLIR0(kPseudoTargetLabel);
2366
2367 // Result is in RAX for div and RDX for rem.
2368 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, rs_r0q, INVALID_SREG, INVALID_SREG};
2369 if (!is_div) {
2370 rl_result.reg.SetReg(r2q);
2371 }
2372
2373 StoreValueWide(rl_dest, rl_result);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002374}
2375
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002376void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002377 rl_src = LoadValueWide(rl_src, kCoreReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002378 RegLocation rl_result;
Elena Sayapinadd644502014-07-01 18:39:52 +07002379 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002380 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2381 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
2382 } else {
2383 rl_result = ForceTempWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002384 OpRegReg(kOpNeg, rl_result.reg.GetLow(), rl_result.reg.GetLow()); // rLow = -rLow
2385 OpRegImm(kOpAdc, rl_result.reg.GetHigh(), 0); // rHigh = rHigh + CF
2386 OpRegReg(kOpNeg, rl_result.reg.GetHigh(), rl_result.reg.GetHigh()); // rHigh = -rHigh
Mark Mendelle02d48f2014-01-15 11:19:23 -08002387 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002388 StoreValueWide(rl_dest, rl_result);
2389}
2390
buzbee091cc402014-03-31 10:14:40 -07002391void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<4> thread_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002392 DCHECK_EQ(kX86, cu_->instruction_set);
2393 X86OpCode opcode = kX86Bkpt;
2394 switch (op) {
2395 case kOpCmp: opcode = kX86Cmp32RT; break;
2396 case kOpMov: opcode = kX86Mov32RT; break;
2397 default:
2398 LOG(FATAL) << "Bad opcode: " << op;
2399 break;
2400 }
2401 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
2402}
2403
2404void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<8> thread_offset) {
2405 DCHECK_EQ(kX86_64, cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002406 X86OpCode opcode = kX86Bkpt;
Elena Sayapinadd644502014-07-01 18:39:52 +07002407 if (cu_->target64 && r_dest.Is64BitSolo()) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07002408 switch (op) {
2409 case kOpCmp: opcode = kX86Cmp64RT; break;
2410 case kOpMov: opcode = kX86Mov64RT; break;
2411 default:
2412 LOG(FATAL) << "Bad opcode(OpRegThreadMem 64): " << op;
2413 break;
2414 }
2415 } else {
2416 switch (op) {
2417 case kOpCmp: opcode = kX86Cmp32RT; break;
2418 case kOpMov: opcode = kX86Mov32RT; break;
2419 default:
2420 LOG(FATAL) << "Bad opcode: " << op;
2421 break;
2422 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002423 }
buzbee091cc402014-03-31 10:14:40 -07002424 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002425}
2426
2427/*
2428 * Generate array load
2429 */
2430void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002431 RegLocation rl_index, RegLocation rl_dest, int scale) {
Mark Mendellca541342014-10-15 16:59:49 -04002432 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002433 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002434 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002435 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002436
Mark Mendell343adb52013-12-18 06:02:17 -08002437 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07002438 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002439 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2440 } else {
2441 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2442 }
2443
Mark Mendell343adb52013-12-18 06:02:17 -08002444 bool constant_index = rl_index.is_const;
2445 int32_t constant_index_value = 0;
2446 if (!constant_index) {
2447 rl_index = LoadValue(rl_index, kCoreReg);
2448 } else {
2449 constant_index_value = mir_graph_->ConstantValue(rl_index);
2450 // If index is constant, just fold it into the data offset
2451 data_offset += constant_index_value << scale;
2452 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002453 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002454 }
2455
Brian Carlstrom7940e442013-07-12 13:46:57 -07002456 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002457 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002458
2459 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002460 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002461 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002462 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002463 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002464 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002465 }
Mark Mendell343adb52013-12-18 06:02:17 -08002466 rl_result = EvalLoc(rl_dest, reg_class, true);
Vladimir Marko3bf7c602014-05-07 14:55:43 +01002467 LoadBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_result.reg, size);
buzbee695d13a2014-04-19 13:32:20 -07002468 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002469 StoreValueWide(rl_dest, rl_result);
2470 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002471 StoreValue(rl_dest, rl_result);
2472 }
2473}
2474
2475/*
2476 * Generate array store
2477 *
2478 */
2479void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002480 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Mark Mendellca541342014-10-15 16:59:49 -04002481 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002482 int len_offset = mirror::Array::LengthOffset().Int32Value();
2483 int data_offset;
2484
buzbee695d13a2014-04-19 13:32:20 -07002485 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002486 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2487 } else {
2488 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2489 }
2490
buzbeea0cd2d72014-06-01 09:33:49 -07002491 rl_array = LoadValue(rl_array, kRefReg);
Mark Mendell343adb52013-12-18 06:02:17 -08002492 bool constant_index = rl_index.is_const;
2493 int32_t constant_index_value = 0;
2494 if (!constant_index) {
2495 rl_index = LoadValue(rl_index, kCoreReg);
2496 } else {
2497 // If index is constant, just fold it into the data offset
2498 constant_index_value = mir_graph_->ConstantValue(rl_index);
2499 data_offset += constant_index_value << scale;
2500 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002501 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002502 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002503
2504 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002505 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002506
2507 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002508 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002509 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002510 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002511 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002512 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002513 }
buzbee695d13a2014-04-19 13:32:20 -07002514 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002515 rl_src = LoadValueWide(rl_src, reg_class);
2516 } else {
2517 rl_src = LoadValue(rl_src, reg_class);
2518 }
2519 // If the src reg can't be byte accessed, move it to a temp first.
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002520 if ((size == kSignedByte || size == kUnsignedByte) && !IsByteRegister(rl_src.reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08002521 RegStorage temp = AllocTemp();
2522 OpRegCopy(temp, rl_src.reg);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002523 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, temp, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002524 } else {
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002525 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_src.reg, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002526 }
Ian Rogersa9a82542013-10-04 11:17:26 -07002527 if (card_mark) {
Ian Rogers773aab12013-10-14 13:50:10 -07002528 // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
Mark Mendell343adb52013-12-18 06:02:17 -08002529 if (!constant_index) {
buzbee091cc402014-03-31 10:14:40 -07002530 FreeTemp(rl_index.reg);
Mark Mendell343adb52013-12-18 06:02:17 -08002531 }
Vladimir Marko743b98c2014-11-24 19:45:41 +00002532 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002533 }
2534}
2535
Mark Mendell4708dcd2014-01-22 09:05:18 -08002536RegLocation X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002537 RegLocation rl_src, int shift_amount, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002538 UNUSED(flags);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002539 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Elena Sayapinadd644502014-07-01 18:39:52 +07002540 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002541 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
2542 switch (opcode) {
2543 case Instruction::SHL_LONG:
2544 case Instruction::SHL_LONG_2ADDR:
2545 op = kOpLsl;
2546 break;
2547 case Instruction::SHR_LONG:
2548 case Instruction::SHR_LONG_2ADDR:
2549 op = kOpAsr;
2550 break;
2551 case Instruction::USHR_LONG:
2552 case Instruction::USHR_LONG_2ADDR:
2553 op = kOpLsr;
2554 break;
2555 default:
2556 LOG(FATAL) << "Unexpected case";
2557 }
2558 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
2559 } else {
2560 switch (opcode) {
2561 case Instruction::SHL_LONG:
2562 case Instruction::SHL_LONG_2ADDR:
2563 DCHECK_NE(shift_amount, 1); // Prevent a double store from happening.
2564 if (shift_amount == 32) {
2565 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2566 LoadConstant(rl_result.reg.GetLow(), 0);
2567 } else if (shift_amount > 31) {
2568 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2569 NewLIR2(kX86Sal32RI, rl_result.reg.GetHighReg(), shift_amount - 32);
2570 LoadConstant(rl_result.reg.GetLow(), 0);
2571 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002572 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002573 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2574 NewLIR3(kX86Shld32RRI, rl_result.reg.GetHighReg(), rl_result.reg.GetLowReg(),
2575 shift_amount);
2576 NewLIR2(kX86Sal32RI, rl_result.reg.GetLowReg(), shift_amount);
2577 }
2578 break;
2579 case Instruction::SHR_LONG:
2580 case Instruction::SHR_LONG_2ADDR:
2581 if (shift_amount == 32) {
2582 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2583 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2584 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2585 } else if (shift_amount > 31) {
2586 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2587 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2588 NewLIR2(kX86Sar32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2589 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2590 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002591 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002592 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2593 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2594 shift_amount);
2595 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), shift_amount);
2596 }
2597 break;
2598 case Instruction::USHR_LONG:
2599 case Instruction::USHR_LONG_2ADDR:
2600 if (shift_amount == 32) {
2601 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2602 LoadConstant(rl_result.reg.GetHigh(), 0);
2603 } else if (shift_amount > 31) {
2604 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2605 NewLIR2(kX86Shr32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2606 LoadConstant(rl_result.reg.GetHigh(), 0);
2607 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002608 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002609 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2610 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2611 shift_amount);
2612 NewLIR2(kX86Shr32RI, rl_result.reg.GetHighReg(), shift_amount);
2613 }
2614 break;
2615 default:
2616 LOG(FATAL) << "Unexpected case";
2617 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08002618 }
2619 return rl_result;
2620}
2621
Brian Carlstrom7940e442013-07-12 13:46:57 -07002622void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002623 RegLocation rl_src, RegLocation rl_shift, int flags) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002624 // Per spec, we only care about low 6 bits of shift amount.
2625 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
2626 if (shift_amount == 0) {
2627 rl_src = LoadValueWide(rl_src, kCoreReg);
2628 StoreValueWide(rl_dest, rl_src);
2629 return;
2630 } else if (shift_amount == 1 &&
2631 (opcode == Instruction::SHL_LONG || opcode == Instruction::SHL_LONG_2ADDR)) {
2632 // Need to handle this here to avoid calling StoreValueWide twice.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002633 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src, rl_src, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002634 return;
2635 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07002636 if (PartiallyIntersects(rl_src, rl_dest)) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002637 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
2638 return;
2639 }
2640 rl_src = LoadValueWide(rl_src, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002641 RegLocation rl_result = GenShiftImmOpLong(opcode, rl_dest, rl_src, shift_amount, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002642 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002643}
2644
2645void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002646 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
2647 int flags) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002648 bool isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002649 switch (opcode) {
2650 case Instruction::ADD_LONG:
2651 case Instruction::AND_LONG:
2652 case Instruction::OR_LONG:
2653 case Instruction::XOR_LONG:
2654 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002655 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002656 } else {
2657 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002658 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002659 }
2660 break;
2661 case Instruction::SUB_LONG:
2662 case Instruction::SUB_LONG_2ADDR:
2663 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002664 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002665 } else {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002666 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002667 isConstSuccess = true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002668 }
2669 break;
2670 case Instruction::ADD_LONG_2ADDR:
2671 case Instruction::OR_LONG_2ADDR:
2672 case Instruction::XOR_LONG_2ADDR:
2673 case Instruction::AND_LONG_2ADDR:
2674 if (rl_src2.is_const) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04002675 if (GenerateTwoOperandInstructions()) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002676 isConstSuccess = GenLongImm(rl_dest, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002677 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002678 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002679 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002680 } else {
2681 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002682 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002683 }
2684 break;
2685 default:
Chao-ying Fua0147762014-06-06 18:38:49 -07002686 isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002687 break;
2688 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002689
2690 if (!isConstSuccess) {
2691 // Default - bail to non-const handler.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002692 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002693 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002694}
2695
2696bool X86Mir2Lir::IsNoOp(Instruction::Code op, int32_t value) {
2697 switch (op) {
2698 case Instruction::AND_LONG_2ADDR:
2699 case Instruction::AND_LONG:
2700 return value == -1;
2701 case Instruction::OR_LONG:
2702 case Instruction::OR_LONG_2ADDR:
2703 case Instruction::XOR_LONG:
2704 case Instruction::XOR_LONG_2ADDR:
2705 return value == 0;
2706 default:
2707 return false;
2708 }
2709}
2710
2711X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
2712 bool is_high_op) {
2713 bool rhs_in_mem = rhs.location != kLocPhysReg;
2714 bool dest_in_mem = dest.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002715 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002716 DCHECK(!rhs_in_mem || !dest_in_mem);
2717 switch (op) {
2718 case Instruction::ADD_LONG:
2719 case Instruction::ADD_LONG_2ADDR:
2720 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002721 return is64Bit ? kX86Add64MR : is_high_op ? kX86Adc32MR : kX86Add32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002722 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002723 return is64Bit ? kX86Add64RM : is_high_op ? kX86Adc32RM : kX86Add32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002724 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002725 return is64Bit ? kX86Add64RR : is_high_op ? kX86Adc32RR : kX86Add32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002726 case Instruction::SUB_LONG:
2727 case Instruction::SUB_LONG_2ADDR:
2728 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002729 return is64Bit ? kX86Sub64MR : is_high_op ? kX86Sbb32MR : kX86Sub32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002730 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002731 return is64Bit ? kX86Sub64RM : is_high_op ? kX86Sbb32RM : kX86Sub32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002732 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002733 return is64Bit ? kX86Sub64RR : is_high_op ? kX86Sbb32RR : kX86Sub32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002734 case Instruction::AND_LONG_2ADDR:
2735 case Instruction::AND_LONG:
2736 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002737 return is64Bit ? kX86And64MR : kX86And32MR;
2738 }
2739 if (is64Bit) {
2740 return rhs_in_mem ? kX86And64RM : kX86And64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002741 }
2742 return rhs_in_mem ? kX86And32RM : kX86And32RR;
2743 case Instruction::OR_LONG:
2744 case Instruction::OR_LONG_2ADDR:
2745 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002746 return is64Bit ? kX86Or64MR : kX86Or32MR;
2747 }
2748 if (is64Bit) {
2749 return rhs_in_mem ? kX86Or64RM : kX86Or64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002750 }
2751 return rhs_in_mem ? kX86Or32RM : kX86Or32RR;
2752 case Instruction::XOR_LONG:
2753 case Instruction::XOR_LONG_2ADDR:
2754 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002755 return is64Bit ? kX86Xor64MR : kX86Xor32MR;
2756 }
2757 if (is64Bit) {
2758 return rhs_in_mem ? kX86Xor64RM : kX86Xor64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002759 }
2760 return rhs_in_mem ? kX86Xor32RM : kX86Xor32RR;
2761 default:
2762 LOG(FATAL) << "Unexpected opcode: " << op;
2763 return kX86Add32RR;
2764 }
2765}
2766
2767X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op,
2768 int32_t value) {
2769 bool in_mem = loc.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002770 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002771 bool byte_imm = IS_SIMM8(value);
buzbee091cc402014-03-31 10:14:40 -07002772 DCHECK(in_mem || !loc.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002773 switch (op) {
2774 case Instruction::ADD_LONG:
2775 case Instruction::ADD_LONG_2ADDR:
2776 if (byte_imm) {
2777 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002778 return is64Bit ? kX86Add64MI8 : is_high_op ? kX86Adc32MI8 : kX86Add32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002779 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002780 return is64Bit ? kX86Add64RI8 : is_high_op ? kX86Adc32RI8 : kX86Add32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002781 }
2782 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002783 return is64Bit ? kX86Add64MI : is_high_op ? kX86Adc32MI : kX86Add32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002784 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002785 return is64Bit ? kX86Add64RI : is_high_op ? kX86Adc32RI : kX86Add32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002786 case Instruction::SUB_LONG:
2787 case Instruction::SUB_LONG_2ADDR:
2788 if (byte_imm) {
2789 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002790 return is64Bit ? kX86Sub64MI8 : is_high_op ? kX86Sbb32MI8 : kX86Sub32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002791 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002792 return is64Bit ? kX86Sub64RI8 : is_high_op ? kX86Sbb32RI8 : kX86Sub32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002793 }
2794 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002795 return is64Bit ? kX86Sub64MI : is_high_op ? kX86Sbb32MI : kX86Sub32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002796 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002797 return is64Bit ? kX86Sub64RI : is_high_op ? kX86Sbb32RI : kX86Sub32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002798 case Instruction::AND_LONG_2ADDR:
2799 case Instruction::AND_LONG:
2800 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002801 if (is64Bit) {
2802 return in_mem ? kX86And64MI8 : kX86And64RI8;
2803 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002804 return in_mem ? kX86And32MI8 : kX86And32RI8;
2805 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002806 if (is64Bit) {
2807 return in_mem ? kX86And64MI : kX86And64RI;
2808 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002809 return in_mem ? kX86And32MI : kX86And32RI;
2810 case Instruction::OR_LONG:
2811 case Instruction::OR_LONG_2ADDR:
2812 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002813 if (is64Bit) {
2814 return in_mem ? kX86Or64MI8 : kX86Or64RI8;
2815 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002816 return in_mem ? kX86Or32MI8 : kX86Or32RI8;
2817 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002818 if (is64Bit) {
2819 return in_mem ? kX86Or64MI : kX86Or64RI;
2820 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002821 return in_mem ? kX86Or32MI : kX86Or32RI;
2822 case Instruction::XOR_LONG:
2823 case Instruction::XOR_LONG_2ADDR:
2824 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002825 if (is64Bit) {
2826 return in_mem ? kX86Xor64MI8 : kX86Xor64RI8;
2827 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002828 return in_mem ? kX86Xor32MI8 : kX86Xor32RI8;
2829 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002830 if (is64Bit) {
2831 return in_mem ? kX86Xor64MI : kX86Xor64RI;
2832 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002833 return in_mem ? kX86Xor32MI : kX86Xor32RI;
2834 default:
2835 LOG(FATAL) << "Unexpected opcode: " << op;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002836 UNREACHABLE();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002837 }
2838}
2839
Chao-ying Fua0147762014-06-06 18:38:49 -07002840bool X86Mir2Lir::GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002841 DCHECK(rl_src.is_const);
2842 int64_t val = mir_graph_->ConstantValueWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002843
Elena Sayapinadd644502014-07-01 18:39:52 +07002844 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002845 // We can do with imm only if it fits 32 bit
2846 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2847 return false;
2848 }
2849
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002850 rl_dest = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002851
2852 if ((rl_dest.location == kLocDalvikFrame) ||
2853 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002854 int r_base = rs_rX86_SP_32.GetReg();
Chao-ying Fua0147762014-06-06 18:38:49 -07002855 int displacement = SRegOffset(rl_dest.s_reg_low);
2856
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002857 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002858 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
2859 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val);
2860 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2861 true /* is_load */, true /* is64bit */);
2862 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2863 false /* is_load */, true /* is64bit */);
2864 return true;
2865 }
2866
2867 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2868 DCHECK_EQ(rl_result.location, kLocPhysReg);
2869 DCHECK(!rl_result.reg.IsFloat());
2870
2871 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2872 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2873
2874 StoreValueWide(rl_dest, rl_result);
2875 return true;
2876 }
2877
Mark Mendelle02d48f2014-01-15 11:19:23 -08002878 int32_t val_lo = Low32Bits(val);
2879 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002880 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002881
2882 // Can we just do this into memory?
2883 if ((rl_dest.location == kLocDalvikFrame) ||
2884 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002885 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002886 int displacement = SRegOffset(rl_dest.s_reg_low);
2887
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002888 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002889 if (!IsNoOp(op, val_lo)) {
2890 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002891 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002892 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002893 true /* is_load */, true /* is64bit */);
2894 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002895 false /* is_load */, true /* is64bit */);
2896 }
2897 if (!IsNoOp(op, val_hi)) {
2898 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
buzbee2700f7e2014-03-07 09:46:20 -08002899 LIR *lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002900 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002901 true /* is_load */, true /* is64bit */);
2902 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002903 false /* is_load */, true /* is64bit */);
2904 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002905 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002906 }
2907
2908 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2909 DCHECK_EQ(rl_result.location, kLocPhysReg);
buzbee091cc402014-03-31 10:14:40 -07002910 DCHECK(!rl_result.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002911
2912 if (!IsNoOp(op, val_lo)) {
2913 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002914 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002915 }
2916 if (!IsNoOp(op, val_hi)) {
2917 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002918 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002919 }
2920 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002921 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002922}
2923
Chao-ying Fua0147762014-06-06 18:38:49 -07002924bool X86Mir2Lir::GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002925 RegLocation rl_src2, Instruction::Code op) {
2926 DCHECK(rl_src2.is_const);
2927 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Chao-ying Fua0147762014-06-06 18:38:49 -07002928
Elena Sayapinadd644502014-07-01 18:39:52 +07002929 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002930 // We can do with imm only if it fits 32 bit
2931 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2932 return false;
2933 }
2934 if (rl_dest.location == kLocPhysReg &&
2935 rl_src1.location == kLocPhysReg && !rl_dest.reg.IsFloat()) {
2936 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002937 OpRegCopy(rl_dest.reg, rl_src1.reg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002938 NewLIR2(x86op, rl_dest.reg.GetReg(), val);
2939 StoreFinalValueWide(rl_dest, rl_dest);
2940 return true;
2941 }
2942
2943 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2944 // We need the values to be in a temporary
2945 RegLocation rl_result = ForceTempWide(rl_src1);
2946
2947 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2948 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2949
2950 StoreFinalValueWide(rl_dest, rl_result);
2951 return true;
2952 }
2953
Mark Mendelle02d48f2014-01-15 11:19:23 -08002954 int32_t val_lo = Low32Bits(val);
2955 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002956 rl_dest = UpdateLocWideTyped(rl_dest);
2957 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002958
2959 // Can we do this directly into the destination registers?
2960 if (rl_dest.location == kLocPhysReg && rl_src1.location == kLocPhysReg &&
buzbee2700f7e2014-03-07 09:46:20 -08002961 rl_dest.reg.GetLowReg() == rl_src1.reg.GetLowReg() &&
buzbee091cc402014-03-31 10:14:40 -07002962 rl_dest.reg.GetHighReg() == rl_src1.reg.GetHighReg() && !rl_dest.reg.IsFloat()) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002963 if (!IsNoOp(op, val_lo)) {
2964 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002965 NewLIR2(x86op, rl_dest.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002966 }
2967 if (!IsNoOp(op, val_hi)) {
2968 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002969 NewLIR2(x86op, rl_dest.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002970 }
Maxim Kazantsev653f2bf2014-02-13 15:11:17 +07002971
2972 StoreFinalValueWide(rl_dest, rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002973 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002974 }
2975
2976 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2977 DCHECK_EQ(rl_src1.location, kLocPhysReg);
2978
2979 // We need the values to be in a temporary
2980 RegLocation rl_result = ForceTempWide(rl_src1);
2981 if (!IsNoOp(op, val_lo)) {
2982 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002983 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002984 }
2985 if (!IsNoOp(op, val_hi)) {
2986 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002987 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002988 }
2989
2990 StoreFinalValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002991 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002992}
2993
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002994// For final classes there are no sub-classes to check and so we can answer the instance-of
2995// question with simple comparisons. Use compares to memory and SETEQ to optimize for x86.
2996void X86Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx,
2997 RegLocation rl_dest, RegLocation rl_src) {
buzbeea0cd2d72014-06-01 09:33:49 -07002998 RegLocation object = LoadValue(rl_src, kRefReg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002999 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003000 RegStorage result_reg = rl_result.reg;
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003001
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07003002 // For 32-bit, SETcc only works with EAX..EDX.
Chao-ying Fua77ee512014-07-01 17:43:41 -07003003 RegStorage object_32reg = object.reg.Is64Bit() ? As32BitReg(object.reg) : object.reg;
Dmitry Petrochenko407f5c12014-07-01 01:21:38 +07003004 if (result_reg.GetRegNum() == object_32reg.GetRegNum() || !IsByteRegister(result_reg)) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04003005 result_reg = AllocateByteRegister();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003006 }
3007
3008 // Assume that there is no match.
3009 LoadConstant(result_reg, 0);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003010 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, nullptr);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003011
Mark Mendellade54a22014-06-09 12:49:55 -04003012 // We will use this register to compare to memory below.
3013 // References are 32 bit in memory, and 64 bit in registers (in 64 bit mode).
3014 // For this reason, force allocation of a 32 bit register to use, so that the
3015 // compare to memory will be done using a 32 bit comparision.
3016 // The LoadRefDisp(s) below will work normally, even in 64 bit mode.
3017 RegStorage check_class = AllocTemp();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003018
3019 // If Method* is already in a register, we can save a copy.
3020 RegLocation rl_method = mir_graph_->GetMethodLoc();
Andreas Gampeccc60262014-07-04 18:02:38 -07003021 int32_t offset_of_type = mirror::Array::DataOffset(
3022 sizeof(mirror::HeapReference<mirror::Class*>)).Int32Value() +
3023 (sizeof(mirror::HeapReference<mirror::Class*>) * type_idx);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003024
3025 if (rl_method.location == kLocPhysReg) {
3026 if (use_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003027 LoadRefDisp(rl_method.reg, ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00003028 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003029 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003030 LoadRefDisp(rl_method.reg, ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00003031 check_class, kNotVolatile);
3032 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003033 }
3034 } else {
3035 LoadCurrMethodDirect(check_class);
3036 if (use_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003037 LoadRefDisp(check_class, ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00003038 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003039 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003040 LoadRefDisp(check_class, ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00003041 check_class, kNotVolatile);
3042 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003043 }
3044 }
3045
3046 // Compare the computed class to the class in the object.
3047 DCHECK_EQ(object.location, kLocPhysReg);
buzbee2700f7e2014-03-07 09:46:20 -08003048 OpRegMem(kOpCmp, check_class, object.reg, mirror::Object::ClassOffset().Int32Value());
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003049
3050 // Set the low byte of the result to 0 or 1 from the compare condition code.
buzbee2700f7e2014-03-07 09:46:20 -08003051 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondEq);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003052
3053 LIR* target = NewLIR0(kPseudoTargetLabel);
3054 null_branchover->target = target;
3055 FreeTemp(check_class);
3056 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08003057 OpRegCopy(rl_result.reg, result_reg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003058 FreeTemp(result_reg);
3059 }
3060 StoreValue(rl_dest, rl_result);
3061}
3062
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003063void X86Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003064 RegLocation rl_lhs, RegLocation rl_rhs, int flags) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003065 OpKind op = kOpBkpt;
3066 bool is_div_rem = false;
3067 bool unary = false;
3068 bool shift_op = false;
3069 bool is_two_addr = false;
3070 RegLocation rl_result;
3071 switch (opcode) {
3072 case Instruction::NEG_INT:
3073 op = kOpNeg;
3074 unary = true;
3075 break;
3076 case Instruction::NOT_INT:
3077 op = kOpMvn;
3078 unary = true;
3079 break;
3080 case Instruction::ADD_INT_2ADDR:
3081 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003082 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003083 case Instruction::ADD_INT:
3084 op = kOpAdd;
3085 break;
3086 case Instruction::SUB_INT_2ADDR:
3087 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003088 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003089 case Instruction::SUB_INT:
3090 op = kOpSub;
3091 break;
3092 case Instruction::MUL_INT_2ADDR:
3093 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003094 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003095 case Instruction::MUL_INT:
3096 op = kOpMul;
3097 break;
3098 case Instruction::DIV_INT_2ADDR:
3099 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003100 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003101 case Instruction::DIV_INT:
3102 op = kOpDiv;
3103 is_div_rem = true;
3104 break;
3105 /* NOTE: returns in kArg1 */
3106 case Instruction::REM_INT_2ADDR:
3107 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003108 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003109 case Instruction::REM_INT:
3110 op = kOpRem;
3111 is_div_rem = true;
3112 break;
3113 case Instruction::AND_INT_2ADDR:
3114 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003115 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003116 case Instruction::AND_INT:
3117 op = kOpAnd;
3118 break;
3119 case Instruction::OR_INT_2ADDR:
3120 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003121 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003122 case Instruction::OR_INT:
3123 op = kOpOr;
3124 break;
3125 case Instruction::XOR_INT_2ADDR:
3126 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003127 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003128 case Instruction::XOR_INT:
3129 op = kOpXor;
3130 break;
3131 case Instruction::SHL_INT_2ADDR:
3132 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003133 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003134 case Instruction::SHL_INT:
3135 shift_op = true;
3136 op = kOpLsl;
3137 break;
3138 case Instruction::SHR_INT_2ADDR:
3139 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003140 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003141 case Instruction::SHR_INT:
3142 shift_op = true;
3143 op = kOpAsr;
3144 break;
3145 case Instruction::USHR_INT_2ADDR:
3146 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003147 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003148 case Instruction::USHR_INT:
3149 shift_op = true;
3150 op = kOpLsr;
3151 break;
3152 default:
3153 LOG(FATAL) << "Invalid word arith op: " << opcode;
3154 }
3155
Mark Mendelle87f9b52014-04-30 14:13:18 -04003156 // Can we convert to a two address instruction?
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003157 if (!is_two_addr &&
3158 (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3159 mir_graph_->SRegToVReg(rl_lhs.s_reg_low))) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04003160 is_two_addr = true;
3161 }
3162
3163 if (!GenerateTwoOperandInstructions()) {
3164 is_two_addr = false;
3165 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003166
3167 // Get the div/rem stuff out of the way.
3168 if (is_div_rem) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07003169 rl_result = GenDivRem(rl_dest, rl_lhs, rl_rhs, op == kOpDiv, flags);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003170 StoreValue(rl_dest, rl_result);
3171 return;
3172 }
3173
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003174 // If we generate any memory access below, it will reference a dalvik reg.
3175 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
3176
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003177 if (unary) {
3178 rl_lhs = LoadValue(rl_lhs, kCoreReg);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003179 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003180 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003181 OpRegReg(op, rl_result.reg, rl_lhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003182 } else {
3183 if (shift_op) {
3184 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003185 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003186 LoadValueDirectFixed(rl_rhs, t_reg);
3187 if (is_two_addr) {
3188 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003189 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003190 if (rl_result.location != kLocPhysReg) {
3191 // Okay, we can do this into memory
buzbee2700f7e2014-03-07 09:46:20 -08003192 OpMemReg(op, rl_result, t_reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003193 FreeTemp(t_reg);
3194 return;
buzbee091cc402014-03-31 10:14:40 -07003195 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003196 // Can do this directly into the result register
buzbee2700f7e2014-03-07 09:46:20 -08003197 OpRegReg(op, rl_result.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003198 FreeTemp(t_reg);
3199 StoreFinalValue(rl_dest, rl_result);
3200 return;
3201 }
3202 }
3203 // Three address form, or we can't do directly.
3204 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3205 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003206 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003207 FreeTemp(t_reg);
3208 } else {
3209 // Multiply is 3 operand only (sort of).
3210 if (is_two_addr && op != kOpMul) {
3211 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003212 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003213 if (rl_result.location == kLocPhysReg) {
Serguei Katkov366f8ae2014-04-15 16:55:26 +07003214 // Ensure res is in a core reg
3215 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003216 // Can we do this from memory directly?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003217 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003218 if (rl_rhs.location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08003219 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003220 StoreFinalValue(rl_dest, rl_result);
3221 return;
buzbee091cc402014-03-31 10:14:40 -07003222 } else if (!rl_rhs.reg.IsFloat()) {
buzbee2700f7e2014-03-07 09:46:20 -08003223 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003224 StoreFinalValue(rl_dest, rl_result);
3225 return;
3226 }
3227 }
3228 rl_rhs = LoadValue(rl_rhs, kCoreReg);
Serguei Katkovd293fb42014-05-19 15:45:42 +07003229 // It might happen rl_rhs and rl_dest are the same VR
3230 // in this case rl_dest is in reg after LoadValue while
3231 // rl_result is not updated yet, so do this
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003232 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003233 if (rl_result.location != kLocPhysReg) {
3234 // Okay, we can do this into memory.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00003235 OpMemReg(op, rl_result, rl_rhs.reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003236 return;
buzbee091cc402014-03-31 10:14:40 -07003237 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003238 // Can do this directly into the result register.
buzbee2700f7e2014-03-07 09:46:20 -08003239 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003240 StoreFinalValue(rl_dest, rl_result);
3241 return;
3242 } else {
3243 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3244 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003245 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003246 }
3247 } else {
3248 // Try to use reg/memory instructions.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003249 rl_lhs = UpdateLocTyped(rl_lhs);
3250 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003251 // We can't optimize with FP registers.
3252 if (!IsOperationSafeWithoutTemps(rl_lhs, rl_rhs)) {
3253 // Something is difficult, so fall back to the standard case.
3254 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3255 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3256 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003257 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003258 } else {
3259 // We can optimize by moving to result and using memory operands.
3260 if (rl_rhs.location != kLocPhysReg) {
3261 // Force LHS into result.
Serguei Katkov66da1362014-03-14 13:33:33 +07003262 // We should be careful with order here
3263 // If rl_dest and rl_lhs points to the same VR we should load first
3264 // If the are different we should find a register first for dest
Chao-ying Fua0147762014-06-06 18:38:49 -07003265 if (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3266 mir_graph_->SRegToVReg(rl_lhs.s_reg_low)) {
Serguei Katkov66da1362014-03-14 13:33:33 +07003267 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3268 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendelle87f9b52014-04-30 14:13:18 -04003269 // No-op if these are the same.
3270 OpRegCopy(rl_result.reg, rl_lhs.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003271 } else {
3272 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003273 LoadValueDirect(rl_lhs, rl_result.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003274 }
buzbee2700f7e2014-03-07 09:46:20 -08003275 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003276 } else if (rl_lhs.location != kLocPhysReg) {
3277 // RHS is in a register; LHS is in memory.
3278 if (op != kOpSub) {
3279 // Force RHS into result and operate on memory.
3280 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003281 OpRegCopy(rl_result.reg, rl_rhs.reg);
3282 OpRegMem(op, rl_result.reg, rl_lhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003283 } else {
3284 // Subtraction isn't commutative.
3285 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3286 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3287 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003288 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003289 }
3290 } else {
3291 // Both are in registers.
3292 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3293 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3294 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003295 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003296 }
3297 }
3298 }
3299 }
3300 }
3301 StoreValue(rl_dest, rl_result);
3302}
3303
3304bool X86Mir2Lir::IsOperationSafeWithoutTemps(RegLocation rl_lhs, RegLocation rl_rhs) {
3305 // If we have non-core registers, then we can't do good things.
buzbee091cc402014-03-31 10:14:40 -07003306 if (rl_lhs.location == kLocPhysReg && rl_lhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003307 return false;
3308 }
buzbee091cc402014-03-31 10:14:40 -07003309 if (rl_rhs.location == kLocPhysReg && rl_rhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003310 return false;
3311 }
3312
3313 // Everything will be fine :-).
3314 return true;
3315}
Chao-ying Fua0147762014-06-06 18:38:49 -07003316
3317void X86Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003318 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07003319 Mir2Lir::GenIntToLong(rl_dest, rl_src);
3320 return;
3321 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003322 rl_src = UpdateLocTyped(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07003323 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
3324 if (rl_src.location == kLocPhysReg) {
3325 NewLIR2(kX86MovsxdRR, rl_result.reg.GetReg(), rl_src.reg.GetReg());
3326 } else {
3327 int displacement = SRegOffset(rl_src.s_reg_low);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003328 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08003329 LIR *m = NewLIR3(kX86MovsxdRM, rl_result.reg.GetReg(), rs_rX86_SP_32.GetReg(),
Chao-ying Fua0147762014-06-06 18:38:49 -07003330 displacement + LOWORD_OFFSET);
3331 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
3332 true /* is_load */, true /* is_64bit */);
3333 }
3334 StoreValueWide(rl_dest, rl_result);
3335}
3336
Yevgeny Rouban6af82062014-11-26 18:11:54 +06003337void X86Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
3338 rl_src = UpdateLocWide(rl_src);
3339 rl_src = NarrowRegLoc(rl_src);
3340 StoreValue(rl_dest, rl_src);
3341
3342 if (cu_->target64) {
3343 // if src and dest are in the same phys reg then StoreValue generates
3344 // no operation but we need explicit 32-bit mov R, R to clear
3345 // the higher 32-bits
3346 rl_dest = UpdateLoc(rl_dest);
3347 if (rl_src.location == kLocPhysReg && rl_dest.location == kLocPhysReg
3348 && IsSameReg(rl_src.reg, rl_dest.reg)) {
3349 LIR* copy_lir = OpRegCopyNoInsert(rl_dest.reg, rl_dest.reg);
3350 // remove nop flag set by OpRegCopyNoInsert if src == dest
3351 copy_lir->flags.is_nop = false;
3352 AppendLIR(copy_lir);
3353 }
3354 }
3355}
3356
Chao-ying Fua0147762014-06-06 18:38:49 -07003357void X86Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
3358 RegLocation rl_src1, RegLocation rl_shift) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003359 if (!cu_->target64) {
Yixin Shouf40f8902014-08-14 14:10:32 -04003360 // Long shift operations in 32-bit. Use shld or shrd to create a 32-bit register filled from
3361 // the other half, shift the other half, if the shift amount is less than 32 we're done,
3362 // otherwise move one register to the other and place zero or sign bits in the other.
3363 LIR* branch;
3364 FlushAllRegs();
3365 LockCallTemps();
3366 LoadValueDirectFixed(rl_shift, rs_rCX);
3367 RegStorage r_tmp = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
3368 LoadValueDirectWideFixed(rl_src1, r_tmp);
3369 switch (opcode) {
3370 case Instruction::SHL_LONG:
3371 case Instruction::SHL_LONG_2ADDR:
3372 NewLIR3(kX86Shld32RRC, r_tmp.GetHighReg(), r_tmp.GetLowReg(), rs_rCX.GetReg());
3373 NewLIR2(kX86Sal32RC, r_tmp.GetLowReg(), rs_rCX.GetReg());
3374 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3375 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3376 OpRegCopy(r_tmp.GetHigh(), r_tmp.GetLow());
3377 LoadConstant(r_tmp.GetLow(), 0);
3378 branch->target = NewLIR0(kPseudoTargetLabel);
3379 break;
3380 case Instruction::SHR_LONG:
3381 case Instruction::SHR_LONG_2ADDR:
3382 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(), rs_rCX.GetReg());
3383 NewLIR2(kX86Sar32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3384 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3385 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3386 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3387 NewLIR2(kX86Sar32RI, r_tmp.GetHighReg(), 31);
3388 branch->target = NewLIR0(kPseudoTargetLabel);
3389 break;
3390 case Instruction::USHR_LONG:
3391 case Instruction::USHR_LONG_2ADDR:
3392 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(),
3393 rs_rCX.GetReg());
3394 NewLIR2(kX86Shr32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3395 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3396 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3397 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3398 LoadConstant(r_tmp.GetHigh(), 0);
3399 branch->target = NewLIR0(kPseudoTargetLabel);
3400 break;
3401 default:
3402 LOG(FATAL) << "Unexpected case: " << opcode;
3403 return;
3404 }
3405 RegLocation rl_result = LocCReturnWide();
3406 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07003407 return;
3408 }
3409
3410 bool is_two_addr = false;
3411 OpKind op = kOpBkpt;
3412 RegLocation rl_result;
3413
3414 switch (opcode) {
3415 case Instruction::SHL_LONG_2ADDR:
3416 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003417 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003418 case Instruction::SHL_LONG:
3419 op = kOpLsl;
3420 break;
3421 case Instruction::SHR_LONG_2ADDR:
3422 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003423 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003424 case Instruction::SHR_LONG:
3425 op = kOpAsr;
3426 break;
3427 case Instruction::USHR_LONG_2ADDR:
3428 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003429 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003430 case Instruction::USHR_LONG:
3431 op = kOpLsr;
3432 break;
3433 default:
3434 op = kOpBkpt;
3435 }
3436
3437 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003438 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Chao-ying Fua0147762014-06-06 18:38:49 -07003439 LoadValueDirectFixed(rl_shift, t_reg);
3440 if (is_two_addr) {
3441 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003442 rl_result = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07003443 if (rl_result.location != kLocPhysReg) {
3444 // Okay, we can do this into memory
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003445 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07003446 OpMemReg(op, rl_result, t_reg.GetReg());
3447 } else if (!rl_result.reg.IsFloat()) {
3448 // Can do this directly into the result register
3449 OpRegReg(op, rl_result.reg, t_reg);
3450 StoreFinalValueWide(rl_dest, rl_result);
3451 }
3452 } else {
3453 // Three address form, or we can't do directly.
3454 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
3455 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
3456 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
3457 StoreFinalValueWide(rl_dest, rl_result);
3458 }
3459
3460 FreeTemp(t_reg);
3461}
3462
Brian Carlstrom7940e442013-07-12 13:46:57 -07003463} // namespace art