blob: 03156dc5ad49bc9af9df915c557c4862e9d3200c [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file contains codegen for the X86 ISA */
18
19#include "codegen_x86.h"
20#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070021#include "dex/reg_storage_eq.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070022#include "mirror/art_method.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070023#include "mirror/array-inl.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080024#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "x86_lir.h"
26
27namespace art {
28
29/*
Brian Carlstrom7940e442013-07-12 13:46:57 -070030 * Compare two 64-bit values
31 * x = y return 0
32 * x < y return -1
33 * x > y return 1
34 */
35void X86Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070036 RegLocation rl_src2) {
Elena Sayapinadd644502014-07-01 18:39:52 +070037 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -070038 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
39 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
40 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Chao-ying Fua0147762014-06-06 18:38:49 -070041 RegStorage temp_reg = AllocTemp();
Serguei Katkov1c557032014-06-23 13:23:38 +070042 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
43 NewLIR2(kX86Set8R, rl_result.reg.GetReg(), kX86CondG); // result = (src1 > src2) ? 1 : 0
44 NewLIR2(kX86Set8R, temp_reg.GetReg(), kX86CondL); // temp = (src1 >= src2) ? 0 : 1
45 NewLIR2(kX86Sub8RR, rl_result.reg.GetReg(), temp_reg.GetReg());
46 NewLIR2(kX86Movsx8qRR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
Serguei Katkov04982232014-06-20 18:17:16 +070047
Chao-ying Fua0147762014-06-06 18:38:49 -070048 StoreValue(rl_dest, rl_result);
49 FreeTemp(temp_reg);
50 return;
51 }
52
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +070053 // Prepare for explicit register usage
54 ExplicitTempRegisterLock(this, 4, &rs_r0, &rs_r1, &rs_r2, &rs_r3);
buzbee091cc402014-03-31 10:14:40 -070055 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_r0, rs_r1);
56 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_r2, rs_r3);
buzbee2700f7e2014-03-07 09:46:20 -080057 LoadValueDirectWideFixed(rl_src1, r_tmp1);
58 LoadValueDirectWideFixed(rl_src2, r_tmp2);
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 // Compute (r1:r0) = (r1:r0) - (r3:r2)
buzbee2700f7e2014-03-07 09:46:20 -080060 OpRegReg(kOpSub, rs_r0, rs_r2); // r0 = r0 - r2
61 OpRegReg(kOpSbc, rs_r1, rs_r3); // r1 = r1 - r3 - CF
buzbee091cc402014-03-31 10:14:40 -070062 NewLIR2(kX86Set8R, rs_r2.GetReg(), kX86CondL); // r2 = (r1:r0) < (r3:r2) ? 1 : 0
63 NewLIR2(kX86Movzx8RR, rs_r2.GetReg(), rs_r2.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -080064 OpReg(kOpNeg, rs_r2); // r2 = -r2
65 OpRegReg(kOpOr, rs_r0, rs_r1); // r0 = high | low - sets ZF
buzbee091cc402014-03-31 10:14:40 -070066 NewLIR2(kX86Set8R, rs_r0.GetReg(), kX86CondNz); // r0 = (r1:r0) != (r3:r2) ? 1 : 0
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 NewLIR2(kX86Movzx8RR, r0, r0);
buzbee2700f7e2014-03-07 09:46:20 -080068 OpRegReg(kOpOr, rs_r0, rs_r2); // r0 = r0 | r2
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 RegLocation rl_result = LocCReturn();
70 StoreValue(rl_dest, rl_result);
71}
72
73X86ConditionCode X86ConditionEncoding(ConditionCode cond) {
74 switch (cond) {
75 case kCondEq: return kX86CondEq;
76 case kCondNe: return kX86CondNe;
77 case kCondCs: return kX86CondC;
78 case kCondCc: return kX86CondNc;
Vladimir Marko58af1f92013-12-19 13:31:15 +000079 case kCondUlt: return kX86CondC;
80 case kCondUge: return kX86CondNc;
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 case kCondMi: return kX86CondS;
82 case kCondPl: return kX86CondNs;
83 case kCondVs: return kX86CondO;
84 case kCondVc: return kX86CondNo;
85 case kCondHi: return kX86CondA;
86 case kCondLs: return kX86CondBe;
87 case kCondGe: return kX86CondGe;
88 case kCondLt: return kX86CondL;
89 case kCondGt: return kX86CondG;
90 case kCondLe: return kX86CondLe;
91 case kCondAl:
92 case kCondNv: LOG(FATAL) << "Should not reach here";
93 }
94 return kX86CondO;
95}
96
buzbee2700f7e2014-03-07 09:46:20 -080097LIR* X86Mir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
Chao-ying Fua77ee512014-07-01 17:43:41 -070098 NewLIR2(src1.Is64Bit() ? kX86Cmp64RR : kX86Cmp32RR, src1.GetReg(), src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 X86ConditionCode cc = X86ConditionEncoding(cond);
100 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ ,
101 cc);
102 branch->target = target;
103 return branch;
104}
105
buzbee2700f7e2014-03-07 09:46:20 -0800106LIR* X86Mir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700107 int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 if ((check_value == 0) && (cond == kCondEq || cond == kCondNe)) {
109 // TODO: when check_value == 0 and reg is rCX, use the jcxz/nz opcode
Chao-ying Fua77ee512014-07-01 17:43:41 -0700110 NewLIR2(reg.Is64Bit() ? kX86Test64RR: kX86Test32RR, reg.GetReg(), reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700112 if (reg.Is64Bit()) {
113 NewLIR2(IS_SIMM8(check_value) ? kX86Cmp64RI8 : kX86Cmp64RI, reg.GetReg(), check_value);
114 } else {
115 NewLIR2(IS_SIMM8(check_value) ? kX86Cmp32RI8 : kX86Cmp32RI, reg.GetReg(), check_value);
116 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 }
118 X86ConditionCode cc = X86ConditionEncoding(cond);
119 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ , cc);
120 branch->target = target;
121 return branch;
122}
123
buzbee2700f7e2014-03-07 09:46:20 -0800124LIR* X86Mir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
125 // If src or dest is a pair, we'll be using low reg.
126 if (r_dest.IsPair()) {
127 r_dest = r_dest.GetLow();
128 }
129 if (r_src.IsPair()) {
130 r_src = r_src.GetLow();
131 }
buzbee091cc402014-03-31 10:14:40 -0700132 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 return OpFpRegCopy(r_dest, r_src);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700134 LIR* res = RawLIR(current_dalvik_offset_, r_dest.Is64Bit() ? kX86Mov64RR : kX86Mov32RR,
buzbee2700f7e2014-03-07 09:46:20 -0800135 r_dest.GetReg(), r_src.GetReg());
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800136 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 res->flags.is_nop = true;
138 }
139 return res;
140}
141
buzbee7a11ab02014-04-28 20:02:38 -0700142void X86Mir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
143 if (r_dest != r_src) {
144 LIR *res = OpRegCopyNoInsert(r_dest, r_src);
145 AppendLIR(res);
146 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147}
148
buzbee2700f7e2014-03-07 09:46:20 -0800149void X86Mir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700150 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700151 bool dest_fp = r_dest.IsFloat();
152 bool src_fp = r_src.IsFloat();
buzbee7a11ab02014-04-28 20:02:38 -0700153 if (dest_fp) {
154 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700155 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 } else {
buzbee7a11ab02014-04-28 20:02:38 -0700157 // TODO: Prevent this from happening in the code. The result is often
158 // unused or could have been loaded more easily from memory.
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700159 if (!r_src.IsPair()) {
160 DCHECK(!r_dest.IsPair());
161 NewLIR2(kX86MovqxrRR, r_dest.GetReg(), r_src.GetReg());
162 } else {
163 NewLIR2(kX86MovdxrRR, r_dest.GetReg(), r_src.GetLowReg());
164 RegStorage r_tmp = AllocTempDouble();
165 NewLIR2(kX86MovdxrRR, r_tmp.GetReg(), r_src.GetHighReg());
166 NewLIR2(kX86PunpckldqRR, r_dest.GetReg(), r_tmp.GetReg());
167 FreeTemp(r_tmp);
168 }
buzbee7a11ab02014-04-28 20:02:38 -0700169 }
170 } else {
171 if (src_fp) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700172 if (!r_dest.IsPair()) {
173 DCHECK(!r_src.IsPair());
174 NewLIR2(kX86MovqrxRR, r_dest.GetReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700175 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700176 NewLIR2(kX86MovdrxRR, r_dest.GetLowReg(), r_src.GetReg());
177 RegStorage temp_reg = AllocTempDouble();
178 NewLIR2(kX86MovsdRR, temp_reg.GetReg(), r_src.GetReg());
179 NewLIR2(kX86PsrlqRI, temp_reg.GetReg(), 32);
180 NewLIR2(kX86MovdrxRR, r_dest.GetHighReg(), temp_reg.GetReg());
181 }
182 } else {
183 DCHECK_EQ(r_dest.IsPair(), r_src.IsPair());
184 if (!r_src.IsPair()) {
185 // Just copy the register directly.
186 OpRegCopy(r_dest, r_src);
187 } else {
188 // Handle overlap
189 if (r_src.GetHighReg() == r_dest.GetLowReg() &&
190 r_src.GetLowReg() == r_dest.GetHighReg()) {
191 // Deal with cycles.
192 RegStorage temp_reg = AllocTemp();
193 OpRegCopy(temp_reg, r_dest.GetHigh());
194 OpRegCopy(r_dest.GetHigh(), r_dest.GetLow());
195 OpRegCopy(r_dest.GetLow(), temp_reg);
196 FreeTemp(temp_reg);
197 } else if (r_src.GetHighReg() == r_dest.GetLowReg()) {
198 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
199 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
200 } else {
201 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
202 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
203 }
buzbee7a11ab02014-04-28 20:02:38 -0700204 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 }
206 }
207 }
208}
209
Andreas Gampe90969af2014-07-15 23:02:11 -0700210void X86Mir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
211 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700212 RegisterClass dest_reg_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700213 DCHECK(!left_op.IsPair() && !right_op.IsPair() && !rs_dest.IsPair());
214 DCHECK(!left_op.IsFloat() && !right_op.IsFloat() && !rs_dest.IsFloat());
215
216 // We really need this check for correctness, otherwise we will need to do more checks in
217 // non zero/one case
218 if (true_val == false_val) {
219 LoadConstantNoClobber(rs_dest, true_val);
220 return;
Andreas Gampe90969af2014-07-15 23:02:11 -0700221 }
222
Serguei Katkov9ee45192014-07-17 14:39:03 +0700223 const bool dest_intersect = IsSameReg(rs_dest, left_op) || IsSameReg(rs_dest, right_op);
224
225 const bool zero_one_case = (true_val == 0 && false_val == 1) || (true_val == 1 && false_val == 0);
226 if (zero_one_case && IsByteRegister(rs_dest)) {
227 if (!dest_intersect) {
228 LoadConstantNoClobber(rs_dest, 0);
229 }
230 OpRegReg(kOpCmp, left_op, right_op);
231 // Set the low byte of the result to 0 or 1 from the compare condition code.
232 NewLIR2(kX86Set8R, rs_dest.GetReg(),
233 X86ConditionEncoding(true_val == 1 ? code : FlipComparisonOrder(code)));
234 if (dest_intersect) {
235 NewLIR2(rs_dest.Is64Bit() ? kX86Movzx8qRR : kX86Movzx8RR, rs_dest.GetReg(), rs_dest.GetReg());
236 }
237 } else {
238 // Be careful rs_dest can be changed only after cmp because it can be the same as one of ops
239 // and it cannot use xor because it makes cc flags to be dirty
240 RegStorage temp_reg = AllocTypedTemp(false, dest_reg_class, false);
241 if (temp_reg.Valid()) {
242 if (false_val == 0 && dest_intersect) {
243 code = FlipComparisonOrder(code);
244 std::swap(true_val, false_val);
245 }
246 if (!dest_intersect) {
247 LoadConstantNoClobber(rs_dest, false_val);
248 }
249 LoadConstantNoClobber(temp_reg, true_val);
250 OpRegReg(kOpCmp, left_op, right_op);
251 if (dest_intersect) {
252 LoadConstantNoClobber(rs_dest, false_val);
253 DCHECK(!last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
254 }
255 OpCondRegReg(kOpCmov, code, rs_dest, temp_reg);
256 FreeTemp(temp_reg);
257 } else {
258 // slow path
259 LIR* cmp_branch = OpCmpBranch(code, left_op, right_op, nullptr);
260 LoadConstantNoClobber(rs_dest, false_val);
261 LIR* that_is_it = NewLIR1(kX86Jmp8, 0);
262 LIR* true_case = NewLIR0(kPseudoTargetLabel);
263 cmp_branch->target = true_case;
264 LoadConstantNoClobber(rs_dest, true_val);
265 LIR* end = NewLIR0(kPseudoTargetLabel);
266 that_is_it->target = end;
267 }
268 }
Andreas Gampe90969af2014-07-15 23:02:11 -0700269}
270
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700271void X86Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700272 UNUSED(bb);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800273 RegLocation rl_result;
274 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
275 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700276 // Avoid using float regs here.
277 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
278 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
Vladimir Markoa1a70742014-03-03 10:28:05 +0000279 ConditionCode ccode = mir->meta.ccode;
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800280
281 // The kMirOpSelect has two variants, one for constants and one for moves.
282 const bool is_constant_case = (mir->ssa_rep->num_uses == 1);
283
284 if (is_constant_case) {
285 int true_val = mir->dalvikInsn.vB;
286 int false_val = mir->dalvikInsn.vC;
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800287
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700288 // simplest strange case
289 if (true_val == false_val) {
290 rl_result = EvalLoc(rl_dest, result_reg_class, true);
291 LoadConstantNoClobber(rl_result.reg, true_val);
292 } else {
293 // TODO: use GenSelectConst32 and handle additional opcode patterns such as
294 // "cmp; setcc; movzx" or "cmp; sbb r0,r0; and r0,$mask; add r0,$literal".
295 rl_src = LoadValue(rl_src, src_reg_class);
296 rl_result = EvalLoc(rl_dest, result_reg_class, true);
297 /*
298 * For ccode == kCondEq:
299 *
300 * 1) When the true case is zero and result_reg is not same as src_reg:
301 * xor result_reg, result_reg
302 * cmp $0, src_reg
303 * mov t1, $false_case
304 * cmovnz result_reg, t1
305 * 2) When the false case is zero and result_reg is not same as src_reg:
306 * xor result_reg, result_reg
307 * cmp $0, src_reg
308 * mov t1, $true_case
309 * cmovz result_reg, t1
310 * 3) All other cases (we do compare first to set eflags):
311 * cmp $0, src_reg
312 * mov result_reg, $false_case
313 * mov t1, $true_case
314 * cmovz result_reg, t1
315 */
316 // FIXME: depending on how you use registers you could get a false != mismatch when dealing
317 // with different views of the same underlying physical resource (i.e. solo32 vs. solo64).
318 const bool result_reg_same_as_src =
319 (rl_src.location == kLocPhysReg && rl_src.reg.GetRegNum() == rl_result.reg.GetRegNum());
320 const bool true_zero_case = (true_val == 0 && false_val != 0 && !result_reg_same_as_src);
321 const bool false_zero_case = (false_val == 0 && true_val != 0 && !result_reg_same_as_src);
322 const bool catch_all_case = !(true_zero_case || false_zero_case);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800323
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700324 if (true_zero_case || false_zero_case) {
325 OpRegReg(kOpXor, rl_result.reg, rl_result.reg);
326 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800327
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700328 if (true_zero_case || false_zero_case || catch_all_case) {
329 OpRegImm(kOpCmp, rl_src.reg, 0);
330 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800331
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700332 if (catch_all_case) {
333 OpRegImm(kOpMov, rl_result.reg, false_val);
334 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800335
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700336 if (true_zero_case || false_zero_case || catch_all_case) {
337 ConditionCode cc = true_zero_case ? NegateComparison(ccode) : ccode;
338 int immediateForTemp = true_zero_case ? false_val : true_val;
339 RegStorage temp1_reg = AllocTypedTemp(false, result_reg_class);
340 OpRegImm(kOpMov, temp1_reg, immediateForTemp);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800341
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700342 OpCondRegReg(kOpCmov, cc, rl_result.reg, temp1_reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800343
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700344 FreeTemp(temp1_reg);
345 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800346 }
347 } else {
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700348 rl_src = LoadValue(rl_src, src_reg_class);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800349 RegLocation rl_true = mir_graph_->GetSrc(mir, 1);
350 RegLocation rl_false = mir_graph_->GetSrc(mir, 2);
buzbeea0cd2d72014-06-01 09:33:49 -0700351 rl_true = LoadValue(rl_true, result_reg_class);
352 rl_false = LoadValue(rl_false, result_reg_class);
353 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800354
355 /*
Vladimir Markoa1a70742014-03-03 10:28:05 +0000356 * For ccode == kCondEq:
357 *
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800358 * 1) When true case is already in place:
359 * cmp $0, src_reg
360 * cmovnz result_reg, false_reg
361 * 2) When false case is already in place:
362 * cmp $0, src_reg
363 * cmovz result_reg, true_reg
364 * 3) When neither cases are in place:
365 * cmp $0, src_reg
Vladimir Markoa1a70742014-03-03 10:28:05 +0000366 * mov result_reg, false_reg
367 * cmovz result_reg, true_reg
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800368 */
369
370 // kMirOpSelect is generated just for conditional cases when comparison is done with zero.
buzbee2700f7e2014-03-07 09:46:20 -0800371 OpRegImm(kOpCmp, rl_src.reg, 0);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800372
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000373 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) {
buzbee2700f7e2014-03-07 09:46:20 -0800374 OpCondRegReg(kOpCmov, NegateComparison(ccode), rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000375 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) {
buzbee2700f7e2014-03-07 09:46:20 -0800376 OpCondRegReg(kOpCmov, ccode, rl_result.reg, rl_true.reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800377 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800378 OpRegCopy(rl_result.reg, rl_false.reg);
379 OpCondRegReg(kOpCmov, ccode, rl_result.reg, rl_true.reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800380 }
381 }
382
383 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384}
385
386void X86Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
buzbee0d829482013-10-11 15:24:55 -0700387 LIR* taken = &block_label_list_[bb->taken];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
389 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Vladimir Markoa8946072014-01-22 10:30:44 +0000390 ConditionCode ccode = mir->meta.ccode;
Mark Mendell412d4f82013-12-18 13:32:36 -0800391
392 if (rl_src1.is_const) {
393 std::swap(rl_src1, rl_src2);
394 ccode = FlipComparisonOrder(ccode);
395 }
396 if (rl_src2.is_const) {
397 // Do special compare/branch against simple const operand
398 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
399 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
400 return;
401 }
402
Elena Sayapinadd644502014-07-01 18:39:52 +0700403 if (cu_->target64) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700404 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
405 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
406
407 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
408 OpCondBranch(ccode, taken);
409 return;
410 }
411
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700412 // Prepare for explicit register usage
413 ExplicitTempRegisterLock(this, 4, &rs_r0, &rs_r1, &rs_r2, &rs_r3);
buzbee091cc402014-03-31 10:14:40 -0700414 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_r0, rs_r1);
415 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_r2, rs_r3);
buzbee2700f7e2014-03-07 09:46:20 -0800416 LoadValueDirectWideFixed(rl_src1, r_tmp1);
417 LoadValueDirectWideFixed(rl_src2, r_tmp2);
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700418
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 // Swap operands and condition code to prevent use of zero flag.
420 if (ccode == kCondLe || ccode == kCondGt) {
421 // Compute (r3:r2) = (r3:r2) - (r1:r0)
buzbee2700f7e2014-03-07 09:46:20 -0800422 OpRegReg(kOpSub, rs_r2, rs_r0); // r2 = r2 - r0
423 OpRegReg(kOpSbc, rs_r3, rs_r1); // r3 = r3 - r1 - CF
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 } else {
425 // Compute (r1:r0) = (r1:r0) - (r3:r2)
buzbee2700f7e2014-03-07 09:46:20 -0800426 OpRegReg(kOpSub, rs_r0, rs_r2); // r0 = r0 - r2
427 OpRegReg(kOpSbc, rs_r1, rs_r3); // r1 = r1 - r3 - CF
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 }
429 switch (ccode) {
430 case kCondEq:
431 case kCondNe:
buzbee2700f7e2014-03-07 09:46:20 -0800432 OpRegReg(kOpOr, rs_r0, rs_r1); // r0 = r0 | r1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 break;
434 case kCondLe:
435 ccode = kCondGe;
436 break;
437 case kCondGt:
438 ccode = kCondLt;
439 break;
440 case kCondLt:
441 case kCondGe:
442 break;
443 default:
444 LOG(FATAL) << "Unexpected ccode: " << ccode;
445 }
446 OpCondBranch(ccode, taken);
447}
448
Mark Mendell412d4f82013-12-18 13:32:36 -0800449void X86Mir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
450 int64_t val, ConditionCode ccode) {
451 int32_t val_lo = Low32Bits(val);
452 int32_t val_hi = High32Bits(val);
453 LIR* taken = &block_label_list_[bb->taken];
Mark Mendell412d4f82013-12-18 13:32:36 -0800454 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Mark Mendell752e2052014-05-01 10:19:04 -0400455 bool is_equality_test = ccode == kCondEq || ccode == kCondNe;
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700456
Elena Sayapinadd644502014-07-01 18:39:52 +0700457 if (cu_->target64) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700458 if (is_equality_test && val == 0) {
459 // We can simplify of comparing for ==, != to 0.
460 NewLIR2(kX86Test64RR, rl_src1.reg.GetReg(), rl_src1.reg.GetReg());
461 } else if (is_equality_test && val_hi == 0 && val_lo > 0) {
462 OpRegImm(kOpCmp, rl_src1.reg, val_lo);
463 } else {
464 RegStorage tmp = AllocTypedTempWide(false, kCoreReg);
465 LoadConstantWide(tmp, val);
466 OpRegReg(kOpCmp, rl_src1.reg, tmp);
467 FreeTemp(tmp);
468 }
469 OpCondBranch(ccode, taken);
470 return;
471 }
472
Mark Mendell752e2052014-05-01 10:19:04 -0400473 if (is_equality_test && val != 0) {
474 rl_src1 = ForceTempWide(rl_src1);
475 }
buzbee2700f7e2014-03-07 09:46:20 -0800476 RegStorage low_reg = rl_src1.reg.GetLow();
477 RegStorage high_reg = rl_src1.reg.GetHigh();
Mark Mendell412d4f82013-12-18 13:32:36 -0800478
Mark Mendell752e2052014-05-01 10:19:04 -0400479 if (is_equality_test) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700480 // We can simplify of comparing for ==, != to 0.
Mark Mendell752e2052014-05-01 10:19:04 -0400481 if (val == 0) {
482 if (IsTemp(low_reg)) {
483 OpRegReg(kOpOr, low_reg, high_reg);
484 // We have now changed it; ignore the old values.
485 Clobber(rl_src1.reg);
486 } else {
487 RegStorage t_reg = AllocTemp();
488 OpRegRegReg(kOpOr, t_reg, low_reg, high_reg);
489 FreeTemp(t_reg);
490 }
491 OpCondBranch(ccode, taken);
492 return;
493 }
494
495 // Need to compute the actual value for ==, !=.
496 OpRegImm(kOpSub, low_reg, val_lo);
497 NewLIR2(kX86Sbb32RI, high_reg.GetReg(), val_hi);
498 OpRegReg(kOpOr, high_reg, low_reg);
499 Clobber(rl_src1.reg);
500 } else if (ccode == kCondLe || ccode == kCondGt) {
501 // Swap operands and condition code to prevent use of zero flag.
502 RegStorage tmp = AllocTypedTempWide(false, kCoreReg);
503 LoadConstantWide(tmp, val);
504 OpRegReg(kOpSub, tmp.GetLow(), low_reg);
505 OpRegReg(kOpSbc, tmp.GetHigh(), high_reg);
506 ccode = (ccode == kCondLe) ? kCondGe : kCondLt;
507 FreeTemp(tmp);
508 } else {
509 // We can use a compare for the low word to set CF.
510 OpRegImm(kOpCmp, low_reg, val_lo);
511 if (IsTemp(high_reg)) {
512 NewLIR2(kX86Sbb32RI, high_reg.GetReg(), val_hi);
513 // We have now changed it; ignore the old values.
514 Clobber(rl_src1.reg);
515 } else {
516 // mov temp_reg, high_reg; sbb temp_reg, high_constant
517 RegStorage t_reg = AllocTemp();
518 OpRegCopy(t_reg, high_reg);
519 NewLIR2(kX86Sbb32RI, t_reg.GetReg(), val_hi);
520 FreeTemp(t_reg);
521 }
Mark Mendell412d4f82013-12-18 13:32:36 -0800522 }
523
Mark Mendell752e2052014-05-01 10:19:04 -0400524 OpCondBranch(ccode, taken);
Mark Mendell412d4f82013-12-18 13:32:36 -0800525}
526
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700527void X86Mir2Lir::CalculateMagicAndShift(int64_t divisor, int64_t& magic, int& shift, bool is_long) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800528 // It does not make sense to calculate magic and shift for zero divisor.
529 DCHECK_NE(divisor, 0);
530
531 /* According to H.S.Warren's Hacker's Delight Chapter 10 and
532 * T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
533 * The magic number M and shift S can be calculated in the following way:
534 * Let nc be the most positive value of numerator(n) such that nc = kd - 1,
535 * where divisor(d) >=2.
536 * Let nc be the most negative value of numerator(n) such that nc = kd + 1,
537 * where divisor(d) <= -2.
538 * Thus nc can be calculated like:
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700539 * nc = exp + exp % d - 1, where d >= 2 and exp = 2^31 for int or 2^63 for long
540 * 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 -0800541 *
542 * So the shift p is the smallest p satisfying
543 * 2^p > nc * (d - 2^p % d), where d >= 2
544 * 2^p > nc * (d + 2^p % d), where d <= -2.
545 *
546 * the magic number M is calcuated by
547 * M = (2^p + d - 2^p % d) / d, where d >= 2
548 * M = (2^p - d - 2^p % d) / d, where d <= -2.
549 *
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700550 * 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 -0800551 * the shift number S.
552 */
553
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700554 int64_t p = (is_long) ? 63 : 31;
555 const uint64_t exp = (is_long) ? 0x8000000000000000ULL : 0x80000000U;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800556
557 // Initialize the computations.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700558 uint64_t abs_d = (divisor >= 0) ? divisor : -divisor;
559 uint64_t tmp = exp + ((is_long) ? static_cast<uint64_t>(divisor) >> 63 :
560 static_cast<uint32_t>(divisor) >> 31);
561 uint64_t abs_nc = tmp - 1 - tmp % abs_d;
562 uint64_t quotient1 = exp / abs_nc;
563 uint64_t remainder1 = exp % abs_nc;
564 uint64_t quotient2 = exp / abs_d;
565 uint64_t remainder2 = exp % abs_d;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800566
567 /*
568 * To avoid handling both positive and negative divisor, Hacker's Delight
569 * introduces a method to handle these 2 cases together to avoid duplication.
570 */
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700571 uint64_t delta;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800572 do {
573 p++;
574 quotient1 = 2 * quotient1;
575 remainder1 = 2 * remainder1;
576 if (remainder1 >= abs_nc) {
577 quotient1++;
578 remainder1 = remainder1 - abs_nc;
579 }
580 quotient2 = 2 * quotient2;
581 remainder2 = 2 * remainder2;
582 if (remainder2 >= abs_d) {
583 quotient2++;
584 remainder2 = remainder2 - abs_d;
585 }
586 delta = abs_d - remainder2;
587 } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
588
589 magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700590
591 if (!is_long) {
592 magic = static_cast<int>(magic);
593 }
594
595 shift = (is_long) ? p - 64 : p - 32;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800596}
597
buzbee2700f7e2014-03-07 09:46:20 -0800598RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg_lo, int lit, bool is_div) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700599 UNUSED(rl_dest, reg_lo, lit, is_div);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 LOG(FATAL) << "Unexpected use of GenDivRemLit for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700601 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602}
603
Mark Mendell2bf31e62014-01-23 12:13:40 -0800604RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src,
605 int imm, bool is_div) {
606 // Use a multiply (and fixup) to perform an int div/rem by a constant.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700607 RegLocation rl_result;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800608
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700609 if (imm == 1) {
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700610 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800611 if (is_div) {
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700612 // x / 1 == x.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700613 LoadValueDirectFixed(rl_src, rl_result.reg);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700614 } else {
615 // x % 1 == 0.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700616 LoadConstantNoClobber(rl_result.reg, 0);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700617 }
618 } else if (imm == -1) { // handle 0x80000000 / -1 special case.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700619 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700620 if (is_div) {
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700621 LoadValueDirectFixed(rl_src, rl_result.reg);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400622
623 // Check if numerator is 0
624 OpRegImm(kOpCmp, rl_result.reg, 0);
625 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
626
627 // handle 0x80000000 / -1
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700628 OpRegImm(kOpCmp, rl_result.reg, 0x80000000);
629 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800630
631 // for x != MIN_INT, x / -1 == -x.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700632 NewLIR1(kX86Neg32R, rl_result.reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800633
Mark Mendell2bf31e62014-01-23 12:13:40 -0800634 // EAX already contains the right value (0x80000000),
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700635 minint_branch->target = NewLIR0(kPseudoTargetLabel);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400636 branch->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800637 } else {
638 // x % -1 == 0.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700639 LoadConstantNoClobber(rl_result.reg, 0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800640 }
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700641 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
642 // Division using shifting.
643 rl_src = LoadValue(rl_src, kCoreReg);
644 rl_result = EvalLoc(rl_dest, kCoreReg, true);
645 if (IsSameReg(rl_result.reg, rl_src.reg)) {
646 RegStorage rs_temp = AllocTypedTemp(false, kCoreReg);
647 rl_result.reg.SetReg(rs_temp.GetReg());
648 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400649
650 // Check if numerator is 0
651 OpRegImm(kOpCmp, rl_src.reg, 0);
652 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
653 LoadConstantNoClobber(rl_result.reg, 0);
654 LIR* done = NewLIR1(kX86Jmp8, 0);
655 branch->target = NewLIR0(kPseudoTargetLabel);
656
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700657 NewLIR3(kX86Lea32RM, rl_result.reg.GetReg(), rl_src.reg.GetReg(), std::abs(imm) - 1);
658 NewLIR2(kX86Test32RR, rl_src.reg.GetReg(), rl_src.reg.GetReg());
659 OpCondRegReg(kOpCmov, kCondPl, rl_result.reg, rl_src.reg);
Andreas Gampe7e499922015-01-06 08:28:12 -0800660 int shift_amount = CTZ(imm);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700661 OpRegImm(kOpAsr, rl_result.reg, shift_amount);
662 if (imm < 0) {
663 OpReg(kOpNeg, rl_result.reg);
664 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400665 done->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800666 } else {
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700667 CHECK(imm <= -2 || imm >= 2);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700668
Mark Mendell2bf31e62014-01-23 12:13:40 -0800669 // Use H.S.Warren's Hacker's Delight Chapter 10 and
670 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700671 int64_t magic;
672 int shift;
673 CalculateMagicAndShift((int64_t)imm, magic, shift, false /* is_long */);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800674
675 /*
676 * For imm >= 2,
677 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
678 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
679 * For imm <= -2,
680 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
681 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
682 * We implement this algorithm in the following way:
683 * 1. multiply magic number m and numerator n, get the higher 32bit result in EDX
684 * 2. if imm > 0 and magic < 0, add numerator to EDX
685 * if imm < 0 and magic > 0, sub numerator from EDX
686 * 3. if S !=0, SAR S bits for EDX
687 * 4. add 1 to EDX if EDX < 0
688 * 5. Thus, EDX is the quotient
689 */
690
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700691 FlushReg(rs_r0);
692 Clobber(rs_r0);
693 LockTemp(rs_r0);
694 FlushReg(rs_r2);
695 Clobber(rs_r2);
696 LockTemp(rs_r2);
697
Mark Mendell3a91f442014-09-02 12:44:24 -0400698 // Assume that the result will be in EDX for divide, and EAX for remainder.
699 rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, is_div ? rs_r2 : rs_r0,
700 INVALID_SREG, INVALID_SREG};
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700701
Mark Mendell3a91f442014-09-02 12:44:24 -0400702 // We need the value at least twice. Load into a temp.
703 rl_src = LoadValue(rl_src, kCoreReg);
704 RegStorage numerator_reg = rl_src.reg;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800705
Mark Mendell3a91f442014-09-02 12:44:24 -0400706 // Check if numerator is 0.
707 OpRegImm(kOpCmp, numerator_reg, 0);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400708 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell3a91f442014-09-02 12:44:24 -0400709 // Return result 0 if numerator was 0.
710 LoadConstantNoClobber(rl_result.reg, 0);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400711 LIR* done = NewLIR1(kX86Jmp8, 0);
712 branch->target = NewLIR0(kPseudoTargetLabel);
713
Mark Mendell3a91f442014-09-02 12:44:24 -0400714 // EAX = magic.
715 LoadConstant(rs_r0, magic);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800716
Mark Mendell3a91f442014-09-02 12:44:24 -0400717 // EDX:EAX = magic * numerator.
718 NewLIR1(kX86Imul32DaR, numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800719
720 if (imm > 0 && magic < 0) {
721 // Add numerator to EDX.
buzbee2700f7e2014-03-07 09:46:20 -0800722 DCHECK(numerator_reg.Valid());
buzbee091cc402014-03-31 10:14:40 -0700723 NewLIR2(kX86Add32RR, rs_r2.GetReg(), numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800724 } else if (imm < 0 && magic > 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800725 DCHECK(numerator_reg.Valid());
buzbee091cc402014-03-31 10:14:40 -0700726 NewLIR2(kX86Sub32RR, rs_r2.GetReg(), numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800727 }
728
729 // Do we need the shift?
730 if (shift != 0) {
731 // Shift EDX by 'shift' bits.
buzbee091cc402014-03-31 10:14:40 -0700732 NewLIR2(kX86Sar32RI, rs_r2.GetReg(), shift);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800733 }
734
735 // Add 1 to EDX if EDX < 0.
736
737 // Move EDX to EAX.
buzbee2700f7e2014-03-07 09:46:20 -0800738 OpRegCopy(rs_r0, rs_r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800739
740 // Move sign bit to bit 0, zeroing the rest.
buzbee091cc402014-03-31 10:14:40 -0700741 NewLIR2(kX86Shr32RI, rs_r2.GetReg(), 31);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800742
743 // EDX = EDX + EAX.
buzbee091cc402014-03-31 10:14:40 -0700744 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r0.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800745
746 // Quotient is in EDX.
747 if (!is_div) {
748 // We need to compute the remainder.
749 // Remainder is divisor - (quotient * imm).
buzbee2700f7e2014-03-07 09:46:20 -0800750 DCHECK(numerator_reg.Valid());
751 OpRegCopy(rs_r0, numerator_reg);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800752
753 // EAX = numerator * imm.
buzbee2700f7e2014-03-07 09:46:20 -0800754 OpRegRegImm(kOpMul, rs_r2, rs_r2, imm);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800755
Mark Mendell3a91f442014-09-02 12:44:24 -0400756 // EAX -= EDX.
buzbee091cc402014-03-31 10:14:40 -0700757 NewLIR2(kX86Sub32RR, rs_r0.GetReg(), rs_r2.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800758
759 // For this case, return the result in EAX.
Mark Mendell2bf31e62014-01-23 12:13:40 -0800760 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400761 done->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800762 }
763
764 return rl_result;
765}
766
buzbee2700f7e2014-03-07 09:46:20 -0800767RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg_lo, RegStorage reg_hi,
768 bool is_div) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700769 UNUSED(rl_dest, reg_lo, reg_hi, is_div);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 LOG(FATAL) << "Unexpected use of GenDivRem for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700771 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772}
773
Mark Mendell2bf31e62014-01-23 12:13:40 -0800774RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700775 RegLocation rl_src2, bool is_div, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700776 UNUSED(rl_dest);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800777 // We have to use fixed registers, so flush all the temps.
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700778
779 // Prepare for explicit register usage.
780 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800781
782 // Load LHS into EAX.
buzbee2700f7e2014-03-07 09:46:20 -0800783 LoadValueDirectFixed(rl_src1, rs_r0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800784
785 // Load RHS into EBX.
buzbee2700f7e2014-03-07 09:46:20 -0800786 LoadValueDirectFixed(rl_src2, rs_r1);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800787
788 // Copy LHS sign bit into EDX.
789 NewLIR0(kx86Cdq32Da);
790
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700791 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800792 // Handle division by zero case.
Mingyao Yange643a172014-04-08 11:02:52 -0700793 GenDivZeroCheck(rs_r1);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800794 }
795
Yixin Shou2ddd1752014-08-26 15:15:13 -0400796 // Check if numerator is 0
797 OpRegImm(kOpCmp, rs_r0, 0);
798 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
799
Mark Mendell2bf31e62014-01-23 12:13:40 -0800800 // Have to catch 0x80000000/-1 case, or we will get an exception!
buzbee2700f7e2014-03-07 09:46:20 -0800801 OpRegImm(kOpCmp, rs_r1, -1);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700802 LIR* minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800803
804 // RHS is -1.
buzbee2700f7e2014-03-07 09:46:20 -0800805 OpRegImm(kOpCmp, rs_r0, 0x80000000);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700806 LIR* minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800807
Yixin Shou2ddd1752014-08-26 15:15:13 -0400808 branch->target = NewLIR0(kPseudoTargetLabel);
809
Mark Mendell2bf31e62014-01-23 12:13:40 -0800810 // In 0x80000000/-1 case.
811 if (!is_div) {
812 // For DIV, EAX is already right. For REM, we need EDX 0.
buzbee2700f7e2014-03-07 09:46:20 -0800813 LoadConstantNoClobber(rs_r2, 0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800814 }
815 LIR* done = NewLIR1(kX86Jmp8, 0);
816
817 // Expected case.
818 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
819 minint_branch->target = minus_one_branch->target;
buzbee091cc402014-03-31 10:14:40 -0700820 NewLIR1(kX86Idivmod32DaR, rs_r1.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800821 done->target = NewLIR0(kPseudoTargetLabel);
822
823 // Result is in EAX for div and EDX for rem.
buzbee091cc402014-03-31 10:14:40 -0700824 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 -0800825 if (!is_div) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000826 rl_result.reg.SetReg(r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800827 }
828 return rl_result;
829}
830
Serban Constantinescu23abec92014-07-02 16:13:38 +0100831bool X86Mir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700832 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800833
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700834 if (is_long && !cu_->target64) {
835 /*
836 * We want to implement the following algorithm
837 * mov eax, low part of arg1
838 * mov edx, high part of arg1
839 * mov ebx, low part of arg2
840 * mov ecx, high part of arg2
841 * mov edi, eax
842 * sub edi, ebx
843 * mov edi, edx
844 * sbb edi, ecx
845 * is_min ? "cmovgel eax, ebx" : "cmovll eax, ebx"
846 * is_min ? "cmovgel edx, ecx" : "cmovll edx, ecx"
847 *
848 * The algorithm above needs 5 registers: a pair for the first operand
849 * (which later will be used as result), a pair for the second operand
850 * and a temp register (e.g. 'edi') for intermediate calculations.
851 * Ideally we have 6 GP caller-save registers in 32-bit mode. They are:
852 * 'eax', 'ebx', 'ecx', 'edx', 'esi' and 'edi'. So there should be
853 * always enough registers to operate on. Practically, there is a pair
854 * of registers 'edi' and 'esi' which holds promoted values and
855 * sometimes should be treated as 'callee save'. If one of the operands
856 * is in the promoted registers then we have enough register to
857 * operate on. Otherwise there is lack of resources and we have to
858 * save 'edi' before calculations and restore after.
859 */
860
861 RegLocation rl_src1 = info->args[0];
862 RegLocation rl_src2 = info->args[2];
863 RegLocation rl_dest = InlineTargetWide(info);
864 int res_vreg, src1_vreg, src2_vreg;
865
Mark Mendella65c1db2014-10-21 17:44:32 -0400866 if (rl_dest.s_reg_low == INVALID_SREG) {
867 // Result is unused, the code is dead. Inlining successful, no code generated.
868 return true;
869 }
870
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700871 /*
872 * If the result register is the same as the second element, then we
873 * need to be careful. The reason is that the first copy will
874 * inadvertently clobber the second element with the first one thus
875 * yielding the wrong result. Thus we do a swap in that case.
876 */
877 res_vreg = mir_graph_->SRegToVReg(rl_dest.s_reg_low);
878 src2_vreg = mir_graph_->SRegToVReg(rl_src2.s_reg_low);
879 if (res_vreg == src2_vreg) {
880 std::swap(rl_src1, rl_src2);
881 }
882
883 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
884 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
885
886 // Pick the first integer as min/max.
887 OpRegCopyWide(rl_result.reg, rl_src1.reg);
888
889 /*
890 * If the integers are both in the same register, then there is
891 * nothing else to do because they are equal and we have already
892 * moved one into the result.
893 */
894 src1_vreg = mir_graph_->SRegToVReg(rl_src1.s_reg_low);
895 src2_vreg = mir_graph_->SRegToVReg(rl_src2.s_reg_low);
896 if (src1_vreg == src2_vreg) {
897 StoreValueWide(rl_dest, rl_result);
898 return true;
899 }
900
901 // Free registers to make some room for the second operand.
902 // But don't try to free ourselves or promoted registers.
903 if (res_vreg != src1_vreg &&
904 IsTemp(rl_src1.reg.GetLow()) && IsTemp(rl_src1.reg.GetHigh())) {
905 FreeTemp(rl_src1.reg);
906 }
907 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
908
909 // Do we have a free register for intermediate calculations?
910 RegStorage tmp = AllocTemp(false);
911 if (tmp == RegStorage::InvalidReg()) {
912 /*
913 * No, will use 'edi'.
914 *
915 * As mentioned above we have 4 temporary and 2 promotable
916 * caller-save registers. Therefore, we assume that a free
917 * register can be allocated only if 'esi' and 'edi' are
918 * already used as operands. If number of promotable registers
919 * increases from 2 to 4 then our assumption fails and operand
920 * data is corrupted.
921 * Let's DCHECK it.
922 */
923 DCHECK(IsTemp(rl_src2.reg.GetLow()) &&
924 IsTemp(rl_src2.reg.GetHigh()) &&
925 IsTemp(rl_result.reg.GetLow()) &&
926 IsTemp(rl_result.reg.GetHigh()));
927 tmp = rs_rDI;
928 NewLIR1(kX86Push32R, tmp.GetReg());
929 }
930
931 // Now we are ready to do calculations.
932 OpRegReg(kOpMov, tmp, rl_result.reg.GetLow());
933 OpRegReg(kOpSub, tmp, rl_src2.reg.GetLow());
934 OpRegReg(kOpMov, tmp, rl_result.reg.GetHigh());
935 OpRegReg(kOpSbc, tmp, rl_src2.reg.GetHigh());
936
937 // Let's put pop 'edi' here to break a bit the dependency chain.
938 if (tmp == rs_rDI) {
939 NewLIR1(kX86Pop32R, tmp.GetReg());
940 }
941
942 // Conditionally move the other integer into the destination register.
943 ConditionCode cc = is_min ? kCondGe : kCondLt;
944 OpCondRegReg(kOpCmov, cc, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
945 OpCondRegReg(kOpCmov, cc, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
946 StoreValueWide(rl_dest, rl_result);
947 return true;
Serban Constantinescu23abec92014-07-02 16:13:38 +0100948 }
949
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800950 // Get the two arguments to the invoke and place them in GP registers.
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800951 RegLocation rl_dest = (is_long) ? InlineTargetWide(info) : InlineTarget(info);
952 if (rl_dest.s_reg_low == INVALID_SREG) {
953 // Result is unused, the code is dead. Inlining successful, no code generated.
954 return true;
955 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 RegLocation rl_src1 = info->args[0];
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700957 RegLocation rl_src2 = (is_long) ? info->args[2] : info->args[1];
958 rl_src1 = (is_long) ? LoadValueWide(rl_src1, kCoreReg) : LoadValue(rl_src1, kCoreReg);
959 rl_src2 = (is_long) ? LoadValueWide(rl_src2, kCoreReg) : LoadValue(rl_src2, kCoreReg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800960
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800962
963 /*
964 * If the result register is the same as the second element, then we need to be careful.
965 * The reason is that the first copy will inadvertently clobber the second element with
966 * the first one thus yielding the wrong result. Thus we do a swap in that case.
967 */
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000968 if (rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800969 std::swap(rl_src1, rl_src2);
970 }
971
972 // Pick the first integer as min/max.
buzbee2700f7e2014-03-07 09:46:20 -0800973 OpRegCopy(rl_result.reg, rl_src1.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800974
975 // If the integers are both in the same register, then there is nothing else to do
976 // because they are equal and we have already moved one into the result.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000977 if (rl_src1.reg.GetReg() != rl_src2.reg.GetReg()) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800978 // It is possible we didn't pick correctly so do the actual comparison now.
buzbee2700f7e2014-03-07 09:46:20 -0800979 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800980
981 // Conditionally move the other integer into the destination register.
982 ConditionCode condition_code = is_min ? kCondGt : kCondLt;
buzbee2700f7e2014-03-07 09:46:20 -0800983 OpCondRegReg(kOpCmov, condition_code, rl_result.reg, rl_src2.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800984 }
985
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700986 if (is_long) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000987 StoreValueWide(rl_dest, rl_result);
988 } else {
Vladimir Markoe508a202013-11-04 15:24:22 +0000989 StoreValue(rl_dest, rl_result);
990 }
991 return true;
992}
993
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700994bool X86Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800995 RegLocation rl_dest = size == k64 ? InlineTargetWide(info) : InlineTarget(info);
996 if (rl_dest.s_reg_low == INVALID_SREG) {
997 // Result is unused, the code is dead. Inlining successful, no code generated.
998 return true;
999 }
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001000 RegLocation rl_src_address = info->args[0]; // long address
1001 RegLocation rl_address;
1002 if (!cu_->target64) {
1003 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[0]
1004 rl_address = LoadValue(rl_src_address, kCoreReg);
1005 } else {
1006 rl_address = LoadValueWide(rl_src_address, kCoreReg);
1007 }
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001008 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1009 // Unaligned access is allowed on x86.
1010 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
1011 if (size == k64) {
1012 StoreValueWide(rl_dest, rl_result);
1013 } else {
1014 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
1015 StoreValue(rl_dest, rl_result);
1016 }
1017 return true;
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001018}
1019
Vladimir Markoe508a202013-11-04 15:24:22 +00001020bool X86Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001021 RegLocation rl_src_address = info->args[0]; // long address
1022 RegLocation rl_address;
1023 if (!cu_->target64) {
1024 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[0]
1025 rl_address = LoadValue(rl_src_address, kCoreReg);
1026 } else {
1027 rl_address = LoadValueWide(rl_src_address, kCoreReg);
1028 }
1029 RegLocation rl_src_value = info->args[2]; // [size] value
1030 RegLocation rl_value;
1031 if (size == k64) {
1032 // Unaligned access is allowed on x86.
1033 rl_value = LoadValueWide(rl_src_value, kCoreReg);
1034 } else {
1035 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
1036 // In 32-bit mode the only EAX..EDX registers can be used with Mov8MR.
1037 if (!cu_->target64 && size == kSignedByte) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001038 rl_src_value = UpdateLocTyped(rl_src_value);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001039 if (rl_src_value.location == kLocPhysReg && !IsByteRegister(rl_src_value.reg)) {
1040 RegStorage temp = AllocateByteRegister();
1041 OpRegCopy(temp, rl_src_value.reg);
1042 rl_value.reg = temp;
1043 } else {
1044 rl_value = LoadValue(rl_src_value, kCoreReg);
1045 }
1046 } else {
1047 rl_value = LoadValue(rl_src_value, kCoreReg);
1048 }
1049 }
1050 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
1051 return true;
Vladimir Markoe508a202013-11-04 15:24:22 +00001052}
1053
buzbee2700f7e2014-03-07 09:46:20 -08001054void X86Mir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
1055 NewLIR5(kX86Lea32RA, r_base.GetReg(), reg1.GetReg(), reg2.GetReg(), scale, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056}
1057
Ian Rogersdd7624d2014-03-14 17:43:00 -07001058void X86Mir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001059 DCHECK_EQ(kX86, cu_->instruction_set);
1060 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
1061}
1062
1063void X86Mir2Lir::OpTlsCmp(ThreadOffset<8> offset, int val) {
1064 DCHECK_EQ(kX86_64, cu_->instruction_set);
Ian Rogers468532e2013-08-05 10:56:33 -07001065 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066}
1067
buzbee2700f7e2014-03-07 09:46:20 -08001068static bool IsInReg(X86Mir2Lir *pMir2Lir, const RegLocation &rl, RegStorage reg) {
1069 return rl.reg.Valid() && rl.reg.GetReg() == reg.GetReg() && (pMir2Lir->IsLive(reg) || rl.home);
Yevgeny Rouband3a2dfa2014-03-18 15:55:16 +07001070}
1071
Vladimir Marko1c282e22013-11-21 14:49:47 +00001072bool X86Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001073 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001074 // Unused - RegLocation rl_src_unsafe = info->args[0];
1075 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
1076 RegLocation rl_src_offset = info->args[2]; // long low
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001077 if (!cu_->target64) {
1078 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
1079 }
Vladimir Markoc29bb612013-11-27 16:47:25 +00001080 RegLocation rl_src_expected = info->args[4]; // int, long or Object
1081 // If is_long, high half is in info->args[5]
1082 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
1083 // If is_long, high half is in info->args[7]
1084
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001085 if (is_long && cu_->target64) {
1086 // 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 -07001087 FlushReg(rs_r0q);
1088 Clobber(rs_r0q);
1089 LockTemp(rs_r0q);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001090
1091 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
1092 RegLocation rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001093 RegLocation rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
1094 LoadValueDirectWide(rl_src_expected, rs_r0q);
Andreas Gampeccc60262014-07-04 18:02:38 -07001095 NewLIR5(kX86LockCmpxchg64AR, rl_object.reg.GetReg(), rl_offset.reg.GetReg(), 0, 0,
1096 rl_new_value.reg.GetReg());
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001097
1098 // After a store we need to insert barrier in case of potential load. Since the
1099 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated.
Hans Boehm48f5c472014-06-27 14:50:10 -07001100 GenMemBarrier(kAnyAny);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001101
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001102 FreeTemp(rs_r0q);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001103 } else if (is_long) {
Yevgeny Rouband3a2dfa2014-03-18 15:55:16 +07001104 // TODO: avoid unnecessary loads of SI and DI when the values are in registers.
1105 // TODO: CFI support.
Vladimir Marko70b797d2013-12-03 15:25:24 +00001106 FlushAllRegs();
1107 LockCallTemps();
buzbee091cc402014-03-31 10:14:40 -07001108 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
1109 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_rBX, rs_rCX);
buzbee2700f7e2014-03-07 09:46:20 -08001110 LoadValueDirectWideFixed(rl_src_expected, r_tmp1);
1111 LoadValueDirectWideFixed(rl_src_new_value, r_tmp2);
buzbee695d13a2014-04-19 13:32:20 -07001112 // FIXME: needs 64-bit update.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001113 const bool obj_in_di = IsInReg(this, rl_src_obj, rs_rDI);
1114 const bool obj_in_si = IsInReg(this, rl_src_obj, rs_rSI);
1115 DCHECK(!obj_in_si || !obj_in_di);
1116 const bool off_in_di = IsInReg(this, rl_src_offset, rs_rDI);
1117 const bool off_in_si = IsInReg(this, rl_src_offset, rs_rSI);
1118 DCHECK(!off_in_si || !off_in_di);
1119 // If obj/offset is in a reg, use that reg. Otherwise, use the empty reg.
1120 RegStorage rs_obj = obj_in_di ? rs_rDI : obj_in_si ? rs_rSI : !off_in_di ? rs_rDI : rs_rSI;
1121 RegStorage rs_off = off_in_si ? rs_rSI : off_in_di ? rs_rDI : !obj_in_si ? rs_rSI : rs_rDI;
1122 bool push_di = (!obj_in_di && !off_in_di) && (rs_obj == rs_rDI || rs_off == rs_rDI);
1123 bool push_si = (!obj_in_si && !off_in_si) && (rs_obj == rs_rSI || rs_off == rs_rSI);
1124 if (push_di) {
1125 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1126 MarkTemp(rs_rDI);
1127 LockTemp(rs_rDI);
1128 }
1129 if (push_si) {
1130 NewLIR1(kX86Push32R, rs_rSI.GetReg());
1131 MarkTemp(rs_rSI);
1132 LockTemp(rs_rSI);
1133 }
1134 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1135 const size_t push_offset = (push_si ? 4u : 0u) + (push_di ? 4u : 0u);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001136 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001137 if (!obj_in_si && !obj_in_di) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001138 LoadWordDisp(rs_rSP, SRegOffset(rl_src_obj.s_reg_low) + push_offset, rs_obj);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001139 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1140 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1141 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - push_offset / 4u;
1142 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
1143 }
1144 if (!off_in_si && !off_in_di) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001145 LoadWordDisp(rs_rSP, SRegOffset(rl_src_offset.s_reg_low) + push_offset, rs_off);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001146 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1147 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1148 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - push_offset / 4u;
1149 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
1150 }
1151 NewLIR4(kX86LockCmpxchg64A, rs_obj.GetReg(), rs_off.GetReg(), 0, 0);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001152
Hans Boehm48f5c472014-06-27 14:50:10 -07001153 // After a store we need to insert barrier to prevent reordering with either
1154 // earlier or later memory accesses. Since
1155 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated,
1156 // and it will be associated with the cmpxchg instruction, preventing both.
1157 GenMemBarrier(kAnyAny);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001158
1159 if (push_si) {
1160 FreeTemp(rs_rSI);
1161 UnmarkTemp(rs_rSI);
1162 NewLIR1(kX86Pop32R, rs_rSI.GetReg());
1163 }
1164 if (push_di) {
1165 FreeTemp(rs_rDI);
1166 UnmarkTemp(rs_rDI);
1167 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
1168 }
Vladimir Marko70b797d2013-12-03 15:25:24 +00001169 FreeCallTemps();
Vladimir Markoc29bb612013-11-27 16:47:25 +00001170 } else {
1171 // EAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in EAX.
buzbee2700f7e2014-03-07 09:46:20 -08001172 FlushReg(rs_r0);
buzbee091cc402014-03-31 10:14:40 -07001173 Clobber(rs_r0);
buzbee2700f7e2014-03-07 09:46:20 -08001174 LockTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001175
buzbeea0cd2d72014-06-01 09:33:49 -07001176 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
buzbee7c02e912014-10-03 13:14:17 -07001177 RegLocation rl_new_value = LoadValue(rl_src_new_value, LocToRegClass(rl_src_new_value));
Vladimir Markoc29bb612013-11-27 16:47:25 +00001178
1179 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
1180 // Mark card for object assuming new value is stored.
buzbee091cc402014-03-31 10:14:40 -07001181 FreeTemp(rs_r0); // Temporarily release EAX for MarkGCCard().
Vladimir Marko743b98c2014-11-24 19:45:41 +00001182 MarkGCCard(0, rl_new_value.reg, rl_object.reg);
buzbee091cc402014-03-31 10:14:40 -07001183 LockTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001184 }
1185
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001186 RegLocation rl_offset;
1187 if (cu_->target64) {
1188 rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
1189 } else {
1190 rl_offset = LoadValue(rl_src_offset, kCoreReg);
1191 }
buzbee2700f7e2014-03-07 09:46:20 -08001192 LoadValueDirect(rl_src_expected, rs_r0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001193 NewLIR5(kX86LockCmpxchgAR, rl_object.reg.GetReg(), rl_offset.reg.GetReg(), 0, 0,
1194 rl_new_value.reg.GetReg());
Vladimir Markoc29bb612013-11-27 16:47:25 +00001195
Hans Boehm48f5c472014-06-27 14:50:10 -07001196 // After a store we need to insert barrier to prevent reordering with either
1197 // earlier or later memory accesses. Since
1198 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated,
1199 // and it will be associated with the cmpxchg instruction, preventing both.
1200 GenMemBarrier(kAnyAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001201
buzbee091cc402014-03-31 10:14:40 -07001202 FreeTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001203 }
1204
1205 // Convert ZF to boolean
1206 RegLocation rl_dest = InlineTarget(info); // boolean place for result
1207 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001208 RegStorage result_reg = rl_result.reg;
1209
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001210 // For 32-bit, SETcc only works with EAX..EDX.
1211 if (!IsByteRegister(result_reg)) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001212 result_reg = AllocateByteRegister();
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001213 }
1214 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondZ);
1215 NewLIR2(kX86Movzx8RR, rl_result.reg.GetReg(), result_reg.GetReg());
1216 if (IsTemp(result_reg)) {
1217 FreeTemp(result_reg);
1218 }
Vladimir Markoc29bb612013-11-27 16:47:25 +00001219 StoreValue(rl_dest, rl_result);
1220 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221}
1222
Yixin Shou8c914c02014-07-28 14:17:09 -04001223void X86Mir2Lir::SwapBits(RegStorage result_reg, int shift, int32_t value) {
1224 RegStorage r_temp = AllocTemp();
1225 OpRegCopy(r_temp, result_reg);
1226 OpRegImm(kOpLsr, result_reg, shift);
1227 OpRegImm(kOpAnd, r_temp, value);
1228 OpRegImm(kOpAnd, result_reg, value);
1229 OpRegImm(kOpLsl, r_temp, shift);
1230 OpRegReg(kOpOr, result_reg, r_temp);
1231 FreeTemp(r_temp);
1232}
1233
1234void X86Mir2Lir::SwapBits64(RegStorage result_reg, int shift, int64_t value) {
1235 RegStorage r_temp = AllocTempWide();
1236 OpRegCopy(r_temp, result_reg);
1237 OpRegImm(kOpLsr, result_reg, shift);
1238 RegStorage r_value = AllocTempWide();
1239 LoadConstantWide(r_value, value);
1240 OpRegReg(kOpAnd, r_temp, r_value);
1241 OpRegReg(kOpAnd, result_reg, r_value);
1242 OpRegImm(kOpLsl, r_temp, shift);
1243 OpRegReg(kOpOr, result_reg, r_temp);
1244 FreeTemp(r_temp);
1245 FreeTemp(r_value);
1246}
1247
1248bool X86Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001249 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info);
1250 if (rl_dest.s_reg_low == INVALID_SREG) {
1251 // Result is unused, the code is dead. Inlining successful, no code generated.
1252 return true;
1253 }
Yixin Shou8c914c02014-07-28 14:17:09 -04001254 RegLocation rl_src_i = info->args[0];
1255 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg)
1256 : LoadValue(rl_src_i, kCoreReg);
Yixin Shou8c914c02014-07-28 14:17:09 -04001257 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1258 if (size == k64) {
1259 if (cu_->instruction_set == kX86_64) {
1260 /* Use one bswap instruction to reverse byte order first and then use 3 rounds of
1261 swapping bits to reverse bits in a long number x. Using bswap to save instructions
1262 compared to generic luni implementation which has 5 rounds of swapping bits.
1263 x = bswap x
1264 x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
1265 x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
1266 x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
1267 */
1268 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1269 SwapBits64(rl_result.reg, 1, 0x5555555555555555);
1270 SwapBits64(rl_result.reg, 2, 0x3333333333333333);
1271 SwapBits64(rl_result.reg, 4, 0x0f0f0f0f0f0f0f0f);
1272 StoreValueWide(rl_dest, rl_result);
1273 return true;
1274 }
1275 RegStorage r_i_low = rl_i.reg.GetLow();
1276 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
1277 // First REV shall clobber rl_result.reg.GetLowReg(), save the value in a temp for the second
1278 // REV.
1279 r_i_low = AllocTemp();
1280 OpRegCopy(r_i_low, rl_i.reg);
1281 }
1282 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1283 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1284 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
1285 FreeTemp(r_i_low);
1286 }
1287 SwapBits(rl_result.reg.GetLow(), 1, 0x55555555);
1288 SwapBits(rl_result.reg.GetLow(), 2, 0x33333333);
1289 SwapBits(rl_result.reg.GetLow(), 4, 0x0f0f0f0f);
1290 SwapBits(rl_result.reg.GetHigh(), 1, 0x55555555);
1291 SwapBits(rl_result.reg.GetHigh(), 2, 0x33333333);
1292 SwapBits(rl_result.reg.GetHigh(), 4, 0x0f0f0f0f);
1293 StoreValueWide(rl_dest, rl_result);
1294 } else {
1295 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1296 SwapBits(rl_result.reg, 1, 0x55555555);
1297 SwapBits(rl_result.reg, 2, 0x33333333);
1298 SwapBits(rl_result.reg, 4, 0x0f0f0f0f);
1299 StoreValue(rl_dest, rl_result);
1300 }
1301 return true;
1302}
1303
buzbee2700f7e2014-03-07 09:46:20 -08001304LIR* X86Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Mark Mendell27dee8b2014-12-01 19:06:12 -05001305 if (cu_->target64) {
1306 // We can do this directly using RIP addressing.
1307 // We don't know the proper offset for the value, so pick one that will force
1308 // 4 byte offset. We will fix this up in the assembler later to have the right
1309 // value.
1310 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
1311 LIR* res = NewLIR3(kX86Mov32RM, reg.GetReg(), kRIPReg, 256);
1312 res->target = target;
1313 res->flags.fixup = kFixupLoad;
1314 return res;
1315 }
1316
Mark Mendell55d0eac2014-02-06 11:02:52 -08001317 CHECK(base_of_code_ != nullptr);
1318
1319 // Address the start of the method
1320 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001321 if (rl_method.wide) {
1322 LoadValueDirectWideFixed(rl_method, reg);
1323 } else {
1324 LoadValueDirectFixed(rl_method, reg);
1325 }
Mark Mendell55d0eac2014-02-06 11:02:52 -08001326 store_method_addr_used_ = true;
1327
1328 // Load the proper value from the literal area.
1329 // We don't know the proper offset for the value, so pick one that will force
1330 // 4 byte offset. We will fix this up in the assembler later to have the right
1331 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001332 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
buzbee2700f7e2014-03-07 09:46:20 -08001333 LIR *res = RawLIR(current_dalvik_offset_, kX86Mov32RM, reg.GetReg(), reg.GetReg(), 256,
1334 0, 0, target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001335 res->target = target;
1336 res->flags.fixup = kFixupLoad;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001337 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338}
1339
buzbee2700f7e2014-03-07 09:46:20 -08001340LIR* X86Mir2Lir::OpVldm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001341 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 LOG(FATAL) << "Unexpected use of OpVldm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001343 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344}
1345
buzbee2700f7e2014-03-07 09:46:20 -08001346LIR* X86Mir2Lir::OpVstm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001347 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 LOG(FATAL) << "Unexpected use of OpVstm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001349 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350}
1351
1352void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1353 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001354 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001355 UNUSED(lit);
buzbee2700f7e2014-03-07 09:46:20 -08001356 RegStorage t_reg = AllocTemp();
1357 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, second_bit - first_bit);
1358 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 FreeTemp(t_reg);
1360 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001361 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 }
1363}
1364
Mingyao Yange643a172014-04-08 11:02:52 -07001365void X86Mir2Lir::GenDivZeroCheckWide(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001366 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001367 DCHECK(reg.Is64Bit());
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001368
Chao-ying Fua0147762014-06-06 18:38:49 -07001369 NewLIR2(kX86Cmp64RI8, reg.GetReg(), 0);
1370 } else {
1371 DCHECK(reg.IsPair());
1372
1373 // We are not supposed to clobber the incoming storage, so allocate a temporary.
1374 RegStorage t_reg = AllocTemp();
1375 // Doing an OR is a quick way to check if both registers are zero. This will set the flags.
1376 OpRegRegReg(kOpOr, t_reg, reg.GetLow(), reg.GetHigh());
1377 // The temp is no longer needed so free it at this time.
1378 FreeTemp(t_reg);
1379 }
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001380
1381 // In case of zero, throw ArithmeticException.
Mingyao Yange643a172014-04-08 11:02:52 -07001382 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001383}
1384
Mingyao Yang80365d92014-04-18 12:10:58 -07001385void X86Mir2Lir::GenArrayBoundsCheck(RegStorage index,
1386 RegStorage array_base,
1387 int len_offset) {
1388 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1389 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001390 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1391 RegStorage index_in, RegStorage array_base_in, int32_t len_offset_in)
1392 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
1393 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001394 }
1395
1396 void Compile() OVERRIDE {
1397 m2l_->ResetRegPool();
1398 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001399 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001400
1401 RegStorage new_index = index_;
1402 // Move index out of kArg1, either directly to kArg0, or to kArg2.
Serguei Katkov4c7cc152014-06-24 00:50:02 +07001403 // TODO: clean-up to check not a number but with type
Andreas Gampeccc60262014-07-04 18:02:38 -07001404 if (index_ == m2l_->TargetReg(kArg1, kNotWide)) {
1405 if (array_base_ == m2l_->TargetReg(kArg0, kRef)) {
1406 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kNotWide), index_);
1407 new_index = m2l_->TargetReg(kArg2, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001408 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001409 m2l_->OpRegCopy(m2l_->TargetReg(kArg0, kNotWide), index_);
1410 new_index = m2l_->TargetReg(kArg0, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001411 }
1412 }
1413 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001414 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1415 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1416 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, new_index,
1417 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001418 }
1419
1420 private:
1421 const RegStorage index_;
1422 const RegStorage array_base_;
1423 const int32_t len_offset_;
1424 };
1425
1426 OpRegMem(kOpCmp, index, array_base, len_offset);
Dave Allison69dfe512014-07-11 17:11:58 +00001427 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001428 LIR* branch = OpCondBranch(kCondUge, nullptr);
1429 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1430 index, array_base, len_offset));
1431}
1432
1433void X86Mir2Lir::GenArrayBoundsCheck(int32_t index,
1434 RegStorage array_base,
1435 int32_t len_offset) {
1436 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1437 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001438 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1439 int32_t index_in, RegStorage array_base_in, int32_t len_offset_in)
1440 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
1441 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001442 }
1443
1444 void Compile() OVERRIDE {
1445 m2l_->ResetRegPool();
1446 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001447 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001448
1449 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001450 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1451 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1452 x86_m2l->LoadConstant(m2l_->TargetReg(kArg0, kNotWide), index_);
1453 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, m2l_->TargetReg(kArg0, kNotWide),
1454 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001455 }
1456
1457 private:
1458 const int32_t index_;
1459 const RegStorage array_base_;
1460 const int32_t len_offset_;
1461 };
1462
1463 NewLIR3(IS_SIMM8(index) ? kX86Cmp32MI8 : kX86Cmp32MI, array_base.GetReg(), len_offset, index);
Dave Allison69dfe512014-07-11 17:11:58 +00001464 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001465 LIR* branch = OpCondBranch(kCondLs, nullptr);
1466 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1467 index, array_base, len_offset));
1468}
1469
Brian Carlstrom7940e442013-07-12 13:46:57 -07001470// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001471LIR* X86Mir2Lir::OpTestSuspend(LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -07001472 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001473 OpTlsCmp(Thread::ThreadFlagsOffset<8>(), 0);
1474 } else {
1475 OpTlsCmp(Thread::ThreadFlagsOffset<4>(), 0);
1476 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 return OpCondBranch((target == NULL) ? kCondNe : kCondEq, target);
1478}
1479
1480// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001481LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 OpRegImm(kOpSub, reg, 1);
Yixin Shoua0dac3e2014-01-23 05:01:22 -08001483 return OpCondBranch(c_code, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484}
1485
buzbee11b63d12013-08-27 07:34:17 -07001486bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001487 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001488 UNUSED(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001489 LOG(FATAL) << "Unexpected use of smallLiteralDive in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001490 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491}
1492
Ian Rogerse2143c02014-03-28 08:47:16 -07001493bool X86Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001494 UNUSED(rl_src, rl_dest, lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001495 LOG(FATAL) << "Unexpected use of easyMultiply in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001496 UNREACHABLE();
Ian Rogerse2143c02014-03-28 08:47:16 -07001497}
1498
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001499LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001500 UNUSED(cond, guide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 LOG(FATAL) << "Unexpected use of OpIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001502 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503}
1504
Dave Allison3da67a52014-04-02 17:03:45 -07001505void X86Mir2Lir::OpEndIT(LIR* it) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001506 UNUSED(it);
Dave Allison3da67a52014-04-02 17:03:45 -07001507 LOG(FATAL) << "Unexpected use of OpEndIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001508 UNREACHABLE();
Dave Allison3da67a52014-04-02 17:03:45 -07001509}
1510
buzbee2700f7e2014-03-07 09:46:20 -08001511void X86Mir2Lir::GenImulRegImm(RegStorage dest, RegStorage src, int val) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001512 switch (val) {
1513 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001514 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001515 break;
1516 case 1:
1517 OpRegCopy(dest, src);
1518 break;
1519 default:
1520 OpRegRegImm(kOpMul, dest, src, val);
1521 break;
1522 }
1523}
1524
buzbee2700f7e2014-03-07 09:46:20 -08001525void X86Mir2Lir::GenImulMemImm(RegStorage dest, int sreg, int displacement, int val) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001526 UNUSED(sreg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001527 // All memory accesses below reference dalvik regs.
1528 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1529
Mark Mendell4708dcd2014-01-22 09:05:18 -08001530 LIR *m;
1531 switch (val) {
1532 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001533 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001534 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001535 case 1: {
1536 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
1537 LoadBaseDisp(rs_rSP, displacement, dest, k32, kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001538 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001539 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001540 default:
buzbee091cc402014-03-31 10:14:40 -07001541 m = NewLIR4(IS_SIMM8(val) ? kX86Imul32RMI8 : kX86Imul32RMI, dest.GetReg(),
Ian Rogersb28c1c02014-11-08 11:21:21 -08001542 rs_rX86_SP_32.GetReg(), displacement, val);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001543 AnnotateDalvikRegAccess(m, displacement >> 2, true /* is_load */, true /* is_64bit */);
1544 break;
1545 }
1546}
1547
Andreas Gampec76c6142014-08-04 16:30:03 -07001548void X86Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001549 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001550 if (!cu_->target64) {
1551 // Some x86 32b ops are fallback.
1552 switch (opcode) {
1553 case Instruction::NOT_LONG:
1554 case Instruction::DIV_LONG:
1555 case Instruction::DIV_LONG_2ADDR:
1556 case Instruction::REM_LONG:
1557 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001558 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001559 return;
1560
1561 default:
1562 // Everything else we can handle.
1563 break;
1564 }
1565 }
1566
1567 switch (opcode) {
1568 case Instruction::NOT_LONG:
1569 GenNotLong(rl_dest, rl_src2);
1570 return;
1571
1572 case Instruction::ADD_LONG:
1573 case Instruction::ADD_LONG_2ADDR:
1574 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1575 return;
1576
1577 case Instruction::SUB_LONG:
1578 case Instruction::SUB_LONG_2ADDR:
1579 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, false);
1580 return;
1581
1582 case Instruction::MUL_LONG:
1583 case Instruction::MUL_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001584 GenMulLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001585 return;
1586
1587 case Instruction::DIV_LONG:
1588 case Instruction::DIV_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001589 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001590 return;
1591
1592 case Instruction::REM_LONG:
1593 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001594 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001595 return;
1596
1597 case Instruction::AND_LONG_2ADDR:
1598 case Instruction::AND_LONG:
1599 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1600 return;
1601
1602 case Instruction::OR_LONG:
1603 case Instruction::OR_LONG_2ADDR:
1604 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1605 return;
1606
1607 case Instruction::XOR_LONG:
1608 case Instruction::XOR_LONG_2ADDR:
1609 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1610 return;
1611
1612 case Instruction::NEG_LONG:
1613 GenNegLong(rl_dest, rl_src2);
1614 return;
1615
1616 default:
1617 LOG(FATAL) << "Invalid long arith op";
1618 return;
1619 }
1620}
1621
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001622bool X86Mir2Lir::GenMulLongConst(RegLocation rl_dest, RegLocation rl_src1, int64_t val, int flags) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001623 // All memory accesses below reference dalvik regs.
1624 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1625
Andreas Gampec76c6142014-08-04 16:30:03 -07001626 if (val == 0) {
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001627 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampec76c6142014-08-04 16:30:03 -07001628 if (cu_->target64) {
1629 OpRegReg(kOpXor, rl_result.reg, rl_result.reg);
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001630 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001631 OpRegReg(kOpXor, rl_result.reg.GetLow(), rl_result.reg.GetLow());
1632 OpRegReg(kOpXor, rl_result.reg.GetHigh(), rl_result.reg.GetHigh());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001633 }
Andreas Gampec76c6142014-08-04 16:30:03 -07001634 StoreValueWide(rl_dest, rl_result);
1635 return true;
1636 } else if (val == 1) {
1637 StoreValueWide(rl_dest, rl_src1);
1638 return true;
1639 } else if (val == 2) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001640 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src1, rl_src1, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001641 return true;
1642 } else if (IsPowerOfTwo(val)) {
Andreas Gampe7e499922015-01-06 08:28:12 -08001643 int shift_amount = CTZ(val);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001644 if (!PartiallyIntersects(rl_src1, rl_dest)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001645 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1646 RegLocation rl_result = GenShiftImmOpLong(Instruction::SHL_LONG, rl_dest, rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001647 shift_amount, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001648 StoreValueWide(rl_dest, rl_result);
1649 return true;
1650 }
1651 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001652
Andreas Gampec76c6142014-08-04 16:30:03 -07001653 // Okay, on 32b just bite the bullet and do it, still better than the general case.
1654 if (!cu_->target64) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001655 int32_t val_lo = Low32Bits(val);
1656 int32_t val_hi = High32Bits(val);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001657 // Prepare for explicit register usage.
1658 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001659 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001660 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1661 int displacement = SRegOffset(rl_src1.s_reg_low);
1662
1663 // ECX <- 1H * 2L
1664 // EAX <- 1L * 2H
1665 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001666 GenImulRegImm(rs_r1, rl_src1.reg.GetHigh(), val_lo);
1667 GenImulRegImm(rs_r0, rl_src1.reg.GetLow(), val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001668 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001669 GenImulMemImm(rs_r1, GetSRegHi(rl_src1.s_reg_low), displacement + HIWORD_OFFSET, val_lo);
1670 GenImulMemImm(rs_r0, rl_src1.s_reg_low, displacement + LOWORD_OFFSET, val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001671 }
1672
1673 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001674 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001675
1676 // EAX <- 2L
buzbee2700f7e2014-03-07 09:46:20 -08001677 LoadConstantNoClobber(rs_r0, val_lo);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001678
1679 // EDX:EAX <- 2L * 1L (double precision)
1680 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001681 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001682 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001683 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001684 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1685 true /* is_load */, true /* is_64bit */);
1686 }
1687
1688 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001689 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001690
1691 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001692 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
1693 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001694 StoreValueWide(rl_dest, rl_result);
Andreas Gampec76c6142014-08-04 16:30:03 -07001695 return true;
1696 }
1697 return false;
1698}
1699
1700void X86Mir2Lir::GenMulLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001701 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001702 if (rl_src1.is_const) {
1703 std::swap(rl_src1, rl_src2);
1704 }
1705
1706 if (rl_src2.is_const) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001707 if (GenMulLongConst(rl_dest, rl_src1, mir_graph_->ConstantValueWide(rl_src2), flags)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001708 return;
1709 }
1710 }
1711
1712 // All memory accesses below reference dalvik regs.
1713 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1714
1715 if (cu_->target64) {
1716 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1717 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1718 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1719 if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1720 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1721 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
1722 } else if (rl_result.reg.GetReg() != rl_src1.reg.GetReg() &&
1723 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1724 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
1725 } else if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1726 rl_result.reg.GetReg() != rl_src2.reg.GetReg()) {
1727 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1728 } else {
1729 OpRegCopy(rl_result.reg, rl_src1.reg);
1730 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1731 }
1732 StoreValueWide(rl_dest, rl_result);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001733 return;
1734 }
1735
Andreas Gampec76c6142014-08-04 16:30:03 -07001736 // Not multiplying by a constant. Do it the hard way
Mark Mendellde99bba2014-02-14 12:15:02 -08001737 // Check for V*V. We can eliminate a multiply in that case, as 2L*1H == 2H*1L.
1738 bool is_square = mir_graph_->SRegToVReg(rl_src1.s_reg_low) ==
1739 mir_graph_->SRegToVReg(rl_src2.s_reg_low);
1740
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001741 // Prepare for explicit register usage.
1742 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001743 rl_src1 = UpdateLocWideTyped(rl_src1);
1744 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001745
1746 // At this point, the VRs are in their home locations.
1747 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1748 bool src2_in_reg = rl_src2.location == kLocPhysReg;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001749 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001750
1751 // ECX <- 1H
1752 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001753 NewLIR2(kX86Mov32RR, rs_r1.GetReg(), rl_src1.reg.GetHighReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001754 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001755 LoadBaseDisp(rs_rSP, SRegOffset(rl_src1.s_reg_low) + HIWORD_OFFSET, rs_r1, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001756 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001757 }
1758
Mark Mendellde99bba2014-02-14 12:15:02 -08001759 if (is_square) {
1760 // Take advantage of the fact that the values are the same.
1761 // ECX <- ECX * 2L (1H * 2L)
1762 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001763 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001764 } else {
1765 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001766 LIR* m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001767 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001768 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1769 true /* is_load */, true /* is_64bit */);
1770 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001771
Mark Mendellde99bba2014-02-14 12:15:02 -08001772 // ECX <- 2*ECX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001773 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001774 } else {
Mark Mendellde99bba2014-02-14 12:15:02 -08001775 // EAX <- 2H
1776 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001777 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetHighReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001778 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001779 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + HIWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001780 kNotVolatile);
Mark Mendellde99bba2014-02-14 12:15:02 -08001781 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001782
Mark Mendellde99bba2014-02-14 12:15:02 -08001783 // EAX <- EAX * 1L (2H * 1L)
1784 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001785 NewLIR2(kX86Imul32RR, rs_r0.GetReg(), rl_src1.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001786 } else {
1787 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001788 LIR *m = NewLIR3(kX86Imul32RM, rs_r0.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001789 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001790 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1791 true /* is_load */, true /* is_64bit */);
1792 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001793
Mark Mendellde99bba2014-02-14 12:15:02 -08001794 // ECX <- ECX * 2L (1H * 2L)
1795 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001796 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001797 } else {
1798 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001799 LIR *m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001800 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001801 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1802 true /* is_load */, true /* is_64bit */);
1803 }
1804
1805 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001806 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001807 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001808
1809 // EAX <- 2L
1810 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001811 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001812 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001813 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + LOWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001814 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001815 }
1816
1817 // EDX:EAX <- 2L * 1L (double precision)
1818 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001819 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001820 } else {
1821 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001822 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001823 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1824 true /* is_load */, true /* is_64bit */);
1825 }
1826
1827 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001828 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001829
1830 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001831 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
buzbee2700f7e2014-03-07 09:46:20 -08001832 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001833 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834}
Mark Mendelle02d48f2014-01-15 11:19:23 -08001835
1836void X86Mir2Lir::GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src,
1837 Instruction::Code op) {
1838 DCHECK_EQ(rl_dest.location, kLocPhysReg);
1839 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
1840 if (rl_src.location == kLocPhysReg) {
1841 // Both operands are in registers.
Serguei Katkovab5545f2014-03-25 10:51:15 +07001842 // But we must ensure that rl_src is in pair
Elena Sayapinadd644502014-07-01 18:39:52 +07001843 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001844 NewLIR2(x86op, rl_dest.reg.GetReg(), rl_src.reg.GetReg());
1845 } else {
1846 rl_src = LoadValueWide(rl_src, kCoreReg);
1847 if (rl_dest.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1848 // The registers are the same, so we would clobber it before the use.
1849 RegStorage temp_reg = AllocTemp();
1850 OpRegCopy(temp_reg, rl_dest.reg);
1851 rl_src.reg.SetHighReg(temp_reg.GetReg());
1852 }
1853 NewLIR2(x86op, rl_dest.reg.GetLowReg(), rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08001854
Chao-ying Fua0147762014-06-06 18:38:49 -07001855 x86op = GetOpcode(op, rl_dest, rl_src, true);
1856 NewLIR2(x86op, rl_dest.reg.GetHighReg(), rl_src.reg.GetHighReg());
Chao-ying Fua0147762014-06-06 18:38:49 -07001857 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08001858 return;
1859 }
1860
1861 // RHS is in memory.
1862 DCHECK((rl_src.location == kLocDalvikFrame) ||
1863 (rl_src.location == kLocCompilerTemp));
Ian Rogersb28c1c02014-11-08 11:21:21 -08001864 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08001865 int displacement = SRegOffset(rl_src.s_reg_low);
1866
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001867 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -07001868 LIR *lir = NewLIR3(x86op, cu_->target64 ? rl_dest.reg.GetReg() : rl_dest.reg.GetLowReg(),
1869 r_base, displacement + LOWORD_OFFSET);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001870 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
1871 true /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07001872 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001873 x86op = GetOpcode(op, rl_dest, rl_src, true);
1874 lir = NewLIR3(x86op, rl_dest.reg.GetHighReg(), r_base, displacement + HIWORD_OFFSET);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001875 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1876 true /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07001877 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001878}
1879
Mark Mendelle02d48f2014-01-15 11:19:23 -08001880void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001881 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001882 if (rl_dest.location == kLocPhysReg) {
1883 // Ensure we are in a register pair
1884 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1885
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001886 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001887 GenLongRegOrMemOp(rl_result, rl_src, op);
1888 StoreFinalValueWide(rl_dest, rl_result);
1889 return;
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001890 } else if (!cu_->target64 && Intersects(rl_src, rl_dest)) {
1891 // Handle the case when src and dest are intersect.
1892 rl_src = LoadValueWide(rl_src, kCoreReg);
1893 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001894 rl_src = UpdateLocWideTyped(rl_src);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001895 GenLongRegOrMemOp(rl_result, rl_src, op);
1896 StoreFinalValueWide(rl_dest, rl_result);
1897 return;
Mark Mendelle02d48f2014-01-15 11:19:23 -08001898 }
1899
1900 // It wasn't in registers, so it better be in memory.
1901 DCHECK((rl_dest.location == kLocDalvikFrame) ||
1902 (rl_dest.location == kLocCompilerTemp));
1903 rl_src = LoadValueWide(rl_src, kCoreReg);
1904
1905 // Operate directly into memory.
1906 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001907 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08001908 int displacement = SRegOffset(rl_dest.s_reg_low);
1909
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001910 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07001911 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET,
Elena Sayapinadd644502014-07-01 18:39:52 +07001912 cu_->target64 ? rl_src.reg.GetReg() : rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08001913 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07001914 true /* is_load */, true /* is64bit */);
1915 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08001916 false /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07001917 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001918 x86op = GetOpcode(op, rl_dest, rl_src, true);
1919 lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, rl_src.reg.GetHighReg());
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001920 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1921 true /* is_load */, true /* is64bit */);
1922 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1923 false /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07001924 }
nikolay serdjuk6b9356c2014-11-13 18:15:23 +06001925
1926 int v_src_reg = mir_graph_->SRegToVReg(rl_src.s_reg_low);
1927 int v_dst_reg = mir_graph_->SRegToVReg(rl_dest.s_reg_low);
1928
1929 // If the left operand is in memory and the right operand is in a register
1930 // and both belong to the same dalvik register then we should clobber the
1931 // right one because it doesn't hold valid data anymore.
1932 if (v_src_reg == v_dst_reg) {
1933 Clobber(rl_src.reg);
1934 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001935}
1936
Mark Mendelle02d48f2014-01-15 11:19:23 -08001937void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src1,
1938 RegLocation rl_src2, Instruction::Code op,
1939 bool is_commutative) {
1940 // Is this really a 2 operand operation?
1941 switch (op) {
1942 case Instruction::ADD_LONG_2ADDR:
1943 case Instruction::SUB_LONG_2ADDR:
1944 case Instruction::AND_LONG_2ADDR:
1945 case Instruction::OR_LONG_2ADDR:
1946 case Instruction::XOR_LONG_2ADDR:
Mark Mendelle87f9b52014-04-30 14:13:18 -04001947 if (GenerateTwoOperandInstructions()) {
1948 GenLongArith(rl_dest, rl_src2, op);
1949 return;
1950 }
1951 break;
1952
Mark Mendelle02d48f2014-01-15 11:19:23 -08001953 default:
1954 break;
1955 }
1956
1957 if (rl_dest.location == kLocPhysReg) {
1958 RegLocation rl_result = LoadValueWide(rl_src1, kCoreReg);
1959
1960 // We are about to clobber the LHS, so it needs to be a temp.
1961 rl_result = ForceTempWide(rl_result);
1962
1963 // Perform the operation using the RHS.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001964 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001965 GenLongRegOrMemOp(rl_result, rl_src2, op);
1966
1967 // And now record that the result is in the temp.
1968 StoreFinalValueWide(rl_dest, rl_result);
1969 return;
1970 }
1971
1972 // It wasn't in registers, so it better be in memory.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001973 DCHECK((rl_dest.location == kLocDalvikFrame) || (rl_dest.location == kLocCompilerTemp));
1974 rl_src1 = UpdateLocWideTyped(rl_src1);
1975 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001976
1977 // Get one of the source operands into temporary register.
1978 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +07001979 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001980 if (IsTemp(rl_src1.reg)) {
1981 GenLongRegOrMemOp(rl_src1, rl_src2, op);
1982 } else if (is_commutative) {
1983 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1984 // We need at least one of them to be a temporary.
1985 if (!IsTemp(rl_src2.reg)) {
1986 rl_src1 = ForceTempWide(rl_src1);
1987 GenLongRegOrMemOp(rl_src1, rl_src2, op);
1988 } else {
1989 GenLongRegOrMemOp(rl_src2, rl_src1, op);
1990 StoreFinalValueWide(rl_dest, rl_src2);
1991 return;
1992 }
1993 } else {
1994 // Need LHS to be the temp.
Mark Mendelle02d48f2014-01-15 11:19:23 -08001995 rl_src1 = ForceTempWide(rl_src1);
Yevgeny Rouban91b6ffa2014-03-07 14:35:44 +07001996 GenLongRegOrMemOp(rl_src1, rl_src2, op);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001997 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08001998 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07001999 if (IsTemp(rl_src1.reg.GetLow()) && IsTemp(rl_src1.reg.GetHigh())) {
2000 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2001 } else if (is_commutative) {
2002 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2003 // We need at least one of them to be a temporary.
2004 if (!(IsTemp(rl_src2.reg.GetLow()) && IsTemp(rl_src2.reg.GetHigh()))) {
2005 rl_src1 = ForceTempWide(rl_src1);
2006 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2007 } else {
2008 GenLongRegOrMemOp(rl_src2, rl_src1, op);
2009 StoreFinalValueWide(rl_dest, rl_src2);
2010 return;
2011 }
2012 } else {
2013 // Need LHS to be the temp.
2014 rl_src1 = ForceTempWide(rl_src1);
2015 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2016 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002017 }
2018
2019 StoreFinalValueWide(rl_dest, rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002020}
2021
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002022void X86Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002023 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002024 rl_src = LoadValueWide(rl_src, kCoreReg);
2025 RegLocation rl_result;
2026 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2027 OpRegCopy(rl_result.reg, rl_src.reg);
2028 OpReg(kOpNot, rl_result.reg);
2029 StoreValueWide(rl_dest, rl_result);
2030 } else {
2031 LOG(FATAL) << "Unexpected use GenNotLong()";
2032 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002033}
2034
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002035void X86Mir2Lir::GenDivRemLongLit(RegLocation rl_dest, RegLocation rl_src,
2036 int64_t imm, bool is_div) {
2037 if (imm == 0) {
2038 GenDivZeroException();
2039 } else if (imm == 1) {
2040 if (is_div) {
2041 // x / 1 == x.
2042 StoreValueWide(rl_dest, rl_src);
2043 } else {
2044 // x % 1 == 0.
2045 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2046 LoadConstantWide(rl_result.reg, 0);
2047 StoreValueWide(rl_dest, rl_result);
2048 }
2049 } else if (imm == -1) { // handle 0x8000000000000000 / -1 special case.
2050 if (is_div) {
2051 rl_src = LoadValueWide(rl_src, kCoreReg);
2052 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2053 RegStorage rs_temp = AllocTempWide();
2054
2055 OpRegCopy(rl_result.reg, rl_src.reg);
2056 LoadConstantWide(rs_temp, 0x8000000000000000);
2057
2058 // If x == MIN_LONG, return MIN_LONG.
2059 OpRegReg(kOpCmp, rl_src.reg, rs_temp);
2060 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
2061
2062 // For x != MIN_LONG, x / -1 == -x.
2063 OpReg(kOpNeg, rl_result.reg);
2064
2065 minint_branch->target = NewLIR0(kPseudoTargetLabel);
2066 FreeTemp(rs_temp);
2067 StoreValueWide(rl_dest, rl_result);
2068 } else {
2069 // x % -1 == 0.
2070 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2071 LoadConstantWide(rl_result.reg, 0);
2072 StoreValueWide(rl_dest, rl_result);
2073 }
2074 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
2075 // Division using shifting.
2076 rl_src = LoadValueWide(rl_src, kCoreReg);
2077 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2078 if (IsSameReg(rl_result.reg, rl_src.reg)) {
2079 RegStorage rs_temp = AllocTypedTempWide(false, kCoreReg);
2080 rl_result.reg.SetReg(rs_temp.GetReg());
2081 }
2082 LoadConstantWide(rl_result.reg, std::abs(imm) - 1);
2083 OpRegReg(kOpAdd, rl_result.reg, rl_src.reg);
2084 NewLIR2(kX86Test64RR, rl_src.reg.GetReg(), rl_src.reg.GetReg());
2085 OpCondRegReg(kOpCmov, kCondPl, rl_result.reg, rl_src.reg);
Andreas Gampe7e499922015-01-06 08:28:12 -08002086 int shift_amount = CTZ(imm);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002087 OpRegImm(kOpAsr, rl_result.reg, shift_amount);
2088 if (imm < 0) {
2089 OpReg(kOpNeg, rl_result.reg);
2090 }
2091 StoreValueWide(rl_dest, rl_result);
2092 } else {
2093 CHECK(imm <= -2 || imm >= 2);
2094
2095 FlushReg(rs_r0q);
2096 Clobber(rs_r0q);
2097 LockTemp(rs_r0q);
2098 FlushReg(rs_r2q);
2099 Clobber(rs_r2q);
2100 LockTemp(rs_r2q);
2101
Mark Mendell3a91f442014-09-02 12:44:24 -04002102 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
2103 is_div ? rs_r2q : rs_r0q, INVALID_SREG, INVALID_SREG};
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002104
2105 // Use H.S.Warren's Hacker's Delight Chapter 10 and
2106 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
2107 int64_t magic;
2108 int shift;
2109 CalculateMagicAndShift(imm, magic, shift, true /* is_long */);
2110
2111 /*
2112 * For imm >= 2,
2113 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
2114 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
2115 * For imm <= -2,
2116 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
2117 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
2118 * We implement this algorithm in the following way:
2119 * 1. multiply magic number m and numerator n, get the higher 64bit result in RDX
2120 * 2. if imm > 0 and magic < 0, add numerator to RDX
2121 * if imm < 0 and magic > 0, sub numerator from RDX
2122 * 3. if S !=0, SAR S bits for RDX
2123 * 4. add 1 to RDX if RDX < 0
2124 * 5. Thus, RDX is the quotient
2125 */
2126
Mark Mendell3a91f442014-09-02 12:44:24 -04002127 // RAX = magic.
2128 LoadConstantWide(rs_r0q, magic);
2129
2130 // Multiply by numerator.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002131 RegStorage numerator_reg;
2132 if (!is_div || (imm > 0 && magic < 0) || (imm < 0 && magic > 0)) {
2133 // We will need the value later.
2134 rl_src = LoadValueWide(rl_src, kCoreReg);
2135 numerator_reg = rl_src.reg;
Mark Mendell3a91f442014-09-02 12:44:24 -04002136
2137 // RDX:RAX = magic * numerator.
2138 NewLIR1(kX86Imul64DaR, numerator_reg.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002139 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002140 // Only need this once. Multiply directly from the value.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002141 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendell3a91f442014-09-02 12:44:24 -04002142 if (rl_src.location != kLocPhysReg) {
2143 // Okay, we can do this from memory.
2144 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2145 int displacement = SRegOffset(rl_src.s_reg_low);
2146 // RDX:RAX = magic * numerator.
Ian Rogersb28c1c02014-11-08 11:21:21 -08002147 LIR *m = NewLIR2(kX86Imul64DaM, rs_rX86_SP_32.GetReg(), displacement);
Mark Mendell3a91f442014-09-02 12:44:24 -04002148 AnnotateDalvikRegAccess(m, displacement >> 2,
2149 true /* is_load */, true /* is_64bit */);
2150 } else {
2151 // RDX:RAX = magic * numerator.
2152 NewLIR1(kX86Imul64DaR, rl_src.reg.GetReg());
2153 }
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002154 }
2155
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002156 if (imm > 0 && magic < 0) {
2157 // Add numerator to RDX.
2158 DCHECK(numerator_reg.Valid());
2159 OpRegReg(kOpAdd, rs_r2q, numerator_reg);
2160 } else if (imm < 0 && magic > 0) {
2161 DCHECK(numerator_reg.Valid());
2162 OpRegReg(kOpSub, rs_r2q, numerator_reg);
2163 }
2164
2165 // Do we need the shift?
2166 if (shift != 0) {
2167 // Shift RDX by 'shift' bits.
2168 OpRegImm(kOpAsr, rs_r2q, shift);
2169 }
2170
2171 // Move RDX to RAX.
2172 OpRegCopyWide(rs_r0q, rs_r2q);
2173
2174 // Move sign bit to bit 0, zeroing the rest.
2175 OpRegImm(kOpLsr, rs_r2q, 63);
2176
2177 // RDX = RDX + RAX.
2178 OpRegReg(kOpAdd, rs_r2q, rs_r0q);
2179
2180 // Quotient is in RDX.
2181 if (!is_div) {
2182 // We need to compute the remainder.
2183 // Remainder is divisor - (quotient * imm).
2184 DCHECK(numerator_reg.Valid());
2185 OpRegCopyWide(rs_r0q, numerator_reg);
2186
2187 // Imul doesn't support 64-bit imms.
2188 if (imm > std::numeric_limits<int32_t>::max() ||
2189 imm < std::numeric_limits<int32_t>::min()) {
2190 RegStorage rs_temp = AllocTempWide();
2191 LoadConstantWide(rs_temp, imm);
2192
2193 // RAX = numerator * imm.
2194 NewLIR2(kX86Imul64RR, rs_r2q.GetReg(), rs_temp.GetReg());
2195
2196 FreeTemp(rs_temp);
2197 } else {
2198 // RAX = numerator * imm.
2199 int short_imm = static_cast<int>(imm);
2200 NewLIR3(kX86Imul64RRI, rs_r2q.GetReg(), rs_r2q.GetReg(), short_imm);
2201 }
2202
Mark Mendell3a91f442014-09-02 12:44:24 -04002203 // RAX -= RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002204 OpRegReg(kOpSub, rs_r0q, rs_r2q);
2205
Mark Mendell3a91f442014-09-02 12:44:24 -04002206 // Result in RAX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002207 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002208 // Result in RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002209 }
2210 StoreValueWide(rl_dest, rl_result);
2211 FreeTemp(rs_r0q);
2212 FreeTemp(rs_r2q);
2213 }
2214}
2215
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002216void X86Mir2Lir::GenDivRemLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002217 RegLocation rl_src2, bool is_div, int flags) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002218 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002219 LOG(FATAL) << "Unexpected use GenDivRemLong()";
2220 return;
2221 }
2222
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002223 if (rl_src2.is_const) {
2224 DCHECK(rl_src2.wide);
2225 int64_t imm = mir_graph_->ConstantValueWide(rl_src2);
2226 GenDivRemLongLit(rl_dest, rl_src1, imm, is_div);
2227 return;
2228 }
2229
Chao-ying Fua0147762014-06-06 18:38:49 -07002230 // We have to use fixed registers, so flush all the temps.
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002231 // Prepare for explicit register usage.
2232 ExplicitTempRegisterLock(this, 4, &rs_r0q, &rs_r1q, &rs_r2q, &rs_r6q);
Chao-ying Fua0147762014-06-06 18:38:49 -07002233
2234 // Load LHS into RAX.
2235 LoadValueDirectWideFixed(rl_src1, rs_r0q);
2236
2237 // Load RHS into RCX.
2238 LoadValueDirectWideFixed(rl_src2, rs_r1q);
2239
2240 // Copy LHS sign bit into RDX.
2241 NewLIR0(kx86Cqo64Da);
2242
2243 // Handle division by zero case.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002244 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2245 GenDivZeroCheckWide(rs_r1q);
2246 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002247
2248 // Have to catch 0x8000000000000000/-1 case, or we will get an exception!
2249 NewLIR2(kX86Cmp64RI8, rs_r1q.GetReg(), -1);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002250 LIR* minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002251
2252 // RHS is -1.
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002253 LoadConstantWide(rs_r6q, 0x8000000000000000);
2254 NewLIR2(kX86Cmp64RR, rs_r0q.GetReg(), rs_r6q.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002255 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002256
2257 // In 0x8000000000000000/-1 case.
2258 if (!is_div) {
2259 // For DIV, RAX is already right. For REM, we need RDX 0.
2260 NewLIR2(kX86Xor64RR, rs_r2q.GetReg(), rs_r2q.GetReg());
2261 }
2262 LIR* done = NewLIR1(kX86Jmp8, 0);
2263
2264 // Expected case.
2265 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
2266 minint_branch->target = minus_one_branch->target;
2267 NewLIR1(kX86Idivmod64DaR, rs_r1q.GetReg());
2268 done->target = NewLIR0(kPseudoTargetLabel);
2269
2270 // Result is in RAX for div and RDX for rem.
2271 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, rs_r0q, INVALID_SREG, INVALID_SREG};
2272 if (!is_div) {
2273 rl_result.reg.SetReg(r2q);
2274 }
2275
2276 StoreValueWide(rl_dest, rl_result);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002277}
2278
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002279void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002280 rl_src = LoadValueWide(rl_src, kCoreReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002281 RegLocation rl_result;
Elena Sayapinadd644502014-07-01 18:39:52 +07002282 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002283 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2284 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
2285 } else {
2286 rl_result = ForceTempWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002287 OpRegReg(kOpNeg, rl_result.reg.GetLow(), rl_result.reg.GetLow()); // rLow = -rLow
2288 OpRegImm(kOpAdc, rl_result.reg.GetHigh(), 0); // rHigh = rHigh + CF
2289 OpRegReg(kOpNeg, rl_result.reg.GetHigh(), rl_result.reg.GetHigh()); // rHigh = -rHigh
Mark Mendelle02d48f2014-01-15 11:19:23 -08002290 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002291 StoreValueWide(rl_dest, rl_result);
2292}
2293
buzbee091cc402014-03-31 10:14:40 -07002294void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<4> thread_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002295 DCHECK_EQ(kX86, cu_->instruction_set);
2296 X86OpCode opcode = kX86Bkpt;
2297 switch (op) {
2298 case kOpCmp: opcode = kX86Cmp32RT; break;
2299 case kOpMov: opcode = kX86Mov32RT; break;
2300 default:
2301 LOG(FATAL) << "Bad opcode: " << op;
2302 break;
2303 }
2304 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
2305}
2306
2307void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<8> thread_offset) {
2308 DCHECK_EQ(kX86_64, cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002309 X86OpCode opcode = kX86Bkpt;
Elena Sayapinadd644502014-07-01 18:39:52 +07002310 if (cu_->target64 && r_dest.Is64BitSolo()) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07002311 switch (op) {
2312 case kOpCmp: opcode = kX86Cmp64RT; break;
2313 case kOpMov: opcode = kX86Mov64RT; break;
2314 default:
2315 LOG(FATAL) << "Bad opcode(OpRegThreadMem 64): " << op;
2316 break;
2317 }
2318 } else {
2319 switch (op) {
2320 case kOpCmp: opcode = kX86Cmp32RT; break;
2321 case kOpMov: opcode = kX86Mov32RT; break;
2322 default:
2323 LOG(FATAL) << "Bad opcode: " << op;
2324 break;
2325 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002326 }
buzbee091cc402014-03-31 10:14:40 -07002327 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002328}
2329
2330/*
2331 * Generate array load
2332 */
2333void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002334 RegLocation rl_index, RegLocation rl_dest, int scale) {
Mark Mendellca541342014-10-15 16:59:49 -04002335 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002336 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002337 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002338 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002339
Mark Mendell343adb52013-12-18 06:02:17 -08002340 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07002341 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002342 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2343 } else {
2344 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2345 }
2346
Mark Mendell343adb52013-12-18 06:02:17 -08002347 bool constant_index = rl_index.is_const;
2348 int32_t constant_index_value = 0;
2349 if (!constant_index) {
2350 rl_index = LoadValue(rl_index, kCoreReg);
2351 } else {
2352 constant_index_value = mir_graph_->ConstantValue(rl_index);
2353 // If index is constant, just fold it into the data offset
2354 data_offset += constant_index_value << scale;
2355 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002356 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002357 }
2358
Brian Carlstrom7940e442013-07-12 13:46:57 -07002359 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002360 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002361
2362 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002363 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002364 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002365 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002366 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002367 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002368 }
Mark Mendell343adb52013-12-18 06:02:17 -08002369 rl_result = EvalLoc(rl_dest, reg_class, true);
Vladimir Marko3bf7c602014-05-07 14:55:43 +01002370 LoadBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_result.reg, size);
buzbee695d13a2014-04-19 13:32:20 -07002371 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002372 StoreValueWide(rl_dest, rl_result);
2373 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002374 StoreValue(rl_dest, rl_result);
2375 }
2376}
2377
2378/*
2379 * Generate array store
2380 *
2381 */
2382void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002383 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Mark Mendellca541342014-10-15 16:59:49 -04002384 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002385 int len_offset = mirror::Array::LengthOffset().Int32Value();
2386 int data_offset;
2387
buzbee695d13a2014-04-19 13:32:20 -07002388 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002389 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2390 } else {
2391 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2392 }
2393
buzbeea0cd2d72014-06-01 09:33:49 -07002394 rl_array = LoadValue(rl_array, kRefReg);
Mark Mendell343adb52013-12-18 06:02:17 -08002395 bool constant_index = rl_index.is_const;
2396 int32_t constant_index_value = 0;
2397 if (!constant_index) {
2398 rl_index = LoadValue(rl_index, kCoreReg);
2399 } else {
2400 // If index is constant, just fold it into the data offset
2401 constant_index_value = mir_graph_->ConstantValue(rl_index);
2402 data_offset += constant_index_value << scale;
2403 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002404 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002405 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002406
2407 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002408 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002409
2410 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002411 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002412 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002413 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002414 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002415 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002416 }
buzbee695d13a2014-04-19 13:32:20 -07002417 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002418 rl_src = LoadValueWide(rl_src, reg_class);
2419 } else {
2420 rl_src = LoadValue(rl_src, reg_class);
2421 }
2422 // If the src reg can't be byte accessed, move it to a temp first.
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002423 if ((size == kSignedByte || size == kUnsignedByte) && !IsByteRegister(rl_src.reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08002424 RegStorage temp = AllocTemp();
2425 OpRegCopy(temp, rl_src.reg);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002426 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, temp, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002427 } else {
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002428 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_src.reg, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002429 }
Ian Rogersa9a82542013-10-04 11:17:26 -07002430 if (card_mark) {
Ian Rogers773aab12013-10-14 13:50:10 -07002431 // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
Mark Mendell343adb52013-12-18 06:02:17 -08002432 if (!constant_index) {
buzbee091cc402014-03-31 10:14:40 -07002433 FreeTemp(rl_index.reg);
Mark Mendell343adb52013-12-18 06:02:17 -08002434 }
Vladimir Marko743b98c2014-11-24 19:45:41 +00002435 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002436 }
2437}
2438
Mark Mendell4708dcd2014-01-22 09:05:18 -08002439RegLocation X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002440 RegLocation rl_src, int shift_amount, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002441 UNUSED(flags);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002442 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Elena Sayapinadd644502014-07-01 18:39:52 +07002443 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002444 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
2445 switch (opcode) {
2446 case Instruction::SHL_LONG:
2447 case Instruction::SHL_LONG_2ADDR:
2448 op = kOpLsl;
2449 break;
2450 case Instruction::SHR_LONG:
2451 case Instruction::SHR_LONG_2ADDR:
2452 op = kOpAsr;
2453 break;
2454 case Instruction::USHR_LONG:
2455 case Instruction::USHR_LONG_2ADDR:
2456 op = kOpLsr;
2457 break;
2458 default:
2459 LOG(FATAL) << "Unexpected case";
2460 }
2461 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
2462 } else {
2463 switch (opcode) {
2464 case Instruction::SHL_LONG:
2465 case Instruction::SHL_LONG_2ADDR:
2466 DCHECK_NE(shift_amount, 1); // Prevent a double store from happening.
2467 if (shift_amount == 32) {
2468 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2469 LoadConstant(rl_result.reg.GetLow(), 0);
2470 } else if (shift_amount > 31) {
2471 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2472 NewLIR2(kX86Sal32RI, rl_result.reg.GetHighReg(), shift_amount - 32);
2473 LoadConstant(rl_result.reg.GetLow(), 0);
2474 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002475 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002476 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2477 NewLIR3(kX86Shld32RRI, rl_result.reg.GetHighReg(), rl_result.reg.GetLowReg(),
2478 shift_amount);
2479 NewLIR2(kX86Sal32RI, rl_result.reg.GetLowReg(), shift_amount);
2480 }
2481 break;
2482 case Instruction::SHR_LONG:
2483 case Instruction::SHR_LONG_2ADDR:
2484 if (shift_amount == 32) {
2485 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2486 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2487 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2488 } else if (shift_amount > 31) {
2489 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2490 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2491 NewLIR2(kX86Sar32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2492 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2493 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002494 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002495 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2496 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2497 shift_amount);
2498 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), shift_amount);
2499 }
2500 break;
2501 case Instruction::USHR_LONG:
2502 case Instruction::USHR_LONG_2ADDR:
2503 if (shift_amount == 32) {
2504 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2505 LoadConstant(rl_result.reg.GetHigh(), 0);
2506 } else if (shift_amount > 31) {
2507 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2508 NewLIR2(kX86Shr32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2509 LoadConstant(rl_result.reg.GetHigh(), 0);
2510 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002511 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002512 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2513 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2514 shift_amount);
2515 NewLIR2(kX86Shr32RI, rl_result.reg.GetHighReg(), shift_amount);
2516 }
2517 break;
2518 default:
2519 LOG(FATAL) << "Unexpected case";
2520 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08002521 }
2522 return rl_result;
2523}
2524
Brian Carlstrom7940e442013-07-12 13:46:57 -07002525void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002526 RegLocation rl_src, RegLocation rl_shift, int flags) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002527 // Per spec, we only care about low 6 bits of shift amount.
2528 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
2529 if (shift_amount == 0) {
2530 rl_src = LoadValueWide(rl_src, kCoreReg);
2531 StoreValueWide(rl_dest, rl_src);
2532 return;
2533 } else if (shift_amount == 1 &&
2534 (opcode == Instruction::SHL_LONG || opcode == Instruction::SHL_LONG_2ADDR)) {
2535 // Need to handle this here to avoid calling StoreValueWide twice.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002536 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src, rl_src, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002537 return;
2538 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07002539 if (PartiallyIntersects(rl_src, rl_dest)) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002540 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
2541 return;
2542 }
2543 rl_src = LoadValueWide(rl_src, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002544 RegLocation rl_result = GenShiftImmOpLong(opcode, rl_dest, rl_src, shift_amount, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002545 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002546}
2547
2548void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002549 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
2550 int flags) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002551 bool isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002552 switch (opcode) {
2553 case Instruction::ADD_LONG:
2554 case Instruction::AND_LONG:
2555 case Instruction::OR_LONG:
2556 case Instruction::XOR_LONG:
2557 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002558 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002559 } else {
2560 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002561 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002562 }
2563 break;
2564 case Instruction::SUB_LONG:
2565 case Instruction::SUB_LONG_2ADDR:
2566 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002567 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002568 } else {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002569 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002570 isConstSuccess = true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002571 }
2572 break;
2573 case Instruction::ADD_LONG_2ADDR:
2574 case Instruction::OR_LONG_2ADDR:
2575 case Instruction::XOR_LONG_2ADDR:
2576 case Instruction::AND_LONG_2ADDR:
2577 if (rl_src2.is_const) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04002578 if (GenerateTwoOperandInstructions()) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002579 isConstSuccess = GenLongImm(rl_dest, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002580 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002581 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002582 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002583 } else {
2584 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002585 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002586 }
2587 break;
2588 default:
Chao-ying Fua0147762014-06-06 18:38:49 -07002589 isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002590 break;
2591 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002592
2593 if (!isConstSuccess) {
2594 // Default - bail to non-const handler.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002595 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002596 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002597}
2598
2599bool X86Mir2Lir::IsNoOp(Instruction::Code op, int32_t value) {
2600 switch (op) {
2601 case Instruction::AND_LONG_2ADDR:
2602 case Instruction::AND_LONG:
2603 return value == -1;
2604 case Instruction::OR_LONG:
2605 case Instruction::OR_LONG_2ADDR:
2606 case Instruction::XOR_LONG:
2607 case Instruction::XOR_LONG_2ADDR:
2608 return value == 0;
2609 default:
2610 return false;
2611 }
2612}
2613
2614X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
2615 bool is_high_op) {
2616 bool rhs_in_mem = rhs.location != kLocPhysReg;
2617 bool dest_in_mem = dest.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002618 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002619 DCHECK(!rhs_in_mem || !dest_in_mem);
2620 switch (op) {
2621 case Instruction::ADD_LONG:
2622 case Instruction::ADD_LONG_2ADDR:
2623 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002624 return is64Bit ? kX86Add64MR : is_high_op ? kX86Adc32MR : kX86Add32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002625 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002626 return is64Bit ? kX86Add64RM : is_high_op ? kX86Adc32RM : kX86Add32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002627 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002628 return is64Bit ? kX86Add64RR : is_high_op ? kX86Adc32RR : kX86Add32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002629 case Instruction::SUB_LONG:
2630 case Instruction::SUB_LONG_2ADDR:
2631 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002632 return is64Bit ? kX86Sub64MR : is_high_op ? kX86Sbb32MR : kX86Sub32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002633 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002634 return is64Bit ? kX86Sub64RM : is_high_op ? kX86Sbb32RM : kX86Sub32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002635 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002636 return is64Bit ? kX86Sub64RR : is_high_op ? kX86Sbb32RR : kX86Sub32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002637 case Instruction::AND_LONG_2ADDR:
2638 case Instruction::AND_LONG:
2639 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002640 return is64Bit ? kX86And64MR : kX86And32MR;
2641 }
2642 if (is64Bit) {
2643 return rhs_in_mem ? kX86And64RM : kX86And64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002644 }
2645 return rhs_in_mem ? kX86And32RM : kX86And32RR;
2646 case Instruction::OR_LONG:
2647 case Instruction::OR_LONG_2ADDR:
2648 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002649 return is64Bit ? kX86Or64MR : kX86Or32MR;
2650 }
2651 if (is64Bit) {
2652 return rhs_in_mem ? kX86Or64RM : kX86Or64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002653 }
2654 return rhs_in_mem ? kX86Or32RM : kX86Or32RR;
2655 case Instruction::XOR_LONG:
2656 case Instruction::XOR_LONG_2ADDR:
2657 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002658 return is64Bit ? kX86Xor64MR : kX86Xor32MR;
2659 }
2660 if (is64Bit) {
2661 return rhs_in_mem ? kX86Xor64RM : kX86Xor64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002662 }
2663 return rhs_in_mem ? kX86Xor32RM : kX86Xor32RR;
2664 default:
2665 LOG(FATAL) << "Unexpected opcode: " << op;
2666 return kX86Add32RR;
2667 }
2668}
2669
2670X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op,
2671 int32_t value) {
2672 bool in_mem = loc.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002673 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002674 bool byte_imm = IS_SIMM8(value);
buzbee091cc402014-03-31 10:14:40 -07002675 DCHECK(in_mem || !loc.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002676 switch (op) {
2677 case Instruction::ADD_LONG:
2678 case Instruction::ADD_LONG_2ADDR:
2679 if (byte_imm) {
2680 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002681 return is64Bit ? kX86Add64MI8 : is_high_op ? kX86Adc32MI8 : kX86Add32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002682 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002683 return is64Bit ? kX86Add64RI8 : is_high_op ? kX86Adc32RI8 : kX86Add32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002684 }
2685 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002686 return is64Bit ? kX86Add64MI : is_high_op ? kX86Adc32MI : kX86Add32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002687 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002688 return is64Bit ? kX86Add64RI : is_high_op ? kX86Adc32RI : kX86Add32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002689 case Instruction::SUB_LONG:
2690 case Instruction::SUB_LONG_2ADDR:
2691 if (byte_imm) {
2692 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002693 return is64Bit ? kX86Sub64MI8 : is_high_op ? kX86Sbb32MI8 : kX86Sub32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002694 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002695 return is64Bit ? kX86Sub64RI8 : is_high_op ? kX86Sbb32RI8 : kX86Sub32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002696 }
2697 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002698 return is64Bit ? kX86Sub64MI : is_high_op ? kX86Sbb32MI : kX86Sub32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002699 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002700 return is64Bit ? kX86Sub64RI : is_high_op ? kX86Sbb32RI : kX86Sub32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002701 case Instruction::AND_LONG_2ADDR:
2702 case Instruction::AND_LONG:
2703 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002704 if (is64Bit) {
2705 return in_mem ? kX86And64MI8 : kX86And64RI8;
2706 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002707 return in_mem ? kX86And32MI8 : kX86And32RI8;
2708 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002709 if (is64Bit) {
2710 return in_mem ? kX86And64MI : kX86And64RI;
2711 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002712 return in_mem ? kX86And32MI : kX86And32RI;
2713 case Instruction::OR_LONG:
2714 case Instruction::OR_LONG_2ADDR:
2715 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002716 if (is64Bit) {
2717 return in_mem ? kX86Or64MI8 : kX86Or64RI8;
2718 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002719 return in_mem ? kX86Or32MI8 : kX86Or32RI8;
2720 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002721 if (is64Bit) {
2722 return in_mem ? kX86Or64MI : kX86Or64RI;
2723 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002724 return in_mem ? kX86Or32MI : kX86Or32RI;
2725 case Instruction::XOR_LONG:
2726 case Instruction::XOR_LONG_2ADDR:
2727 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002728 if (is64Bit) {
2729 return in_mem ? kX86Xor64MI8 : kX86Xor64RI8;
2730 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002731 return in_mem ? kX86Xor32MI8 : kX86Xor32RI8;
2732 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002733 if (is64Bit) {
2734 return in_mem ? kX86Xor64MI : kX86Xor64RI;
2735 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002736 return in_mem ? kX86Xor32MI : kX86Xor32RI;
2737 default:
2738 LOG(FATAL) << "Unexpected opcode: " << op;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002739 UNREACHABLE();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002740 }
2741}
2742
Chao-ying Fua0147762014-06-06 18:38:49 -07002743bool X86Mir2Lir::GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002744 DCHECK(rl_src.is_const);
2745 int64_t val = mir_graph_->ConstantValueWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002746
Elena Sayapinadd644502014-07-01 18:39:52 +07002747 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002748 // We can do with imm only if it fits 32 bit
2749 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2750 return false;
2751 }
2752
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002753 rl_dest = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002754
2755 if ((rl_dest.location == kLocDalvikFrame) ||
2756 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002757 int r_base = rs_rX86_SP_32.GetReg();
Chao-ying Fua0147762014-06-06 18:38:49 -07002758 int displacement = SRegOffset(rl_dest.s_reg_low);
2759
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002760 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002761 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
2762 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val);
2763 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2764 true /* is_load */, true /* is64bit */);
2765 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2766 false /* is_load */, true /* is64bit */);
2767 return true;
2768 }
2769
2770 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2771 DCHECK_EQ(rl_result.location, kLocPhysReg);
2772 DCHECK(!rl_result.reg.IsFloat());
2773
2774 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2775 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2776
2777 StoreValueWide(rl_dest, rl_result);
2778 return true;
2779 }
2780
Mark Mendelle02d48f2014-01-15 11:19:23 -08002781 int32_t val_lo = Low32Bits(val);
2782 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002783 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002784
2785 // Can we just do this into memory?
2786 if ((rl_dest.location == kLocDalvikFrame) ||
2787 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002788 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002789 int displacement = SRegOffset(rl_dest.s_reg_low);
2790
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002791 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002792 if (!IsNoOp(op, val_lo)) {
2793 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002794 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002795 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002796 true /* is_load */, true /* is64bit */);
2797 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002798 false /* is_load */, true /* is64bit */);
2799 }
2800 if (!IsNoOp(op, val_hi)) {
2801 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
buzbee2700f7e2014-03-07 09:46:20 -08002802 LIR *lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002803 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002804 true /* is_load */, true /* is64bit */);
2805 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002806 false /* is_load */, true /* is64bit */);
2807 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002808 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002809 }
2810
2811 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2812 DCHECK_EQ(rl_result.location, kLocPhysReg);
buzbee091cc402014-03-31 10:14:40 -07002813 DCHECK(!rl_result.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002814
2815 if (!IsNoOp(op, val_lo)) {
2816 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002817 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002818 }
2819 if (!IsNoOp(op, val_hi)) {
2820 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002821 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002822 }
2823 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002824 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002825}
2826
Chao-ying Fua0147762014-06-06 18:38:49 -07002827bool X86Mir2Lir::GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002828 RegLocation rl_src2, Instruction::Code op) {
2829 DCHECK(rl_src2.is_const);
2830 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Chao-ying Fua0147762014-06-06 18:38:49 -07002831
Elena Sayapinadd644502014-07-01 18:39:52 +07002832 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002833 // We can do with imm only if it fits 32 bit
2834 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2835 return false;
2836 }
2837 if (rl_dest.location == kLocPhysReg &&
2838 rl_src1.location == kLocPhysReg && !rl_dest.reg.IsFloat()) {
2839 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002840 OpRegCopy(rl_dest.reg, rl_src1.reg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002841 NewLIR2(x86op, rl_dest.reg.GetReg(), val);
2842 StoreFinalValueWide(rl_dest, rl_dest);
2843 return true;
2844 }
2845
2846 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2847 // We need the values to be in a temporary
2848 RegLocation rl_result = ForceTempWide(rl_src1);
2849
2850 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2851 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2852
2853 StoreFinalValueWide(rl_dest, rl_result);
2854 return true;
2855 }
2856
Mark Mendelle02d48f2014-01-15 11:19:23 -08002857 int32_t val_lo = Low32Bits(val);
2858 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002859 rl_dest = UpdateLocWideTyped(rl_dest);
2860 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002861
2862 // Can we do this directly into the destination registers?
2863 if (rl_dest.location == kLocPhysReg && rl_src1.location == kLocPhysReg &&
buzbee2700f7e2014-03-07 09:46:20 -08002864 rl_dest.reg.GetLowReg() == rl_src1.reg.GetLowReg() &&
buzbee091cc402014-03-31 10:14:40 -07002865 rl_dest.reg.GetHighReg() == rl_src1.reg.GetHighReg() && !rl_dest.reg.IsFloat()) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002866 if (!IsNoOp(op, val_lo)) {
2867 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002868 NewLIR2(x86op, rl_dest.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002869 }
2870 if (!IsNoOp(op, val_hi)) {
2871 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002872 NewLIR2(x86op, rl_dest.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002873 }
Maxim Kazantsev653f2bf2014-02-13 15:11:17 +07002874
2875 StoreFinalValueWide(rl_dest, rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002876 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002877 }
2878
2879 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2880 DCHECK_EQ(rl_src1.location, kLocPhysReg);
2881
2882 // We need the values to be in a temporary
2883 RegLocation rl_result = ForceTempWide(rl_src1);
2884 if (!IsNoOp(op, val_lo)) {
2885 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002886 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002887 }
2888 if (!IsNoOp(op, val_hi)) {
2889 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002890 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002891 }
2892
2893 StoreFinalValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002894 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002895}
2896
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002897// For final classes there are no sub-classes to check and so we can answer the instance-of
2898// question with simple comparisons. Use compares to memory and SETEQ to optimize for x86.
2899void X86Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx,
2900 RegLocation rl_dest, RegLocation rl_src) {
buzbeea0cd2d72014-06-01 09:33:49 -07002901 RegLocation object = LoadValue(rl_src, kRefReg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002902 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002903 RegStorage result_reg = rl_result.reg;
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002904
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002905 // For 32-bit, SETcc only works with EAX..EDX.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002906 RegStorage object_32reg = object.reg.Is64Bit() ? As32BitReg(object.reg) : object.reg;
Dmitry Petrochenko407f5c12014-07-01 01:21:38 +07002907 if (result_reg.GetRegNum() == object_32reg.GetRegNum() || !IsByteRegister(result_reg)) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04002908 result_reg = AllocateByteRegister();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002909 }
2910
2911 // Assume that there is no match.
2912 LoadConstant(result_reg, 0);
buzbee2700f7e2014-03-07 09:46:20 -08002913 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002914
Mark Mendellade54a22014-06-09 12:49:55 -04002915 // We will use this register to compare to memory below.
2916 // References are 32 bit in memory, and 64 bit in registers (in 64 bit mode).
2917 // For this reason, force allocation of a 32 bit register to use, so that the
2918 // compare to memory will be done using a 32 bit comparision.
2919 // The LoadRefDisp(s) below will work normally, even in 64 bit mode.
2920 RegStorage check_class = AllocTemp();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002921
2922 // If Method* is already in a register, we can save a copy.
2923 RegLocation rl_method = mir_graph_->GetMethodLoc();
Andreas Gampeccc60262014-07-04 18:02:38 -07002924 int32_t offset_of_type = mirror::Array::DataOffset(
2925 sizeof(mirror::HeapReference<mirror::Class*>)).Int32Value() +
2926 (sizeof(mirror::HeapReference<mirror::Class*>) * type_idx);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002927
2928 if (rl_method.location == kLocPhysReg) {
2929 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07002930 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002931 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002932 } else {
buzbee695d13a2014-04-19 13:32:20 -07002933 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002934 check_class, kNotVolatile);
2935 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002936 }
2937 } else {
2938 LoadCurrMethodDirect(check_class);
2939 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07002940 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002941 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002942 } else {
buzbee695d13a2014-04-19 13:32:20 -07002943 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002944 check_class, kNotVolatile);
2945 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002946 }
2947 }
2948
2949 // Compare the computed class to the class in the object.
2950 DCHECK_EQ(object.location, kLocPhysReg);
buzbee2700f7e2014-03-07 09:46:20 -08002951 OpRegMem(kOpCmp, check_class, object.reg, mirror::Object::ClassOffset().Int32Value());
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002952
2953 // Set the low byte of the result to 0 or 1 from the compare condition code.
buzbee2700f7e2014-03-07 09:46:20 -08002954 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondEq);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002955
2956 LIR* target = NewLIR0(kPseudoTargetLabel);
2957 null_branchover->target = target;
2958 FreeTemp(check_class);
2959 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08002960 OpRegCopy(rl_result.reg, result_reg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002961 FreeTemp(result_reg);
2962 }
2963 StoreValue(rl_dest, rl_result);
2964}
2965
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002966void X86Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002967 RegLocation rl_lhs, RegLocation rl_rhs, int flags) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002968 OpKind op = kOpBkpt;
2969 bool is_div_rem = false;
2970 bool unary = false;
2971 bool shift_op = false;
2972 bool is_two_addr = false;
2973 RegLocation rl_result;
2974 switch (opcode) {
2975 case Instruction::NEG_INT:
2976 op = kOpNeg;
2977 unary = true;
2978 break;
2979 case Instruction::NOT_INT:
2980 op = kOpMvn;
2981 unary = true;
2982 break;
2983 case Instruction::ADD_INT_2ADDR:
2984 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002985 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002986 case Instruction::ADD_INT:
2987 op = kOpAdd;
2988 break;
2989 case Instruction::SUB_INT_2ADDR:
2990 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002991 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002992 case Instruction::SUB_INT:
2993 op = kOpSub;
2994 break;
2995 case Instruction::MUL_INT_2ADDR:
2996 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002997 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002998 case Instruction::MUL_INT:
2999 op = kOpMul;
3000 break;
3001 case Instruction::DIV_INT_2ADDR:
3002 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003003 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003004 case Instruction::DIV_INT:
3005 op = kOpDiv;
3006 is_div_rem = true;
3007 break;
3008 /* NOTE: returns in kArg1 */
3009 case Instruction::REM_INT_2ADDR:
3010 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003011 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003012 case Instruction::REM_INT:
3013 op = kOpRem;
3014 is_div_rem = true;
3015 break;
3016 case Instruction::AND_INT_2ADDR:
3017 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003018 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003019 case Instruction::AND_INT:
3020 op = kOpAnd;
3021 break;
3022 case Instruction::OR_INT_2ADDR:
3023 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003024 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003025 case Instruction::OR_INT:
3026 op = kOpOr;
3027 break;
3028 case Instruction::XOR_INT_2ADDR:
3029 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003030 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003031 case Instruction::XOR_INT:
3032 op = kOpXor;
3033 break;
3034 case Instruction::SHL_INT_2ADDR:
3035 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003036 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003037 case Instruction::SHL_INT:
3038 shift_op = true;
3039 op = kOpLsl;
3040 break;
3041 case Instruction::SHR_INT_2ADDR:
3042 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003043 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003044 case Instruction::SHR_INT:
3045 shift_op = true;
3046 op = kOpAsr;
3047 break;
3048 case Instruction::USHR_INT_2ADDR:
3049 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003050 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003051 case Instruction::USHR_INT:
3052 shift_op = true;
3053 op = kOpLsr;
3054 break;
3055 default:
3056 LOG(FATAL) << "Invalid word arith op: " << opcode;
3057 }
3058
Mark Mendelle87f9b52014-04-30 14:13:18 -04003059 // Can we convert to a two address instruction?
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003060 if (!is_two_addr &&
3061 (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3062 mir_graph_->SRegToVReg(rl_lhs.s_reg_low))) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04003063 is_two_addr = true;
3064 }
3065
3066 if (!GenerateTwoOperandInstructions()) {
3067 is_two_addr = false;
3068 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003069
3070 // Get the div/rem stuff out of the way.
3071 if (is_div_rem) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07003072 rl_result = GenDivRem(rl_dest, rl_lhs, rl_rhs, op == kOpDiv, flags);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003073 StoreValue(rl_dest, rl_result);
3074 return;
3075 }
3076
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003077 // If we generate any memory access below, it will reference a dalvik reg.
3078 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
3079
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003080 if (unary) {
3081 rl_lhs = LoadValue(rl_lhs, kCoreReg);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003082 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003083 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003084 OpRegReg(op, rl_result.reg, rl_lhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003085 } else {
3086 if (shift_op) {
3087 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003088 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003089 LoadValueDirectFixed(rl_rhs, t_reg);
3090 if (is_two_addr) {
3091 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003092 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003093 if (rl_result.location != kLocPhysReg) {
3094 // Okay, we can do this into memory
buzbee2700f7e2014-03-07 09:46:20 -08003095 OpMemReg(op, rl_result, t_reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003096 FreeTemp(t_reg);
3097 return;
buzbee091cc402014-03-31 10:14:40 -07003098 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003099 // Can do this directly into the result register
buzbee2700f7e2014-03-07 09:46:20 -08003100 OpRegReg(op, rl_result.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003101 FreeTemp(t_reg);
3102 StoreFinalValue(rl_dest, rl_result);
3103 return;
3104 }
3105 }
3106 // Three address form, or we can't do directly.
3107 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3108 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003109 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003110 FreeTemp(t_reg);
3111 } else {
3112 // Multiply is 3 operand only (sort of).
3113 if (is_two_addr && op != kOpMul) {
3114 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003115 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003116 if (rl_result.location == kLocPhysReg) {
Serguei Katkov366f8ae2014-04-15 16:55:26 +07003117 // Ensure res is in a core reg
3118 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003119 // Can we do this from memory directly?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003120 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003121 if (rl_rhs.location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08003122 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003123 StoreFinalValue(rl_dest, rl_result);
3124 return;
buzbee091cc402014-03-31 10:14:40 -07003125 } else if (!rl_rhs.reg.IsFloat()) {
buzbee2700f7e2014-03-07 09:46:20 -08003126 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003127 StoreFinalValue(rl_dest, rl_result);
3128 return;
3129 }
3130 }
3131 rl_rhs = LoadValue(rl_rhs, kCoreReg);
Serguei Katkovd293fb42014-05-19 15:45:42 +07003132 // It might happen rl_rhs and rl_dest are the same VR
3133 // in this case rl_dest is in reg after LoadValue while
3134 // rl_result is not updated yet, so do this
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003135 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003136 if (rl_result.location != kLocPhysReg) {
3137 // Okay, we can do this into memory.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00003138 OpMemReg(op, rl_result, rl_rhs.reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003139 return;
buzbee091cc402014-03-31 10:14:40 -07003140 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003141 // Can do this directly into the result register.
buzbee2700f7e2014-03-07 09:46:20 -08003142 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003143 StoreFinalValue(rl_dest, rl_result);
3144 return;
3145 } else {
3146 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3147 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003148 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003149 }
3150 } else {
3151 // Try to use reg/memory instructions.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003152 rl_lhs = UpdateLocTyped(rl_lhs);
3153 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003154 // We can't optimize with FP registers.
3155 if (!IsOperationSafeWithoutTemps(rl_lhs, rl_rhs)) {
3156 // Something is difficult, so fall back to the standard case.
3157 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3158 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3159 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003160 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003161 } else {
3162 // We can optimize by moving to result and using memory operands.
3163 if (rl_rhs.location != kLocPhysReg) {
3164 // Force LHS into result.
Serguei Katkov66da1362014-03-14 13:33:33 +07003165 // We should be careful with order here
3166 // If rl_dest and rl_lhs points to the same VR we should load first
3167 // If the are different we should find a register first for dest
Chao-ying Fua0147762014-06-06 18:38:49 -07003168 if (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3169 mir_graph_->SRegToVReg(rl_lhs.s_reg_low)) {
Serguei Katkov66da1362014-03-14 13:33:33 +07003170 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3171 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendelle87f9b52014-04-30 14:13:18 -04003172 // No-op if these are the same.
3173 OpRegCopy(rl_result.reg, rl_lhs.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003174 } else {
3175 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003176 LoadValueDirect(rl_lhs, rl_result.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003177 }
buzbee2700f7e2014-03-07 09:46:20 -08003178 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003179 } else if (rl_lhs.location != kLocPhysReg) {
3180 // RHS is in a register; LHS is in memory.
3181 if (op != kOpSub) {
3182 // Force RHS into result and operate on memory.
3183 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003184 OpRegCopy(rl_result.reg, rl_rhs.reg);
3185 OpRegMem(op, rl_result.reg, rl_lhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003186 } else {
3187 // Subtraction isn't commutative.
3188 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3189 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3190 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003191 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003192 }
3193 } else {
3194 // Both are in registers.
3195 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3196 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3197 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003198 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003199 }
3200 }
3201 }
3202 }
3203 }
3204 StoreValue(rl_dest, rl_result);
3205}
3206
3207bool X86Mir2Lir::IsOperationSafeWithoutTemps(RegLocation rl_lhs, RegLocation rl_rhs) {
3208 // If we have non-core registers, then we can't do good things.
buzbee091cc402014-03-31 10:14:40 -07003209 if (rl_lhs.location == kLocPhysReg && rl_lhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003210 return false;
3211 }
buzbee091cc402014-03-31 10:14:40 -07003212 if (rl_rhs.location == kLocPhysReg && rl_rhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003213 return false;
3214 }
3215
3216 // Everything will be fine :-).
3217 return true;
3218}
Chao-ying Fua0147762014-06-06 18:38:49 -07003219
3220void X86Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003221 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07003222 Mir2Lir::GenIntToLong(rl_dest, rl_src);
3223 return;
3224 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003225 rl_src = UpdateLocTyped(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07003226 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
3227 if (rl_src.location == kLocPhysReg) {
3228 NewLIR2(kX86MovsxdRR, rl_result.reg.GetReg(), rl_src.reg.GetReg());
3229 } else {
3230 int displacement = SRegOffset(rl_src.s_reg_low);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003231 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08003232 LIR *m = NewLIR3(kX86MovsxdRM, rl_result.reg.GetReg(), rs_rX86_SP_32.GetReg(),
Chao-ying Fua0147762014-06-06 18:38:49 -07003233 displacement + LOWORD_OFFSET);
3234 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
3235 true /* is_load */, true /* is_64bit */);
3236 }
3237 StoreValueWide(rl_dest, rl_result);
3238}
3239
Yevgeny Rouban6af82062014-11-26 18:11:54 +06003240void X86Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
3241 rl_src = UpdateLocWide(rl_src);
3242 rl_src = NarrowRegLoc(rl_src);
3243 StoreValue(rl_dest, rl_src);
3244
3245 if (cu_->target64) {
3246 // if src and dest are in the same phys reg then StoreValue generates
3247 // no operation but we need explicit 32-bit mov R, R to clear
3248 // the higher 32-bits
3249 rl_dest = UpdateLoc(rl_dest);
3250 if (rl_src.location == kLocPhysReg && rl_dest.location == kLocPhysReg
3251 && IsSameReg(rl_src.reg, rl_dest.reg)) {
3252 LIR* copy_lir = OpRegCopyNoInsert(rl_dest.reg, rl_dest.reg);
3253 // remove nop flag set by OpRegCopyNoInsert if src == dest
3254 copy_lir->flags.is_nop = false;
3255 AppendLIR(copy_lir);
3256 }
3257 }
3258}
3259
Chao-ying Fua0147762014-06-06 18:38:49 -07003260void X86Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
3261 RegLocation rl_src1, RegLocation rl_shift) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003262 if (!cu_->target64) {
Yixin Shouf40f8902014-08-14 14:10:32 -04003263 // Long shift operations in 32-bit. Use shld or shrd to create a 32-bit register filled from
3264 // the other half, shift the other half, if the shift amount is less than 32 we're done,
3265 // otherwise move one register to the other and place zero or sign bits in the other.
3266 LIR* branch;
3267 FlushAllRegs();
3268 LockCallTemps();
3269 LoadValueDirectFixed(rl_shift, rs_rCX);
3270 RegStorage r_tmp = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
3271 LoadValueDirectWideFixed(rl_src1, r_tmp);
3272 switch (opcode) {
3273 case Instruction::SHL_LONG:
3274 case Instruction::SHL_LONG_2ADDR:
3275 NewLIR3(kX86Shld32RRC, r_tmp.GetHighReg(), r_tmp.GetLowReg(), rs_rCX.GetReg());
3276 NewLIR2(kX86Sal32RC, r_tmp.GetLowReg(), rs_rCX.GetReg());
3277 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3278 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3279 OpRegCopy(r_tmp.GetHigh(), r_tmp.GetLow());
3280 LoadConstant(r_tmp.GetLow(), 0);
3281 branch->target = NewLIR0(kPseudoTargetLabel);
3282 break;
3283 case Instruction::SHR_LONG:
3284 case Instruction::SHR_LONG_2ADDR:
3285 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(), rs_rCX.GetReg());
3286 NewLIR2(kX86Sar32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3287 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3288 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3289 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3290 NewLIR2(kX86Sar32RI, r_tmp.GetHighReg(), 31);
3291 branch->target = NewLIR0(kPseudoTargetLabel);
3292 break;
3293 case Instruction::USHR_LONG:
3294 case Instruction::USHR_LONG_2ADDR:
3295 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(),
3296 rs_rCX.GetReg());
3297 NewLIR2(kX86Shr32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3298 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3299 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3300 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3301 LoadConstant(r_tmp.GetHigh(), 0);
3302 branch->target = NewLIR0(kPseudoTargetLabel);
3303 break;
3304 default:
3305 LOG(FATAL) << "Unexpected case: " << opcode;
3306 return;
3307 }
3308 RegLocation rl_result = LocCReturnWide();
3309 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07003310 return;
3311 }
3312
3313 bool is_two_addr = false;
3314 OpKind op = kOpBkpt;
3315 RegLocation rl_result;
3316
3317 switch (opcode) {
3318 case Instruction::SHL_LONG_2ADDR:
3319 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003320 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003321 case Instruction::SHL_LONG:
3322 op = kOpLsl;
3323 break;
3324 case Instruction::SHR_LONG_2ADDR:
3325 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003326 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003327 case Instruction::SHR_LONG:
3328 op = kOpAsr;
3329 break;
3330 case Instruction::USHR_LONG_2ADDR:
3331 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003332 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003333 case Instruction::USHR_LONG:
3334 op = kOpLsr;
3335 break;
3336 default:
3337 op = kOpBkpt;
3338 }
3339
3340 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003341 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Chao-ying Fua0147762014-06-06 18:38:49 -07003342 LoadValueDirectFixed(rl_shift, t_reg);
3343 if (is_two_addr) {
3344 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003345 rl_result = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07003346 if (rl_result.location != kLocPhysReg) {
3347 // Okay, we can do this into memory
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003348 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07003349 OpMemReg(op, rl_result, t_reg.GetReg());
3350 } else if (!rl_result.reg.IsFloat()) {
3351 // Can do this directly into the result register
3352 OpRegReg(op, rl_result.reg, t_reg);
3353 StoreFinalValueWide(rl_dest, rl_result);
3354 }
3355 } else {
3356 // Three address form, or we can't do directly.
3357 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
3358 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
3359 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
3360 StoreFinalValueWide(rl_dest, rl_result);
3361 }
3362
3363 FreeTemp(t_reg);
3364}
3365
Brian Carlstrom7940e442013-07-12 13:46:57 -07003366} // namespace art