blob: 943bfc0300cfffea440a92d365633bf619ea1d55 [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
21#include "base/logging.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070023#include "dex/reg_storage_eq.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "mirror/art_method.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080026#include "utils.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
1413void X86Mir2Lir::OpPcRelDexCacheArrayLoad(const DexFile* dex_file, int offset,
1414 RegStorage r_dest) {
1415 if (cu_->target64) {
1416 LIR* mov = NewLIR3(kX86Mov32RM, r_dest.GetReg(), kRIPReg, kDummy32BitOffset);
1417 mov->flags.fixup = kFixupLabel;
1418 mov->operands[3] = WrapPointer(dex_file);
1419 mov->operands[4] = offset;
Vladimir Marko1961b602015-04-08 20:51:48 +01001420 mov->target = mov; // Used for pc_insn_offset (not used by x86-64 relative patcher).
Vladimir Markodc56cc52015-03-27 18:18:36 +00001421 dex_cache_access_insns_.push_back(mov);
1422 } else {
Vladimir Marko1961b602015-04-08 20:51:48 +01001423 // Get the PC to a register and get the anchor. Use r_dest for the temp if needed.
1424 LIR* anchor;
1425 RegStorage r_pc = GetPcAndAnchor(&anchor, r_dest);
1426 LIR* mov = NewLIR3(kX86Mov32RM, r_dest.GetReg(), r_pc.GetReg(), kDummy32BitOffset);
1427 mov->flags.fixup = kFixupLabel;
1428 mov->operands[3] = WrapPointer(dex_file);
1429 mov->operands[4] = offset;
1430 mov->target = anchor; // Used for pc_insn_offset.
1431 dex_cache_access_insns_.push_back(mov);
Vladimir Markodc56cc52015-03-27 18:18:36 +00001432 }
1433}
1434
buzbee2700f7e2014-03-07 09:46:20 -08001435LIR* X86Mir2Lir::OpVldm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001436 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001437 LOG(FATAL) << "Unexpected use of OpVldm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001438 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001439}
1440
buzbee2700f7e2014-03-07 09:46:20 -08001441LIR* X86Mir2Lir::OpVstm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001442 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 LOG(FATAL) << "Unexpected use of OpVstm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001444 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445}
1446
1447void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1448 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001449 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001450 UNUSED(lit);
buzbee2700f7e2014-03-07 09:46:20 -08001451 RegStorage t_reg = AllocTemp();
1452 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, second_bit - first_bit);
1453 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454 FreeTemp(t_reg);
1455 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001456 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001457 }
1458}
1459
Mingyao Yange643a172014-04-08 11:02:52 -07001460void X86Mir2Lir::GenDivZeroCheckWide(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001461 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001462 DCHECK(reg.Is64Bit());
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001463
Chao-ying Fua0147762014-06-06 18:38:49 -07001464 NewLIR2(kX86Cmp64RI8, reg.GetReg(), 0);
1465 } else {
1466 DCHECK(reg.IsPair());
1467
1468 // We are not supposed to clobber the incoming storage, so allocate a temporary.
1469 RegStorage t_reg = AllocTemp();
1470 // Doing an OR is a quick way to check if both registers are zero. This will set the flags.
1471 OpRegRegReg(kOpOr, t_reg, reg.GetLow(), reg.GetHigh());
1472 // The temp is no longer needed so free it at this time.
1473 FreeTemp(t_reg);
1474 }
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001475
1476 // In case of zero, throw ArithmeticException.
Mingyao Yange643a172014-04-08 11:02:52 -07001477 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478}
1479
Mingyao Yang80365d92014-04-18 12:10:58 -07001480void X86Mir2Lir::GenArrayBoundsCheck(RegStorage index,
1481 RegStorage array_base,
1482 int len_offset) {
1483 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1484 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001485 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1486 RegStorage index_in, RegStorage array_base_in, int32_t len_offset_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001487 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001488 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001489 }
1490
1491 void Compile() OVERRIDE {
1492 m2l_->ResetRegPool();
1493 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001494 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001495
1496 RegStorage new_index = index_;
1497 // Move index out of kArg1, either directly to kArg0, or to kArg2.
Serguei Katkov4c7cc152014-06-24 00:50:02 +07001498 // TODO: clean-up to check not a number but with type
Andreas Gampeccc60262014-07-04 18:02:38 -07001499 if (index_ == m2l_->TargetReg(kArg1, kNotWide)) {
1500 if (array_base_ == m2l_->TargetReg(kArg0, kRef)) {
1501 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kNotWide), index_);
1502 new_index = m2l_->TargetReg(kArg2, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001503 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001504 m2l_->OpRegCopy(m2l_->TargetReg(kArg0, kNotWide), index_);
1505 new_index = m2l_->TargetReg(kArg0, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001506 }
1507 }
1508 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001509 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1510 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1511 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, new_index,
1512 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001513 }
1514
1515 private:
1516 const RegStorage index_;
1517 const RegStorage array_base_;
1518 const int32_t len_offset_;
1519 };
1520
1521 OpRegMem(kOpCmp, index, array_base, len_offset);
Dave Allison69dfe512014-07-11 17:11:58 +00001522 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001523 LIR* branch = OpCondBranch(kCondUge, nullptr);
1524 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1525 index, array_base, len_offset));
1526}
1527
1528void X86Mir2Lir::GenArrayBoundsCheck(int32_t index,
1529 RegStorage array_base,
1530 int32_t len_offset) {
1531 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1532 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001533 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1534 int32_t index_in, RegStorage array_base_in, int32_t len_offset_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001535 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001536 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001537 }
1538
1539 void Compile() OVERRIDE {
1540 m2l_->ResetRegPool();
1541 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001542 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001543
1544 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001545 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1546 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1547 x86_m2l->LoadConstant(m2l_->TargetReg(kArg0, kNotWide), index_);
1548 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, m2l_->TargetReg(kArg0, kNotWide),
1549 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001550 }
1551
1552 private:
1553 const int32_t index_;
1554 const RegStorage array_base_;
1555 const int32_t len_offset_;
1556 };
1557
1558 NewLIR3(IS_SIMM8(index) ? kX86Cmp32MI8 : kX86Cmp32MI, array_base.GetReg(), len_offset, index);
Dave Allison69dfe512014-07-11 17:11:58 +00001559 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001560 LIR* branch = OpCondBranch(kCondLs, nullptr);
1561 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1562 index, array_base, len_offset));
1563}
1564
Brian Carlstrom7940e442013-07-12 13:46:57 -07001565// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001566LIR* X86Mir2Lir::OpTestSuspend(LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -07001567 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001568 OpTlsCmp(Thread::ThreadFlagsOffset<8>(), 0);
1569 } else {
1570 OpTlsCmp(Thread::ThreadFlagsOffset<4>(), 0);
1571 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001572 return OpCondBranch((target == nullptr) ? kCondNe : kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573}
1574
1575// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001576LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 OpRegImm(kOpSub, reg, 1);
Yixin Shoua0dac3e2014-01-23 05:01:22 -08001578 return OpCondBranch(c_code, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579}
1580
buzbee11b63d12013-08-27 07:34:17 -07001581bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001582 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001583 UNUSED(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 LOG(FATAL) << "Unexpected use of smallLiteralDive in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001585 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001586}
1587
Ian Rogerse2143c02014-03-28 08:47:16 -07001588bool X86Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001589 UNUSED(rl_src, rl_dest, lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001590 LOG(FATAL) << "Unexpected use of easyMultiply in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001591 UNREACHABLE();
Ian Rogerse2143c02014-03-28 08:47:16 -07001592}
1593
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001594LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001595 UNUSED(cond, guide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 LOG(FATAL) << "Unexpected use of OpIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001597 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001598}
1599
Dave Allison3da67a52014-04-02 17:03:45 -07001600void X86Mir2Lir::OpEndIT(LIR* it) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001601 UNUSED(it);
Dave Allison3da67a52014-04-02 17:03:45 -07001602 LOG(FATAL) << "Unexpected use of OpEndIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001603 UNREACHABLE();
Dave Allison3da67a52014-04-02 17:03:45 -07001604}
1605
buzbee2700f7e2014-03-07 09:46:20 -08001606void X86Mir2Lir::GenImulRegImm(RegStorage dest, RegStorage src, int val) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001607 switch (val) {
1608 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001609 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001610 break;
1611 case 1:
1612 OpRegCopy(dest, src);
1613 break;
1614 default:
1615 OpRegRegImm(kOpMul, dest, src, val);
1616 break;
1617 }
1618}
1619
buzbee2700f7e2014-03-07 09:46:20 -08001620void X86Mir2Lir::GenImulMemImm(RegStorage dest, int sreg, int displacement, int val) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001621 UNUSED(sreg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001622 // All memory accesses below reference dalvik regs.
1623 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1624
Mark Mendell4708dcd2014-01-22 09:05:18 -08001625 LIR *m;
1626 switch (val) {
1627 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001628 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001629 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001630 case 1: {
1631 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
1632 LoadBaseDisp(rs_rSP, displacement, dest, k32, kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001633 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001634 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001635 default:
buzbee091cc402014-03-31 10:14:40 -07001636 m = NewLIR4(IS_SIMM8(val) ? kX86Imul32RMI8 : kX86Imul32RMI, dest.GetReg(),
Ian Rogersb28c1c02014-11-08 11:21:21 -08001637 rs_rX86_SP_32.GetReg(), displacement, val);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001638 AnnotateDalvikRegAccess(m, displacement >> 2, true /* is_load */, true /* is_64bit */);
1639 break;
1640 }
1641}
1642
Andreas Gampec76c6142014-08-04 16:30:03 -07001643void X86Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001644 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001645 if (!cu_->target64) {
1646 // Some x86 32b ops are fallback.
1647 switch (opcode) {
1648 case Instruction::NOT_LONG:
1649 case Instruction::DIV_LONG:
1650 case Instruction::DIV_LONG_2ADDR:
1651 case Instruction::REM_LONG:
1652 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001653 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001654 return;
1655
1656 default:
1657 // Everything else we can handle.
1658 break;
1659 }
1660 }
1661
1662 switch (opcode) {
1663 case Instruction::NOT_LONG:
1664 GenNotLong(rl_dest, rl_src2);
1665 return;
1666
1667 case Instruction::ADD_LONG:
1668 case Instruction::ADD_LONG_2ADDR:
1669 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1670 return;
1671
1672 case Instruction::SUB_LONG:
1673 case Instruction::SUB_LONG_2ADDR:
1674 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, false);
1675 return;
1676
1677 case Instruction::MUL_LONG:
1678 case Instruction::MUL_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001679 GenMulLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001680 return;
1681
1682 case Instruction::DIV_LONG:
1683 case Instruction::DIV_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001684 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001685 return;
1686
1687 case Instruction::REM_LONG:
1688 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001689 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001690 return;
1691
1692 case Instruction::AND_LONG_2ADDR:
1693 case Instruction::AND_LONG:
1694 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1695 return;
1696
1697 case Instruction::OR_LONG:
1698 case Instruction::OR_LONG_2ADDR:
1699 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1700 return;
1701
1702 case Instruction::XOR_LONG:
1703 case Instruction::XOR_LONG_2ADDR:
1704 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1705 return;
1706
1707 case Instruction::NEG_LONG:
1708 GenNegLong(rl_dest, rl_src2);
1709 return;
1710
1711 default:
1712 LOG(FATAL) << "Invalid long arith op";
1713 return;
1714 }
1715}
1716
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001717bool X86Mir2Lir::GenMulLongConst(RegLocation rl_dest, RegLocation rl_src1, int64_t val, int flags) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001718 // All memory accesses below reference dalvik regs.
1719 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1720
Andreas Gampec76c6142014-08-04 16:30:03 -07001721 if (val == 0) {
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001722 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampec76c6142014-08-04 16:30:03 -07001723 if (cu_->target64) {
1724 OpRegReg(kOpXor, rl_result.reg, rl_result.reg);
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001725 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001726 OpRegReg(kOpXor, rl_result.reg.GetLow(), rl_result.reg.GetLow());
1727 OpRegReg(kOpXor, rl_result.reg.GetHigh(), rl_result.reg.GetHigh());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001728 }
Andreas Gampec76c6142014-08-04 16:30:03 -07001729 StoreValueWide(rl_dest, rl_result);
1730 return true;
1731 } else if (val == 1) {
1732 StoreValueWide(rl_dest, rl_src1);
1733 return true;
1734 } else if (val == 2) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001735 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src1, rl_src1, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001736 return true;
1737 } else if (IsPowerOfTwo(val)) {
Andreas Gampe7e499922015-01-06 08:28:12 -08001738 int shift_amount = CTZ(val);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001739 if (!PartiallyIntersects(rl_src1, rl_dest)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001740 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1741 RegLocation rl_result = GenShiftImmOpLong(Instruction::SHL_LONG, rl_dest, rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001742 shift_amount, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001743 StoreValueWide(rl_dest, rl_result);
1744 return true;
1745 }
1746 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001747
Andreas Gampec76c6142014-08-04 16:30:03 -07001748 // Okay, on 32b just bite the bullet and do it, still better than the general case.
1749 if (!cu_->target64) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001750 int32_t val_lo = Low32Bits(val);
1751 int32_t val_hi = High32Bits(val);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001752 // Prepare for explicit register usage.
1753 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001754 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001755 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1756 int displacement = SRegOffset(rl_src1.s_reg_low);
1757
1758 // ECX <- 1H * 2L
1759 // EAX <- 1L * 2H
1760 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001761 GenImulRegImm(rs_r1, rl_src1.reg.GetHigh(), val_lo);
1762 GenImulRegImm(rs_r0, rl_src1.reg.GetLow(), val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001763 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001764 GenImulMemImm(rs_r1, GetSRegHi(rl_src1.s_reg_low), displacement + HIWORD_OFFSET, val_lo);
1765 GenImulMemImm(rs_r0, rl_src1.s_reg_low, displacement + LOWORD_OFFSET, val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001766 }
1767
1768 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001769 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001770
1771 // EAX <- 2L
buzbee2700f7e2014-03-07 09:46:20 -08001772 LoadConstantNoClobber(rs_r0, val_lo);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001773
1774 // EDX:EAX <- 2L * 1L (double precision)
1775 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001776 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001777 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001778 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001779 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1780 true /* is_load */, true /* is_64bit */);
1781 }
1782
1783 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001784 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001785
1786 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001787 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
1788 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001789 StoreValueWide(rl_dest, rl_result);
Andreas Gampec76c6142014-08-04 16:30:03 -07001790 return true;
1791 }
1792 return false;
1793}
1794
1795void X86Mir2Lir::GenMulLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001796 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001797 if (rl_src1.is_const) {
1798 std::swap(rl_src1, rl_src2);
1799 }
1800
1801 if (rl_src2.is_const) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001802 if (GenMulLongConst(rl_dest, rl_src1, mir_graph_->ConstantValueWide(rl_src2), flags)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001803 return;
1804 }
1805 }
1806
1807 // All memory accesses below reference dalvik regs.
1808 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1809
1810 if (cu_->target64) {
1811 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1812 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1813 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1814 if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1815 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1816 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
1817 } else if (rl_result.reg.GetReg() != rl_src1.reg.GetReg() &&
1818 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1819 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
1820 } else if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1821 rl_result.reg.GetReg() != rl_src2.reg.GetReg()) {
1822 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1823 } else {
1824 OpRegCopy(rl_result.reg, rl_src1.reg);
1825 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1826 }
1827 StoreValueWide(rl_dest, rl_result);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001828 return;
1829 }
1830
Andreas Gampec76c6142014-08-04 16:30:03 -07001831 // Not multiplying by a constant. Do it the hard way
Mark Mendellde99bba2014-02-14 12:15:02 -08001832 // Check for V*V. We can eliminate a multiply in that case, as 2L*1H == 2H*1L.
1833 bool is_square = mir_graph_->SRegToVReg(rl_src1.s_reg_low) ==
1834 mir_graph_->SRegToVReg(rl_src2.s_reg_low);
1835
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001836 // Prepare for explicit register usage.
1837 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001838 rl_src1 = UpdateLocWideTyped(rl_src1);
1839 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001840
1841 // At this point, the VRs are in their home locations.
1842 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1843 bool src2_in_reg = rl_src2.location == kLocPhysReg;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001844 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001845
1846 // ECX <- 1H
1847 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001848 NewLIR2(kX86Mov32RR, rs_r1.GetReg(), rl_src1.reg.GetHighReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001849 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001850 LoadBaseDisp(rs_rSP, SRegOffset(rl_src1.s_reg_low) + HIWORD_OFFSET, rs_r1, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001851 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001852 }
1853
Mark Mendellde99bba2014-02-14 12:15:02 -08001854 if (is_square) {
1855 // Take advantage of the fact that the values are the same.
1856 // ECX <- ECX * 2L (1H * 2L)
1857 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001858 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001859 } else {
1860 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001861 LIR* m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001862 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001863 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1864 true /* is_load */, true /* is_64bit */);
1865 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001866
Mark Mendellde99bba2014-02-14 12:15:02 -08001867 // ECX <- 2*ECX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001868 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001869 } else {
Mark Mendellde99bba2014-02-14 12:15:02 -08001870 // EAX <- 2H
1871 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001872 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetHighReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001873 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001874 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + HIWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001875 kNotVolatile);
Mark Mendellde99bba2014-02-14 12:15:02 -08001876 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001877
Mark Mendellde99bba2014-02-14 12:15:02 -08001878 // EAX <- EAX * 1L (2H * 1L)
1879 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001880 NewLIR2(kX86Imul32RR, rs_r0.GetReg(), rl_src1.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001881 } else {
1882 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001883 LIR *m = NewLIR3(kX86Imul32RM, rs_r0.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001884 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001885 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1886 true /* is_load */, true /* is_64bit */);
1887 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001888
Mark Mendellde99bba2014-02-14 12:15:02 -08001889 // ECX <- ECX * 2L (1H * 2L)
1890 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001891 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001892 } else {
1893 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001894 LIR *m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001895 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001896 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1897 true /* is_load */, true /* is_64bit */);
1898 }
1899
1900 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001901 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001902 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001903
1904 // EAX <- 2L
1905 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001906 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001907 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001908 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + LOWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001909 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001910 }
1911
1912 // EDX:EAX <- 2L * 1L (double precision)
1913 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001914 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001915 } else {
1916 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001917 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001918 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1919 true /* is_load */, true /* is_64bit */);
1920 }
1921
1922 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001923 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001924
1925 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001926 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
buzbee2700f7e2014-03-07 09:46:20 -08001927 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001928 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001929}
Mark Mendelle02d48f2014-01-15 11:19:23 -08001930
1931void X86Mir2Lir::GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src,
1932 Instruction::Code op) {
1933 DCHECK_EQ(rl_dest.location, kLocPhysReg);
1934 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
1935 if (rl_src.location == kLocPhysReg) {
1936 // Both operands are in registers.
Serguei Katkovab5545f2014-03-25 10:51:15 +07001937 // But we must ensure that rl_src is in pair
Elena Sayapinadd644502014-07-01 18:39:52 +07001938 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001939 NewLIR2(x86op, rl_dest.reg.GetReg(), rl_src.reg.GetReg());
1940 } else {
1941 rl_src = LoadValueWide(rl_src, kCoreReg);
1942 if (rl_dest.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1943 // The registers are the same, so we would clobber it before the use.
1944 RegStorage temp_reg = AllocTemp();
1945 OpRegCopy(temp_reg, rl_dest.reg);
1946 rl_src.reg.SetHighReg(temp_reg.GetReg());
1947 }
1948 NewLIR2(x86op, rl_dest.reg.GetLowReg(), rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08001949
Chao-ying Fua0147762014-06-06 18:38:49 -07001950 x86op = GetOpcode(op, rl_dest, rl_src, true);
1951 NewLIR2(x86op, rl_dest.reg.GetHighReg(), rl_src.reg.GetHighReg());
Chao-ying Fua0147762014-06-06 18:38:49 -07001952 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08001953 return;
1954 }
1955
1956 // RHS is in memory.
1957 DCHECK((rl_src.location == kLocDalvikFrame) ||
1958 (rl_src.location == kLocCompilerTemp));
Ian Rogersb28c1c02014-11-08 11:21:21 -08001959 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08001960 int displacement = SRegOffset(rl_src.s_reg_low);
1961
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001962 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -07001963 LIR *lir = NewLIR3(x86op, cu_->target64 ? rl_dest.reg.GetReg() : rl_dest.reg.GetLowReg(),
1964 r_base, displacement + LOWORD_OFFSET);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001965 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
1966 true /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07001967 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001968 x86op = GetOpcode(op, rl_dest, rl_src, true);
1969 lir = NewLIR3(x86op, rl_dest.reg.GetHighReg(), r_base, displacement + HIWORD_OFFSET);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001970 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1971 true /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07001972 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973}
1974
Mark Mendelle02d48f2014-01-15 11:19:23 -08001975void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001976 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001977 if (rl_dest.location == kLocPhysReg) {
1978 // Ensure we are in a register pair
1979 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1980
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001981 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001982 GenLongRegOrMemOp(rl_result, rl_src, op);
1983 StoreFinalValueWide(rl_dest, rl_result);
1984 return;
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001985 } else if (!cu_->target64 && Intersects(rl_src, rl_dest)) {
1986 // Handle the case when src and dest are intersect.
1987 rl_src = LoadValueWide(rl_src, kCoreReg);
1988 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001989 rl_src = UpdateLocWideTyped(rl_src);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001990 GenLongRegOrMemOp(rl_result, rl_src, op);
1991 StoreFinalValueWide(rl_dest, rl_result);
1992 return;
Mark Mendelle02d48f2014-01-15 11:19:23 -08001993 }
1994
1995 // It wasn't in registers, so it better be in memory.
1996 DCHECK((rl_dest.location == kLocDalvikFrame) ||
1997 (rl_dest.location == kLocCompilerTemp));
1998 rl_src = LoadValueWide(rl_src, kCoreReg);
1999
2000 // Operate directly into memory.
2001 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
Ian Rogersb28c1c02014-11-08 11:21:21 -08002002 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002003 int displacement = SRegOffset(rl_dest.s_reg_low);
2004
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002005 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002006 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET,
Elena Sayapinadd644502014-07-01 18:39:52 +07002007 cu_->target64 ? rl_src.reg.GetReg() : rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002008 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002009 true /* is_load */, true /* is64bit */);
2010 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002011 false /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07002012 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002013 x86op = GetOpcode(op, rl_dest, rl_src, true);
2014 lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, rl_src.reg.GetHighReg());
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002015 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
2016 true /* is_load */, true /* is64bit */);
2017 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
2018 false /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07002019 }
nikolay serdjuk6b9356c2014-11-13 18:15:23 +06002020
2021 int v_src_reg = mir_graph_->SRegToVReg(rl_src.s_reg_low);
2022 int v_dst_reg = mir_graph_->SRegToVReg(rl_dest.s_reg_low);
2023
2024 // If the left operand is in memory and the right operand is in a register
2025 // and both belong to the same dalvik register then we should clobber the
2026 // right one because it doesn't hold valid data anymore.
2027 if (v_src_reg == v_dst_reg) {
2028 Clobber(rl_src.reg);
2029 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030}
2031
Mark Mendelle02d48f2014-01-15 11:19:23 -08002032void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src1,
2033 RegLocation rl_src2, Instruction::Code op,
2034 bool is_commutative) {
2035 // Is this really a 2 operand operation?
2036 switch (op) {
2037 case Instruction::ADD_LONG_2ADDR:
2038 case Instruction::SUB_LONG_2ADDR:
2039 case Instruction::AND_LONG_2ADDR:
2040 case Instruction::OR_LONG_2ADDR:
2041 case Instruction::XOR_LONG_2ADDR:
Mark Mendelle87f9b52014-04-30 14:13:18 -04002042 if (GenerateTwoOperandInstructions()) {
2043 GenLongArith(rl_dest, rl_src2, op);
2044 return;
2045 }
2046 break;
2047
Mark Mendelle02d48f2014-01-15 11:19:23 -08002048 default:
2049 break;
2050 }
2051
2052 if (rl_dest.location == kLocPhysReg) {
2053 RegLocation rl_result = LoadValueWide(rl_src1, kCoreReg);
2054
2055 // We are about to clobber the LHS, so it needs to be a temp.
2056 rl_result = ForceTempWide(rl_result);
2057
2058 // Perform the operation using the RHS.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002059 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002060 GenLongRegOrMemOp(rl_result, rl_src2, op);
2061
2062 // And now record that the result is in the temp.
2063 StoreFinalValueWide(rl_dest, rl_result);
2064 return;
2065 }
2066
2067 // It wasn't in registers, so it better be in memory.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002068 DCHECK((rl_dest.location == kLocDalvikFrame) || (rl_dest.location == kLocCompilerTemp));
2069 rl_src1 = UpdateLocWideTyped(rl_src1);
2070 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002071
2072 // Get one of the source operands into temporary register.
2073 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +07002074 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002075 if (IsTemp(rl_src1.reg)) {
2076 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2077 } else if (is_commutative) {
2078 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2079 // We need at least one of them to be a temporary.
2080 if (!IsTemp(rl_src2.reg)) {
2081 rl_src1 = ForceTempWide(rl_src1);
2082 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2083 } else {
2084 GenLongRegOrMemOp(rl_src2, rl_src1, op);
2085 StoreFinalValueWide(rl_dest, rl_src2);
2086 return;
2087 }
2088 } else {
2089 // Need LHS to be the temp.
Mark Mendelle02d48f2014-01-15 11:19:23 -08002090 rl_src1 = ForceTempWide(rl_src1);
Yevgeny Rouban91b6ffa2014-03-07 14:35:44 +07002091 GenLongRegOrMemOp(rl_src1, rl_src2, op);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002092 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002093 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002094 if (IsTemp(rl_src1.reg.GetLow()) && IsTemp(rl_src1.reg.GetHigh())) {
2095 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2096 } else if (is_commutative) {
2097 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2098 // We need at least one of them to be a temporary.
2099 if (!(IsTemp(rl_src2.reg.GetLow()) && IsTemp(rl_src2.reg.GetHigh()))) {
2100 rl_src1 = ForceTempWide(rl_src1);
2101 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2102 } else {
2103 GenLongRegOrMemOp(rl_src2, rl_src1, op);
2104 StoreFinalValueWide(rl_dest, rl_src2);
2105 return;
2106 }
2107 } else {
2108 // Need LHS to be the temp.
2109 rl_src1 = ForceTempWide(rl_src1);
2110 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2111 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002112 }
2113
2114 StoreFinalValueWide(rl_dest, rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002115}
2116
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002117void X86Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002118 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002119 rl_src = LoadValueWide(rl_src, kCoreReg);
2120 RegLocation rl_result;
2121 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2122 OpRegCopy(rl_result.reg, rl_src.reg);
2123 OpReg(kOpNot, rl_result.reg);
2124 StoreValueWide(rl_dest, rl_result);
2125 } else {
2126 LOG(FATAL) << "Unexpected use GenNotLong()";
2127 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002128}
2129
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002130void X86Mir2Lir::GenDivRemLongLit(RegLocation rl_dest, RegLocation rl_src,
2131 int64_t imm, bool is_div) {
2132 if (imm == 0) {
2133 GenDivZeroException();
2134 } else if (imm == 1) {
2135 if (is_div) {
2136 // x / 1 == x.
2137 StoreValueWide(rl_dest, rl_src);
2138 } else {
2139 // x % 1 == 0.
2140 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2141 LoadConstantWide(rl_result.reg, 0);
2142 StoreValueWide(rl_dest, rl_result);
2143 }
2144 } else if (imm == -1) { // handle 0x8000000000000000 / -1 special case.
2145 if (is_div) {
2146 rl_src = LoadValueWide(rl_src, kCoreReg);
2147 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2148 RegStorage rs_temp = AllocTempWide();
2149
2150 OpRegCopy(rl_result.reg, rl_src.reg);
2151 LoadConstantWide(rs_temp, 0x8000000000000000);
2152
2153 // If x == MIN_LONG, return MIN_LONG.
2154 OpRegReg(kOpCmp, rl_src.reg, rs_temp);
2155 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
2156
2157 // For x != MIN_LONG, x / -1 == -x.
2158 OpReg(kOpNeg, rl_result.reg);
2159
2160 minint_branch->target = NewLIR0(kPseudoTargetLabel);
2161 FreeTemp(rs_temp);
2162 StoreValueWide(rl_dest, rl_result);
2163 } else {
2164 // x % -1 == 0.
2165 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2166 LoadConstantWide(rl_result.reg, 0);
2167 StoreValueWide(rl_dest, rl_result);
2168 }
2169 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
2170 // Division using shifting.
2171 rl_src = LoadValueWide(rl_src, kCoreReg);
2172 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2173 if (IsSameReg(rl_result.reg, rl_src.reg)) {
2174 RegStorage rs_temp = AllocTypedTempWide(false, kCoreReg);
2175 rl_result.reg.SetReg(rs_temp.GetReg());
2176 }
2177 LoadConstantWide(rl_result.reg, std::abs(imm) - 1);
2178 OpRegReg(kOpAdd, rl_result.reg, rl_src.reg);
2179 NewLIR2(kX86Test64RR, rl_src.reg.GetReg(), rl_src.reg.GetReg());
2180 OpCondRegReg(kOpCmov, kCondPl, rl_result.reg, rl_src.reg);
Andreas Gampe7e499922015-01-06 08:28:12 -08002181 int shift_amount = CTZ(imm);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002182 OpRegImm(kOpAsr, rl_result.reg, shift_amount);
2183 if (imm < 0) {
2184 OpReg(kOpNeg, rl_result.reg);
2185 }
2186 StoreValueWide(rl_dest, rl_result);
2187 } else {
2188 CHECK(imm <= -2 || imm >= 2);
2189
2190 FlushReg(rs_r0q);
2191 Clobber(rs_r0q);
2192 LockTemp(rs_r0q);
2193 FlushReg(rs_r2q);
2194 Clobber(rs_r2q);
2195 LockTemp(rs_r2q);
2196
Mark Mendell3a91f442014-09-02 12:44:24 -04002197 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
2198 is_div ? rs_r2q : rs_r0q, INVALID_SREG, INVALID_SREG};
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002199
2200 // Use H.S.Warren's Hacker's Delight Chapter 10 and
2201 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
2202 int64_t magic;
2203 int shift;
2204 CalculateMagicAndShift(imm, magic, shift, true /* is_long */);
2205
2206 /*
2207 * For imm >= 2,
2208 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
2209 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
2210 * For imm <= -2,
2211 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
2212 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
2213 * We implement this algorithm in the following way:
2214 * 1. multiply magic number m and numerator n, get the higher 64bit result in RDX
2215 * 2. if imm > 0 and magic < 0, add numerator to RDX
2216 * if imm < 0 and magic > 0, sub numerator from RDX
2217 * 3. if S !=0, SAR S bits for RDX
2218 * 4. add 1 to RDX if RDX < 0
2219 * 5. Thus, RDX is the quotient
2220 */
2221
Mark Mendell3a91f442014-09-02 12:44:24 -04002222 // RAX = magic.
2223 LoadConstantWide(rs_r0q, magic);
2224
2225 // Multiply by numerator.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002226 RegStorage numerator_reg;
2227 if (!is_div || (imm > 0 && magic < 0) || (imm < 0 && magic > 0)) {
2228 // We will need the value later.
2229 rl_src = LoadValueWide(rl_src, kCoreReg);
2230 numerator_reg = rl_src.reg;
Mark Mendell3a91f442014-09-02 12:44:24 -04002231
2232 // RDX:RAX = magic * numerator.
2233 NewLIR1(kX86Imul64DaR, numerator_reg.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002234 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002235 // Only need this once. Multiply directly from the value.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002236 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendell3a91f442014-09-02 12:44:24 -04002237 if (rl_src.location != kLocPhysReg) {
2238 // Okay, we can do this from memory.
2239 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2240 int displacement = SRegOffset(rl_src.s_reg_low);
2241 // RDX:RAX = magic * numerator.
Ian Rogersb28c1c02014-11-08 11:21:21 -08002242 LIR *m = NewLIR2(kX86Imul64DaM, rs_rX86_SP_32.GetReg(), displacement);
Mark Mendell3a91f442014-09-02 12:44:24 -04002243 AnnotateDalvikRegAccess(m, displacement >> 2,
2244 true /* is_load */, true /* is_64bit */);
2245 } else {
2246 // RDX:RAX = magic * numerator.
2247 NewLIR1(kX86Imul64DaR, rl_src.reg.GetReg());
2248 }
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002249 }
2250
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002251 if (imm > 0 && magic < 0) {
2252 // Add numerator to RDX.
2253 DCHECK(numerator_reg.Valid());
2254 OpRegReg(kOpAdd, rs_r2q, numerator_reg);
2255 } else if (imm < 0 && magic > 0) {
2256 DCHECK(numerator_reg.Valid());
2257 OpRegReg(kOpSub, rs_r2q, numerator_reg);
2258 }
2259
2260 // Do we need the shift?
2261 if (shift != 0) {
2262 // Shift RDX by 'shift' bits.
2263 OpRegImm(kOpAsr, rs_r2q, shift);
2264 }
2265
2266 // Move RDX to RAX.
2267 OpRegCopyWide(rs_r0q, rs_r2q);
2268
2269 // Move sign bit to bit 0, zeroing the rest.
2270 OpRegImm(kOpLsr, rs_r2q, 63);
2271
2272 // RDX = RDX + RAX.
2273 OpRegReg(kOpAdd, rs_r2q, rs_r0q);
2274
2275 // Quotient is in RDX.
2276 if (!is_div) {
2277 // We need to compute the remainder.
2278 // Remainder is divisor - (quotient * imm).
2279 DCHECK(numerator_reg.Valid());
2280 OpRegCopyWide(rs_r0q, numerator_reg);
2281
2282 // Imul doesn't support 64-bit imms.
2283 if (imm > std::numeric_limits<int32_t>::max() ||
2284 imm < std::numeric_limits<int32_t>::min()) {
2285 RegStorage rs_temp = AllocTempWide();
2286 LoadConstantWide(rs_temp, imm);
2287
2288 // RAX = numerator * imm.
2289 NewLIR2(kX86Imul64RR, rs_r2q.GetReg(), rs_temp.GetReg());
2290
2291 FreeTemp(rs_temp);
2292 } else {
2293 // RAX = numerator * imm.
2294 int short_imm = static_cast<int>(imm);
2295 NewLIR3(kX86Imul64RRI, rs_r2q.GetReg(), rs_r2q.GetReg(), short_imm);
2296 }
2297
Mark Mendell3a91f442014-09-02 12:44:24 -04002298 // RAX -= RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002299 OpRegReg(kOpSub, rs_r0q, rs_r2q);
2300
Mark Mendell3a91f442014-09-02 12:44:24 -04002301 // Result in RAX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002302 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002303 // Result in RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002304 }
2305 StoreValueWide(rl_dest, rl_result);
2306 FreeTemp(rs_r0q);
2307 FreeTemp(rs_r2q);
2308 }
2309}
2310
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002311void X86Mir2Lir::GenDivRemLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002312 RegLocation rl_src2, bool is_div, int flags) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002313 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002314 LOG(FATAL) << "Unexpected use GenDivRemLong()";
2315 return;
2316 }
2317
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002318 if (rl_src2.is_const) {
2319 DCHECK(rl_src2.wide);
2320 int64_t imm = mir_graph_->ConstantValueWide(rl_src2);
2321 GenDivRemLongLit(rl_dest, rl_src1, imm, is_div);
2322 return;
2323 }
2324
Chao-ying Fua0147762014-06-06 18:38:49 -07002325 // We have to use fixed registers, so flush all the temps.
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002326 // Prepare for explicit register usage.
2327 ExplicitTempRegisterLock(this, 4, &rs_r0q, &rs_r1q, &rs_r2q, &rs_r6q);
Chao-ying Fua0147762014-06-06 18:38:49 -07002328
2329 // Load LHS into RAX.
2330 LoadValueDirectWideFixed(rl_src1, rs_r0q);
2331
2332 // Load RHS into RCX.
2333 LoadValueDirectWideFixed(rl_src2, rs_r1q);
2334
2335 // Copy LHS sign bit into RDX.
2336 NewLIR0(kx86Cqo64Da);
2337
2338 // Handle division by zero case.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002339 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2340 GenDivZeroCheckWide(rs_r1q);
2341 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002342
2343 // Have to catch 0x8000000000000000/-1 case, or we will get an exception!
2344 NewLIR2(kX86Cmp64RI8, rs_r1q.GetReg(), -1);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002345 LIR* minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002346
2347 // RHS is -1.
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002348 LoadConstantWide(rs_r6q, 0x8000000000000000);
2349 NewLIR2(kX86Cmp64RR, rs_r0q.GetReg(), rs_r6q.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002350 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002351
2352 // In 0x8000000000000000/-1 case.
2353 if (!is_div) {
2354 // For DIV, RAX is already right. For REM, we need RDX 0.
2355 NewLIR2(kX86Xor64RR, rs_r2q.GetReg(), rs_r2q.GetReg());
2356 }
2357 LIR* done = NewLIR1(kX86Jmp8, 0);
2358
2359 // Expected case.
2360 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
2361 minint_branch->target = minus_one_branch->target;
2362 NewLIR1(kX86Idivmod64DaR, rs_r1q.GetReg());
2363 done->target = NewLIR0(kPseudoTargetLabel);
2364
2365 // Result is in RAX for div and RDX for rem.
2366 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, rs_r0q, INVALID_SREG, INVALID_SREG};
2367 if (!is_div) {
2368 rl_result.reg.SetReg(r2q);
2369 }
2370
2371 StoreValueWide(rl_dest, rl_result);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002372}
2373
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002374void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002375 rl_src = LoadValueWide(rl_src, kCoreReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002376 RegLocation rl_result;
Elena Sayapinadd644502014-07-01 18:39:52 +07002377 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002378 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2379 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
2380 } else {
2381 rl_result = ForceTempWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002382 OpRegReg(kOpNeg, rl_result.reg.GetLow(), rl_result.reg.GetLow()); // rLow = -rLow
2383 OpRegImm(kOpAdc, rl_result.reg.GetHigh(), 0); // rHigh = rHigh + CF
2384 OpRegReg(kOpNeg, rl_result.reg.GetHigh(), rl_result.reg.GetHigh()); // rHigh = -rHigh
Mark Mendelle02d48f2014-01-15 11:19:23 -08002385 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002386 StoreValueWide(rl_dest, rl_result);
2387}
2388
buzbee091cc402014-03-31 10:14:40 -07002389void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<4> thread_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002390 DCHECK_EQ(kX86, cu_->instruction_set);
2391 X86OpCode opcode = kX86Bkpt;
2392 switch (op) {
2393 case kOpCmp: opcode = kX86Cmp32RT; break;
2394 case kOpMov: opcode = kX86Mov32RT; break;
2395 default:
2396 LOG(FATAL) << "Bad opcode: " << op;
2397 break;
2398 }
2399 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
2400}
2401
2402void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<8> thread_offset) {
2403 DCHECK_EQ(kX86_64, cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002404 X86OpCode opcode = kX86Bkpt;
Elena Sayapinadd644502014-07-01 18:39:52 +07002405 if (cu_->target64 && r_dest.Is64BitSolo()) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07002406 switch (op) {
2407 case kOpCmp: opcode = kX86Cmp64RT; break;
2408 case kOpMov: opcode = kX86Mov64RT; break;
2409 default:
2410 LOG(FATAL) << "Bad opcode(OpRegThreadMem 64): " << op;
2411 break;
2412 }
2413 } else {
2414 switch (op) {
2415 case kOpCmp: opcode = kX86Cmp32RT; break;
2416 case kOpMov: opcode = kX86Mov32RT; break;
2417 default:
2418 LOG(FATAL) << "Bad opcode: " << op;
2419 break;
2420 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002421 }
buzbee091cc402014-03-31 10:14:40 -07002422 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002423}
2424
2425/*
2426 * Generate array load
2427 */
2428void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002429 RegLocation rl_index, RegLocation rl_dest, int scale) {
Mark Mendellca541342014-10-15 16:59:49 -04002430 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002431 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002432 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002433 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002434
Mark Mendell343adb52013-12-18 06:02:17 -08002435 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07002436 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002437 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2438 } else {
2439 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2440 }
2441
Mark Mendell343adb52013-12-18 06:02:17 -08002442 bool constant_index = rl_index.is_const;
2443 int32_t constant_index_value = 0;
2444 if (!constant_index) {
2445 rl_index = LoadValue(rl_index, kCoreReg);
2446 } else {
2447 constant_index_value = mir_graph_->ConstantValue(rl_index);
2448 // If index is constant, just fold it into the data offset
2449 data_offset += constant_index_value << scale;
2450 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002451 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002452 }
2453
Brian Carlstrom7940e442013-07-12 13:46:57 -07002454 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002455 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002456
2457 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002458 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002459 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002460 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002461 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002462 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002463 }
Mark Mendell343adb52013-12-18 06:02:17 -08002464 rl_result = EvalLoc(rl_dest, reg_class, true);
Vladimir Marko3bf7c602014-05-07 14:55:43 +01002465 LoadBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_result.reg, size);
buzbee695d13a2014-04-19 13:32:20 -07002466 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002467 StoreValueWide(rl_dest, rl_result);
2468 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002469 StoreValue(rl_dest, rl_result);
2470 }
2471}
2472
2473/*
2474 * Generate array store
2475 *
2476 */
2477void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002478 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Mark Mendellca541342014-10-15 16:59:49 -04002479 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002480 int len_offset = mirror::Array::LengthOffset().Int32Value();
2481 int data_offset;
2482
buzbee695d13a2014-04-19 13:32:20 -07002483 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002484 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2485 } else {
2486 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2487 }
2488
buzbeea0cd2d72014-06-01 09:33:49 -07002489 rl_array = LoadValue(rl_array, kRefReg);
Mark Mendell343adb52013-12-18 06:02:17 -08002490 bool constant_index = rl_index.is_const;
2491 int32_t constant_index_value = 0;
2492 if (!constant_index) {
2493 rl_index = LoadValue(rl_index, kCoreReg);
2494 } else {
2495 // If index is constant, just fold it into the data offset
2496 constant_index_value = mir_graph_->ConstantValue(rl_index);
2497 data_offset += constant_index_value << scale;
2498 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002499 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002500 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002501
2502 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002503 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002504
2505 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002506 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002507 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002508 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002509 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002510 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002511 }
buzbee695d13a2014-04-19 13:32:20 -07002512 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002513 rl_src = LoadValueWide(rl_src, reg_class);
2514 } else {
2515 rl_src = LoadValue(rl_src, reg_class);
2516 }
2517 // If the src reg can't be byte accessed, move it to a temp first.
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002518 if ((size == kSignedByte || size == kUnsignedByte) && !IsByteRegister(rl_src.reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08002519 RegStorage temp = AllocTemp();
2520 OpRegCopy(temp, rl_src.reg);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002521 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, temp, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002522 } else {
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002523 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_src.reg, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002524 }
Ian Rogersa9a82542013-10-04 11:17:26 -07002525 if (card_mark) {
Ian Rogers773aab12013-10-14 13:50:10 -07002526 // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
Mark Mendell343adb52013-12-18 06:02:17 -08002527 if (!constant_index) {
buzbee091cc402014-03-31 10:14:40 -07002528 FreeTemp(rl_index.reg);
Mark Mendell343adb52013-12-18 06:02:17 -08002529 }
Vladimir Marko743b98c2014-11-24 19:45:41 +00002530 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002531 }
2532}
2533
Mark Mendell4708dcd2014-01-22 09:05:18 -08002534RegLocation X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002535 RegLocation rl_src, int shift_amount, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002536 UNUSED(flags);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002537 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Elena Sayapinadd644502014-07-01 18:39:52 +07002538 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002539 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
2540 switch (opcode) {
2541 case Instruction::SHL_LONG:
2542 case Instruction::SHL_LONG_2ADDR:
2543 op = kOpLsl;
2544 break;
2545 case Instruction::SHR_LONG:
2546 case Instruction::SHR_LONG_2ADDR:
2547 op = kOpAsr;
2548 break;
2549 case Instruction::USHR_LONG:
2550 case Instruction::USHR_LONG_2ADDR:
2551 op = kOpLsr;
2552 break;
2553 default:
2554 LOG(FATAL) << "Unexpected case";
2555 }
2556 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
2557 } else {
2558 switch (opcode) {
2559 case Instruction::SHL_LONG:
2560 case Instruction::SHL_LONG_2ADDR:
2561 DCHECK_NE(shift_amount, 1); // Prevent a double store from happening.
2562 if (shift_amount == 32) {
2563 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2564 LoadConstant(rl_result.reg.GetLow(), 0);
2565 } else if (shift_amount > 31) {
2566 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2567 NewLIR2(kX86Sal32RI, rl_result.reg.GetHighReg(), shift_amount - 32);
2568 LoadConstant(rl_result.reg.GetLow(), 0);
2569 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002570 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002571 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2572 NewLIR3(kX86Shld32RRI, rl_result.reg.GetHighReg(), rl_result.reg.GetLowReg(),
2573 shift_amount);
2574 NewLIR2(kX86Sal32RI, rl_result.reg.GetLowReg(), shift_amount);
2575 }
2576 break;
2577 case Instruction::SHR_LONG:
2578 case Instruction::SHR_LONG_2ADDR:
2579 if (shift_amount == 32) {
2580 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2581 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2582 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2583 } else if (shift_amount > 31) {
2584 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2585 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2586 NewLIR2(kX86Sar32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2587 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2588 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002589 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002590 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2591 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2592 shift_amount);
2593 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), shift_amount);
2594 }
2595 break;
2596 case Instruction::USHR_LONG:
2597 case Instruction::USHR_LONG_2ADDR:
2598 if (shift_amount == 32) {
2599 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2600 LoadConstant(rl_result.reg.GetHigh(), 0);
2601 } else if (shift_amount > 31) {
2602 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2603 NewLIR2(kX86Shr32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2604 LoadConstant(rl_result.reg.GetHigh(), 0);
2605 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002606 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002607 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2608 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2609 shift_amount);
2610 NewLIR2(kX86Shr32RI, rl_result.reg.GetHighReg(), shift_amount);
2611 }
2612 break;
2613 default:
2614 LOG(FATAL) << "Unexpected case";
2615 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08002616 }
2617 return rl_result;
2618}
2619
Brian Carlstrom7940e442013-07-12 13:46:57 -07002620void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002621 RegLocation rl_src, RegLocation rl_shift, int flags) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002622 // Per spec, we only care about low 6 bits of shift amount.
2623 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
2624 if (shift_amount == 0) {
2625 rl_src = LoadValueWide(rl_src, kCoreReg);
2626 StoreValueWide(rl_dest, rl_src);
2627 return;
2628 } else if (shift_amount == 1 &&
2629 (opcode == Instruction::SHL_LONG || opcode == Instruction::SHL_LONG_2ADDR)) {
2630 // Need to handle this here to avoid calling StoreValueWide twice.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002631 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src, rl_src, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002632 return;
2633 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07002634 if (PartiallyIntersects(rl_src, rl_dest)) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002635 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
2636 return;
2637 }
2638 rl_src = LoadValueWide(rl_src, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002639 RegLocation rl_result = GenShiftImmOpLong(opcode, rl_dest, rl_src, shift_amount, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002640 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002641}
2642
2643void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002644 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
2645 int flags) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002646 bool isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002647 switch (opcode) {
2648 case Instruction::ADD_LONG:
2649 case Instruction::AND_LONG:
2650 case Instruction::OR_LONG:
2651 case Instruction::XOR_LONG:
2652 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002653 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002654 } else {
2655 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002656 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002657 }
2658 break;
2659 case Instruction::SUB_LONG:
2660 case Instruction::SUB_LONG_2ADDR:
2661 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002662 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002663 } else {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002664 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002665 isConstSuccess = true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002666 }
2667 break;
2668 case Instruction::ADD_LONG_2ADDR:
2669 case Instruction::OR_LONG_2ADDR:
2670 case Instruction::XOR_LONG_2ADDR:
2671 case Instruction::AND_LONG_2ADDR:
2672 if (rl_src2.is_const) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04002673 if (GenerateTwoOperandInstructions()) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002674 isConstSuccess = GenLongImm(rl_dest, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002675 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002676 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002677 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002678 } else {
2679 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002680 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002681 }
2682 break;
2683 default:
Chao-ying Fua0147762014-06-06 18:38:49 -07002684 isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002685 break;
2686 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002687
2688 if (!isConstSuccess) {
2689 // Default - bail to non-const handler.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002690 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002691 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002692}
2693
2694bool X86Mir2Lir::IsNoOp(Instruction::Code op, int32_t value) {
2695 switch (op) {
2696 case Instruction::AND_LONG_2ADDR:
2697 case Instruction::AND_LONG:
2698 return value == -1;
2699 case Instruction::OR_LONG:
2700 case Instruction::OR_LONG_2ADDR:
2701 case Instruction::XOR_LONG:
2702 case Instruction::XOR_LONG_2ADDR:
2703 return value == 0;
2704 default:
2705 return false;
2706 }
2707}
2708
2709X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
2710 bool is_high_op) {
2711 bool rhs_in_mem = rhs.location != kLocPhysReg;
2712 bool dest_in_mem = dest.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002713 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002714 DCHECK(!rhs_in_mem || !dest_in_mem);
2715 switch (op) {
2716 case Instruction::ADD_LONG:
2717 case Instruction::ADD_LONG_2ADDR:
2718 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002719 return is64Bit ? kX86Add64MR : is_high_op ? kX86Adc32MR : kX86Add32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002720 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002721 return is64Bit ? kX86Add64RM : is_high_op ? kX86Adc32RM : kX86Add32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002722 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002723 return is64Bit ? kX86Add64RR : is_high_op ? kX86Adc32RR : kX86Add32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002724 case Instruction::SUB_LONG:
2725 case Instruction::SUB_LONG_2ADDR:
2726 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002727 return is64Bit ? kX86Sub64MR : is_high_op ? kX86Sbb32MR : kX86Sub32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002728 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002729 return is64Bit ? kX86Sub64RM : is_high_op ? kX86Sbb32RM : kX86Sub32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002730 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002731 return is64Bit ? kX86Sub64RR : is_high_op ? kX86Sbb32RR : kX86Sub32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002732 case Instruction::AND_LONG_2ADDR:
2733 case Instruction::AND_LONG:
2734 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002735 return is64Bit ? kX86And64MR : kX86And32MR;
2736 }
2737 if (is64Bit) {
2738 return rhs_in_mem ? kX86And64RM : kX86And64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002739 }
2740 return rhs_in_mem ? kX86And32RM : kX86And32RR;
2741 case Instruction::OR_LONG:
2742 case Instruction::OR_LONG_2ADDR:
2743 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002744 return is64Bit ? kX86Or64MR : kX86Or32MR;
2745 }
2746 if (is64Bit) {
2747 return rhs_in_mem ? kX86Or64RM : kX86Or64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002748 }
2749 return rhs_in_mem ? kX86Or32RM : kX86Or32RR;
2750 case Instruction::XOR_LONG:
2751 case Instruction::XOR_LONG_2ADDR:
2752 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002753 return is64Bit ? kX86Xor64MR : kX86Xor32MR;
2754 }
2755 if (is64Bit) {
2756 return rhs_in_mem ? kX86Xor64RM : kX86Xor64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002757 }
2758 return rhs_in_mem ? kX86Xor32RM : kX86Xor32RR;
2759 default:
2760 LOG(FATAL) << "Unexpected opcode: " << op;
2761 return kX86Add32RR;
2762 }
2763}
2764
2765X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op,
2766 int32_t value) {
2767 bool in_mem = loc.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002768 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002769 bool byte_imm = IS_SIMM8(value);
buzbee091cc402014-03-31 10:14:40 -07002770 DCHECK(in_mem || !loc.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002771 switch (op) {
2772 case Instruction::ADD_LONG:
2773 case Instruction::ADD_LONG_2ADDR:
2774 if (byte_imm) {
2775 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002776 return is64Bit ? kX86Add64MI8 : is_high_op ? kX86Adc32MI8 : kX86Add32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002777 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002778 return is64Bit ? kX86Add64RI8 : is_high_op ? kX86Adc32RI8 : kX86Add32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002779 }
2780 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002781 return is64Bit ? kX86Add64MI : is_high_op ? kX86Adc32MI : kX86Add32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002782 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002783 return is64Bit ? kX86Add64RI : is_high_op ? kX86Adc32RI : kX86Add32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002784 case Instruction::SUB_LONG:
2785 case Instruction::SUB_LONG_2ADDR:
2786 if (byte_imm) {
2787 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002788 return is64Bit ? kX86Sub64MI8 : is_high_op ? kX86Sbb32MI8 : kX86Sub32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002789 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002790 return is64Bit ? kX86Sub64RI8 : is_high_op ? kX86Sbb32RI8 : kX86Sub32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002791 }
2792 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002793 return is64Bit ? kX86Sub64MI : is_high_op ? kX86Sbb32MI : kX86Sub32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002794 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002795 return is64Bit ? kX86Sub64RI : is_high_op ? kX86Sbb32RI : kX86Sub32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002796 case Instruction::AND_LONG_2ADDR:
2797 case Instruction::AND_LONG:
2798 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002799 if (is64Bit) {
2800 return in_mem ? kX86And64MI8 : kX86And64RI8;
2801 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002802 return in_mem ? kX86And32MI8 : kX86And32RI8;
2803 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002804 if (is64Bit) {
2805 return in_mem ? kX86And64MI : kX86And64RI;
2806 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002807 return in_mem ? kX86And32MI : kX86And32RI;
2808 case Instruction::OR_LONG:
2809 case Instruction::OR_LONG_2ADDR:
2810 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002811 if (is64Bit) {
2812 return in_mem ? kX86Or64MI8 : kX86Or64RI8;
2813 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002814 return in_mem ? kX86Or32MI8 : kX86Or32RI8;
2815 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002816 if (is64Bit) {
2817 return in_mem ? kX86Or64MI : kX86Or64RI;
2818 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002819 return in_mem ? kX86Or32MI : kX86Or32RI;
2820 case Instruction::XOR_LONG:
2821 case Instruction::XOR_LONG_2ADDR:
2822 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002823 if (is64Bit) {
2824 return in_mem ? kX86Xor64MI8 : kX86Xor64RI8;
2825 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002826 return in_mem ? kX86Xor32MI8 : kX86Xor32RI8;
2827 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002828 if (is64Bit) {
2829 return in_mem ? kX86Xor64MI : kX86Xor64RI;
2830 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002831 return in_mem ? kX86Xor32MI : kX86Xor32RI;
2832 default:
2833 LOG(FATAL) << "Unexpected opcode: " << op;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002834 UNREACHABLE();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002835 }
2836}
2837
Chao-ying Fua0147762014-06-06 18:38:49 -07002838bool X86Mir2Lir::GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002839 DCHECK(rl_src.is_const);
2840 int64_t val = mir_graph_->ConstantValueWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002841
Elena Sayapinadd644502014-07-01 18:39:52 +07002842 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002843 // We can do with imm only if it fits 32 bit
2844 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2845 return false;
2846 }
2847
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002848 rl_dest = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002849
2850 if ((rl_dest.location == kLocDalvikFrame) ||
2851 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002852 int r_base = rs_rX86_SP_32.GetReg();
Chao-ying Fua0147762014-06-06 18:38:49 -07002853 int displacement = SRegOffset(rl_dest.s_reg_low);
2854
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002855 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002856 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
2857 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val);
2858 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2859 true /* is_load */, true /* is64bit */);
2860 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2861 false /* is_load */, true /* is64bit */);
2862 return true;
2863 }
2864
2865 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2866 DCHECK_EQ(rl_result.location, kLocPhysReg);
2867 DCHECK(!rl_result.reg.IsFloat());
2868
2869 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2870 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2871
2872 StoreValueWide(rl_dest, rl_result);
2873 return true;
2874 }
2875
Mark Mendelle02d48f2014-01-15 11:19:23 -08002876 int32_t val_lo = Low32Bits(val);
2877 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002878 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002879
2880 // Can we just do this into memory?
2881 if ((rl_dest.location == kLocDalvikFrame) ||
2882 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002883 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002884 int displacement = SRegOffset(rl_dest.s_reg_low);
2885
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002886 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002887 if (!IsNoOp(op, val_lo)) {
2888 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002889 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002890 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002891 true /* is_load */, true /* is64bit */);
2892 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002893 false /* is_load */, true /* is64bit */);
2894 }
2895 if (!IsNoOp(op, val_hi)) {
2896 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
buzbee2700f7e2014-03-07 09:46:20 -08002897 LIR *lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002898 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002899 true /* is_load */, true /* is64bit */);
2900 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002901 false /* is_load */, true /* is64bit */);
2902 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002903 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002904 }
2905
2906 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2907 DCHECK_EQ(rl_result.location, kLocPhysReg);
buzbee091cc402014-03-31 10:14:40 -07002908 DCHECK(!rl_result.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002909
2910 if (!IsNoOp(op, val_lo)) {
2911 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002912 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002913 }
2914 if (!IsNoOp(op, val_hi)) {
2915 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002916 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002917 }
2918 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002919 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002920}
2921
Chao-ying Fua0147762014-06-06 18:38:49 -07002922bool X86Mir2Lir::GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002923 RegLocation rl_src2, Instruction::Code op) {
2924 DCHECK(rl_src2.is_const);
2925 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Chao-ying Fua0147762014-06-06 18:38:49 -07002926
Elena Sayapinadd644502014-07-01 18:39:52 +07002927 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002928 // We can do with imm only if it fits 32 bit
2929 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2930 return false;
2931 }
2932 if (rl_dest.location == kLocPhysReg &&
2933 rl_src1.location == kLocPhysReg && !rl_dest.reg.IsFloat()) {
2934 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002935 OpRegCopy(rl_dest.reg, rl_src1.reg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002936 NewLIR2(x86op, rl_dest.reg.GetReg(), val);
2937 StoreFinalValueWide(rl_dest, rl_dest);
2938 return true;
2939 }
2940
2941 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2942 // We need the values to be in a temporary
2943 RegLocation rl_result = ForceTempWide(rl_src1);
2944
2945 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2946 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2947
2948 StoreFinalValueWide(rl_dest, rl_result);
2949 return true;
2950 }
2951
Mark Mendelle02d48f2014-01-15 11:19:23 -08002952 int32_t val_lo = Low32Bits(val);
2953 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002954 rl_dest = UpdateLocWideTyped(rl_dest);
2955 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002956
2957 // Can we do this directly into the destination registers?
2958 if (rl_dest.location == kLocPhysReg && rl_src1.location == kLocPhysReg &&
buzbee2700f7e2014-03-07 09:46:20 -08002959 rl_dest.reg.GetLowReg() == rl_src1.reg.GetLowReg() &&
buzbee091cc402014-03-31 10:14:40 -07002960 rl_dest.reg.GetHighReg() == rl_src1.reg.GetHighReg() && !rl_dest.reg.IsFloat()) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002961 if (!IsNoOp(op, val_lo)) {
2962 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002963 NewLIR2(x86op, rl_dest.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002964 }
2965 if (!IsNoOp(op, val_hi)) {
2966 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002967 NewLIR2(x86op, rl_dest.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002968 }
Maxim Kazantsev653f2bf2014-02-13 15:11:17 +07002969
2970 StoreFinalValueWide(rl_dest, rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002971 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002972 }
2973
2974 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2975 DCHECK_EQ(rl_src1.location, kLocPhysReg);
2976
2977 // We need the values to be in a temporary
2978 RegLocation rl_result = ForceTempWide(rl_src1);
2979 if (!IsNoOp(op, val_lo)) {
2980 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002981 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002982 }
2983 if (!IsNoOp(op, val_hi)) {
2984 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002985 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002986 }
2987
2988 StoreFinalValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002989 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002990}
2991
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002992// For final classes there are no sub-classes to check and so we can answer the instance-of
2993// question with simple comparisons. Use compares to memory and SETEQ to optimize for x86.
2994void X86Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx,
2995 RegLocation rl_dest, RegLocation rl_src) {
buzbeea0cd2d72014-06-01 09:33:49 -07002996 RegLocation object = LoadValue(rl_src, kRefReg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002997 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002998 RegStorage result_reg = rl_result.reg;
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002999
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07003000 // For 32-bit, SETcc only works with EAX..EDX.
Chao-ying Fua77ee512014-07-01 17:43:41 -07003001 RegStorage object_32reg = object.reg.Is64Bit() ? As32BitReg(object.reg) : object.reg;
Dmitry Petrochenko407f5c12014-07-01 01:21:38 +07003002 if (result_reg.GetRegNum() == object_32reg.GetRegNum() || !IsByteRegister(result_reg)) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04003003 result_reg = AllocateByteRegister();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003004 }
3005
3006 // Assume that there is no match.
3007 LoadConstant(result_reg, 0);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003008 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, nullptr);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003009
Mark Mendellade54a22014-06-09 12:49:55 -04003010 // We will use this register to compare to memory below.
3011 // References are 32 bit in memory, and 64 bit in registers (in 64 bit mode).
3012 // For this reason, force allocation of a 32 bit register to use, so that the
3013 // compare to memory will be done using a 32 bit comparision.
3014 // The LoadRefDisp(s) below will work normally, even in 64 bit mode.
3015 RegStorage check_class = AllocTemp();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003016
3017 // If Method* is already in a register, we can save a copy.
3018 RegLocation rl_method = mir_graph_->GetMethodLoc();
Andreas Gampeccc60262014-07-04 18:02:38 -07003019 int32_t offset_of_type = mirror::Array::DataOffset(
3020 sizeof(mirror::HeapReference<mirror::Class*>)).Int32Value() +
3021 (sizeof(mirror::HeapReference<mirror::Class*>) * type_idx);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003022
3023 if (rl_method.location == kLocPhysReg) {
3024 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07003025 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00003026 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003027 } else {
buzbee695d13a2014-04-19 13:32:20 -07003028 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00003029 check_class, kNotVolatile);
3030 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003031 }
3032 } else {
3033 LoadCurrMethodDirect(check_class);
3034 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07003035 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00003036 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003037 } else {
buzbee695d13a2014-04-19 13:32:20 -07003038 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00003039 check_class, kNotVolatile);
3040 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003041 }
3042 }
3043
3044 // Compare the computed class to the class in the object.
3045 DCHECK_EQ(object.location, kLocPhysReg);
buzbee2700f7e2014-03-07 09:46:20 -08003046 OpRegMem(kOpCmp, check_class, object.reg, mirror::Object::ClassOffset().Int32Value());
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003047
3048 // Set the low byte of the result to 0 or 1 from the compare condition code.
buzbee2700f7e2014-03-07 09:46:20 -08003049 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondEq);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003050
3051 LIR* target = NewLIR0(kPseudoTargetLabel);
3052 null_branchover->target = target;
3053 FreeTemp(check_class);
3054 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08003055 OpRegCopy(rl_result.reg, result_reg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08003056 FreeTemp(result_reg);
3057 }
3058 StoreValue(rl_dest, rl_result);
3059}
3060
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003061void X86Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07003062 RegLocation rl_lhs, RegLocation rl_rhs, int flags) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003063 OpKind op = kOpBkpt;
3064 bool is_div_rem = false;
3065 bool unary = false;
3066 bool shift_op = false;
3067 bool is_two_addr = false;
3068 RegLocation rl_result;
3069 switch (opcode) {
3070 case Instruction::NEG_INT:
3071 op = kOpNeg;
3072 unary = true;
3073 break;
3074 case Instruction::NOT_INT:
3075 op = kOpMvn;
3076 unary = true;
3077 break;
3078 case Instruction::ADD_INT_2ADDR:
3079 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003080 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003081 case Instruction::ADD_INT:
3082 op = kOpAdd;
3083 break;
3084 case Instruction::SUB_INT_2ADDR:
3085 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003086 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003087 case Instruction::SUB_INT:
3088 op = kOpSub;
3089 break;
3090 case Instruction::MUL_INT_2ADDR:
3091 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003092 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003093 case Instruction::MUL_INT:
3094 op = kOpMul;
3095 break;
3096 case Instruction::DIV_INT_2ADDR:
3097 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003098 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003099 case Instruction::DIV_INT:
3100 op = kOpDiv;
3101 is_div_rem = true;
3102 break;
3103 /* NOTE: returns in kArg1 */
3104 case Instruction::REM_INT_2ADDR:
3105 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003106 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003107 case Instruction::REM_INT:
3108 op = kOpRem;
3109 is_div_rem = true;
3110 break;
3111 case Instruction::AND_INT_2ADDR:
3112 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003113 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003114 case Instruction::AND_INT:
3115 op = kOpAnd;
3116 break;
3117 case Instruction::OR_INT_2ADDR:
3118 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003119 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003120 case Instruction::OR_INT:
3121 op = kOpOr;
3122 break;
3123 case Instruction::XOR_INT_2ADDR:
3124 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003125 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003126 case Instruction::XOR_INT:
3127 op = kOpXor;
3128 break;
3129 case Instruction::SHL_INT_2ADDR:
3130 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003131 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003132 case Instruction::SHL_INT:
3133 shift_op = true;
3134 op = kOpLsl;
3135 break;
3136 case Instruction::SHR_INT_2ADDR:
3137 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003138 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003139 case Instruction::SHR_INT:
3140 shift_op = true;
3141 op = kOpAsr;
3142 break;
3143 case Instruction::USHR_INT_2ADDR:
3144 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003145 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003146 case Instruction::USHR_INT:
3147 shift_op = true;
3148 op = kOpLsr;
3149 break;
3150 default:
3151 LOG(FATAL) << "Invalid word arith op: " << opcode;
3152 }
3153
Mark Mendelle87f9b52014-04-30 14:13:18 -04003154 // Can we convert to a two address instruction?
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003155 if (!is_two_addr &&
3156 (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3157 mir_graph_->SRegToVReg(rl_lhs.s_reg_low))) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04003158 is_two_addr = true;
3159 }
3160
3161 if (!GenerateTwoOperandInstructions()) {
3162 is_two_addr = false;
3163 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003164
3165 // Get the div/rem stuff out of the way.
3166 if (is_div_rem) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07003167 rl_result = GenDivRem(rl_dest, rl_lhs, rl_rhs, op == kOpDiv, flags);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003168 StoreValue(rl_dest, rl_result);
3169 return;
3170 }
3171
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003172 // If we generate any memory access below, it will reference a dalvik reg.
3173 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
3174
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003175 if (unary) {
3176 rl_lhs = LoadValue(rl_lhs, kCoreReg);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003177 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003178 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003179 OpRegReg(op, rl_result.reg, rl_lhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003180 } else {
3181 if (shift_op) {
3182 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003183 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003184 LoadValueDirectFixed(rl_rhs, t_reg);
3185 if (is_two_addr) {
3186 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003187 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003188 if (rl_result.location != kLocPhysReg) {
3189 // Okay, we can do this into memory
buzbee2700f7e2014-03-07 09:46:20 -08003190 OpMemReg(op, rl_result, t_reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003191 FreeTemp(t_reg);
3192 return;
buzbee091cc402014-03-31 10:14:40 -07003193 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003194 // Can do this directly into the result register
buzbee2700f7e2014-03-07 09:46:20 -08003195 OpRegReg(op, rl_result.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003196 FreeTemp(t_reg);
3197 StoreFinalValue(rl_dest, rl_result);
3198 return;
3199 }
3200 }
3201 // Three address form, or we can't do directly.
3202 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3203 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003204 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003205 FreeTemp(t_reg);
3206 } else {
3207 // Multiply is 3 operand only (sort of).
3208 if (is_two_addr && op != kOpMul) {
3209 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003210 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003211 if (rl_result.location == kLocPhysReg) {
Serguei Katkov366f8ae2014-04-15 16:55:26 +07003212 // Ensure res is in a core reg
3213 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003214 // Can we do this from memory directly?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003215 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003216 if (rl_rhs.location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08003217 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003218 StoreFinalValue(rl_dest, rl_result);
3219 return;
buzbee091cc402014-03-31 10:14:40 -07003220 } else if (!rl_rhs.reg.IsFloat()) {
buzbee2700f7e2014-03-07 09:46:20 -08003221 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003222 StoreFinalValue(rl_dest, rl_result);
3223 return;
3224 }
3225 }
3226 rl_rhs = LoadValue(rl_rhs, kCoreReg);
Serguei Katkovd293fb42014-05-19 15:45:42 +07003227 // It might happen rl_rhs and rl_dest are the same VR
3228 // in this case rl_dest is in reg after LoadValue while
3229 // rl_result is not updated yet, so do this
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003230 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003231 if (rl_result.location != kLocPhysReg) {
3232 // Okay, we can do this into memory.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00003233 OpMemReg(op, rl_result, rl_rhs.reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003234 return;
buzbee091cc402014-03-31 10:14:40 -07003235 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003236 // Can do this directly into the result register.
buzbee2700f7e2014-03-07 09:46:20 -08003237 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003238 StoreFinalValue(rl_dest, rl_result);
3239 return;
3240 } else {
3241 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3242 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003243 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003244 }
3245 } else {
3246 // Try to use reg/memory instructions.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003247 rl_lhs = UpdateLocTyped(rl_lhs);
3248 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003249 // We can't optimize with FP registers.
3250 if (!IsOperationSafeWithoutTemps(rl_lhs, rl_rhs)) {
3251 // Something is difficult, so fall back to the standard case.
3252 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3253 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3254 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003255 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003256 } else {
3257 // We can optimize by moving to result and using memory operands.
3258 if (rl_rhs.location != kLocPhysReg) {
3259 // Force LHS into result.
Serguei Katkov66da1362014-03-14 13:33:33 +07003260 // We should be careful with order here
3261 // If rl_dest and rl_lhs points to the same VR we should load first
3262 // If the are different we should find a register first for dest
Chao-ying Fua0147762014-06-06 18:38:49 -07003263 if (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3264 mir_graph_->SRegToVReg(rl_lhs.s_reg_low)) {
Serguei Katkov66da1362014-03-14 13:33:33 +07003265 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3266 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendelle87f9b52014-04-30 14:13:18 -04003267 // No-op if these are the same.
3268 OpRegCopy(rl_result.reg, rl_lhs.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003269 } else {
3270 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003271 LoadValueDirect(rl_lhs, rl_result.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003272 }
buzbee2700f7e2014-03-07 09:46:20 -08003273 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003274 } else if (rl_lhs.location != kLocPhysReg) {
3275 // RHS is in a register; LHS is in memory.
3276 if (op != kOpSub) {
3277 // Force RHS into result and operate on memory.
3278 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003279 OpRegCopy(rl_result.reg, rl_rhs.reg);
3280 OpRegMem(op, rl_result.reg, rl_lhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003281 } else {
3282 // Subtraction isn't commutative.
3283 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3284 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3285 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003286 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003287 }
3288 } else {
3289 // Both are in registers.
3290 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3291 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3292 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003293 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003294 }
3295 }
3296 }
3297 }
3298 }
3299 StoreValue(rl_dest, rl_result);
3300}
3301
3302bool X86Mir2Lir::IsOperationSafeWithoutTemps(RegLocation rl_lhs, RegLocation rl_rhs) {
3303 // If we have non-core registers, then we can't do good things.
buzbee091cc402014-03-31 10:14:40 -07003304 if (rl_lhs.location == kLocPhysReg && rl_lhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003305 return false;
3306 }
buzbee091cc402014-03-31 10:14:40 -07003307 if (rl_rhs.location == kLocPhysReg && rl_rhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003308 return false;
3309 }
3310
3311 // Everything will be fine :-).
3312 return true;
3313}
Chao-ying Fua0147762014-06-06 18:38:49 -07003314
3315void X86Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003316 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07003317 Mir2Lir::GenIntToLong(rl_dest, rl_src);
3318 return;
3319 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003320 rl_src = UpdateLocTyped(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07003321 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
3322 if (rl_src.location == kLocPhysReg) {
3323 NewLIR2(kX86MovsxdRR, rl_result.reg.GetReg(), rl_src.reg.GetReg());
3324 } else {
3325 int displacement = SRegOffset(rl_src.s_reg_low);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003326 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08003327 LIR *m = NewLIR3(kX86MovsxdRM, rl_result.reg.GetReg(), rs_rX86_SP_32.GetReg(),
Chao-ying Fua0147762014-06-06 18:38:49 -07003328 displacement + LOWORD_OFFSET);
3329 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
3330 true /* is_load */, true /* is_64bit */);
3331 }
3332 StoreValueWide(rl_dest, rl_result);
3333}
3334
Yevgeny Rouban6af82062014-11-26 18:11:54 +06003335void X86Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
3336 rl_src = UpdateLocWide(rl_src);
3337 rl_src = NarrowRegLoc(rl_src);
3338 StoreValue(rl_dest, rl_src);
3339
3340 if (cu_->target64) {
3341 // if src and dest are in the same phys reg then StoreValue generates
3342 // no operation but we need explicit 32-bit mov R, R to clear
3343 // the higher 32-bits
3344 rl_dest = UpdateLoc(rl_dest);
3345 if (rl_src.location == kLocPhysReg && rl_dest.location == kLocPhysReg
3346 && IsSameReg(rl_src.reg, rl_dest.reg)) {
3347 LIR* copy_lir = OpRegCopyNoInsert(rl_dest.reg, rl_dest.reg);
3348 // remove nop flag set by OpRegCopyNoInsert if src == dest
3349 copy_lir->flags.is_nop = false;
3350 AppendLIR(copy_lir);
3351 }
3352 }
3353}
3354
Chao-ying Fua0147762014-06-06 18:38:49 -07003355void X86Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
3356 RegLocation rl_src1, RegLocation rl_shift) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003357 if (!cu_->target64) {
Yixin Shouf40f8902014-08-14 14:10:32 -04003358 // Long shift operations in 32-bit. Use shld or shrd to create a 32-bit register filled from
3359 // the other half, shift the other half, if the shift amount is less than 32 we're done,
3360 // otherwise move one register to the other and place zero or sign bits in the other.
3361 LIR* branch;
3362 FlushAllRegs();
3363 LockCallTemps();
3364 LoadValueDirectFixed(rl_shift, rs_rCX);
3365 RegStorage r_tmp = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
3366 LoadValueDirectWideFixed(rl_src1, r_tmp);
3367 switch (opcode) {
3368 case Instruction::SHL_LONG:
3369 case Instruction::SHL_LONG_2ADDR:
3370 NewLIR3(kX86Shld32RRC, r_tmp.GetHighReg(), r_tmp.GetLowReg(), rs_rCX.GetReg());
3371 NewLIR2(kX86Sal32RC, r_tmp.GetLowReg(), rs_rCX.GetReg());
3372 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3373 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3374 OpRegCopy(r_tmp.GetHigh(), r_tmp.GetLow());
3375 LoadConstant(r_tmp.GetLow(), 0);
3376 branch->target = NewLIR0(kPseudoTargetLabel);
3377 break;
3378 case Instruction::SHR_LONG:
3379 case Instruction::SHR_LONG_2ADDR:
3380 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(), rs_rCX.GetReg());
3381 NewLIR2(kX86Sar32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3382 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3383 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3384 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3385 NewLIR2(kX86Sar32RI, r_tmp.GetHighReg(), 31);
3386 branch->target = NewLIR0(kPseudoTargetLabel);
3387 break;
3388 case Instruction::USHR_LONG:
3389 case Instruction::USHR_LONG_2ADDR:
3390 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(),
3391 rs_rCX.GetReg());
3392 NewLIR2(kX86Shr32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3393 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3394 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3395 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3396 LoadConstant(r_tmp.GetHigh(), 0);
3397 branch->target = NewLIR0(kPseudoTargetLabel);
3398 break;
3399 default:
3400 LOG(FATAL) << "Unexpected case: " << opcode;
3401 return;
3402 }
3403 RegLocation rl_result = LocCReturnWide();
3404 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07003405 return;
3406 }
3407
3408 bool is_two_addr = false;
3409 OpKind op = kOpBkpt;
3410 RegLocation rl_result;
3411
3412 switch (opcode) {
3413 case Instruction::SHL_LONG_2ADDR:
3414 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003415 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003416 case Instruction::SHL_LONG:
3417 op = kOpLsl;
3418 break;
3419 case Instruction::SHR_LONG_2ADDR:
3420 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003421 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003422 case Instruction::SHR_LONG:
3423 op = kOpAsr;
3424 break;
3425 case Instruction::USHR_LONG_2ADDR:
3426 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003427 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003428 case Instruction::USHR_LONG:
3429 op = kOpLsr;
3430 break;
3431 default:
3432 op = kOpBkpt;
3433 }
3434
3435 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003436 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Chao-ying Fua0147762014-06-06 18:38:49 -07003437 LoadValueDirectFixed(rl_shift, t_reg);
3438 if (is_two_addr) {
3439 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003440 rl_result = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07003441 if (rl_result.location != kLocPhysReg) {
3442 // Okay, we can do this into memory
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003443 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07003444 OpMemReg(op, rl_result, t_reg.GetReg());
3445 } else if (!rl_result.reg.IsFloat()) {
3446 // Can do this directly into the result register
3447 OpRegReg(op, rl_result.reg, t_reg);
3448 StoreFinalValueWide(rl_dest, rl_result);
3449 }
3450 } else {
3451 // Three address form, or we can't do directly.
3452 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
3453 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
3454 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
3455 StoreFinalValueWide(rl_dest, rl_result);
3456 }
3457
3458 FreeTemp(t_reg);
3459}
3460
Brian Carlstrom7940e442013-07-12 13:46:57 -07003461} // namespace art