blob: 25e34e30279989b484648c24c6e7c09ac43f452b [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file contains codegen for the X86 ISA */
18
19#include "codegen_x86.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020
21#include "base/logging.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070023#include "dex/reg_storage_eq.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "mirror/art_method.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080026#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "x86_lir.h"
28
29namespace art {
30
31/*
Brian Carlstrom7940e442013-07-12 13:46:57 -070032 * Compare two 64-bit values
33 * x = y return 0
34 * x < y return -1
35 * x > y return 1
36 */
37void X86Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070038 RegLocation rl_src2) {
Elena Sayapinadd644502014-07-01 18:39:52 +070039 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -070040 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
41 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
42 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Chao-ying Fua0147762014-06-06 18:38:49 -070043 RegStorage temp_reg = AllocTemp();
Serguei Katkov1c557032014-06-23 13:23:38 +070044 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
45 NewLIR2(kX86Set8R, rl_result.reg.GetReg(), kX86CondG); // result = (src1 > src2) ? 1 : 0
46 NewLIR2(kX86Set8R, temp_reg.GetReg(), kX86CondL); // temp = (src1 >= src2) ? 0 : 1
47 NewLIR2(kX86Sub8RR, rl_result.reg.GetReg(), temp_reg.GetReg());
48 NewLIR2(kX86Movsx8qRR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
Serguei Katkov04982232014-06-20 18:17:16 +070049
Chao-ying Fua0147762014-06-06 18:38:49 -070050 StoreValue(rl_dest, rl_result);
51 FreeTemp(temp_reg);
52 return;
53 }
54
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +070055 // Prepare for explicit register usage
56 ExplicitTempRegisterLock(this, 4, &rs_r0, &rs_r1, &rs_r2, &rs_r3);
buzbee091cc402014-03-31 10:14:40 -070057 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_r0, rs_r1);
58 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_r2, rs_r3);
buzbee2700f7e2014-03-07 09:46:20 -080059 LoadValueDirectWideFixed(rl_src1, r_tmp1);
60 LoadValueDirectWideFixed(rl_src2, r_tmp2);
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 // Compute (r1:r0) = (r1:r0) - (r3:r2)
buzbee2700f7e2014-03-07 09:46:20 -080062 OpRegReg(kOpSub, rs_r0, rs_r2); // r0 = r0 - r2
63 OpRegReg(kOpSbc, rs_r1, rs_r3); // r1 = r1 - r3 - CF
buzbee091cc402014-03-31 10:14:40 -070064 NewLIR2(kX86Set8R, rs_r2.GetReg(), kX86CondL); // r2 = (r1:r0) < (r3:r2) ? 1 : 0
65 NewLIR2(kX86Movzx8RR, rs_r2.GetReg(), rs_r2.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -080066 OpReg(kOpNeg, rs_r2); // r2 = -r2
67 OpRegReg(kOpOr, rs_r0, rs_r1); // r0 = high | low - sets ZF
buzbee091cc402014-03-31 10:14:40 -070068 NewLIR2(kX86Set8R, rs_r0.GetReg(), kX86CondNz); // r0 = (r1:r0) != (r3:r2) ? 1 : 0
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 NewLIR2(kX86Movzx8RR, r0, r0);
buzbee2700f7e2014-03-07 09:46:20 -080070 OpRegReg(kOpOr, rs_r0, rs_r2); // r0 = r0 | r2
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 RegLocation rl_result = LocCReturn();
72 StoreValue(rl_dest, rl_result);
73}
74
75X86ConditionCode X86ConditionEncoding(ConditionCode cond) {
76 switch (cond) {
77 case kCondEq: return kX86CondEq;
78 case kCondNe: return kX86CondNe;
79 case kCondCs: return kX86CondC;
80 case kCondCc: return kX86CondNc;
Vladimir Marko58af1f92013-12-19 13:31:15 +000081 case kCondUlt: return kX86CondC;
82 case kCondUge: return kX86CondNc;
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 case kCondMi: return kX86CondS;
84 case kCondPl: return kX86CondNs;
85 case kCondVs: return kX86CondO;
86 case kCondVc: return kX86CondNo;
87 case kCondHi: return kX86CondA;
88 case kCondLs: return kX86CondBe;
89 case kCondGe: return kX86CondGe;
90 case kCondLt: return kX86CondL;
91 case kCondGt: return kX86CondG;
92 case kCondLe: return kX86CondLe;
93 case kCondAl:
94 case kCondNv: LOG(FATAL) << "Should not reach here";
95 }
96 return kX86CondO;
97}
98
buzbee2700f7e2014-03-07 09:46:20 -080099LIR* X86Mir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700100 NewLIR2(src1.Is64Bit() ? kX86Cmp64RR : kX86Cmp32RR, src1.GetReg(), src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 X86ConditionCode cc = X86ConditionEncoding(cond);
102 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ ,
103 cc);
104 branch->target = target;
105 return branch;
106}
107
buzbee2700f7e2014-03-07 09:46:20 -0800108LIR* X86Mir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700109 int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 if ((check_value == 0) && (cond == kCondEq || cond == kCondNe)) {
111 // TODO: when check_value == 0 and reg is rCX, use the jcxz/nz opcode
Chao-ying Fua77ee512014-07-01 17:43:41 -0700112 NewLIR2(reg.Is64Bit() ? kX86Test64RR: kX86Test32RR, reg.GetReg(), reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700114 if (reg.Is64Bit()) {
115 NewLIR2(IS_SIMM8(check_value) ? kX86Cmp64RI8 : kX86Cmp64RI, reg.GetReg(), check_value);
116 } else {
117 NewLIR2(IS_SIMM8(check_value) ? kX86Cmp32RI8 : kX86Cmp32RI, reg.GetReg(), check_value);
118 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 }
120 X86ConditionCode cc = X86ConditionEncoding(cond);
121 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ , cc);
122 branch->target = target;
123 return branch;
124}
125
buzbee2700f7e2014-03-07 09:46:20 -0800126LIR* X86Mir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
127 // If src or dest is a pair, we'll be using low reg.
128 if (r_dest.IsPair()) {
129 r_dest = r_dest.GetLow();
130 }
131 if (r_src.IsPair()) {
132 r_src = r_src.GetLow();
133 }
buzbee091cc402014-03-31 10:14:40 -0700134 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 return OpFpRegCopy(r_dest, r_src);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700136 LIR* res = RawLIR(current_dalvik_offset_, r_dest.Is64Bit() ? kX86Mov64RR : kX86Mov32RR,
buzbee2700f7e2014-03-07 09:46:20 -0800137 r_dest.GetReg(), r_src.GetReg());
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800138 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 res->flags.is_nop = true;
140 }
141 return res;
142}
143
buzbee7a11ab02014-04-28 20:02:38 -0700144void X86Mir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
145 if (r_dest != r_src) {
146 LIR *res = OpRegCopyNoInsert(r_dest, r_src);
147 AppendLIR(res);
148 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149}
150
buzbee2700f7e2014-03-07 09:46:20 -0800151void X86Mir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700152 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700153 bool dest_fp = r_dest.IsFloat();
154 bool src_fp = r_src.IsFloat();
buzbee7a11ab02014-04-28 20:02:38 -0700155 if (dest_fp) {
156 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700157 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 } else {
buzbee7a11ab02014-04-28 20:02:38 -0700159 // TODO: Prevent this from happening in the code. The result is often
160 // unused or could have been loaded more easily from memory.
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700161 if (!r_src.IsPair()) {
162 DCHECK(!r_dest.IsPair());
163 NewLIR2(kX86MovqxrRR, r_dest.GetReg(), r_src.GetReg());
164 } else {
165 NewLIR2(kX86MovdxrRR, r_dest.GetReg(), r_src.GetLowReg());
166 RegStorage r_tmp = AllocTempDouble();
167 NewLIR2(kX86MovdxrRR, r_tmp.GetReg(), r_src.GetHighReg());
168 NewLIR2(kX86PunpckldqRR, r_dest.GetReg(), r_tmp.GetReg());
169 FreeTemp(r_tmp);
170 }
buzbee7a11ab02014-04-28 20:02:38 -0700171 }
172 } else {
173 if (src_fp) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700174 if (!r_dest.IsPair()) {
175 DCHECK(!r_src.IsPair());
176 NewLIR2(kX86MovqrxRR, r_dest.GetReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700177 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700178 NewLIR2(kX86MovdrxRR, r_dest.GetLowReg(), r_src.GetReg());
179 RegStorage temp_reg = AllocTempDouble();
180 NewLIR2(kX86MovsdRR, temp_reg.GetReg(), r_src.GetReg());
181 NewLIR2(kX86PsrlqRI, temp_reg.GetReg(), 32);
182 NewLIR2(kX86MovdrxRR, r_dest.GetHighReg(), temp_reg.GetReg());
183 }
184 } else {
185 DCHECK_EQ(r_dest.IsPair(), r_src.IsPair());
186 if (!r_src.IsPair()) {
187 // Just copy the register directly.
188 OpRegCopy(r_dest, r_src);
189 } else {
190 // Handle overlap
191 if (r_src.GetHighReg() == r_dest.GetLowReg() &&
192 r_src.GetLowReg() == r_dest.GetHighReg()) {
193 // Deal with cycles.
194 RegStorage temp_reg = AllocTemp();
195 OpRegCopy(temp_reg, r_dest.GetHigh());
196 OpRegCopy(r_dest.GetHigh(), r_dest.GetLow());
197 OpRegCopy(r_dest.GetLow(), temp_reg);
198 FreeTemp(temp_reg);
199 } else if (r_src.GetHighReg() == r_dest.GetLowReg()) {
200 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
201 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
202 } else {
203 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
204 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
205 }
buzbee7a11ab02014-04-28 20:02:38 -0700206 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 }
208 }
209 }
210}
211
Andreas Gampe90969af2014-07-15 23:02:11 -0700212void X86Mir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
213 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700214 RegisterClass dest_reg_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700215 DCHECK(!left_op.IsPair() && !right_op.IsPair() && !rs_dest.IsPair());
216 DCHECK(!left_op.IsFloat() && !right_op.IsFloat() && !rs_dest.IsFloat());
217
218 // We really need this check for correctness, otherwise we will need to do more checks in
219 // non zero/one case
220 if (true_val == false_val) {
221 LoadConstantNoClobber(rs_dest, true_val);
222 return;
Andreas Gampe90969af2014-07-15 23:02:11 -0700223 }
224
Serguei Katkov9ee45192014-07-17 14:39:03 +0700225 const bool dest_intersect = IsSameReg(rs_dest, left_op) || IsSameReg(rs_dest, right_op);
226
227 const bool zero_one_case = (true_val == 0 && false_val == 1) || (true_val == 1 && false_val == 0);
228 if (zero_one_case && IsByteRegister(rs_dest)) {
229 if (!dest_intersect) {
230 LoadConstantNoClobber(rs_dest, 0);
231 }
232 OpRegReg(kOpCmp, left_op, right_op);
233 // Set the low byte of the result to 0 or 1 from the compare condition code.
234 NewLIR2(kX86Set8R, rs_dest.GetReg(),
235 X86ConditionEncoding(true_val == 1 ? code : FlipComparisonOrder(code)));
236 if (dest_intersect) {
237 NewLIR2(rs_dest.Is64Bit() ? kX86Movzx8qRR : kX86Movzx8RR, rs_dest.GetReg(), rs_dest.GetReg());
238 }
239 } else {
240 // Be careful rs_dest can be changed only after cmp because it can be the same as one of ops
241 // and it cannot use xor because it makes cc flags to be dirty
242 RegStorage temp_reg = AllocTypedTemp(false, dest_reg_class, false);
243 if (temp_reg.Valid()) {
244 if (false_val == 0 && dest_intersect) {
245 code = FlipComparisonOrder(code);
246 std::swap(true_val, false_val);
247 }
248 if (!dest_intersect) {
249 LoadConstantNoClobber(rs_dest, false_val);
250 }
251 LoadConstantNoClobber(temp_reg, true_val);
252 OpRegReg(kOpCmp, left_op, right_op);
253 if (dest_intersect) {
254 LoadConstantNoClobber(rs_dest, false_val);
255 DCHECK(!last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
256 }
257 OpCondRegReg(kOpCmov, code, rs_dest, temp_reg);
258 FreeTemp(temp_reg);
259 } else {
260 // slow path
261 LIR* cmp_branch = OpCmpBranch(code, left_op, right_op, nullptr);
262 LoadConstantNoClobber(rs_dest, false_val);
263 LIR* that_is_it = NewLIR1(kX86Jmp8, 0);
264 LIR* true_case = NewLIR0(kPseudoTargetLabel);
265 cmp_branch->target = true_case;
266 LoadConstantNoClobber(rs_dest, true_val);
267 LIR* end = NewLIR0(kPseudoTargetLabel);
268 that_is_it->target = end;
269 }
270 }
Andreas Gampe90969af2014-07-15 23:02:11 -0700271}
272
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700273void X86Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700274 UNUSED(bb);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800275 RegLocation rl_result;
276 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
277 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700278 // Avoid using float regs here.
279 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
280 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
Vladimir Markoa1a70742014-03-03 10:28:05 +0000281 ConditionCode ccode = mir->meta.ccode;
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800282
283 // The kMirOpSelect has two variants, one for constants and one for moves.
284 const bool is_constant_case = (mir->ssa_rep->num_uses == 1);
285
286 if (is_constant_case) {
287 int true_val = mir->dalvikInsn.vB;
288 int false_val = mir->dalvikInsn.vC;
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800289
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700290 // simplest strange case
291 if (true_val == false_val) {
292 rl_result = EvalLoc(rl_dest, result_reg_class, true);
293 LoadConstantNoClobber(rl_result.reg, true_val);
294 } else {
295 // TODO: use GenSelectConst32 and handle additional opcode patterns such as
296 // "cmp; setcc; movzx" or "cmp; sbb r0,r0; and r0,$mask; add r0,$literal".
297 rl_src = LoadValue(rl_src, src_reg_class);
298 rl_result = EvalLoc(rl_dest, result_reg_class, true);
299 /*
300 * For ccode == kCondEq:
301 *
302 * 1) When the true case is zero and result_reg is not same as src_reg:
303 * xor result_reg, result_reg
304 * cmp $0, src_reg
305 * mov t1, $false_case
306 * cmovnz result_reg, t1
307 * 2) When the false case is zero and result_reg is not same as src_reg:
308 * xor result_reg, result_reg
309 * cmp $0, src_reg
310 * mov t1, $true_case
311 * cmovz result_reg, t1
312 * 3) All other cases (we do compare first to set eflags):
313 * cmp $0, src_reg
314 * mov result_reg, $false_case
315 * mov t1, $true_case
316 * cmovz result_reg, t1
317 */
318 // FIXME: depending on how you use registers you could get a false != mismatch when dealing
319 // with different views of the same underlying physical resource (i.e. solo32 vs. solo64).
320 const bool result_reg_same_as_src =
321 (rl_src.location == kLocPhysReg && rl_src.reg.GetRegNum() == rl_result.reg.GetRegNum());
322 const bool true_zero_case = (true_val == 0 && false_val != 0 && !result_reg_same_as_src);
323 const bool false_zero_case = (false_val == 0 && true_val != 0 && !result_reg_same_as_src);
324 const bool catch_all_case = !(true_zero_case || false_zero_case);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800325
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700326 if (true_zero_case || false_zero_case) {
327 OpRegReg(kOpXor, rl_result.reg, rl_result.reg);
328 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800329
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700330 if (true_zero_case || false_zero_case || catch_all_case) {
331 OpRegImm(kOpCmp, rl_src.reg, 0);
332 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800333
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700334 if (catch_all_case) {
335 OpRegImm(kOpMov, rl_result.reg, false_val);
336 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800337
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700338 if (true_zero_case || false_zero_case || catch_all_case) {
339 ConditionCode cc = true_zero_case ? NegateComparison(ccode) : ccode;
340 int immediateForTemp = true_zero_case ? false_val : true_val;
341 RegStorage temp1_reg = AllocTypedTemp(false, result_reg_class);
342 OpRegImm(kOpMov, temp1_reg, immediateForTemp);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800343
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700344 OpCondRegReg(kOpCmov, cc, rl_result.reg, temp1_reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800345
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700346 FreeTemp(temp1_reg);
347 }
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800348 }
349 } else {
Jean Christophe Beyler3f51e7d2014-09-04 08:34:28 -0700350 rl_src = LoadValue(rl_src, src_reg_class);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800351 RegLocation rl_true = mir_graph_->GetSrc(mir, 1);
352 RegLocation rl_false = mir_graph_->GetSrc(mir, 2);
buzbeea0cd2d72014-06-01 09:33:49 -0700353 rl_true = LoadValue(rl_true, result_reg_class);
354 rl_false = LoadValue(rl_false, result_reg_class);
355 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800356
357 /*
Vladimir Markoa1a70742014-03-03 10:28:05 +0000358 * For ccode == kCondEq:
359 *
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800360 * 1) When true case is already in place:
361 * cmp $0, src_reg
362 * cmovnz result_reg, false_reg
363 * 2) When false case is already in place:
364 * cmp $0, src_reg
365 * cmovz result_reg, true_reg
366 * 3) When neither cases are in place:
367 * cmp $0, src_reg
Vladimir Markoa1a70742014-03-03 10:28:05 +0000368 * mov result_reg, false_reg
369 * cmovz result_reg, true_reg
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800370 */
371
372 // kMirOpSelect is generated just for conditional cases when comparison is done with zero.
buzbee2700f7e2014-03-07 09:46:20 -0800373 OpRegImm(kOpCmp, rl_src.reg, 0);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800374
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000375 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) {
buzbee2700f7e2014-03-07 09:46:20 -0800376 OpCondRegReg(kOpCmov, NegateComparison(ccode), rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000377 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) {
buzbee2700f7e2014-03-07 09:46:20 -0800378 OpCondRegReg(kOpCmov, ccode, rl_result.reg, rl_true.reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800379 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800380 OpRegCopy(rl_result.reg, rl_false.reg);
381 OpCondRegReg(kOpCmov, ccode, rl_result.reg, rl_true.reg);
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800382 }
383 }
384
385 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386}
387
388void X86Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
buzbee0d829482013-10-11 15:24:55 -0700389 LIR* taken = &block_label_list_[bb->taken];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
391 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Vladimir Markoa8946072014-01-22 10:30:44 +0000392 ConditionCode ccode = mir->meta.ccode;
Mark Mendell412d4f82013-12-18 13:32:36 -0800393
394 if (rl_src1.is_const) {
395 std::swap(rl_src1, rl_src2);
396 ccode = FlipComparisonOrder(ccode);
397 }
398 if (rl_src2.is_const) {
399 // Do special compare/branch against simple const operand
400 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
401 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
402 return;
403 }
404
Elena Sayapinadd644502014-07-01 18:39:52 +0700405 if (cu_->target64) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700406 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
407 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
408
409 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
410 OpCondBranch(ccode, taken);
411 return;
412 }
413
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700414 // Prepare for explicit register usage
415 ExplicitTempRegisterLock(this, 4, &rs_r0, &rs_r1, &rs_r2, &rs_r3);
buzbee091cc402014-03-31 10:14:40 -0700416 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_r0, rs_r1);
417 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_r2, rs_r3);
buzbee2700f7e2014-03-07 09:46:20 -0800418 LoadValueDirectWideFixed(rl_src1, r_tmp1);
419 LoadValueDirectWideFixed(rl_src2, r_tmp2);
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700420
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 // Swap operands and condition code to prevent use of zero flag.
422 if (ccode == kCondLe || ccode == kCondGt) {
423 // Compute (r3:r2) = (r3:r2) - (r1:r0)
buzbee2700f7e2014-03-07 09:46:20 -0800424 OpRegReg(kOpSub, rs_r2, rs_r0); // r2 = r2 - r0
425 OpRegReg(kOpSbc, rs_r3, rs_r1); // r3 = r3 - r1 - CF
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 } else {
427 // Compute (r1:r0) = (r1:r0) - (r3:r2)
buzbee2700f7e2014-03-07 09:46:20 -0800428 OpRegReg(kOpSub, rs_r0, rs_r2); // r0 = r0 - r2
429 OpRegReg(kOpSbc, rs_r1, rs_r3); // r1 = r1 - r3 - CF
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 }
431 switch (ccode) {
432 case kCondEq:
433 case kCondNe:
buzbee2700f7e2014-03-07 09:46:20 -0800434 OpRegReg(kOpOr, rs_r0, rs_r1); // r0 = r0 | r1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 break;
436 case kCondLe:
437 ccode = kCondGe;
438 break;
439 case kCondGt:
440 ccode = kCondLt;
441 break;
442 case kCondLt:
443 case kCondGe:
444 break;
445 default:
446 LOG(FATAL) << "Unexpected ccode: " << ccode;
447 }
448 OpCondBranch(ccode, taken);
449}
450
Mark Mendell412d4f82013-12-18 13:32:36 -0800451void X86Mir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
452 int64_t val, ConditionCode ccode) {
453 int32_t val_lo = Low32Bits(val);
454 int32_t val_hi = High32Bits(val);
455 LIR* taken = &block_label_list_[bb->taken];
Mark Mendell412d4f82013-12-18 13:32:36 -0800456 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Mark Mendell752e2052014-05-01 10:19:04 -0400457 bool is_equality_test = ccode == kCondEq || ccode == kCondNe;
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700458
Elena Sayapinadd644502014-07-01 18:39:52 +0700459 if (cu_->target64) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700460 if (is_equality_test && val == 0) {
461 // We can simplify of comparing for ==, != to 0.
462 NewLIR2(kX86Test64RR, rl_src1.reg.GetReg(), rl_src1.reg.GetReg());
463 } else if (is_equality_test && val_hi == 0 && val_lo > 0) {
464 OpRegImm(kOpCmp, rl_src1.reg, val_lo);
465 } else {
466 RegStorage tmp = AllocTypedTempWide(false, kCoreReg);
467 LoadConstantWide(tmp, val);
468 OpRegReg(kOpCmp, rl_src1.reg, tmp);
469 FreeTemp(tmp);
470 }
471 OpCondBranch(ccode, taken);
472 return;
473 }
474
Mark Mendell752e2052014-05-01 10:19:04 -0400475 if (is_equality_test && val != 0) {
476 rl_src1 = ForceTempWide(rl_src1);
477 }
buzbee2700f7e2014-03-07 09:46:20 -0800478 RegStorage low_reg = rl_src1.reg.GetLow();
479 RegStorage high_reg = rl_src1.reg.GetHigh();
Mark Mendell412d4f82013-12-18 13:32:36 -0800480
Mark Mendell752e2052014-05-01 10:19:04 -0400481 if (is_equality_test) {
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +0700482 // We can simplify of comparing for ==, != to 0.
Mark Mendell752e2052014-05-01 10:19:04 -0400483 if (val == 0) {
484 if (IsTemp(low_reg)) {
485 OpRegReg(kOpOr, low_reg, high_reg);
486 // We have now changed it; ignore the old values.
487 Clobber(rl_src1.reg);
488 } else {
489 RegStorage t_reg = AllocTemp();
490 OpRegRegReg(kOpOr, t_reg, low_reg, high_reg);
491 FreeTemp(t_reg);
492 }
493 OpCondBranch(ccode, taken);
494 return;
495 }
496
497 // Need to compute the actual value for ==, !=.
498 OpRegImm(kOpSub, low_reg, val_lo);
499 NewLIR2(kX86Sbb32RI, high_reg.GetReg(), val_hi);
500 OpRegReg(kOpOr, high_reg, low_reg);
501 Clobber(rl_src1.reg);
502 } else if (ccode == kCondLe || ccode == kCondGt) {
503 // Swap operands and condition code to prevent use of zero flag.
504 RegStorage tmp = AllocTypedTempWide(false, kCoreReg);
505 LoadConstantWide(tmp, val);
506 OpRegReg(kOpSub, tmp.GetLow(), low_reg);
507 OpRegReg(kOpSbc, tmp.GetHigh(), high_reg);
508 ccode = (ccode == kCondLe) ? kCondGe : kCondLt;
509 FreeTemp(tmp);
510 } else {
511 // We can use a compare for the low word to set CF.
512 OpRegImm(kOpCmp, low_reg, val_lo);
513 if (IsTemp(high_reg)) {
514 NewLIR2(kX86Sbb32RI, high_reg.GetReg(), val_hi);
515 // We have now changed it; ignore the old values.
516 Clobber(rl_src1.reg);
517 } else {
518 // mov temp_reg, high_reg; sbb temp_reg, high_constant
519 RegStorage t_reg = AllocTemp();
520 OpRegCopy(t_reg, high_reg);
521 NewLIR2(kX86Sbb32RI, t_reg.GetReg(), val_hi);
522 FreeTemp(t_reg);
523 }
Mark Mendell412d4f82013-12-18 13:32:36 -0800524 }
525
Mark Mendell752e2052014-05-01 10:19:04 -0400526 OpCondBranch(ccode, taken);
Mark Mendell412d4f82013-12-18 13:32:36 -0800527}
528
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700529void X86Mir2Lir::CalculateMagicAndShift(int64_t divisor, int64_t& magic, int& shift, bool is_long) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800530 // It does not make sense to calculate magic and shift for zero divisor.
531 DCHECK_NE(divisor, 0);
532
533 /* According to H.S.Warren's Hacker's Delight Chapter 10 and
534 * T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
535 * The magic number M and shift S can be calculated in the following way:
536 * Let nc be the most positive value of numerator(n) such that nc = kd - 1,
537 * where divisor(d) >=2.
538 * Let nc be the most negative value of numerator(n) such that nc = kd + 1,
539 * where divisor(d) <= -2.
540 * Thus nc can be calculated like:
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700541 * nc = exp + exp % d - 1, where d >= 2 and exp = 2^31 for int or 2^63 for long
542 * nc = -exp + (exp + 1) % d, where d >= 2 and exp = 2^31 for int or 2^63 for long
Mark Mendell2bf31e62014-01-23 12:13:40 -0800543 *
544 * So the shift p is the smallest p satisfying
545 * 2^p > nc * (d - 2^p % d), where d >= 2
546 * 2^p > nc * (d + 2^p % d), where d <= -2.
547 *
548 * the magic number M is calcuated by
549 * M = (2^p + d - 2^p % d) / d, where d >= 2
550 * M = (2^p - d - 2^p % d) / d, where d <= -2.
551 *
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700552 * Notice that p is always bigger than or equal to 32/64, so we just return 32-p/64-p as
Mark Mendell2bf31e62014-01-23 12:13:40 -0800553 * the shift number S.
554 */
555
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700556 int64_t p = (is_long) ? 63 : 31;
557 const uint64_t exp = (is_long) ? 0x8000000000000000ULL : 0x80000000U;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800558
559 // Initialize the computations.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700560 uint64_t abs_d = (divisor >= 0) ? divisor : -divisor;
561 uint64_t tmp = exp + ((is_long) ? static_cast<uint64_t>(divisor) >> 63 :
562 static_cast<uint32_t>(divisor) >> 31);
563 uint64_t abs_nc = tmp - 1 - tmp % abs_d;
564 uint64_t quotient1 = exp / abs_nc;
565 uint64_t remainder1 = exp % abs_nc;
566 uint64_t quotient2 = exp / abs_d;
567 uint64_t remainder2 = exp % abs_d;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800568
569 /*
570 * To avoid handling both positive and negative divisor, Hacker's Delight
571 * introduces a method to handle these 2 cases together to avoid duplication.
572 */
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700573 uint64_t delta;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800574 do {
575 p++;
576 quotient1 = 2 * quotient1;
577 remainder1 = 2 * remainder1;
578 if (remainder1 >= abs_nc) {
579 quotient1++;
580 remainder1 = remainder1 - abs_nc;
581 }
582 quotient2 = 2 * quotient2;
583 remainder2 = 2 * remainder2;
584 if (remainder2 >= abs_d) {
585 quotient2++;
586 remainder2 = remainder2 - abs_d;
587 }
588 delta = abs_d - remainder2;
589 } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
590
591 magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700592
593 if (!is_long) {
594 magic = static_cast<int>(magic);
595 }
596
597 shift = (is_long) ? p - 64 : p - 32;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800598}
599
buzbee2700f7e2014-03-07 09:46:20 -0800600RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg_lo, int lit, bool is_div) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700601 UNUSED(rl_dest, reg_lo, lit, is_div);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 LOG(FATAL) << "Unexpected use of GenDivRemLit for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700603 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604}
605
Mark Mendell2bf31e62014-01-23 12:13:40 -0800606RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src,
607 int imm, bool is_div) {
608 // Use a multiply (and fixup) to perform an int div/rem by a constant.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700609 RegLocation rl_result;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800610
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700611 if (imm == 1) {
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700612 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800613 if (is_div) {
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700614 // x / 1 == x.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700615 LoadValueDirectFixed(rl_src, rl_result.reg);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700616 } else {
617 // x % 1 == 0.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700618 LoadConstantNoClobber(rl_result.reg, 0);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700619 }
620 } else if (imm == -1) { // handle 0x80000000 / -1 special case.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700621 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700622 if (is_div) {
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700623 LoadValueDirectFixed(rl_src, rl_result.reg);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400624
625 // Check if numerator is 0
626 OpRegImm(kOpCmp, rl_result.reg, 0);
627 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
628
629 // handle 0x80000000 / -1
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700630 OpRegImm(kOpCmp, rl_result.reg, 0x80000000);
631 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800632
633 // for x != MIN_INT, x / -1 == -x.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700634 NewLIR1(kX86Neg32R, rl_result.reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800635
Mark Mendell2bf31e62014-01-23 12:13:40 -0800636 // EAX already contains the right value (0x80000000),
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700637 minint_branch->target = NewLIR0(kPseudoTargetLabel);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400638 branch->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800639 } else {
640 // x % -1 == 0.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700641 LoadConstantNoClobber(rl_result.reg, 0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800642 }
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700643 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
644 // Division using shifting.
645 rl_src = LoadValue(rl_src, kCoreReg);
646 rl_result = EvalLoc(rl_dest, kCoreReg, true);
647 if (IsSameReg(rl_result.reg, rl_src.reg)) {
648 RegStorage rs_temp = AllocTypedTemp(false, kCoreReg);
649 rl_result.reg.SetReg(rs_temp.GetReg());
650 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400651
652 // Check if numerator is 0
653 OpRegImm(kOpCmp, rl_src.reg, 0);
654 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
655 LoadConstantNoClobber(rl_result.reg, 0);
656 LIR* done = NewLIR1(kX86Jmp8, 0);
657 branch->target = NewLIR0(kPseudoTargetLabel);
658
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700659 NewLIR3(kX86Lea32RM, rl_result.reg.GetReg(), rl_src.reg.GetReg(), std::abs(imm) - 1);
660 NewLIR2(kX86Test32RR, rl_src.reg.GetReg(), rl_src.reg.GetReg());
661 OpCondRegReg(kOpCmov, kCondPl, rl_result.reg, rl_src.reg);
Andreas Gampe7e499922015-01-06 08:28:12 -0800662 int shift_amount = CTZ(imm);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700663 OpRegImm(kOpAsr, rl_result.reg, shift_amount);
664 if (imm < 0) {
665 OpReg(kOpNeg, rl_result.reg);
666 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400667 done->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800668 } else {
Alexei Zavjalov79aa4232014-02-13 13:55:50 +0700669 CHECK(imm <= -2 || imm >= 2);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700670
Mark Mendell2bf31e62014-01-23 12:13:40 -0800671 // Use H.S.Warren's Hacker's Delight Chapter 10 and
672 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700673 int64_t magic;
674 int shift;
675 CalculateMagicAndShift((int64_t)imm, magic, shift, false /* is_long */);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800676
677 /*
678 * For imm >= 2,
679 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
680 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
681 * For imm <= -2,
682 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
683 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
684 * We implement this algorithm in the following way:
685 * 1. multiply magic number m and numerator n, get the higher 32bit result in EDX
686 * 2. if imm > 0 and magic < 0, add numerator to EDX
687 * if imm < 0 and magic > 0, sub numerator from EDX
688 * 3. if S !=0, SAR S bits for EDX
689 * 4. add 1 to EDX if EDX < 0
690 * 5. Thus, EDX is the quotient
691 */
692
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700693 FlushReg(rs_r0);
694 Clobber(rs_r0);
695 LockTemp(rs_r0);
696 FlushReg(rs_r2);
697 Clobber(rs_r2);
698 LockTemp(rs_r2);
699
Mark Mendell3a91f442014-09-02 12:44:24 -0400700 // Assume that the result will be in EDX for divide, and EAX for remainder.
701 rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, is_div ? rs_r2 : rs_r0,
702 INVALID_SREG, INVALID_SREG};
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +0700703
Mark Mendell3a91f442014-09-02 12:44:24 -0400704 // We need the value at least twice. Load into a temp.
705 rl_src = LoadValue(rl_src, kCoreReg);
706 RegStorage numerator_reg = rl_src.reg;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800707
Mark Mendell3a91f442014-09-02 12:44:24 -0400708 // Check if numerator is 0.
709 OpRegImm(kOpCmp, numerator_reg, 0);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400710 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell3a91f442014-09-02 12:44:24 -0400711 // Return result 0 if numerator was 0.
712 LoadConstantNoClobber(rl_result.reg, 0);
Yixin Shou2ddd1752014-08-26 15:15:13 -0400713 LIR* done = NewLIR1(kX86Jmp8, 0);
714 branch->target = NewLIR0(kPseudoTargetLabel);
715
Mark Mendell3a91f442014-09-02 12:44:24 -0400716 // EAX = magic.
717 LoadConstant(rs_r0, magic);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800718
Mark Mendell3a91f442014-09-02 12:44:24 -0400719 // EDX:EAX = magic * numerator.
720 NewLIR1(kX86Imul32DaR, numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800721
722 if (imm > 0 && magic < 0) {
723 // Add numerator to EDX.
buzbee2700f7e2014-03-07 09:46:20 -0800724 DCHECK(numerator_reg.Valid());
buzbee091cc402014-03-31 10:14:40 -0700725 NewLIR2(kX86Add32RR, rs_r2.GetReg(), numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800726 } else if (imm < 0 && magic > 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800727 DCHECK(numerator_reg.Valid());
buzbee091cc402014-03-31 10:14:40 -0700728 NewLIR2(kX86Sub32RR, rs_r2.GetReg(), numerator_reg.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800729 }
730
731 // Do we need the shift?
732 if (shift != 0) {
733 // Shift EDX by 'shift' bits.
buzbee091cc402014-03-31 10:14:40 -0700734 NewLIR2(kX86Sar32RI, rs_r2.GetReg(), shift);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800735 }
736
737 // Add 1 to EDX if EDX < 0.
738
739 // Move EDX to EAX.
buzbee2700f7e2014-03-07 09:46:20 -0800740 OpRegCopy(rs_r0, rs_r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800741
742 // Move sign bit to bit 0, zeroing the rest.
buzbee091cc402014-03-31 10:14:40 -0700743 NewLIR2(kX86Shr32RI, rs_r2.GetReg(), 31);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800744
745 // EDX = EDX + EAX.
buzbee091cc402014-03-31 10:14:40 -0700746 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r0.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800747
748 // Quotient is in EDX.
749 if (!is_div) {
750 // We need to compute the remainder.
751 // Remainder is divisor - (quotient * imm).
buzbee2700f7e2014-03-07 09:46:20 -0800752 DCHECK(numerator_reg.Valid());
753 OpRegCopy(rs_r0, numerator_reg);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800754
755 // EAX = numerator * imm.
buzbee2700f7e2014-03-07 09:46:20 -0800756 OpRegRegImm(kOpMul, rs_r2, rs_r2, imm);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800757
Mark Mendell3a91f442014-09-02 12:44:24 -0400758 // EAX -= EDX.
buzbee091cc402014-03-31 10:14:40 -0700759 NewLIR2(kX86Sub32RR, rs_r0.GetReg(), rs_r2.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800760
761 // For this case, return the result in EAX.
Mark Mendell2bf31e62014-01-23 12:13:40 -0800762 }
Yixin Shou2ddd1752014-08-26 15:15:13 -0400763 done->target = NewLIR0(kPseudoTargetLabel);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800764 }
765
766 return rl_result;
767}
768
buzbee2700f7e2014-03-07 09:46:20 -0800769RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg_lo, RegStorage reg_hi,
770 bool is_div) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700771 UNUSED(rl_dest, reg_lo, reg_hi, is_div);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 LOG(FATAL) << "Unexpected use of GenDivRem for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700773 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774}
775
Mark Mendell2bf31e62014-01-23 12:13:40 -0800776RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700777 RegLocation rl_src2, bool is_div, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700778 UNUSED(rl_dest);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800779 // We have to use fixed registers, so flush all the temps.
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700780
781 // Prepare for explicit register usage.
782 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800783
784 // Load LHS into EAX.
buzbee2700f7e2014-03-07 09:46:20 -0800785 LoadValueDirectFixed(rl_src1, rs_r0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800786
787 // Load RHS into EBX.
buzbee2700f7e2014-03-07 09:46:20 -0800788 LoadValueDirectFixed(rl_src2, rs_r1);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800789
790 // Copy LHS sign bit into EDX.
791 NewLIR0(kx86Cdq32Da);
792
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700793 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800794 // Handle division by zero case.
Mingyao Yange643a172014-04-08 11:02:52 -0700795 GenDivZeroCheck(rs_r1);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800796 }
797
Yixin Shou2ddd1752014-08-26 15:15:13 -0400798 // Check if numerator is 0
799 OpRegImm(kOpCmp, rs_r0, 0);
800 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
801
Mark Mendell2bf31e62014-01-23 12:13:40 -0800802 // Have to catch 0x80000000/-1 case, or we will get an exception!
buzbee2700f7e2014-03-07 09:46:20 -0800803 OpRegImm(kOpCmp, rs_r1, -1);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700804 LIR* minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800805
806 // RHS is -1.
buzbee2700f7e2014-03-07 09:46:20 -0800807 OpRegImm(kOpCmp, rs_r0, 0x80000000);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +0700808 LIR* minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800809
Yixin Shou2ddd1752014-08-26 15:15:13 -0400810 branch->target = NewLIR0(kPseudoTargetLabel);
811
Mark Mendell2bf31e62014-01-23 12:13:40 -0800812 // In 0x80000000/-1 case.
813 if (!is_div) {
814 // For DIV, EAX is already right. For REM, we need EDX 0.
buzbee2700f7e2014-03-07 09:46:20 -0800815 LoadConstantNoClobber(rs_r2, 0);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800816 }
817 LIR* done = NewLIR1(kX86Jmp8, 0);
818
819 // Expected case.
820 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
821 minint_branch->target = minus_one_branch->target;
buzbee091cc402014-03-31 10:14:40 -0700822 NewLIR1(kX86Idivmod32DaR, rs_r1.GetReg());
Mark Mendell2bf31e62014-01-23 12:13:40 -0800823 done->target = NewLIR0(kPseudoTargetLabel);
824
825 // Result is in EAX for div and EDX for rem.
buzbee091cc402014-03-31 10:14:40 -0700826 RegLocation rl_result = {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, rs_r0, INVALID_SREG, INVALID_SREG};
Mark Mendell2bf31e62014-01-23 12:13:40 -0800827 if (!is_div) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000828 rl_result.reg.SetReg(r2);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800829 }
830 return rl_result;
831}
832
Serban Constantinescu23abec92014-07-02 16:13:38 +0100833bool X86Mir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700834 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800835
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700836 if (is_long && !cu_->target64) {
837 /*
838 * We want to implement the following algorithm
839 * mov eax, low part of arg1
840 * mov edx, high part of arg1
841 * mov ebx, low part of arg2
842 * mov ecx, high part of arg2
843 * mov edi, eax
844 * sub edi, ebx
845 * mov edi, edx
846 * sbb edi, ecx
847 * is_min ? "cmovgel eax, ebx" : "cmovll eax, ebx"
848 * is_min ? "cmovgel edx, ecx" : "cmovll edx, ecx"
849 *
850 * The algorithm above needs 5 registers: a pair for the first operand
851 * (which later will be used as result), a pair for the second operand
852 * and a temp register (e.g. 'edi') for intermediate calculations.
853 * Ideally we have 6 GP caller-save registers in 32-bit mode. They are:
854 * 'eax', 'ebx', 'ecx', 'edx', 'esi' and 'edi'. So there should be
855 * always enough registers to operate on. Practically, there is a pair
856 * of registers 'edi' and 'esi' which holds promoted values and
857 * sometimes should be treated as 'callee save'. If one of the operands
858 * is in the promoted registers then we have enough register to
859 * operate on. Otherwise there is lack of resources and we have to
860 * save 'edi' before calculations and restore after.
861 */
862
863 RegLocation rl_src1 = info->args[0];
864 RegLocation rl_src2 = info->args[2];
865 RegLocation rl_dest = InlineTargetWide(info);
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700866
Mark Mendella65c1db2014-10-21 17:44:32 -0400867 if (rl_dest.s_reg_low == INVALID_SREG) {
868 // Result is unused, the code is dead. Inlining successful, no code generated.
869 return true;
870 }
871
nikolay serdjuk55693282015-01-20 17:03:02 +0600872 if (PartiallyIntersects(rl_src1, rl_dest) &&
873 PartiallyIntersects(rl_src2, rl_dest)) {
874 // A special case which we don't want to handle.
875 // This is when src1 is mapped on v0 and v1,
876 // src2 is mapped on v2, v3,
877 // result is mapped on v1, v2
878 return false;
879 }
880
881
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700882 /*
883 * If the result register is the same as the second element, then we
884 * need to be careful. The reason is that the first copy will
885 * inadvertently clobber the second element with the first one thus
886 * yielding the wrong result. Thus we do a swap in that case.
887 */
nikolay serdjuk55693282015-01-20 17:03:02 +0600888 if (Intersects(rl_src2, rl_dest)) {
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700889 std::swap(rl_src1, rl_src2);
890 }
891
892 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
893 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
894
895 // Pick the first integer as min/max.
896 OpRegCopyWide(rl_result.reg, rl_src1.reg);
897
898 /*
899 * If the integers are both in the same register, then there is
900 * nothing else to do because they are equal and we have already
901 * moved one into the result.
902 */
nikolay serdjuk55693282015-01-20 17:03:02 +0600903 if (mir_graph_->SRegToVReg(rl_src1.s_reg_low) ==
904 mir_graph_->SRegToVReg(rl_src2.s_reg_low)) {
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700905 StoreValueWide(rl_dest, rl_result);
906 return true;
907 }
908
909 // Free registers to make some room for the second operand.
nikolay serdjuk55693282015-01-20 17:03:02 +0600910 // But don't try to free part of a source which intersects
911 // part of result or promoted registers.
912
913 if (IsTemp(rl_src1.reg.GetLow()) &&
914 (rl_src1.reg.GetLowReg() != rl_result.reg.GetHighReg()) &&
915 (rl_src1.reg.GetLowReg() != rl_result.reg.GetLowReg())) {
916 // Is low part temporary and doesn't intersect any parts of result?
917 FreeTemp(rl_src1.reg.GetLow());
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700918 }
nikolay serdjuk55693282015-01-20 17:03:02 +0600919
920 if (IsTemp(rl_src1.reg.GetHigh()) &&
921 (rl_src1.reg.GetHighReg() != rl_result.reg.GetLowReg()) &&
922 (rl_src1.reg.GetHighReg() != rl_result.reg.GetHighReg())) {
923 // Is high part temporary and doesn't intersect any parts of result?
924 FreeTemp(rl_src1.reg.GetHigh());
925 }
926
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700927 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
928
929 // Do we have a free register for intermediate calculations?
930 RegStorage tmp = AllocTemp(false);
931 if (tmp == RegStorage::InvalidReg()) {
932 /*
933 * No, will use 'edi'.
934 *
935 * As mentioned above we have 4 temporary and 2 promotable
936 * caller-save registers. Therefore, we assume that a free
937 * register can be allocated only if 'esi' and 'edi' are
938 * already used as operands. If number of promotable registers
939 * increases from 2 to 4 then our assumption fails and operand
940 * data is corrupted.
941 * Let's DCHECK it.
942 */
943 DCHECK(IsTemp(rl_src2.reg.GetLow()) &&
944 IsTemp(rl_src2.reg.GetHigh()) &&
945 IsTemp(rl_result.reg.GetLow()) &&
946 IsTemp(rl_result.reg.GetHigh()));
947 tmp = rs_rDI;
948 NewLIR1(kX86Push32R, tmp.GetReg());
949 }
950
951 // Now we are ready to do calculations.
952 OpRegReg(kOpMov, tmp, rl_result.reg.GetLow());
953 OpRegReg(kOpSub, tmp, rl_src2.reg.GetLow());
954 OpRegReg(kOpMov, tmp, rl_result.reg.GetHigh());
955 OpRegReg(kOpSbc, tmp, rl_src2.reg.GetHigh());
956
957 // Let's put pop 'edi' here to break a bit the dependency chain.
958 if (tmp == rs_rDI) {
959 NewLIR1(kX86Pop32R, tmp.GetReg());
nikolay serdjuk55693282015-01-20 17:03:02 +0600960 } else {
961 FreeTemp(tmp);
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700962 }
963
964 // Conditionally move the other integer into the destination register.
965 ConditionCode cc = is_min ? kCondGe : kCondLt;
966 OpCondRegReg(kOpCmov, cc, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
967 OpCondRegReg(kOpCmov, cc, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
nikolay serdjuk55693282015-01-20 17:03:02 +0600968 FreeTemp(rl_src2.reg);
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700969 StoreValueWide(rl_dest, rl_result);
970 return true;
Serban Constantinescu23abec92014-07-02 16:13:38 +0100971 }
972
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800973 // Get the two arguments to the invoke and place them in GP registers.
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800974 RegLocation rl_dest = (is_long) ? InlineTargetWide(info) : InlineTarget(info);
975 if (rl_dest.s_reg_low == INVALID_SREG) {
976 // Result is unused, the code is dead. Inlining successful, no code generated.
977 return true;
978 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 RegLocation rl_src1 = info->args[0];
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700980 RegLocation rl_src2 = (is_long) ? info->args[2] : info->args[1];
981 rl_src1 = (is_long) ? LoadValueWide(rl_src1, kCoreReg) : LoadValue(rl_src1, kCoreReg);
982 rl_src2 = (is_long) ? LoadValueWide(rl_src2, kCoreReg) : LoadValue(rl_src2, kCoreReg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800983
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800985
986 /*
987 * If the result register is the same as the second element, then we need to be careful.
988 * The reason is that the first copy will inadvertently clobber the second element with
989 * the first one thus yielding the wrong result. Thus we do a swap in that case.
990 */
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000991 if (rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800992 std::swap(rl_src1, rl_src2);
993 }
994
995 // Pick the first integer as min/max.
buzbee2700f7e2014-03-07 09:46:20 -0800996 OpRegCopy(rl_result.reg, rl_src1.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800997
998 // If the integers are both in the same register, then there is nothing else to do
999 // because they are equal and we have already moved one into the result.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001000 if (rl_src1.reg.GetReg() != rl_src2.reg.GetReg()) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001001 // It is possible we didn't pick correctly so do the actual comparison now.
buzbee2700f7e2014-03-07 09:46:20 -08001002 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001003
1004 // Conditionally move the other integer into the destination register.
1005 ConditionCode condition_code = is_min ? kCondGt : kCondLt;
buzbee2700f7e2014-03-07 09:46:20 -08001006 OpCondRegReg(kOpCmov, condition_code, rl_result.reg, rl_src2.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -08001007 }
1008
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001009 if (is_long) {
Vladimir Markoe508a202013-11-04 15:24:22 +00001010 StoreValueWide(rl_dest, rl_result);
1011 } else {
Vladimir Markoe508a202013-11-04 15:24:22 +00001012 StoreValue(rl_dest, rl_result);
1013 }
1014 return true;
1015}
1016
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001017bool X86Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001018 RegLocation rl_dest = size == k64 ? InlineTargetWide(info) : InlineTarget(info);
1019 if (rl_dest.s_reg_low == INVALID_SREG) {
1020 // Result is unused, the code is dead. Inlining successful, no code generated.
1021 return true;
1022 }
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001023 RegLocation rl_src_address = info->args[0]; // long address
1024 RegLocation rl_address;
1025 if (!cu_->target64) {
1026 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[0]
1027 rl_address = LoadValue(rl_src_address, kCoreReg);
1028 } else {
1029 rl_address = LoadValueWide(rl_src_address, kCoreReg);
1030 }
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001031 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1032 // Unaligned access is allowed on x86.
1033 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
1034 if (size == k64) {
1035 StoreValueWide(rl_dest, rl_result);
1036 } else {
1037 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
1038 StoreValue(rl_dest, rl_result);
1039 }
1040 return true;
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001041}
1042
Vladimir Markoe508a202013-11-04 15:24:22 +00001043bool X86Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001044 RegLocation rl_src_address = info->args[0]; // long address
1045 RegLocation rl_address;
1046 if (!cu_->target64) {
1047 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[0]
1048 rl_address = LoadValue(rl_src_address, kCoreReg);
1049 } else {
1050 rl_address = LoadValueWide(rl_src_address, kCoreReg);
1051 }
1052 RegLocation rl_src_value = info->args[2]; // [size] value
1053 RegLocation rl_value;
1054 if (size == k64) {
1055 // Unaligned access is allowed on x86.
1056 rl_value = LoadValueWide(rl_src_value, kCoreReg);
1057 } else {
1058 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
1059 // In 32-bit mode the only EAX..EDX registers can be used with Mov8MR.
1060 if (!cu_->target64 && size == kSignedByte) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001061 rl_src_value = UpdateLocTyped(rl_src_value);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001062 if (rl_src_value.location == kLocPhysReg && !IsByteRegister(rl_src_value.reg)) {
1063 RegStorage temp = AllocateByteRegister();
1064 OpRegCopy(temp, rl_src_value.reg);
1065 rl_value.reg = temp;
1066 } else {
1067 rl_value = LoadValue(rl_src_value, kCoreReg);
1068 }
1069 } else {
1070 rl_value = LoadValue(rl_src_value, kCoreReg);
1071 }
1072 }
1073 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
1074 return true;
Vladimir Markoe508a202013-11-04 15:24:22 +00001075}
1076
buzbee2700f7e2014-03-07 09:46:20 -08001077void X86Mir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
1078 NewLIR5(kX86Lea32RA, r_base.GetReg(), reg1.GetReg(), reg2.GetReg(), scale, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079}
1080
Ian Rogersdd7624d2014-03-14 17:43:00 -07001081void X86Mir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001082 DCHECK_EQ(kX86, cu_->instruction_set);
1083 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
1084}
1085
1086void X86Mir2Lir::OpTlsCmp(ThreadOffset<8> offset, int val) {
1087 DCHECK_EQ(kX86_64, cu_->instruction_set);
Ian Rogers468532e2013-08-05 10:56:33 -07001088 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089}
1090
buzbee2700f7e2014-03-07 09:46:20 -08001091static bool IsInReg(X86Mir2Lir *pMir2Lir, const RegLocation &rl, RegStorage reg) {
1092 return rl.reg.Valid() && rl.reg.GetReg() == reg.GetReg() && (pMir2Lir->IsLive(reg) || rl.home);
Yevgeny Rouband3a2dfa2014-03-18 15:55:16 +07001093}
1094
Vladimir Marko1c282e22013-11-21 14:49:47 +00001095bool X86Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001096 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001097 // Unused - RegLocation rl_src_unsafe = info->args[0];
1098 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
1099 RegLocation rl_src_offset = info->args[2]; // long low
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001100 if (!cu_->target64) {
1101 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
1102 }
Vladimir Markoc29bb612013-11-27 16:47:25 +00001103 RegLocation rl_src_expected = info->args[4]; // int, long or Object
1104 // If is_long, high half is in info->args[5]
1105 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
1106 // If is_long, high half is in info->args[7]
1107
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001108 if (is_long && cu_->target64) {
1109 // 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 -07001110 FlushReg(rs_r0q);
1111 Clobber(rs_r0q);
1112 LockTemp(rs_r0q);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001113
1114 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
1115 RegLocation rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001116 RegLocation rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
1117 LoadValueDirectWide(rl_src_expected, rs_r0q);
Andreas Gampeccc60262014-07-04 18:02:38 -07001118 NewLIR5(kX86LockCmpxchg64AR, rl_object.reg.GetReg(), rl_offset.reg.GetReg(), 0, 0,
1119 rl_new_value.reg.GetReg());
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001120
1121 // After a store we need to insert barrier in case of potential load. Since the
1122 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated.
Hans Boehm48f5c472014-06-27 14:50:10 -07001123 GenMemBarrier(kAnyAny);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001124
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001125 FreeTemp(rs_r0q);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001126 } else if (is_long) {
Yevgeny Rouband3a2dfa2014-03-18 15:55:16 +07001127 // TODO: avoid unnecessary loads of SI and DI when the values are in registers.
1128 // TODO: CFI support.
Vladimir Marko70b797d2013-12-03 15:25:24 +00001129 FlushAllRegs();
1130 LockCallTemps();
buzbee091cc402014-03-31 10:14:40 -07001131 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
1132 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_rBX, rs_rCX);
buzbee2700f7e2014-03-07 09:46:20 -08001133 LoadValueDirectWideFixed(rl_src_expected, r_tmp1);
1134 LoadValueDirectWideFixed(rl_src_new_value, r_tmp2);
buzbee695d13a2014-04-19 13:32:20 -07001135 // FIXME: needs 64-bit update.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001136 const bool obj_in_di = IsInReg(this, rl_src_obj, rs_rDI);
1137 const bool obj_in_si = IsInReg(this, rl_src_obj, rs_rSI);
1138 DCHECK(!obj_in_si || !obj_in_di);
1139 const bool off_in_di = IsInReg(this, rl_src_offset, rs_rDI);
1140 const bool off_in_si = IsInReg(this, rl_src_offset, rs_rSI);
1141 DCHECK(!off_in_si || !off_in_di);
1142 // If obj/offset is in a reg, use that reg. Otherwise, use the empty reg.
1143 RegStorage rs_obj = obj_in_di ? rs_rDI : obj_in_si ? rs_rSI : !off_in_di ? rs_rDI : rs_rSI;
1144 RegStorage rs_off = off_in_si ? rs_rSI : off_in_di ? rs_rDI : !obj_in_si ? rs_rSI : rs_rDI;
1145 bool push_di = (!obj_in_di && !off_in_di) && (rs_obj == rs_rDI || rs_off == rs_rDI);
1146 bool push_si = (!obj_in_si && !off_in_si) && (rs_obj == rs_rSI || rs_off == rs_rSI);
1147 if (push_di) {
1148 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1149 MarkTemp(rs_rDI);
1150 LockTemp(rs_rDI);
1151 }
1152 if (push_si) {
1153 NewLIR1(kX86Push32R, rs_rSI.GetReg());
1154 MarkTemp(rs_rSI);
1155 LockTemp(rs_rSI);
1156 }
1157 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1158 const size_t push_offset = (push_si ? 4u : 0u) + (push_di ? 4u : 0u);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001159 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001160 if (!obj_in_si && !obj_in_di) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001161 LoadWordDisp(rs_rSP, SRegOffset(rl_src_obj.s_reg_low) + push_offset, rs_obj);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001162 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1163 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1164 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - push_offset / 4u;
1165 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
1166 }
1167 if (!off_in_si && !off_in_di) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001168 LoadWordDisp(rs_rSP, SRegOffset(rl_src_offset.s_reg_low) + push_offset, rs_off);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001169 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1170 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1171 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - push_offset / 4u;
1172 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
1173 }
1174 NewLIR4(kX86LockCmpxchg64A, rs_obj.GetReg(), rs_off.GetReg(), 0, 0);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001175
Hans Boehm48f5c472014-06-27 14:50:10 -07001176 // After a store we need to insert barrier to prevent reordering with either
1177 // earlier or later memory accesses. Since
1178 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated,
1179 // and it will be associated with the cmpxchg instruction, preventing both.
1180 GenMemBarrier(kAnyAny);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001181
1182 if (push_si) {
1183 FreeTemp(rs_rSI);
1184 UnmarkTemp(rs_rSI);
1185 NewLIR1(kX86Pop32R, rs_rSI.GetReg());
1186 }
1187 if (push_di) {
1188 FreeTemp(rs_rDI);
1189 UnmarkTemp(rs_rDI);
1190 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
1191 }
Vladimir Marko70b797d2013-12-03 15:25:24 +00001192 FreeCallTemps();
Vladimir Markoc29bb612013-11-27 16:47:25 +00001193 } else {
1194 // EAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in EAX.
buzbee2700f7e2014-03-07 09:46:20 -08001195 FlushReg(rs_r0);
buzbee091cc402014-03-31 10:14:40 -07001196 Clobber(rs_r0);
buzbee2700f7e2014-03-07 09:46:20 -08001197 LockTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001198
buzbeea0cd2d72014-06-01 09:33:49 -07001199 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
buzbee7c02e912014-10-03 13:14:17 -07001200 RegLocation rl_new_value = LoadValue(rl_src_new_value, LocToRegClass(rl_src_new_value));
Vladimir Markoc29bb612013-11-27 16:47:25 +00001201
1202 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
1203 // Mark card for object assuming new value is stored.
buzbee091cc402014-03-31 10:14:40 -07001204 FreeTemp(rs_r0); // Temporarily release EAX for MarkGCCard().
Vladimir Marko743b98c2014-11-24 19:45:41 +00001205 MarkGCCard(0, rl_new_value.reg, rl_object.reg);
buzbee091cc402014-03-31 10:14:40 -07001206 LockTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001207 }
1208
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001209 RegLocation rl_offset;
1210 if (cu_->target64) {
1211 rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
1212 } else {
1213 rl_offset = LoadValue(rl_src_offset, kCoreReg);
1214 }
buzbee2700f7e2014-03-07 09:46:20 -08001215 LoadValueDirect(rl_src_expected, rs_r0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001216 NewLIR5(kX86LockCmpxchgAR, rl_object.reg.GetReg(), rl_offset.reg.GetReg(), 0, 0,
1217 rl_new_value.reg.GetReg());
Vladimir Markoc29bb612013-11-27 16:47:25 +00001218
Hans Boehm48f5c472014-06-27 14:50:10 -07001219 // After a store we need to insert barrier to prevent reordering with either
1220 // earlier or later memory accesses. Since
1221 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated,
1222 // and it will be associated with the cmpxchg instruction, preventing both.
1223 GenMemBarrier(kAnyAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001224
buzbee091cc402014-03-31 10:14:40 -07001225 FreeTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001226 }
1227
1228 // Convert ZF to boolean
1229 RegLocation rl_dest = InlineTarget(info); // boolean place for result
1230 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001231 RegStorage result_reg = rl_result.reg;
1232
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001233 // For 32-bit, SETcc only works with EAX..EDX.
1234 if (!IsByteRegister(result_reg)) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001235 result_reg = AllocateByteRegister();
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001236 }
1237 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondZ);
1238 NewLIR2(kX86Movzx8RR, rl_result.reg.GetReg(), result_reg.GetReg());
1239 if (IsTemp(result_reg)) {
1240 FreeTemp(result_reg);
1241 }
Vladimir Markoc29bb612013-11-27 16:47:25 +00001242 StoreValue(rl_dest, rl_result);
1243 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244}
1245
Yixin Shou8c914c02014-07-28 14:17:09 -04001246void X86Mir2Lir::SwapBits(RegStorage result_reg, int shift, int32_t value) {
1247 RegStorage r_temp = AllocTemp();
1248 OpRegCopy(r_temp, result_reg);
1249 OpRegImm(kOpLsr, result_reg, shift);
1250 OpRegImm(kOpAnd, r_temp, value);
1251 OpRegImm(kOpAnd, result_reg, value);
1252 OpRegImm(kOpLsl, r_temp, shift);
1253 OpRegReg(kOpOr, result_reg, r_temp);
1254 FreeTemp(r_temp);
1255}
1256
1257void X86Mir2Lir::SwapBits64(RegStorage result_reg, int shift, int64_t value) {
1258 RegStorage r_temp = AllocTempWide();
1259 OpRegCopy(r_temp, result_reg);
1260 OpRegImm(kOpLsr, result_reg, shift);
1261 RegStorage r_value = AllocTempWide();
1262 LoadConstantWide(r_value, value);
1263 OpRegReg(kOpAnd, r_temp, r_value);
1264 OpRegReg(kOpAnd, result_reg, r_value);
1265 OpRegImm(kOpLsl, r_temp, shift);
1266 OpRegReg(kOpOr, result_reg, r_temp);
1267 FreeTemp(r_temp);
1268 FreeTemp(r_value);
1269}
1270
1271bool X86Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001272 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info);
1273 if (rl_dest.s_reg_low == INVALID_SREG) {
1274 // Result is unused, the code is dead. Inlining successful, no code generated.
1275 return true;
1276 }
Yixin Shou8c914c02014-07-28 14:17:09 -04001277 RegLocation rl_src_i = info->args[0];
1278 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg)
1279 : LoadValue(rl_src_i, kCoreReg);
Yixin Shou8c914c02014-07-28 14:17:09 -04001280 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1281 if (size == k64) {
1282 if (cu_->instruction_set == kX86_64) {
1283 /* Use one bswap instruction to reverse byte order first and then use 3 rounds of
1284 swapping bits to reverse bits in a long number x. Using bswap to save instructions
1285 compared to generic luni implementation which has 5 rounds of swapping bits.
1286 x = bswap x
1287 x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
1288 x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
1289 x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
1290 */
1291 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1292 SwapBits64(rl_result.reg, 1, 0x5555555555555555);
1293 SwapBits64(rl_result.reg, 2, 0x3333333333333333);
1294 SwapBits64(rl_result.reg, 4, 0x0f0f0f0f0f0f0f0f);
1295 StoreValueWide(rl_dest, rl_result);
1296 return true;
1297 }
1298 RegStorage r_i_low = rl_i.reg.GetLow();
1299 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
1300 // First REV shall clobber rl_result.reg.GetLowReg(), save the value in a temp for the second
1301 // REV.
1302 r_i_low = AllocTemp();
1303 OpRegCopy(r_i_low, rl_i.reg);
1304 }
1305 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1306 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1307 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
1308 FreeTemp(r_i_low);
1309 }
1310 SwapBits(rl_result.reg.GetLow(), 1, 0x55555555);
1311 SwapBits(rl_result.reg.GetLow(), 2, 0x33333333);
1312 SwapBits(rl_result.reg.GetLow(), 4, 0x0f0f0f0f);
1313 SwapBits(rl_result.reg.GetHigh(), 1, 0x55555555);
1314 SwapBits(rl_result.reg.GetHigh(), 2, 0x33333333);
1315 SwapBits(rl_result.reg.GetHigh(), 4, 0x0f0f0f0f);
1316 StoreValueWide(rl_dest, rl_result);
1317 } else {
1318 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1319 SwapBits(rl_result.reg, 1, 0x55555555);
1320 SwapBits(rl_result.reg, 2, 0x33333333);
1321 SwapBits(rl_result.reg, 4, 0x0f0f0f0f);
1322 StoreValue(rl_dest, rl_result);
1323 }
1324 return true;
1325}
1326
buzbee2700f7e2014-03-07 09:46:20 -08001327LIR* X86Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Mark Mendell27dee8b2014-12-01 19:06:12 -05001328 if (cu_->target64) {
1329 // We can do this directly using RIP addressing.
1330 // We don't know the proper offset for the value, so pick one that will force
1331 // 4 byte offset. We will fix this up in the assembler later to have the right
1332 // value.
1333 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
1334 LIR* res = NewLIR3(kX86Mov32RM, reg.GetReg(), kRIPReg, 256);
1335 res->target = target;
1336 res->flags.fixup = kFixupLoad;
1337 return res;
1338 }
1339
Mark Mendell55d0eac2014-02-06 11:02:52 -08001340 CHECK(base_of_code_ != nullptr);
1341
1342 // Address the start of the method
1343 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001344 if (rl_method.wide) {
1345 LoadValueDirectWideFixed(rl_method, reg);
1346 } else {
1347 LoadValueDirectFixed(rl_method, reg);
1348 }
Mark Mendell55d0eac2014-02-06 11:02:52 -08001349 store_method_addr_used_ = true;
1350
1351 // Load the proper value from the literal area.
1352 // We don't know the proper offset for the value, so pick one that will force
1353 // 4 byte offset. We will fix this up in the assembler later to have the right
1354 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001355 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
buzbee2700f7e2014-03-07 09:46:20 -08001356 LIR *res = RawLIR(current_dalvik_offset_, kX86Mov32RM, reg.GetReg(), reg.GetReg(), 256,
1357 0, 0, target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001358 res->target = target;
1359 res->flags.fixup = kFixupLoad;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001360 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361}
1362
buzbee2700f7e2014-03-07 09:46:20 -08001363LIR* X86Mir2Lir::OpVldm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001364 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 LOG(FATAL) << "Unexpected use of OpVldm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001366 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367}
1368
buzbee2700f7e2014-03-07 09:46:20 -08001369LIR* X86Mir2Lir::OpVstm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001370 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 LOG(FATAL) << "Unexpected use of OpVstm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001372 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373}
1374
1375void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1376 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001377 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001378 UNUSED(lit);
buzbee2700f7e2014-03-07 09:46:20 -08001379 RegStorage t_reg = AllocTemp();
1380 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, second_bit - first_bit);
1381 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 FreeTemp(t_reg);
1383 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001384 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 }
1386}
1387
Mingyao Yange643a172014-04-08 11:02:52 -07001388void X86Mir2Lir::GenDivZeroCheckWide(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001389 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001390 DCHECK(reg.Is64Bit());
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001391
Chao-ying Fua0147762014-06-06 18:38:49 -07001392 NewLIR2(kX86Cmp64RI8, reg.GetReg(), 0);
1393 } else {
1394 DCHECK(reg.IsPair());
1395
1396 // We are not supposed to clobber the incoming storage, so allocate a temporary.
1397 RegStorage t_reg = AllocTemp();
1398 // Doing an OR is a quick way to check if both registers are zero. This will set the flags.
1399 OpRegRegReg(kOpOr, t_reg, reg.GetLow(), reg.GetHigh());
1400 // The temp is no longer needed so free it at this time.
1401 FreeTemp(t_reg);
1402 }
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001403
1404 // In case of zero, throw ArithmeticException.
Mingyao Yange643a172014-04-08 11:02:52 -07001405 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406}
1407
Mingyao Yang80365d92014-04-18 12:10:58 -07001408void X86Mir2Lir::GenArrayBoundsCheck(RegStorage index,
1409 RegStorage array_base,
1410 int len_offset) {
1411 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1412 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001413 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1414 RegStorage index_in, RegStorage array_base_in, int32_t len_offset_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001415 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001416 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001417 }
1418
1419 void Compile() OVERRIDE {
1420 m2l_->ResetRegPool();
1421 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001422 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001423
1424 RegStorage new_index = index_;
1425 // Move index out of kArg1, either directly to kArg0, or to kArg2.
Serguei Katkov4c7cc152014-06-24 00:50:02 +07001426 // TODO: clean-up to check not a number but with type
Andreas Gampeccc60262014-07-04 18:02:38 -07001427 if (index_ == m2l_->TargetReg(kArg1, kNotWide)) {
1428 if (array_base_ == m2l_->TargetReg(kArg0, kRef)) {
1429 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kNotWide), index_);
1430 new_index = m2l_->TargetReg(kArg2, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001431 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001432 m2l_->OpRegCopy(m2l_->TargetReg(kArg0, kNotWide), index_);
1433 new_index = m2l_->TargetReg(kArg0, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001434 }
1435 }
1436 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001437 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1438 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1439 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, new_index,
1440 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001441 }
1442
1443 private:
1444 const RegStorage index_;
1445 const RegStorage array_base_;
1446 const int32_t len_offset_;
1447 };
1448
1449 OpRegMem(kOpCmp, index, array_base, len_offset);
Dave Allison69dfe512014-07-11 17:11:58 +00001450 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001451 LIR* branch = OpCondBranch(kCondUge, nullptr);
1452 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1453 index, array_base, len_offset));
1454}
1455
1456void X86Mir2Lir::GenArrayBoundsCheck(int32_t index,
1457 RegStorage array_base,
1458 int32_t len_offset) {
1459 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1460 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001461 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1462 int32_t index_in, RegStorage array_base_in, int32_t len_offset_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001463 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001464 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001465 }
1466
1467 void Compile() OVERRIDE {
1468 m2l_->ResetRegPool();
1469 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001470 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001471
1472 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001473 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1474 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1475 x86_m2l->LoadConstant(m2l_->TargetReg(kArg0, kNotWide), index_);
1476 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, m2l_->TargetReg(kArg0, kNotWide),
1477 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001478 }
1479
1480 private:
1481 const int32_t index_;
1482 const RegStorage array_base_;
1483 const int32_t len_offset_;
1484 };
1485
1486 NewLIR3(IS_SIMM8(index) ? kX86Cmp32MI8 : kX86Cmp32MI, array_base.GetReg(), len_offset, index);
Dave Allison69dfe512014-07-11 17:11:58 +00001487 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001488 LIR* branch = OpCondBranch(kCondLs, nullptr);
1489 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1490 index, array_base, len_offset));
1491}
1492
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001494LIR* X86Mir2Lir::OpTestSuspend(LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -07001495 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001496 OpTlsCmp(Thread::ThreadFlagsOffset<8>(), 0);
1497 } else {
1498 OpTlsCmp(Thread::ThreadFlagsOffset<4>(), 0);
1499 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 return OpCondBranch((target == NULL) ? kCondNe : kCondEq, target);
1501}
1502
1503// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001504LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 OpRegImm(kOpSub, reg, 1);
Yixin Shoua0dac3e2014-01-23 05:01:22 -08001506 return OpCondBranch(c_code, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507}
1508
buzbee11b63d12013-08-27 07:34:17 -07001509bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001510 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001511 UNUSED(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512 LOG(FATAL) << "Unexpected use of smallLiteralDive in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001513 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514}
1515
Ian Rogerse2143c02014-03-28 08:47:16 -07001516bool X86Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001517 UNUSED(rl_src, rl_dest, lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001518 LOG(FATAL) << "Unexpected use of easyMultiply in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001519 UNREACHABLE();
Ian Rogerse2143c02014-03-28 08:47:16 -07001520}
1521
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001522LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001523 UNUSED(cond, guide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 LOG(FATAL) << "Unexpected use of OpIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001525 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526}
1527
Dave Allison3da67a52014-04-02 17:03:45 -07001528void X86Mir2Lir::OpEndIT(LIR* it) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001529 UNUSED(it);
Dave Allison3da67a52014-04-02 17:03:45 -07001530 LOG(FATAL) << "Unexpected use of OpEndIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001531 UNREACHABLE();
Dave Allison3da67a52014-04-02 17:03:45 -07001532}
1533
buzbee2700f7e2014-03-07 09:46:20 -08001534void X86Mir2Lir::GenImulRegImm(RegStorage dest, RegStorage src, int val) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001535 switch (val) {
1536 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001537 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001538 break;
1539 case 1:
1540 OpRegCopy(dest, src);
1541 break;
1542 default:
1543 OpRegRegImm(kOpMul, dest, src, val);
1544 break;
1545 }
1546}
1547
buzbee2700f7e2014-03-07 09:46:20 -08001548void X86Mir2Lir::GenImulMemImm(RegStorage dest, int sreg, int displacement, int val) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001549 UNUSED(sreg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001550 // All memory accesses below reference dalvik regs.
1551 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1552
Mark Mendell4708dcd2014-01-22 09:05:18 -08001553 LIR *m;
1554 switch (val) {
1555 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001556 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001557 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001558 case 1: {
1559 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
1560 LoadBaseDisp(rs_rSP, displacement, dest, k32, kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001561 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001562 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001563 default:
buzbee091cc402014-03-31 10:14:40 -07001564 m = NewLIR4(IS_SIMM8(val) ? kX86Imul32RMI8 : kX86Imul32RMI, dest.GetReg(),
Ian Rogersb28c1c02014-11-08 11:21:21 -08001565 rs_rX86_SP_32.GetReg(), displacement, val);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001566 AnnotateDalvikRegAccess(m, displacement >> 2, true /* is_load */, true /* is_64bit */);
1567 break;
1568 }
1569}
1570
Andreas Gampec76c6142014-08-04 16:30:03 -07001571void X86Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001572 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001573 if (!cu_->target64) {
1574 // Some x86 32b ops are fallback.
1575 switch (opcode) {
1576 case Instruction::NOT_LONG:
1577 case Instruction::DIV_LONG:
1578 case Instruction::DIV_LONG_2ADDR:
1579 case Instruction::REM_LONG:
1580 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001581 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001582 return;
1583
1584 default:
1585 // Everything else we can handle.
1586 break;
1587 }
1588 }
1589
1590 switch (opcode) {
1591 case Instruction::NOT_LONG:
1592 GenNotLong(rl_dest, rl_src2);
1593 return;
1594
1595 case Instruction::ADD_LONG:
1596 case Instruction::ADD_LONG_2ADDR:
1597 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1598 return;
1599
1600 case Instruction::SUB_LONG:
1601 case Instruction::SUB_LONG_2ADDR:
1602 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, false);
1603 return;
1604
1605 case Instruction::MUL_LONG:
1606 case Instruction::MUL_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001607 GenMulLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001608 return;
1609
1610 case Instruction::DIV_LONG:
1611 case Instruction::DIV_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001612 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001613 return;
1614
1615 case Instruction::REM_LONG:
1616 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001617 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001618 return;
1619
1620 case Instruction::AND_LONG_2ADDR:
1621 case Instruction::AND_LONG:
1622 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1623 return;
1624
1625 case Instruction::OR_LONG:
1626 case Instruction::OR_LONG_2ADDR:
1627 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1628 return;
1629
1630 case Instruction::XOR_LONG:
1631 case Instruction::XOR_LONG_2ADDR:
1632 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1633 return;
1634
1635 case Instruction::NEG_LONG:
1636 GenNegLong(rl_dest, rl_src2);
1637 return;
1638
1639 default:
1640 LOG(FATAL) << "Invalid long arith op";
1641 return;
1642 }
1643}
1644
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001645bool X86Mir2Lir::GenMulLongConst(RegLocation rl_dest, RegLocation rl_src1, int64_t val, int flags) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001646 // All memory accesses below reference dalvik regs.
1647 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1648
Andreas Gampec76c6142014-08-04 16:30:03 -07001649 if (val == 0) {
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001650 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampec76c6142014-08-04 16:30:03 -07001651 if (cu_->target64) {
1652 OpRegReg(kOpXor, rl_result.reg, rl_result.reg);
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001653 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001654 OpRegReg(kOpXor, rl_result.reg.GetLow(), rl_result.reg.GetLow());
1655 OpRegReg(kOpXor, rl_result.reg.GetHigh(), rl_result.reg.GetHigh());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001656 }
Andreas Gampec76c6142014-08-04 16:30:03 -07001657 StoreValueWide(rl_dest, rl_result);
1658 return true;
1659 } else if (val == 1) {
1660 StoreValueWide(rl_dest, rl_src1);
1661 return true;
1662 } else if (val == 2) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001663 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src1, rl_src1, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001664 return true;
1665 } else if (IsPowerOfTwo(val)) {
Andreas Gampe7e499922015-01-06 08:28:12 -08001666 int shift_amount = CTZ(val);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001667 if (!PartiallyIntersects(rl_src1, rl_dest)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001668 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1669 RegLocation rl_result = GenShiftImmOpLong(Instruction::SHL_LONG, rl_dest, rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001670 shift_amount, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001671 StoreValueWide(rl_dest, rl_result);
1672 return true;
1673 }
1674 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001675
Andreas Gampec76c6142014-08-04 16:30:03 -07001676 // Okay, on 32b just bite the bullet and do it, still better than the general case.
1677 if (!cu_->target64) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001678 int32_t val_lo = Low32Bits(val);
1679 int32_t val_hi = High32Bits(val);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001680 // Prepare for explicit register usage.
1681 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001682 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001683 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1684 int displacement = SRegOffset(rl_src1.s_reg_low);
1685
1686 // ECX <- 1H * 2L
1687 // EAX <- 1L * 2H
1688 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001689 GenImulRegImm(rs_r1, rl_src1.reg.GetHigh(), val_lo);
1690 GenImulRegImm(rs_r0, rl_src1.reg.GetLow(), val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001691 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001692 GenImulMemImm(rs_r1, GetSRegHi(rl_src1.s_reg_low), displacement + HIWORD_OFFSET, val_lo);
1693 GenImulMemImm(rs_r0, rl_src1.s_reg_low, displacement + LOWORD_OFFSET, val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001694 }
1695
1696 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001697 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001698
1699 // EAX <- 2L
buzbee2700f7e2014-03-07 09:46:20 -08001700 LoadConstantNoClobber(rs_r0, val_lo);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001701
1702 // EDX:EAX <- 2L * 1L (double precision)
1703 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001704 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001705 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001706 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001707 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1708 true /* is_load */, true /* is_64bit */);
1709 }
1710
1711 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001712 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001713
1714 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001715 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
1716 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001717 StoreValueWide(rl_dest, rl_result);
Andreas Gampec76c6142014-08-04 16:30:03 -07001718 return true;
1719 }
1720 return false;
1721}
1722
1723void X86Mir2Lir::GenMulLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001724 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001725 if (rl_src1.is_const) {
1726 std::swap(rl_src1, rl_src2);
1727 }
1728
1729 if (rl_src2.is_const) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001730 if (GenMulLongConst(rl_dest, rl_src1, mir_graph_->ConstantValueWide(rl_src2), flags)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001731 return;
1732 }
1733 }
1734
1735 // All memory accesses below reference dalvik regs.
1736 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1737
1738 if (cu_->target64) {
1739 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1740 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1741 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1742 if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1743 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1744 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
1745 } else if (rl_result.reg.GetReg() != rl_src1.reg.GetReg() &&
1746 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1747 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
1748 } else if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1749 rl_result.reg.GetReg() != rl_src2.reg.GetReg()) {
1750 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1751 } else {
1752 OpRegCopy(rl_result.reg, rl_src1.reg);
1753 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1754 }
1755 StoreValueWide(rl_dest, rl_result);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001756 return;
1757 }
1758
Andreas Gampec76c6142014-08-04 16:30:03 -07001759 // Not multiplying by a constant. Do it the hard way
Mark Mendellde99bba2014-02-14 12:15:02 -08001760 // Check for V*V. We can eliminate a multiply in that case, as 2L*1H == 2H*1L.
1761 bool is_square = mir_graph_->SRegToVReg(rl_src1.s_reg_low) ==
1762 mir_graph_->SRegToVReg(rl_src2.s_reg_low);
1763
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001764 // Prepare for explicit register usage.
1765 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001766 rl_src1 = UpdateLocWideTyped(rl_src1);
1767 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001768
1769 // At this point, the VRs are in their home locations.
1770 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1771 bool src2_in_reg = rl_src2.location == kLocPhysReg;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001772 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001773
1774 // ECX <- 1H
1775 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001776 NewLIR2(kX86Mov32RR, rs_r1.GetReg(), rl_src1.reg.GetHighReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001777 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001778 LoadBaseDisp(rs_rSP, SRegOffset(rl_src1.s_reg_low) + HIWORD_OFFSET, rs_r1, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001779 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001780 }
1781
Mark Mendellde99bba2014-02-14 12:15:02 -08001782 if (is_square) {
1783 // Take advantage of the fact that the values are the same.
1784 // ECX <- ECX * 2L (1H * 2L)
1785 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001786 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001787 } else {
1788 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001789 LIR* m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001790 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001791 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1792 true /* is_load */, true /* is_64bit */);
1793 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001794
Mark Mendellde99bba2014-02-14 12:15:02 -08001795 // ECX <- 2*ECX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001796 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001797 } else {
Mark Mendellde99bba2014-02-14 12:15:02 -08001798 // EAX <- 2H
1799 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001800 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetHighReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001801 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001802 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + HIWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001803 kNotVolatile);
Mark Mendellde99bba2014-02-14 12:15:02 -08001804 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001805
Mark Mendellde99bba2014-02-14 12:15:02 -08001806 // EAX <- EAX * 1L (2H * 1L)
1807 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001808 NewLIR2(kX86Imul32RR, rs_r0.GetReg(), rl_src1.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001809 } else {
1810 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001811 LIR *m = NewLIR3(kX86Imul32RM, rs_r0.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001812 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001813 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1814 true /* is_load */, true /* is_64bit */);
1815 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001816
Mark Mendellde99bba2014-02-14 12:15:02 -08001817 // ECX <- ECX * 2L (1H * 2L)
1818 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001819 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001820 } else {
1821 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001822 LIR *m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001823 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001824 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1825 true /* is_load */, true /* is_64bit */);
1826 }
1827
1828 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001829 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001830 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001831
1832 // EAX <- 2L
1833 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001834 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001835 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001836 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + LOWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001837 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001838 }
1839
1840 // EDX:EAX <- 2L * 1L (double precision)
1841 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001842 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001843 } else {
1844 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001845 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001846 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1847 true /* is_load */, true /* is_64bit */);
1848 }
1849
1850 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001851 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001852
1853 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001854 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
buzbee2700f7e2014-03-07 09:46:20 -08001855 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001856 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001857}
Mark Mendelle02d48f2014-01-15 11:19:23 -08001858
1859void X86Mir2Lir::GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src,
1860 Instruction::Code op) {
1861 DCHECK_EQ(rl_dest.location, kLocPhysReg);
1862 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
1863 if (rl_src.location == kLocPhysReg) {
1864 // Both operands are in registers.
Serguei Katkovab5545f2014-03-25 10:51:15 +07001865 // But we must ensure that rl_src is in pair
Elena Sayapinadd644502014-07-01 18:39:52 +07001866 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001867 NewLIR2(x86op, rl_dest.reg.GetReg(), rl_src.reg.GetReg());
1868 } else {
1869 rl_src = LoadValueWide(rl_src, kCoreReg);
1870 if (rl_dest.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1871 // The registers are the same, so we would clobber it before the use.
1872 RegStorage temp_reg = AllocTemp();
1873 OpRegCopy(temp_reg, rl_dest.reg);
1874 rl_src.reg.SetHighReg(temp_reg.GetReg());
1875 }
1876 NewLIR2(x86op, rl_dest.reg.GetLowReg(), rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08001877
Chao-ying Fua0147762014-06-06 18:38:49 -07001878 x86op = GetOpcode(op, rl_dest, rl_src, true);
1879 NewLIR2(x86op, rl_dest.reg.GetHighReg(), rl_src.reg.GetHighReg());
Chao-ying Fua0147762014-06-06 18:38:49 -07001880 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08001881 return;
1882 }
1883
1884 // RHS is in memory.
1885 DCHECK((rl_src.location == kLocDalvikFrame) ||
1886 (rl_src.location == kLocCompilerTemp));
Ian Rogersb28c1c02014-11-08 11:21:21 -08001887 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08001888 int displacement = SRegOffset(rl_src.s_reg_low);
1889
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001890 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -07001891 LIR *lir = NewLIR3(x86op, cu_->target64 ? rl_dest.reg.GetReg() : rl_dest.reg.GetLowReg(),
1892 r_base, displacement + LOWORD_OFFSET);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001893 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
1894 true /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07001895 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001896 x86op = GetOpcode(op, rl_dest, rl_src, true);
1897 lir = NewLIR3(x86op, rl_dest.reg.GetHighReg(), r_base, displacement + HIWORD_OFFSET);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001898 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1899 true /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07001900 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001901}
1902
Mark Mendelle02d48f2014-01-15 11:19:23 -08001903void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001904 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001905 if (rl_dest.location == kLocPhysReg) {
1906 // Ensure we are in a register pair
1907 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1908
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001909 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001910 GenLongRegOrMemOp(rl_result, rl_src, op);
1911 StoreFinalValueWide(rl_dest, rl_result);
1912 return;
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001913 } else if (!cu_->target64 && Intersects(rl_src, rl_dest)) {
1914 // Handle the case when src and dest are intersect.
1915 rl_src = LoadValueWide(rl_src, kCoreReg);
1916 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001917 rl_src = UpdateLocWideTyped(rl_src);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001918 GenLongRegOrMemOp(rl_result, rl_src, op);
1919 StoreFinalValueWide(rl_dest, rl_result);
1920 return;
Mark Mendelle02d48f2014-01-15 11:19:23 -08001921 }
1922
1923 // It wasn't in registers, so it better be in memory.
1924 DCHECK((rl_dest.location == kLocDalvikFrame) ||
1925 (rl_dest.location == kLocCompilerTemp));
1926 rl_src = LoadValueWide(rl_src, kCoreReg);
1927
1928 // Operate directly into memory.
1929 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001930 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08001931 int displacement = SRegOffset(rl_dest.s_reg_low);
1932
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001933 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07001934 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET,
Elena Sayapinadd644502014-07-01 18:39:52 +07001935 cu_->target64 ? rl_src.reg.GetReg() : rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08001936 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07001937 true /* is_load */, true /* is64bit */);
1938 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08001939 false /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07001940 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001941 x86op = GetOpcode(op, rl_dest, rl_src, true);
1942 lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, rl_src.reg.GetHighReg());
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001943 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1944 true /* is_load */, true /* is64bit */);
1945 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1946 false /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07001947 }
nikolay serdjuk6b9356c2014-11-13 18:15:23 +06001948
1949 int v_src_reg = mir_graph_->SRegToVReg(rl_src.s_reg_low);
1950 int v_dst_reg = mir_graph_->SRegToVReg(rl_dest.s_reg_low);
1951
1952 // If the left operand is in memory and the right operand is in a register
1953 // and both belong to the same dalvik register then we should clobber the
1954 // right one because it doesn't hold valid data anymore.
1955 if (v_src_reg == v_dst_reg) {
1956 Clobber(rl_src.reg);
1957 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958}
1959
Mark Mendelle02d48f2014-01-15 11:19:23 -08001960void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src1,
1961 RegLocation rl_src2, Instruction::Code op,
1962 bool is_commutative) {
1963 // Is this really a 2 operand operation?
1964 switch (op) {
1965 case Instruction::ADD_LONG_2ADDR:
1966 case Instruction::SUB_LONG_2ADDR:
1967 case Instruction::AND_LONG_2ADDR:
1968 case Instruction::OR_LONG_2ADDR:
1969 case Instruction::XOR_LONG_2ADDR:
Mark Mendelle87f9b52014-04-30 14:13:18 -04001970 if (GenerateTwoOperandInstructions()) {
1971 GenLongArith(rl_dest, rl_src2, op);
1972 return;
1973 }
1974 break;
1975
Mark Mendelle02d48f2014-01-15 11:19:23 -08001976 default:
1977 break;
1978 }
1979
1980 if (rl_dest.location == kLocPhysReg) {
1981 RegLocation rl_result = LoadValueWide(rl_src1, kCoreReg);
1982
1983 // We are about to clobber the LHS, so it needs to be a temp.
1984 rl_result = ForceTempWide(rl_result);
1985
1986 // Perform the operation using the RHS.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001987 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001988 GenLongRegOrMemOp(rl_result, rl_src2, op);
1989
1990 // And now record that the result is in the temp.
1991 StoreFinalValueWide(rl_dest, rl_result);
1992 return;
1993 }
1994
1995 // It wasn't in registers, so it better be in memory.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001996 DCHECK((rl_dest.location == kLocDalvikFrame) || (rl_dest.location == kLocCompilerTemp));
1997 rl_src1 = UpdateLocWideTyped(rl_src1);
1998 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001999
2000 // Get one of the source operands into temporary register.
2001 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +07002002 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002003 if (IsTemp(rl_src1.reg)) {
2004 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2005 } else if (is_commutative) {
2006 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2007 // We need at least one of them to be a temporary.
2008 if (!IsTemp(rl_src2.reg)) {
2009 rl_src1 = ForceTempWide(rl_src1);
2010 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2011 } else {
2012 GenLongRegOrMemOp(rl_src2, rl_src1, op);
2013 StoreFinalValueWide(rl_dest, rl_src2);
2014 return;
2015 }
2016 } else {
2017 // Need LHS to be the temp.
Mark Mendelle02d48f2014-01-15 11:19:23 -08002018 rl_src1 = ForceTempWide(rl_src1);
Yevgeny Rouban91b6ffa2014-03-07 14:35:44 +07002019 GenLongRegOrMemOp(rl_src1, rl_src2, op);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002020 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002021 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002022 if (IsTemp(rl_src1.reg.GetLow()) && IsTemp(rl_src1.reg.GetHigh())) {
2023 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2024 } else if (is_commutative) {
2025 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2026 // We need at least one of them to be a temporary.
2027 if (!(IsTemp(rl_src2.reg.GetLow()) && IsTemp(rl_src2.reg.GetHigh()))) {
2028 rl_src1 = ForceTempWide(rl_src1);
2029 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2030 } else {
2031 GenLongRegOrMemOp(rl_src2, rl_src1, op);
2032 StoreFinalValueWide(rl_dest, rl_src2);
2033 return;
2034 }
2035 } else {
2036 // Need LHS to be the temp.
2037 rl_src1 = ForceTempWide(rl_src1);
2038 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2039 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002040 }
2041
2042 StoreFinalValueWide(rl_dest, rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043}
2044
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002045void X86Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002046 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002047 rl_src = LoadValueWide(rl_src, kCoreReg);
2048 RegLocation rl_result;
2049 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2050 OpRegCopy(rl_result.reg, rl_src.reg);
2051 OpReg(kOpNot, rl_result.reg);
2052 StoreValueWide(rl_dest, rl_result);
2053 } else {
2054 LOG(FATAL) << "Unexpected use GenNotLong()";
2055 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002056}
2057
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002058void X86Mir2Lir::GenDivRemLongLit(RegLocation rl_dest, RegLocation rl_src,
2059 int64_t imm, bool is_div) {
2060 if (imm == 0) {
2061 GenDivZeroException();
2062 } else if (imm == 1) {
2063 if (is_div) {
2064 // x / 1 == x.
2065 StoreValueWide(rl_dest, rl_src);
2066 } else {
2067 // x % 1 == 0.
2068 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2069 LoadConstantWide(rl_result.reg, 0);
2070 StoreValueWide(rl_dest, rl_result);
2071 }
2072 } else if (imm == -1) { // handle 0x8000000000000000 / -1 special case.
2073 if (is_div) {
2074 rl_src = LoadValueWide(rl_src, kCoreReg);
2075 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2076 RegStorage rs_temp = AllocTempWide();
2077
2078 OpRegCopy(rl_result.reg, rl_src.reg);
2079 LoadConstantWide(rs_temp, 0x8000000000000000);
2080
2081 // If x == MIN_LONG, return MIN_LONG.
2082 OpRegReg(kOpCmp, rl_src.reg, rs_temp);
2083 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
2084
2085 // For x != MIN_LONG, x / -1 == -x.
2086 OpReg(kOpNeg, rl_result.reg);
2087
2088 minint_branch->target = NewLIR0(kPseudoTargetLabel);
2089 FreeTemp(rs_temp);
2090 StoreValueWide(rl_dest, rl_result);
2091 } else {
2092 // x % -1 == 0.
2093 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2094 LoadConstantWide(rl_result.reg, 0);
2095 StoreValueWide(rl_dest, rl_result);
2096 }
2097 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
2098 // Division using shifting.
2099 rl_src = LoadValueWide(rl_src, kCoreReg);
2100 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2101 if (IsSameReg(rl_result.reg, rl_src.reg)) {
2102 RegStorage rs_temp = AllocTypedTempWide(false, kCoreReg);
2103 rl_result.reg.SetReg(rs_temp.GetReg());
2104 }
2105 LoadConstantWide(rl_result.reg, std::abs(imm) - 1);
2106 OpRegReg(kOpAdd, rl_result.reg, rl_src.reg);
2107 NewLIR2(kX86Test64RR, rl_src.reg.GetReg(), rl_src.reg.GetReg());
2108 OpCondRegReg(kOpCmov, kCondPl, rl_result.reg, rl_src.reg);
Andreas Gampe7e499922015-01-06 08:28:12 -08002109 int shift_amount = CTZ(imm);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002110 OpRegImm(kOpAsr, rl_result.reg, shift_amount);
2111 if (imm < 0) {
2112 OpReg(kOpNeg, rl_result.reg);
2113 }
2114 StoreValueWide(rl_dest, rl_result);
2115 } else {
2116 CHECK(imm <= -2 || imm >= 2);
2117
2118 FlushReg(rs_r0q);
2119 Clobber(rs_r0q);
2120 LockTemp(rs_r0q);
2121 FlushReg(rs_r2q);
2122 Clobber(rs_r2q);
2123 LockTemp(rs_r2q);
2124
Mark Mendell3a91f442014-09-02 12:44:24 -04002125 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
2126 is_div ? rs_r2q : rs_r0q, INVALID_SREG, INVALID_SREG};
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002127
2128 // Use H.S.Warren's Hacker's Delight Chapter 10 and
2129 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
2130 int64_t magic;
2131 int shift;
2132 CalculateMagicAndShift(imm, magic, shift, true /* is_long */);
2133
2134 /*
2135 * For imm >= 2,
2136 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
2137 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
2138 * For imm <= -2,
2139 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
2140 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
2141 * We implement this algorithm in the following way:
2142 * 1. multiply magic number m and numerator n, get the higher 64bit result in RDX
2143 * 2. if imm > 0 and magic < 0, add numerator to RDX
2144 * if imm < 0 and magic > 0, sub numerator from RDX
2145 * 3. if S !=0, SAR S bits for RDX
2146 * 4. add 1 to RDX if RDX < 0
2147 * 5. Thus, RDX is the quotient
2148 */
2149
Mark Mendell3a91f442014-09-02 12:44:24 -04002150 // RAX = magic.
2151 LoadConstantWide(rs_r0q, magic);
2152
2153 // Multiply by numerator.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002154 RegStorage numerator_reg;
2155 if (!is_div || (imm > 0 && magic < 0) || (imm < 0 && magic > 0)) {
2156 // We will need the value later.
2157 rl_src = LoadValueWide(rl_src, kCoreReg);
2158 numerator_reg = rl_src.reg;
Mark Mendell3a91f442014-09-02 12:44:24 -04002159
2160 // RDX:RAX = magic * numerator.
2161 NewLIR1(kX86Imul64DaR, numerator_reg.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002162 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002163 // Only need this once. Multiply directly from the value.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002164 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendell3a91f442014-09-02 12:44:24 -04002165 if (rl_src.location != kLocPhysReg) {
2166 // Okay, we can do this from memory.
2167 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2168 int displacement = SRegOffset(rl_src.s_reg_low);
2169 // RDX:RAX = magic * numerator.
Ian Rogersb28c1c02014-11-08 11:21:21 -08002170 LIR *m = NewLIR2(kX86Imul64DaM, rs_rX86_SP_32.GetReg(), displacement);
Mark Mendell3a91f442014-09-02 12:44:24 -04002171 AnnotateDalvikRegAccess(m, displacement >> 2,
2172 true /* is_load */, true /* is_64bit */);
2173 } else {
2174 // RDX:RAX = magic * numerator.
2175 NewLIR1(kX86Imul64DaR, rl_src.reg.GetReg());
2176 }
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002177 }
2178
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002179 if (imm > 0 && magic < 0) {
2180 // Add numerator to RDX.
2181 DCHECK(numerator_reg.Valid());
2182 OpRegReg(kOpAdd, rs_r2q, numerator_reg);
2183 } else if (imm < 0 && magic > 0) {
2184 DCHECK(numerator_reg.Valid());
2185 OpRegReg(kOpSub, rs_r2q, numerator_reg);
2186 }
2187
2188 // Do we need the shift?
2189 if (shift != 0) {
2190 // Shift RDX by 'shift' bits.
2191 OpRegImm(kOpAsr, rs_r2q, shift);
2192 }
2193
2194 // Move RDX to RAX.
2195 OpRegCopyWide(rs_r0q, rs_r2q);
2196
2197 // Move sign bit to bit 0, zeroing the rest.
2198 OpRegImm(kOpLsr, rs_r2q, 63);
2199
2200 // RDX = RDX + RAX.
2201 OpRegReg(kOpAdd, rs_r2q, rs_r0q);
2202
2203 // Quotient is in RDX.
2204 if (!is_div) {
2205 // We need to compute the remainder.
2206 // Remainder is divisor - (quotient * imm).
2207 DCHECK(numerator_reg.Valid());
2208 OpRegCopyWide(rs_r0q, numerator_reg);
2209
2210 // Imul doesn't support 64-bit imms.
2211 if (imm > std::numeric_limits<int32_t>::max() ||
2212 imm < std::numeric_limits<int32_t>::min()) {
2213 RegStorage rs_temp = AllocTempWide();
2214 LoadConstantWide(rs_temp, imm);
2215
2216 // RAX = numerator * imm.
2217 NewLIR2(kX86Imul64RR, rs_r2q.GetReg(), rs_temp.GetReg());
2218
2219 FreeTemp(rs_temp);
2220 } else {
2221 // RAX = numerator * imm.
2222 int short_imm = static_cast<int>(imm);
2223 NewLIR3(kX86Imul64RRI, rs_r2q.GetReg(), rs_r2q.GetReg(), short_imm);
2224 }
2225
Mark Mendell3a91f442014-09-02 12:44:24 -04002226 // RAX -= RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002227 OpRegReg(kOpSub, rs_r0q, rs_r2q);
2228
Mark Mendell3a91f442014-09-02 12:44:24 -04002229 // Result in RAX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002230 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002231 // Result in RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002232 }
2233 StoreValueWide(rl_dest, rl_result);
2234 FreeTemp(rs_r0q);
2235 FreeTemp(rs_r2q);
2236 }
2237}
2238
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002239void X86Mir2Lir::GenDivRemLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002240 RegLocation rl_src2, bool is_div, int flags) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002241 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002242 LOG(FATAL) << "Unexpected use GenDivRemLong()";
2243 return;
2244 }
2245
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002246 if (rl_src2.is_const) {
2247 DCHECK(rl_src2.wide);
2248 int64_t imm = mir_graph_->ConstantValueWide(rl_src2);
2249 GenDivRemLongLit(rl_dest, rl_src1, imm, is_div);
2250 return;
2251 }
2252
Chao-ying Fua0147762014-06-06 18:38:49 -07002253 // We have to use fixed registers, so flush all the temps.
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002254 // Prepare for explicit register usage.
2255 ExplicitTempRegisterLock(this, 4, &rs_r0q, &rs_r1q, &rs_r2q, &rs_r6q);
Chao-ying Fua0147762014-06-06 18:38:49 -07002256
2257 // Load LHS into RAX.
2258 LoadValueDirectWideFixed(rl_src1, rs_r0q);
2259
2260 // Load RHS into RCX.
2261 LoadValueDirectWideFixed(rl_src2, rs_r1q);
2262
2263 // Copy LHS sign bit into RDX.
2264 NewLIR0(kx86Cqo64Da);
2265
2266 // Handle division by zero case.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002267 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2268 GenDivZeroCheckWide(rs_r1q);
2269 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002270
2271 // Have to catch 0x8000000000000000/-1 case, or we will get an exception!
2272 NewLIR2(kX86Cmp64RI8, rs_r1q.GetReg(), -1);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002273 LIR* minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002274
2275 // RHS is -1.
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002276 LoadConstantWide(rs_r6q, 0x8000000000000000);
2277 NewLIR2(kX86Cmp64RR, rs_r0q.GetReg(), rs_r6q.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002278 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002279
2280 // In 0x8000000000000000/-1 case.
2281 if (!is_div) {
2282 // For DIV, RAX is already right. For REM, we need RDX 0.
2283 NewLIR2(kX86Xor64RR, rs_r2q.GetReg(), rs_r2q.GetReg());
2284 }
2285 LIR* done = NewLIR1(kX86Jmp8, 0);
2286
2287 // Expected case.
2288 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
2289 minint_branch->target = minus_one_branch->target;
2290 NewLIR1(kX86Idivmod64DaR, rs_r1q.GetReg());
2291 done->target = NewLIR0(kPseudoTargetLabel);
2292
2293 // Result is in RAX for div and RDX for rem.
2294 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, rs_r0q, INVALID_SREG, INVALID_SREG};
2295 if (!is_div) {
2296 rl_result.reg.SetReg(r2q);
2297 }
2298
2299 StoreValueWide(rl_dest, rl_result);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002300}
2301
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002302void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002303 rl_src = LoadValueWide(rl_src, kCoreReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002304 RegLocation rl_result;
Elena Sayapinadd644502014-07-01 18:39:52 +07002305 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002306 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2307 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
2308 } else {
2309 rl_result = ForceTempWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002310 OpRegReg(kOpNeg, rl_result.reg.GetLow(), rl_result.reg.GetLow()); // rLow = -rLow
2311 OpRegImm(kOpAdc, rl_result.reg.GetHigh(), 0); // rHigh = rHigh + CF
2312 OpRegReg(kOpNeg, rl_result.reg.GetHigh(), rl_result.reg.GetHigh()); // rHigh = -rHigh
Mark Mendelle02d48f2014-01-15 11:19:23 -08002313 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002314 StoreValueWide(rl_dest, rl_result);
2315}
2316
buzbee091cc402014-03-31 10:14:40 -07002317void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<4> thread_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002318 DCHECK_EQ(kX86, cu_->instruction_set);
2319 X86OpCode opcode = kX86Bkpt;
2320 switch (op) {
2321 case kOpCmp: opcode = kX86Cmp32RT; break;
2322 case kOpMov: opcode = kX86Mov32RT; break;
2323 default:
2324 LOG(FATAL) << "Bad opcode: " << op;
2325 break;
2326 }
2327 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
2328}
2329
2330void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<8> thread_offset) {
2331 DCHECK_EQ(kX86_64, cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002332 X86OpCode opcode = kX86Bkpt;
Elena Sayapinadd644502014-07-01 18:39:52 +07002333 if (cu_->target64 && r_dest.Is64BitSolo()) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07002334 switch (op) {
2335 case kOpCmp: opcode = kX86Cmp64RT; break;
2336 case kOpMov: opcode = kX86Mov64RT; break;
2337 default:
2338 LOG(FATAL) << "Bad opcode(OpRegThreadMem 64): " << op;
2339 break;
2340 }
2341 } else {
2342 switch (op) {
2343 case kOpCmp: opcode = kX86Cmp32RT; break;
2344 case kOpMov: opcode = kX86Mov32RT; break;
2345 default:
2346 LOG(FATAL) << "Bad opcode: " << op;
2347 break;
2348 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002349 }
buzbee091cc402014-03-31 10:14:40 -07002350 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002351}
2352
2353/*
2354 * Generate array load
2355 */
2356void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002357 RegLocation rl_index, RegLocation rl_dest, int scale) {
Mark Mendellca541342014-10-15 16:59:49 -04002358 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002359 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002360 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002361 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002362
Mark Mendell343adb52013-12-18 06:02:17 -08002363 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07002364 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002365 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2366 } else {
2367 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2368 }
2369
Mark Mendell343adb52013-12-18 06:02:17 -08002370 bool constant_index = rl_index.is_const;
2371 int32_t constant_index_value = 0;
2372 if (!constant_index) {
2373 rl_index = LoadValue(rl_index, kCoreReg);
2374 } else {
2375 constant_index_value = mir_graph_->ConstantValue(rl_index);
2376 // If index is constant, just fold it into the data offset
2377 data_offset += constant_index_value << scale;
2378 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002379 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002380 }
2381
Brian Carlstrom7940e442013-07-12 13:46:57 -07002382 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002383 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002384
2385 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002386 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002387 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002388 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002389 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002390 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002391 }
Mark Mendell343adb52013-12-18 06:02:17 -08002392 rl_result = EvalLoc(rl_dest, reg_class, true);
Vladimir Marko3bf7c602014-05-07 14:55:43 +01002393 LoadBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_result.reg, size);
buzbee695d13a2014-04-19 13:32:20 -07002394 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002395 StoreValueWide(rl_dest, rl_result);
2396 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002397 StoreValue(rl_dest, rl_result);
2398 }
2399}
2400
2401/*
2402 * Generate array store
2403 *
2404 */
2405void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002406 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Mark Mendellca541342014-10-15 16:59:49 -04002407 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002408 int len_offset = mirror::Array::LengthOffset().Int32Value();
2409 int data_offset;
2410
buzbee695d13a2014-04-19 13:32:20 -07002411 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002412 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2413 } else {
2414 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2415 }
2416
buzbeea0cd2d72014-06-01 09:33:49 -07002417 rl_array = LoadValue(rl_array, kRefReg);
Mark Mendell343adb52013-12-18 06:02:17 -08002418 bool constant_index = rl_index.is_const;
2419 int32_t constant_index_value = 0;
2420 if (!constant_index) {
2421 rl_index = LoadValue(rl_index, kCoreReg);
2422 } else {
2423 // If index is constant, just fold it into the data offset
2424 constant_index_value = mir_graph_->ConstantValue(rl_index);
2425 data_offset += constant_index_value << scale;
2426 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002427 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002428 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002429
2430 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002431 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002432
2433 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002434 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002435 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002436 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002437 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002438 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002439 }
buzbee695d13a2014-04-19 13:32:20 -07002440 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002441 rl_src = LoadValueWide(rl_src, reg_class);
2442 } else {
2443 rl_src = LoadValue(rl_src, reg_class);
2444 }
2445 // If the src reg can't be byte accessed, move it to a temp first.
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002446 if ((size == kSignedByte || size == kUnsignedByte) && !IsByteRegister(rl_src.reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08002447 RegStorage temp = AllocTemp();
2448 OpRegCopy(temp, rl_src.reg);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002449 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, temp, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002450 } else {
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002451 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_src.reg, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002452 }
Ian Rogersa9a82542013-10-04 11:17:26 -07002453 if (card_mark) {
Ian Rogers773aab12013-10-14 13:50:10 -07002454 // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
Mark Mendell343adb52013-12-18 06:02:17 -08002455 if (!constant_index) {
buzbee091cc402014-03-31 10:14:40 -07002456 FreeTemp(rl_index.reg);
Mark Mendell343adb52013-12-18 06:02:17 -08002457 }
Vladimir Marko743b98c2014-11-24 19:45:41 +00002458 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002459 }
2460}
2461
Mark Mendell4708dcd2014-01-22 09:05:18 -08002462RegLocation X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002463 RegLocation rl_src, int shift_amount, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002464 UNUSED(flags);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002465 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Elena Sayapinadd644502014-07-01 18:39:52 +07002466 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002467 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
2468 switch (opcode) {
2469 case Instruction::SHL_LONG:
2470 case Instruction::SHL_LONG_2ADDR:
2471 op = kOpLsl;
2472 break;
2473 case Instruction::SHR_LONG:
2474 case Instruction::SHR_LONG_2ADDR:
2475 op = kOpAsr;
2476 break;
2477 case Instruction::USHR_LONG:
2478 case Instruction::USHR_LONG_2ADDR:
2479 op = kOpLsr;
2480 break;
2481 default:
2482 LOG(FATAL) << "Unexpected case";
2483 }
2484 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
2485 } else {
2486 switch (opcode) {
2487 case Instruction::SHL_LONG:
2488 case Instruction::SHL_LONG_2ADDR:
2489 DCHECK_NE(shift_amount, 1); // Prevent a double store from happening.
2490 if (shift_amount == 32) {
2491 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2492 LoadConstant(rl_result.reg.GetLow(), 0);
2493 } else if (shift_amount > 31) {
2494 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2495 NewLIR2(kX86Sal32RI, rl_result.reg.GetHighReg(), shift_amount - 32);
2496 LoadConstant(rl_result.reg.GetLow(), 0);
2497 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002498 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002499 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2500 NewLIR3(kX86Shld32RRI, rl_result.reg.GetHighReg(), rl_result.reg.GetLowReg(),
2501 shift_amount);
2502 NewLIR2(kX86Sal32RI, rl_result.reg.GetLowReg(), shift_amount);
2503 }
2504 break;
2505 case Instruction::SHR_LONG:
2506 case Instruction::SHR_LONG_2ADDR:
2507 if (shift_amount == 32) {
2508 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2509 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2510 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2511 } else if (shift_amount > 31) {
2512 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2513 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2514 NewLIR2(kX86Sar32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2515 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2516 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002517 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002518 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2519 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2520 shift_amount);
2521 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), shift_amount);
2522 }
2523 break;
2524 case Instruction::USHR_LONG:
2525 case Instruction::USHR_LONG_2ADDR:
2526 if (shift_amount == 32) {
2527 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2528 LoadConstant(rl_result.reg.GetHigh(), 0);
2529 } else if (shift_amount > 31) {
2530 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2531 NewLIR2(kX86Shr32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2532 LoadConstant(rl_result.reg.GetHigh(), 0);
2533 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002534 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002535 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2536 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2537 shift_amount);
2538 NewLIR2(kX86Shr32RI, rl_result.reg.GetHighReg(), shift_amount);
2539 }
2540 break;
2541 default:
2542 LOG(FATAL) << "Unexpected case";
2543 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08002544 }
2545 return rl_result;
2546}
2547
Brian Carlstrom7940e442013-07-12 13:46:57 -07002548void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002549 RegLocation rl_src, RegLocation rl_shift, int flags) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002550 // Per spec, we only care about low 6 bits of shift amount.
2551 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
2552 if (shift_amount == 0) {
2553 rl_src = LoadValueWide(rl_src, kCoreReg);
2554 StoreValueWide(rl_dest, rl_src);
2555 return;
2556 } else if (shift_amount == 1 &&
2557 (opcode == Instruction::SHL_LONG || opcode == Instruction::SHL_LONG_2ADDR)) {
2558 // Need to handle this here to avoid calling StoreValueWide twice.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002559 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src, rl_src, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002560 return;
2561 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07002562 if (PartiallyIntersects(rl_src, rl_dest)) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002563 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
2564 return;
2565 }
2566 rl_src = LoadValueWide(rl_src, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002567 RegLocation rl_result = GenShiftImmOpLong(opcode, rl_dest, rl_src, shift_amount, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002568 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002569}
2570
2571void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002572 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
2573 int flags) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002574 bool isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002575 switch (opcode) {
2576 case Instruction::ADD_LONG:
2577 case Instruction::AND_LONG:
2578 case Instruction::OR_LONG:
2579 case Instruction::XOR_LONG:
2580 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002581 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002582 } else {
2583 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002584 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002585 }
2586 break;
2587 case Instruction::SUB_LONG:
2588 case Instruction::SUB_LONG_2ADDR:
2589 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002590 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002591 } else {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002592 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002593 isConstSuccess = true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002594 }
2595 break;
2596 case Instruction::ADD_LONG_2ADDR:
2597 case Instruction::OR_LONG_2ADDR:
2598 case Instruction::XOR_LONG_2ADDR:
2599 case Instruction::AND_LONG_2ADDR:
2600 if (rl_src2.is_const) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04002601 if (GenerateTwoOperandInstructions()) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002602 isConstSuccess = GenLongImm(rl_dest, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002603 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002604 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002605 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002606 } else {
2607 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002608 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002609 }
2610 break;
2611 default:
Chao-ying Fua0147762014-06-06 18:38:49 -07002612 isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002613 break;
2614 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002615
2616 if (!isConstSuccess) {
2617 // Default - bail to non-const handler.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002618 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002619 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002620}
2621
2622bool X86Mir2Lir::IsNoOp(Instruction::Code op, int32_t value) {
2623 switch (op) {
2624 case Instruction::AND_LONG_2ADDR:
2625 case Instruction::AND_LONG:
2626 return value == -1;
2627 case Instruction::OR_LONG:
2628 case Instruction::OR_LONG_2ADDR:
2629 case Instruction::XOR_LONG:
2630 case Instruction::XOR_LONG_2ADDR:
2631 return value == 0;
2632 default:
2633 return false;
2634 }
2635}
2636
2637X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
2638 bool is_high_op) {
2639 bool rhs_in_mem = rhs.location != kLocPhysReg;
2640 bool dest_in_mem = dest.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002641 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002642 DCHECK(!rhs_in_mem || !dest_in_mem);
2643 switch (op) {
2644 case Instruction::ADD_LONG:
2645 case Instruction::ADD_LONG_2ADDR:
2646 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002647 return is64Bit ? kX86Add64MR : is_high_op ? kX86Adc32MR : kX86Add32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002648 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002649 return is64Bit ? kX86Add64RM : is_high_op ? kX86Adc32RM : kX86Add32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002650 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002651 return is64Bit ? kX86Add64RR : is_high_op ? kX86Adc32RR : kX86Add32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002652 case Instruction::SUB_LONG:
2653 case Instruction::SUB_LONG_2ADDR:
2654 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002655 return is64Bit ? kX86Sub64MR : is_high_op ? kX86Sbb32MR : kX86Sub32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002656 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002657 return is64Bit ? kX86Sub64RM : is_high_op ? kX86Sbb32RM : kX86Sub32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002658 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002659 return is64Bit ? kX86Sub64RR : is_high_op ? kX86Sbb32RR : kX86Sub32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002660 case Instruction::AND_LONG_2ADDR:
2661 case Instruction::AND_LONG:
2662 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002663 return is64Bit ? kX86And64MR : kX86And32MR;
2664 }
2665 if (is64Bit) {
2666 return rhs_in_mem ? kX86And64RM : kX86And64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002667 }
2668 return rhs_in_mem ? kX86And32RM : kX86And32RR;
2669 case Instruction::OR_LONG:
2670 case Instruction::OR_LONG_2ADDR:
2671 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002672 return is64Bit ? kX86Or64MR : kX86Or32MR;
2673 }
2674 if (is64Bit) {
2675 return rhs_in_mem ? kX86Or64RM : kX86Or64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002676 }
2677 return rhs_in_mem ? kX86Or32RM : kX86Or32RR;
2678 case Instruction::XOR_LONG:
2679 case Instruction::XOR_LONG_2ADDR:
2680 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002681 return is64Bit ? kX86Xor64MR : kX86Xor32MR;
2682 }
2683 if (is64Bit) {
2684 return rhs_in_mem ? kX86Xor64RM : kX86Xor64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002685 }
2686 return rhs_in_mem ? kX86Xor32RM : kX86Xor32RR;
2687 default:
2688 LOG(FATAL) << "Unexpected opcode: " << op;
2689 return kX86Add32RR;
2690 }
2691}
2692
2693X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op,
2694 int32_t value) {
2695 bool in_mem = loc.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002696 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002697 bool byte_imm = IS_SIMM8(value);
buzbee091cc402014-03-31 10:14:40 -07002698 DCHECK(in_mem || !loc.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002699 switch (op) {
2700 case Instruction::ADD_LONG:
2701 case Instruction::ADD_LONG_2ADDR:
2702 if (byte_imm) {
2703 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002704 return is64Bit ? kX86Add64MI8 : is_high_op ? kX86Adc32MI8 : kX86Add32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002705 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002706 return is64Bit ? kX86Add64RI8 : is_high_op ? kX86Adc32RI8 : kX86Add32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002707 }
2708 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002709 return is64Bit ? kX86Add64MI : is_high_op ? kX86Adc32MI : kX86Add32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002710 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002711 return is64Bit ? kX86Add64RI : is_high_op ? kX86Adc32RI : kX86Add32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002712 case Instruction::SUB_LONG:
2713 case Instruction::SUB_LONG_2ADDR:
2714 if (byte_imm) {
2715 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002716 return is64Bit ? kX86Sub64MI8 : is_high_op ? kX86Sbb32MI8 : kX86Sub32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002717 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002718 return is64Bit ? kX86Sub64RI8 : is_high_op ? kX86Sbb32RI8 : kX86Sub32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002719 }
2720 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002721 return is64Bit ? kX86Sub64MI : is_high_op ? kX86Sbb32MI : kX86Sub32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002722 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002723 return is64Bit ? kX86Sub64RI : is_high_op ? kX86Sbb32RI : kX86Sub32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002724 case Instruction::AND_LONG_2ADDR:
2725 case Instruction::AND_LONG:
2726 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002727 if (is64Bit) {
2728 return in_mem ? kX86And64MI8 : kX86And64RI8;
2729 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002730 return in_mem ? kX86And32MI8 : kX86And32RI8;
2731 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002732 if (is64Bit) {
2733 return in_mem ? kX86And64MI : kX86And64RI;
2734 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002735 return in_mem ? kX86And32MI : kX86And32RI;
2736 case Instruction::OR_LONG:
2737 case Instruction::OR_LONG_2ADDR:
2738 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002739 if (is64Bit) {
2740 return in_mem ? kX86Or64MI8 : kX86Or64RI8;
2741 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002742 return in_mem ? kX86Or32MI8 : kX86Or32RI8;
2743 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002744 if (is64Bit) {
2745 return in_mem ? kX86Or64MI : kX86Or64RI;
2746 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002747 return in_mem ? kX86Or32MI : kX86Or32RI;
2748 case Instruction::XOR_LONG:
2749 case Instruction::XOR_LONG_2ADDR:
2750 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002751 if (is64Bit) {
2752 return in_mem ? kX86Xor64MI8 : kX86Xor64RI8;
2753 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002754 return in_mem ? kX86Xor32MI8 : kX86Xor32RI8;
2755 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002756 if (is64Bit) {
2757 return in_mem ? kX86Xor64MI : kX86Xor64RI;
2758 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002759 return in_mem ? kX86Xor32MI : kX86Xor32RI;
2760 default:
2761 LOG(FATAL) << "Unexpected opcode: " << op;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002762 UNREACHABLE();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002763 }
2764}
2765
Chao-ying Fua0147762014-06-06 18:38:49 -07002766bool X86Mir2Lir::GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002767 DCHECK(rl_src.is_const);
2768 int64_t val = mir_graph_->ConstantValueWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002769
Elena Sayapinadd644502014-07-01 18:39:52 +07002770 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002771 // We can do with imm only if it fits 32 bit
2772 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2773 return false;
2774 }
2775
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002776 rl_dest = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002777
2778 if ((rl_dest.location == kLocDalvikFrame) ||
2779 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002780 int r_base = rs_rX86_SP_32.GetReg();
Chao-ying Fua0147762014-06-06 18:38:49 -07002781 int displacement = SRegOffset(rl_dest.s_reg_low);
2782
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002783 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002784 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
2785 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val);
2786 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2787 true /* is_load */, true /* is64bit */);
2788 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2789 false /* is_load */, true /* is64bit */);
2790 return true;
2791 }
2792
2793 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2794 DCHECK_EQ(rl_result.location, kLocPhysReg);
2795 DCHECK(!rl_result.reg.IsFloat());
2796
2797 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2798 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2799
2800 StoreValueWide(rl_dest, rl_result);
2801 return true;
2802 }
2803
Mark Mendelle02d48f2014-01-15 11:19:23 -08002804 int32_t val_lo = Low32Bits(val);
2805 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002806 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002807
2808 // Can we just do this into memory?
2809 if ((rl_dest.location == kLocDalvikFrame) ||
2810 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002811 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002812 int displacement = SRegOffset(rl_dest.s_reg_low);
2813
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002814 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002815 if (!IsNoOp(op, val_lo)) {
2816 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002817 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002818 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002819 true /* is_load */, true /* is64bit */);
2820 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002821 false /* is_load */, true /* is64bit */);
2822 }
2823 if (!IsNoOp(op, val_hi)) {
2824 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
buzbee2700f7e2014-03-07 09:46:20 -08002825 LIR *lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002826 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002827 true /* is_load */, true /* is64bit */);
2828 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002829 false /* is_load */, true /* is64bit */);
2830 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002831 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002832 }
2833
2834 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2835 DCHECK_EQ(rl_result.location, kLocPhysReg);
buzbee091cc402014-03-31 10:14:40 -07002836 DCHECK(!rl_result.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002837
2838 if (!IsNoOp(op, val_lo)) {
2839 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002840 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002841 }
2842 if (!IsNoOp(op, val_hi)) {
2843 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002844 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002845 }
2846 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002847 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002848}
2849
Chao-ying Fua0147762014-06-06 18:38:49 -07002850bool X86Mir2Lir::GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002851 RegLocation rl_src2, Instruction::Code op) {
2852 DCHECK(rl_src2.is_const);
2853 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Chao-ying Fua0147762014-06-06 18:38:49 -07002854
Elena Sayapinadd644502014-07-01 18:39:52 +07002855 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002856 // We can do with imm only if it fits 32 bit
2857 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2858 return false;
2859 }
2860 if (rl_dest.location == kLocPhysReg &&
2861 rl_src1.location == kLocPhysReg && !rl_dest.reg.IsFloat()) {
2862 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002863 OpRegCopy(rl_dest.reg, rl_src1.reg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002864 NewLIR2(x86op, rl_dest.reg.GetReg(), val);
2865 StoreFinalValueWide(rl_dest, rl_dest);
2866 return true;
2867 }
2868
2869 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2870 // We need the values to be in a temporary
2871 RegLocation rl_result = ForceTempWide(rl_src1);
2872
2873 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2874 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2875
2876 StoreFinalValueWide(rl_dest, rl_result);
2877 return true;
2878 }
2879
Mark Mendelle02d48f2014-01-15 11:19:23 -08002880 int32_t val_lo = Low32Bits(val);
2881 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002882 rl_dest = UpdateLocWideTyped(rl_dest);
2883 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002884
2885 // Can we do this directly into the destination registers?
2886 if (rl_dest.location == kLocPhysReg && rl_src1.location == kLocPhysReg &&
buzbee2700f7e2014-03-07 09:46:20 -08002887 rl_dest.reg.GetLowReg() == rl_src1.reg.GetLowReg() &&
buzbee091cc402014-03-31 10:14:40 -07002888 rl_dest.reg.GetHighReg() == rl_src1.reg.GetHighReg() && !rl_dest.reg.IsFloat()) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002889 if (!IsNoOp(op, val_lo)) {
2890 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002891 NewLIR2(x86op, rl_dest.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002892 }
2893 if (!IsNoOp(op, val_hi)) {
2894 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002895 NewLIR2(x86op, rl_dest.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002896 }
Maxim Kazantsev653f2bf2014-02-13 15:11:17 +07002897
2898 StoreFinalValueWide(rl_dest, rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002899 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002900 }
2901
2902 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2903 DCHECK_EQ(rl_src1.location, kLocPhysReg);
2904
2905 // We need the values to be in a temporary
2906 RegLocation rl_result = ForceTempWide(rl_src1);
2907 if (!IsNoOp(op, val_lo)) {
2908 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002909 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002910 }
2911 if (!IsNoOp(op, val_hi)) {
2912 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002913 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002914 }
2915
2916 StoreFinalValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002917 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002918}
2919
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002920// For final classes there are no sub-classes to check and so we can answer the instance-of
2921// question with simple comparisons. Use compares to memory and SETEQ to optimize for x86.
2922void X86Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx,
2923 RegLocation rl_dest, RegLocation rl_src) {
buzbeea0cd2d72014-06-01 09:33:49 -07002924 RegLocation object = LoadValue(rl_src, kRefReg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002925 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002926 RegStorage result_reg = rl_result.reg;
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002927
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002928 // For 32-bit, SETcc only works with EAX..EDX.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002929 RegStorage object_32reg = object.reg.Is64Bit() ? As32BitReg(object.reg) : object.reg;
Dmitry Petrochenko407f5c12014-07-01 01:21:38 +07002930 if (result_reg.GetRegNum() == object_32reg.GetRegNum() || !IsByteRegister(result_reg)) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04002931 result_reg = AllocateByteRegister();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002932 }
2933
2934 // Assume that there is no match.
2935 LoadConstant(result_reg, 0);
buzbee2700f7e2014-03-07 09:46:20 -08002936 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002937
Mark Mendellade54a22014-06-09 12:49:55 -04002938 // We will use this register to compare to memory below.
2939 // References are 32 bit in memory, and 64 bit in registers (in 64 bit mode).
2940 // For this reason, force allocation of a 32 bit register to use, so that the
2941 // compare to memory will be done using a 32 bit comparision.
2942 // The LoadRefDisp(s) below will work normally, even in 64 bit mode.
2943 RegStorage check_class = AllocTemp();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002944
2945 // If Method* is already in a register, we can save a copy.
2946 RegLocation rl_method = mir_graph_->GetMethodLoc();
Andreas Gampeccc60262014-07-04 18:02:38 -07002947 int32_t offset_of_type = mirror::Array::DataOffset(
2948 sizeof(mirror::HeapReference<mirror::Class*>)).Int32Value() +
2949 (sizeof(mirror::HeapReference<mirror::Class*>) * type_idx);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002950
2951 if (rl_method.location == kLocPhysReg) {
2952 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07002953 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002954 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002955 } else {
buzbee695d13a2014-04-19 13:32:20 -07002956 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002957 check_class, kNotVolatile);
2958 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002959 }
2960 } else {
2961 LoadCurrMethodDirect(check_class);
2962 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07002963 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002964 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002965 } else {
buzbee695d13a2014-04-19 13:32:20 -07002966 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002967 check_class, kNotVolatile);
2968 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002969 }
2970 }
2971
2972 // Compare the computed class to the class in the object.
2973 DCHECK_EQ(object.location, kLocPhysReg);
buzbee2700f7e2014-03-07 09:46:20 -08002974 OpRegMem(kOpCmp, check_class, object.reg, mirror::Object::ClassOffset().Int32Value());
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002975
2976 // Set the low byte of the result to 0 or 1 from the compare condition code.
buzbee2700f7e2014-03-07 09:46:20 -08002977 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondEq);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002978
2979 LIR* target = NewLIR0(kPseudoTargetLabel);
2980 null_branchover->target = target;
2981 FreeTemp(check_class);
2982 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08002983 OpRegCopy(rl_result.reg, result_reg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002984 FreeTemp(result_reg);
2985 }
2986 StoreValue(rl_dest, rl_result);
2987}
2988
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002989void X86Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002990 RegLocation rl_lhs, RegLocation rl_rhs, int flags) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002991 OpKind op = kOpBkpt;
2992 bool is_div_rem = false;
2993 bool unary = false;
2994 bool shift_op = false;
2995 bool is_two_addr = false;
2996 RegLocation rl_result;
2997 switch (opcode) {
2998 case Instruction::NEG_INT:
2999 op = kOpNeg;
3000 unary = true;
3001 break;
3002 case Instruction::NOT_INT:
3003 op = kOpMvn;
3004 unary = true;
3005 break;
3006 case Instruction::ADD_INT_2ADDR:
3007 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003008 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003009 case Instruction::ADD_INT:
3010 op = kOpAdd;
3011 break;
3012 case Instruction::SUB_INT_2ADDR:
3013 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003014 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003015 case Instruction::SUB_INT:
3016 op = kOpSub;
3017 break;
3018 case Instruction::MUL_INT_2ADDR:
3019 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003020 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003021 case Instruction::MUL_INT:
3022 op = kOpMul;
3023 break;
3024 case Instruction::DIV_INT_2ADDR:
3025 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003026 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003027 case Instruction::DIV_INT:
3028 op = kOpDiv;
3029 is_div_rem = true;
3030 break;
3031 /* NOTE: returns in kArg1 */
3032 case Instruction::REM_INT_2ADDR:
3033 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003034 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003035 case Instruction::REM_INT:
3036 op = kOpRem;
3037 is_div_rem = true;
3038 break;
3039 case Instruction::AND_INT_2ADDR:
3040 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003041 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003042 case Instruction::AND_INT:
3043 op = kOpAnd;
3044 break;
3045 case Instruction::OR_INT_2ADDR:
3046 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003047 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003048 case Instruction::OR_INT:
3049 op = kOpOr;
3050 break;
3051 case Instruction::XOR_INT_2ADDR:
3052 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003053 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003054 case Instruction::XOR_INT:
3055 op = kOpXor;
3056 break;
3057 case Instruction::SHL_INT_2ADDR:
3058 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003059 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003060 case Instruction::SHL_INT:
3061 shift_op = true;
3062 op = kOpLsl;
3063 break;
3064 case Instruction::SHR_INT_2ADDR:
3065 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003066 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003067 case Instruction::SHR_INT:
3068 shift_op = true;
3069 op = kOpAsr;
3070 break;
3071 case Instruction::USHR_INT_2ADDR:
3072 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003073 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003074 case Instruction::USHR_INT:
3075 shift_op = true;
3076 op = kOpLsr;
3077 break;
3078 default:
3079 LOG(FATAL) << "Invalid word arith op: " << opcode;
3080 }
3081
Mark Mendelle87f9b52014-04-30 14:13:18 -04003082 // Can we convert to a two address instruction?
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003083 if (!is_two_addr &&
3084 (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3085 mir_graph_->SRegToVReg(rl_lhs.s_reg_low))) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04003086 is_two_addr = true;
3087 }
3088
3089 if (!GenerateTwoOperandInstructions()) {
3090 is_two_addr = false;
3091 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003092
3093 // Get the div/rem stuff out of the way.
3094 if (is_div_rem) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07003095 rl_result = GenDivRem(rl_dest, rl_lhs, rl_rhs, op == kOpDiv, flags);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003096 StoreValue(rl_dest, rl_result);
3097 return;
3098 }
3099
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003100 // If we generate any memory access below, it will reference a dalvik reg.
3101 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
3102
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003103 if (unary) {
3104 rl_lhs = LoadValue(rl_lhs, kCoreReg);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003105 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003106 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003107 OpRegReg(op, rl_result.reg, rl_lhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003108 } else {
3109 if (shift_op) {
3110 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003111 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003112 LoadValueDirectFixed(rl_rhs, t_reg);
3113 if (is_two_addr) {
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) {
3117 // Okay, we can do this into memory
buzbee2700f7e2014-03-07 09:46:20 -08003118 OpMemReg(op, rl_result, t_reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003119 FreeTemp(t_reg);
3120 return;
buzbee091cc402014-03-31 10:14:40 -07003121 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003122 // Can do this directly into the result register
buzbee2700f7e2014-03-07 09:46:20 -08003123 OpRegReg(op, rl_result.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003124 FreeTemp(t_reg);
3125 StoreFinalValue(rl_dest, rl_result);
3126 return;
3127 }
3128 }
3129 // Three address form, or we can't do directly.
3130 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3131 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003132 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003133 FreeTemp(t_reg);
3134 } else {
3135 // Multiply is 3 operand only (sort of).
3136 if (is_two_addr && op != kOpMul) {
3137 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003138 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003139 if (rl_result.location == kLocPhysReg) {
Serguei Katkov366f8ae2014-04-15 16:55:26 +07003140 // Ensure res is in a core reg
3141 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003142 // Can we do this from memory directly?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003143 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003144 if (rl_rhs.location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08003145 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003146 StoreFinalValue(rl_dest, rl_result);
3147 return;
buzbee091cc402014-03-31 10:14:40 -07003148 } else if (!rl_rhs.reg.IsFloat()) {
buzbee2700f7e2014-03-07 09:46:20 -08003149 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003150 StoreFinalValue(rl_dest, rl_result);
3151 return;
3152 }
3153 }
3154 rl_rhs = LoadValue(rl_rhs, kCoreReg);
Serguei Katkovd293fb42014-05-19 15:45:42 +07003155 // It might happen rl_rhs and rl_dest are the same VR
3156 // in this case rl_dest is in reg after LoadValue while
3157 // rl_result is not updated yet, so do this
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003158 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003159 if (rl_result.location != kLocPhysReg) {
3160 // Okay, we can do this into memory.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00003161 OpMemReg(op, rl_result, rl_rhs.reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003162 return;
buzbee091cc402014-03-31 10:14:40 -07003163 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003164 // Can do this directly into the result register.
buzbee2700f7e2014-03-07 09:46:20 -08003165 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003166 StoreFinalValue(rl_dest, rl_result);
3167 return;
3168 } else {
3169 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3170 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003171 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003172 }
3173 } else {
3174 // Try to use reg/memory instructions.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003175 rl_lhs = UpdateLocTyped(rl_lhs);
3176 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003177 // We can't optimize with FP registers.
3178 if (!IsOperationSafeWithoutTemps(rl_lhs, rl_rhs)) {
3179 // Something is difficult, so fall back to the standard case.
3180 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3181 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3182 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003183 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003184 } else {
3185 // We can optimize by moving to result and using memory operands.
3186 if (rl_rhs.location != kLocPhysReg) {
3187 // Force LHS into result.
Serguei Katkov66da1362014-03-14 13:33:33 +07003188 // We should be careful with order here
3189 // If rl_dest and rl_lhs points to the same VR we should load first
3190 // If the are different we should find a register first for dest
Chao-ying Fua0147762014-06-06 18:38:49 -07003191 if (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3192 mir_graph_->SRegToVReg(rl_lhs.s_reg_low)) {
Serguei Katkov66da1362014-03-14 13:33:33 +07003193 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3194 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendelle87f9b52014-04-30 14:13:18 -04003195 // No-op if these are the same.
3196 OpRegCopy(rl_result.reg, rl_lhs.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003197 } else {
3198 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003199 LoadValueDirect(rl_lhs, rl_result.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003200 }
buzbee2700f7e2014-03-07 09:46:20 -08003201 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003202 } else if (rl_lhs.location != kLocPhysReg) {
3203 // RHS is in a register; LHS is in memory.
3204 if (op != kOpSub) {
3205 // Force RHS into result and operate on memory.
3206 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003207 OpRegCopy(rl_result.reg, rl_rhs.reg);
3208 OpRegMem(op, rl_result.reg, rl_lhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003209 } else {
3210 // Subtraction isn't commutative.
3211 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3212 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3213 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003214 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003215 }
3216 } else {
3217 // Both are in registers.
3218 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3219 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3220 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003221 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003222 }
3223 }
3224 }
3225 }
3226 }
3227 StoreValue(rl_dest, rl_result);
3228}
3229
3230bool X86Mir2Lir::IsOperationSafeWithoutTemps(RegLocation rl_lhs, RegLocation rl_rhs) {
3231 // If we have non-core registers, then we can't do good things.
buzbee091cc402014-03-31 10:14:40 -07003232 if (rl_lhs.location == kLocPhysReg && rl_lhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003233 return false;
3234 }
buzbee091cc402014-03-31 10:14:40 -07003235 if (rl_rhs.location == kLocPhysReg && rl_rhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003236 return false;
3237 }
3238
3239 // Everything will be fine :-).
3240 return true;
3241}
Chao-ying Fua0147762014-06-06 18:38:49 -07003242
3243void X86Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003244 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07003245 Mir2Lir::GenIntToLong(rl_dest, rl_src);
3246 return;
3247 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003248 rl_src = UpdateLocTyped(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07003249 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
3250 if (rl_src.location == kLocPhysReg) {
3251 NewLIR2(kX86MovsxdRR, rl_result.reg.GetReg(), rl_src.reg.GetReg());
3252 } else {
3253 int displacement = SRegOffset(rl_src.s_reg_low);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003254 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08003255 LIR *m = NewLIR3(kX86MovsxdRM, rl_result.reg.GetReg(), rs_rX86_SP_32.GetReg(),
Chao-ying Fua0147762014-06-06 18:38:49 -07003256 displacement + LOWORD_OFFSET);
3257 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
3258 true /* is_load */, true /* is_64bit */);
3259 }
3260 StoreValueWide(rl_dest, rl_result);
3261}
3262
Yevgeny Rouban6af82062014-11-26 18:11:54 +06003263void X86Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
3264 rl_src = UpdateLocWide(rl_src);
3265 rl_src = NarrowRegLoc(rl_src);
3266 StoreValue(rl_dest, rl_src);
3267
3268 if (cu_->target64) {
3269 // if src and dest are in the same phys reg then StoreValue generates
3270 // no operation but we need explicit 32-bit mov R, R to clear
3271 // the higher 32-bits
3272 rl_dest = UpdateLoc(rl_dest);
3273 if (rl_src.location == kLocPhysReg && rl_dest.location == kLocPhysReg
3274 && IsSameReg(rl_src.reg, rl_dest.reg)) {
3275 LIR* copy_lir = OpRegCopyNoInsert(rl_dest.reg, rl_dest.reg);
3276 // remove nop flag set by OpRegCopyNoInsert if src == dest
3277 copy_lir->flags.is_nop = false;
3278 AppendLIR(copy_lir);
3279 }
3280 }
3281}
3282
Chao-ying Fua0147762014-06-06 18:38:49 -07003283void X86Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
3284 RegLocation rl_src1, RegLocation rl_shift) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003285 if (!cu_->target64) {
Yixin Shouf40f8902014-08-14 14:10:32 -04003286 // Long shift operations in 32-bit. Use shld or shrd to create a 32-bit register filled from
3287 // the other half, shift the other half, if the shift amount is less than 32 we're done,
3288 // otherwise move one register to the other and place zero or sign bits in the other.
3289 LIR* branch;
3290 FlushAllRegs();
3291 LockCallTemps();
3292 LoadValueDirectFixed(rl_shift, rs_rCX);
3293 RegStorage r_tmp = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
3294 LoadValueDirectWideFixed(rl_src1, r_tmp);
3295 switch (opcode) {
3296 case Instruction::SHL_LONG:
3297 case Instruction::SHL_LONG_2ADDR:
3298 NewLIR3(kX86Shld32RRC, r_tmp.GetHighReg(), r_tmp.GetLowReg(), rs_rCX.GetReg());
3299 NewLIR2(kX86Sal32RC, r_tmp.GetLowReg(), rs_rCX.GetReg());
3300 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3301 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3302 OpRegCopy(r_tmp.GetHigh(), r_tmp.GetLow());
3303 LoadConstant(r_tmp.GetLow(), 0);
3304 branch->target = NewLIR0(kPseudoTargetLabel);
3305 break;
3306 case Instruction::SHR_LONG:
3307 case Instruction::SHR_LONG_2ADDR:
3308 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(), rs_rCX.GetReg());
3309 NewLIR2(kX86Sar32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3310 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3311 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3312 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3313 NewLIR2(kX86Sar32RI, r_tmp.GetHighReg(), 31);
3314 branch->target = NewLIR0(kPseudoTargetLabel);
3315 break;
3316 case Instruction::USHR_LONG:
3317 case Instruction::USHR_LONG_2ADDR:
3318 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(),
3319 rs_rCX.GetReg());
3320 NewLIR2(kX86Shr32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3321 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3322 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3323 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3324 LoadConstant(r_tmp.GetHigh(), 0);
3325 branch->target = NewLIR0(kPseudoTargetLabel);
3326 break;
3327 default:
3328 LOG(FATAL) << "Unexpected case: " << opcode;
3329 return;
3330 }
3331 RegLocation rl_result = LocCReturnWide();
3332 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07003333 return;
3334 }
3335
3336 bool is_two_addr = false;
3337 OpKind op = kOpBkpt;
3338 RegLocation rl_result;
3339
3340 switch (opcode) {
3341 case Instruction::SHL_LONG_2ADDR:
3342 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003343 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003344 case Instruction::SHL_LONG:
3345 op = kOpLsl;
3346 break;
3347 case Instruction::SHR_LONG_2ADDR:
3348 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003349 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003350 case Instruction::SHR_LONG:
3351 op = kOpAsr;
3352 break;
3353 case Instruction::USHR_LONG_2ADDR:
3354 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003355 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003356 case Instruction::USHR_LONG:
3357 op = kOpLsr;
3358 break;
3359 default:
3360 op = kOpBkpt;
3361 }
3362
3363 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003364 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Chao-ying Fua0147762014-06-06 18:38:49 -07003365 LoadValueDirectFixed(rl_shift, t_reg);
3366 if (is_two_addr) {
3367 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003368 rl_result = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07003369 if (rl_result.location != kLocPhysReg) {
3370 // Okay, we can do this into memory
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003371 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07003372 OpMemReg(op, rl_result, t_reg.GetReg());
3373 } else if (!rl_result.reg.IsFloat()) {
3374 // Can do this directly into the result register
3375 OpRegReg(op, rl_result.reg, t_reg);
3376 StoreFinalValueWide(rl_dest, rl_result);
3377 }
3378 } else {
3379 // Three address form, or we can't do directly.
3380 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
3381 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
3382 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
3383 StoreFinalValueWide(rl_dest, rl_result);
3384 }
3385
3386 FreeTemp(t_reg);
3387}
3388
Brian Carlstrom7940e442013-07-12 13:46:57 -07003389} // namespace art