blob: 4fe7a43a854b2af929cb5f2409af27e558cace96 [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);
866 int res_vreg, src1_vreg, src2_vreg;
867
Mark Mendella65c1db2014-10-21 17:44:32 -0400868 if (rl_dest.s_reg_low == INVALID_SREG) {
869 // Result is unused, the code is dead. Inlining successful, no code generated.
870 return true;
871 }
872
nikolay serdjuk4ab6f502014-08-08 09:55:06 +0700873 /*
874 * If the result register is the same as the second element, then we
875 * need to be careful. The reason is that the first copy will
876 * inadvertently clobber the second element with the first one thus
877 * yielding the wrong result. Thus we do a swap in that case.
878 */
879 res_vreg = mir_graph_->SRegToVReg(rl_dest.s_reg_low);
880 src2_vreg = mir_graph_->SRegToVReg(rl_src2.s_reg_low);
881 if (res_vreg == src2_vreg) {
882 std::swap(rl_src1, rl_src2);
883 }
884
885 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
886 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
887
888 // Pick the first integer as min/max.
889 OpRegCopyWide(rl_result.reg, rl_src1.reg);
890
891 /*
892 * If the integers are both in the same register, then there is
893 * nothing else to do because they are equal and we have already
894 * moved one into the result.
895 */
896 src1_vreg = mir_graph_->SRegToVReg(rl_src1.s_reg_low);
897 src2_vreg = mir_graph_->SRegToVReg(rl_src2.s_reg_low);
898 if (src1_vreg == src2_vreg) {
899 StoreValueWide(rl_dest, rl_result);
900 return true;
901 }
902
903 // Free registers to make some room for the second operand.
904 // But don't try to free ourselves or promoted registers.
905 if (res_vreg != src1_vreg &&
906 IsTemp(rl_src1.reg.GetLow()) && IsTemp(rl_src1.reg.GetHigh())) {
907 FreeTemp(rl_src1.reg);
908 }
909 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
910
911 // Do we have a free register for intermediate calculations?
912 RegStorage tmp = AllocTemp(false);
913 if (tmp == RegStorage::InvalidReg()) {
914 /*
915 * No, will use 'edi'.
916 *
917 * As mentioned above we have 4 temporary and 2 promotable
918 * caller-save registers. Therefore, we assume that a free
919 * register can be allocated only if 'esi' and 'edi' are
920 * already used as operands. If number of promotable registers
921 * increases from 2 to 4 then our assumption fails and operand
922 * data is corrupted.
923 * Let's DCHECK it.
924 */
925 DCHECK(IsTemp(rl_src2.reg.GetLow()) &&
926 IsTemp(rl_src2.reg.GetHigh()) &&
927 IsTemp(rl_result.reg.GetLow()) &&
928 IsTemp(rl_result.reg.GetHigh()));
929 tmp = rs_rDI;
930 NewLIR1(kX86Push32R, tmp.GetReg());
931 }
932
933 // Now we are ready to do calculations.
934 OpRegReg(kOpMov, tmp, rl_result.reg.GetLow());
935 OpRegReg(kOpSub, tmp, rl_src2.reg.GetLow());
936 OpRegReg(kOpMov, tmp, rl_result.reg.GetHigh());
937 OpRegReg(kOpSbc, tmp, rl_src2.reg.GetHigh());
938
939 // Let's put pop 'edi' here to break a bit the dependency chain.
940 if (tmp == rs_rDI) {
941 NewLIR1(kX86Pop32R, tmp.GetReg());
942 }
943
944 // Conditionally move the other integer into the destination register.
945 ConditionCode cc = is_min ? kCondGe : kCondLt;
946 OpCondRegReg(kOpCmov, cc, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
947 OpCondRegReg(kOpCmov, cc, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
948 StoreValueWide(rl_dest, rl_result);
949 return true;
Serban Constantinescu23abec92014-07-02 16:13:38 +0100950 }
951
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800952 // Get the two arguments to the invoke and place them in GP registers.
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800953 RegLocation rl_dest = (is_long) ? InlineTargetWide(info) : InlineTarget(info);
954 if (rl_dest.s_reg_low == INVALID_SREG) {
955 // Result is unused, the code is dead. Inlining successful, no code generated.
956 return true;
957 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 RegLocation rl_src1 = info->args[0];
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700959 RegLocation rl_src2 = (is_long) ? info->args[2] : info->args[1];
960 rl_src1 = (is_long) ? LoadValueWide(rl_src1, kCoreReg) : LoadValue(rl_src1, kCoreReg);
961 rl_src2 = (is_long) ? LoadValueWide(rl_src2, kCoreReg) : LoadValue(rl_src2, kCoreReg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800962
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800964
965 /*
966 * If the result register is the same as the second element, then we need to be careful.
967 * The reason is that the first copy will inadvertently clobber the second element with
968 * the first one thus yielding the wrong result. Thus we do a swap in that case.
969 */
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000970 if (rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800971 std::swap(rl_src1, rl_src2);
972 }
973
974 // Pick the first integer as min/max.
buzbee2700f7e2014-03-07 09:46:20 -0800975 OpRegCopy(rl_result.reg, rl_src1.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800976
977 // If the integers are both in the same register, then there is nothing else to do
978 // because they are equal and we have already moved one into the result.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000979 if (rl_src1.reg.GetReg() != rl_src2.reg.GetReg()) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800980 // It is possible we didn't pick correctly so do the actual comparison now.
buzbee2700f7e2014-03-07 09:46:20 -0800981 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800982
983 // Conditionally move the other integer into the destination register.
984 ConditionCode condition_code = is_min ? kCondGt : kCondLt;
buzbee2700f7e2014-03-07 09:46:20 -0800985 OpCondRegReg(kOpCmov, condition_code, rl_result.reg, rl_src2.reg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800986 }
987
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700988 if (is_long) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000989 StoreValueWide(rl_dest, rl_result);
990 } else {
Vladimir Markoe508a202013-11-04 15:24:22 +0000991 StoreValue(rl_dest, rl_result);
992 }
993 return true;
994}
995
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700996bool X86Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800997 RegLocation rl_dest = size == k64 ? InlineTargetWide(info) : InlineTarget(info);
998 if (rl_dest.s_reg_low == INVALID_SREG) {
999 // Result is unused, the code is dead. Inlining successful, no code generated.
1000 return true;
1001 }
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001002 RegLocation rl_src_address = info->args[0]; // long address
1003 RegLocation rl_address;
1004 if (!cu_->target64) {
1005 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[0]
1006 rl_address = LoadValue(rl_src_address, kCoreReg);
1007 } else {
1008 rl_address = LoadValueWide(rl_src_address, kCoreReg);
1009 }
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001010 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1011 // Unaligned access is allowed on x86.
1012 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
1013 if (size == k64) {
1014 StoreValueWide(rl_dest, rl_result);
1015 } else {
1016 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
1017 StoreValue(rl_dest, rl_result);
1018 }
1019 return true;
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001020}
1021
Vladimir Markoe508a202013-11-04 15:24:22 +00001022bool X86Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
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 }
1031 RegLocation rl_src_value = info->args[2]; // [size] value
1032 RegLocation rl_value;
1033 if (size == k64) {
1034 // Unaligned access is allowed on x86.
1035 rl_value = LoadValueWide(rl_src_value, kCoreReg);
1036 } else {
1037 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
1038 // In 32-bit mode the only EAX..EDX registers can be used with Mov8MR.
1039 if (!cu_->target64 && size == kSignedByte) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001040 rl_src_value = UpdateLocTyped(rl_src_value);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001041 if (rl_src_value.location == kLocPhysReg && !IsByteRegister(rl_src_value.reg)) {
1042 RegStorage temp = AllocateByteRegister();
1043 OpRegCopy(temp, rl_src_value.reg);
1044 rl_value.reg = temp;
1045 } else {
1046 rl_value = LoadValue(rl_src_value, kCoreReg);
1047 }
1048 } else {
1049 rl_value = LoadValue(rl_src_value, kCoreReg);
1050 }
1051 }
1052 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
1053 return true;
Vladimir Markoe508a202013-11-04 15:24:22 +00001054}
1055
buzbee2700f7e2014-03-07 09:46:20 -08001056void X86Mir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
1057 NewLIR5(kX86Lea32RA, r_base.GetReg(), reg1.GetReg(), reg2.GetReg(), scale, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058}
1059
Ian Rogersdd7624d2014-03-14 17:43:00 -07001060void X86Mir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001061 DCHECK_EQ(kX86, cu_->instruction_set);
1062 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
1063}
1064
1065void X86Mir2Lir::OpTlsCmp(ThreadOffset<8> offset, int val) {
1066 DCHECK_EQ(kX86_64, cu_->instruction_set);
Ian Rogers468532e2013-08-05 10:56:33 -07001067 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068}
1069
buzbee2700f7e2014-03-07 09:46:20 -08001070static bool IsInReg(X86Mir2Lir *pMir2Lir, const RegLocation &rl, RegStorage reg) {
1071 return rl.reg.Valid() && rl.reg.GetReg() == reg.GetReg() && (pMir2Lir->IsLive(reg) || rl.home);
Yevgeny Rouband3a2dfa2014-03-18 15:55:16 +07001072}
1073
Vladimir Marko1c282e22013-11-21 14:49:47 +00001074bool X86Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001075 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001076 // Unused - RegLocation rl_src_unsafe = info->args[0];
1077 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
1078 RegLocation rl_src_offset = info->args[2]; // long low
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001079 if (!cu_->target64) {
1080 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
1081 }
Vladimir Markoc29bb612013-11-27 16:47:25 +00001082 RegLocation rl_src_expected = info->args[4]; // int, long or Object
1083 // If is_long, high half is in info->args[5]
1084 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
1085 // If is_long, high half is in info->args[7]
1086
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001087 if (is_long && cu_->target64) {
1088 // 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 -07001089 FlushReg(rs_r0q);
1090 Clobber(rs_r0q);
1091 LockTemp(rs_r0q);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001092
1093 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
1094 RegLocation rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001095 RegLocation rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
1096 LoadValueDirectWide(rl_src_expected, rs_r0q);
Andreas Gampeccc60262014-07-04 18:02:38 -07001097 NewLIR5(kX86LockCmpxchg64AR, rl_object.reg.GetReg(), rl_offset.reg.GetReg(), 0, 0,
1098 rl_new_value.reg.GetReg());
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001099
1100 // After a store we need to insert barrier in case of potential load. Since the
1101 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated.
Hans Boehm48f5c472014-06-27 14:50:10 -07001102 GenMemBarrier(kAnyAny);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001103
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001104 FreeTemp(rs_r0q);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001105 } else if (is_long) {
Yevgeny Rouband3a2dfa2014-03-18 15:55:16 +07001106 // TODO: avoid unnecessary loads of SI and DI when the values are in registers.
1107 // TODO: CFI support.
Vladimir Marko70b797d2013-12-03 15:25:24 +00001108 FlushAllRegs();
1109 LockCallTemps();
buzbee091cc402014-03-31 10:14:40 -07001110 RegStorage r_tmp1 = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
1111 RegStorage r_tmp2 = RegStorage::MakeRegPair(rs_rBX, rs_rCX);
buzbee2700f7e2014-03-07 09:46:20 -08001112 LoadValueDirectWideFixed(rl_src_expected, r_tmp1);
1113 LoadValueDirectWideFixed(rl_src_new_value, r_tmp2);
buzbee695d13a2014-04-19 13:32:20 -07001114 // FIXME: needs 64-bit update.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001115 const bool obj_in_di = IsInReg(this, rl_src_obj, rs_rDI);
1116 const bool obj_in_si = IsInReg(this, rl_src_obj, rs_rSI);
1117 DCHECK(!obj_in_si || !obj_in_di);
1118 const bool off_in_di = IsInReg(this, rl_src_offset, rs_rDI);
1119 const bool off_in_si = IsInReg(this, rl_src_offset, rs_rSI);
1120 DCHECK(!off_in_si || !off_in_di);
1121 // If obj/offset is in a reg, use that reg. Otherwise, use the empty reg.
1122 RegStorage rs_obj = obj_in_di ? rs_rDI : obj_in_si ? rs_rSI : !off_in_di ? rs_rDI : rs_rSI;
1123 RegStorage rs_off = off_in_si ? rs_rSI : off_in_di ? rs_rDI : !obj_in_si ? rs_rSI : rs_rDI;
1124 bool push_di = (!obj_in_di && !off_in_di) && (rs_obj == rs_rDI || rs_off == rs_rDI);
1125 bool push_si = (!obj_in_si && !off_in_si) && (rs_obj == rs_rSI || rs_off == rs_rSI);
1126 if (push_di) {
1127 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1128 MarkTemp(rs_rDI);
1129 LockTemp(rs_rDI);
1130 }
1131 if (push_si) {
1132 NewLIR1(kX86Push32R, rs_rSI.GetReg());
1133 MarkTemp(rs_rSI);
1134 LockTemp(rs_rSI);
1135 }
1136 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1137 const size_t push_offset = (push_si ? 4u : 0u) + (push_di ? 4u : 0u);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001138 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001139 if (!obj_in_si && !obj_in_di) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001140 LoadWordDisp(rs_rSP, SRegOffset(rl_src_obj.s_reg_low) + push_offset, rs_obj);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001141 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1142 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1143 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - push_offset / 4u;
1144 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
1145 }
1146 if (!off_in_si && !off_in_di) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001147 LoadWordDisp(rs_rSP, SRegOffset(rl_src_offset.s_reg_low) + push_offset, rs_off);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001148 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1149 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1150 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - push_offset / 4u;
1151 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
1152 }
1153 NewLIR4(kX86LockCmpxchg64A, rs_obj.GetReg(), rs_off.GetReg(), 0, 0);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001154
Hans Boehm48f5c472014-06-27 14:50:10 -07001155 // After a store we need to insert barrier to prevent reordering with either
1156 // earlier or later memory accesses. Since
1157 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated,
1158 // and it will be associated with the cmpxchg instruction, preventing both.
1159 GenMemBarrier(kAnyAny);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001160
1161 if (push_si) {
1162 FreeTemp(rs_rSI);
1163 UnmarkTemp(rs_rSI);
1164 NewLIR1(kX86Pop32R, rs_rSI.GetReg());
1165 }
1166 if (push_di) {
1167 FreeTemp(rs_rDI);
1168 UnmarkTemp(rs_rDI);
1169 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
1170 }
Vladimir Marko70b797d2013-12-03 15:25:24 +00001171 FreeCallTemps();
Vladimir Markoc29bb612013-11-27 16:47:25 +00001172 } else {
1173 // EAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in EAX.
buzbee2700f7e2014-03-07 09:46:20 -08001174 FlushReg(rs_r0);
buzbee091cc402014-03-31 10:14:40 -07001175 Clobber(rs_r0);
buzbee2700f7e2014-03-07 09:46:20 -08001176 LockTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001177
buzbeea0cd2d72014-06-01 09:33:49 -07001178 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
buzbee7c02e912014-10-03 13:14:17 -07001179 RegLocation rl_new_value = LoadValue(rl_src_new_value, LocToRegClass(rl_src_new_value));
Vladimir Markoc29bb612013-11-27 16:47:25 +00001180
1181 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
1182 // Mark card for object assuming new value is stored.
buzbee091cc402014-03-31 10:14:40 -07001183 FreeTemp(rs_r0); // Temporarily release EAX for MarkGCCard().
Vladimir Marko743b98c2014-11-24 19:45:41 +00001184 MarkGCCard(0, rl_new_value.reg, rl_object.reg);
buzbee091cc402014-03-31 10:14:40 -07001185 LockTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001186 }
1187
Chao-ying Fu021b60f2014-07-09 11:32:31 -07001188 RegLocation rl_offset;
1189 if (cu_->target64) {
1190 rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
1191 } else {
1192 rl_offset = LoadValue(rl_src_offset, kCoreReg);
1193 }
buzbee2700f7e2014-03-07 09:46:20 -08001194 LoadValueDirect(rl_src_expected, rs_r0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001195 NewLIR5(kX86LockCmpxchgAR, rl_object.reg.GetReg(), rl_offset.reg.GetReg(), 0, 0,
1196 rl_new_value.reg.GetReg());
Vladimir Markoc29bb612013-11-27 16:47:25 +00001197
Hans Boehm48f5c472014-06-27 14:50:10 -07001198 // After a store we need to insert barrier to prevent reordering with either
1199 // earlier or later memory accesses. Since
1200 // locked cmpxchg has full barrier semantics, only a scheduling barrier will be generated,
1201 // and it will be associated with the cmpxchg instruction, preventing both.
1202 GenMemBarrier(kAnyAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001203
buzbee091cc402014-03-31 10:14:40 -07001204 FreeTemp(rs_r0);
Vladimir Markoc29bb612013-11-27 16:47:25 +00001205 }
1206
1207 // Convert ZF to boolean
1208 RegLocation rl_dest = InlineTarget(info); // boolean place for result
1209 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001210 RegStorage result_reg = rl_result.reg;
1211
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001212 // For 32-bit, SETcc only works with EAX..EDX.
1213 if (!IsByteRegister(result_reg)) {
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001214 result_reg = AllocateByteRegister();
Ian Rogers0f9b9c52014-06-09 01:32:12 -07001215 }
1216 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondZ);
1217 NewLIR2(kX86Movzx8RR, rl_result.reg.GetReg(), result_reg.GetReg());
1218 if (IsTemp(result_reg)) {
1219 FreeTemp(result_reg);
1220 }
Vladimir Markoc29bb612013-11-27 16:47:25 +00001221 StoreValue(rl_dest, rl_result);
1222 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223}
1224
Yixin Shou8c914c02014-07-28 14:17:09 -04001225void X86Mir2Lir::SwapBits(RegStorage result_reg, int shift, int32_t value) {
1226 RegStorage r_temp = AllocTemp();
1227 OpRegCopy(r_temp, result_reg);
1228 OpRegImm(kOpLsr, result_reg, shift);
1229 OpRegImm(kOpAnd, r_temp, value);
1230 OpRegImm(kOpAnd, result_reg, value);
1231 OpRegImm(kOpLsl, r_temp, shift);
1232 OpRegReg(kOpOr, result_reg, r_temp);
1233 FreeTemp(r_temp);
1234}
1235
1236void X86Mir2Lir::SwapBits64(RegStorage result_reg, int shift, int64_t value) {
1237 RegStorage r_temp = AllocTempWide();
1238 OpRegCopy(r_temp, result_reg);
1239 OpRegImm(kOpLsr, result_reg, shift);
1240 RegStorage r_value = AllocTempWide();
1241 LoadConstantWide(r_value, value);
1242 OpRegReg(kOpAnd, r_temp, r_value);
1243 OpRegReg(kOpAnd, result_reg, r_value);
1244 OpRegImm(kOpLsl, r_temp, shift);
1245 OpRegReg(kOpOr, result_reg, r_temp);
1246 FreeTemp(r_temp);
1247 FreeTemp(r_value);
1248}
1249
1250bool X86Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001251 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info);
1252 if (rl_dest.s_reg_low == INVALID_SREG) {
1253 // Result is unused, the code is dead. Inlining successful, no code generated.
1254 return true;
1255 }
Yixin Shou8c914c02014-07-28 14:17:09 -04001256 RegLocation rl_src_i = info->args[0];
1257 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg)
1258 : LoadValue(rl_src_i, kCoreReg);
Yixin Shou8c914c02014-07-28 14:17:09 -04001259 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1260 if (size == k64) {
1261 if (cu_->instruction_set == kX86_64) {
1262 /* Use one bswap instruction to reverse byte order first and then use 3 rounds of
1263 swapping bits to reverse bits in a long number x. Using bswap to save instructions
1264 compared to generic luni implementation which has 5 rounds of swapping bits.
1265 x = bswap x
1266 x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
1267 x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
1268 x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
1269 */
1270 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1271 SwapBits64(rl_result.reg, 1, 0x5555555555555555);
1272 SwapBits64(rl_result.reg, 2, 0x3333333333333333);
1273 SwapBits64(rl_result.reg, 4, 0x0f0f0f0f0f0f0f0f);
1274 StoreValueWide(rl_dest, rl_result);
1275 return true;
1276 }
1277 RegStorage r_i_low = rl_i.reg.GetLow();
1278 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
1279 // First REV shall clobber rl_result.reg.GetLowReg(), save the value in a temp for the second
1280 // REV.
1281 r_i_low = AllocTemp();
1282 OpRegCopy(r_i_low, rl_i.reg);
1283 }
1284 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1285 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1286 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
1287 FreeTemp(r_i_low);
1288 }
1289 SwapBits(rl_result.reg.GetLow(), 1, 0x55555555);
1290 SwapBits(rl_result.reg.GetLow(), 2, 0x33333333);
1291 SwapBits(rl_result.reg.GetLow(), 4, 0x0f0f0f0f);
1292 SwapBits(rl_result.reg.GetHigh(), 1, 0x55555555);
1293 SwapBits(rl_result.reg.GetHigh(), 2, 0x33333333);
1294 SwapBits(rl_result.reg.GetHigh(), 4, 0x0f0f0f0f);
1295 StoreValueWide(rl_dest, rl_result);
1296 } else {
1297 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1298 SwapBits(rl_result.reg, 1, 0x55555555);
1299 SwapBits(rl_result.reg, 2, 0x33333333);
1300 SwapBits(rl_result.reg, 4, 0x0f0f0f0f);
1301 StoreValue(rl_dest, rl_result);
1302 }
1303 return true;
1304}
1305
buzbee2700f7e2014-03-07 09:46:20 -08001306LIR* X86Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Mark Mendell27dee8b2014-12-01 19:06:12 -05001307 if (cu_->target64) {
1308 // We can do this directly using RIP addressing.
1309 // We don't know the proper offset for the value, so pick one that will force
1310 // 4 byte offset. We will fix this up in the assembler later to have the right
1311 // value.
1312 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
1313 LIR* res = NewLIR3(kX86Mov32RM, reg.GetReg(), kRIPReg, 256);
1314 res->target = target;
1315 res->flags.fixup = kFixupLoad;
1316 return res;
1317 }
1318
Mark Mendell55d0eac2014-02-06 11:02:52 -08001319 CHECK(base_of_code_ != nullptr);
1320
1321 // Address the start of the method
1322 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001323 if (rl_method.wide) {
1324 LoadValueDirectWideFixed(rl_method, reg);
1325 } else {
1326 LoadValueDirectFixed(rl_method, reg);
1327 }
Mark Mendell55d0eac2014-02-06 11:02:52 -08001328 store_method_addr_used_ = true;
1329
1330 // Load the proper value from the literal area.
1331 // We don't know the proper offset for the value, so pick one that will force
1332 // 4 byte offset. We will fix this up in the assembler later to have the right
1333 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001334 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
buzbee2700f7e2014-03-07 09:46:20 -08001335 LIR *res = RawLIR(current_dalvik_offset_, kX86Mov32RM, reg.GetReg(), reg.GetReg(), 256,
1336 0, 0, target);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001337 res->target = target;
1338 res->flags.fixup = kFixupLoad;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001339 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340}
1341
buzbee2700f7e2014-03-07 09:46:20 -08001342LIR* X86Mir2Lir::OpVldm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001343 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 LOG(FATAL) << "Unexpected use of OpVldm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001345 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346}
1347
buzbee2700f7e2014-03-07 09:46:20 -08001348LIR* X86Mir2Lir::OpVstm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001349 UNUSED(r_base, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 LOG(FATAL) << "Unexpected use of OpVstm for x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001351 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352}
1353
1354void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1355 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001356 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001357 UNUSED(lit);
buzbee2700f7e2014-03-07 09:46:20 -08001358 RegStorage t_reg = AllocTemp();
1359 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, second_bit - first_bit);
1360 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 FreeTemp(t_reg);
1362 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001363 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 }
1365}
1366
Mingyao Yange643a172014-04-08 11:02:52 -07001367void X86Mir2Lir::GenDivZeroCheckWide(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001368 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001369 DCHECK(reg.Is64Bit());
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001370
Chao-ying Fua0147762014-06-06 18:38:49 -07001371 NewLIR2(kX86Cmp64RI8, reg.GetReg(), 0);
1372 } else {
1373 DCHECK(reg.IsPair());
1374
1375 // We are not supposed to clobber the incoming storage, so allocate a temporary.
1376 RegStorage t_reg = AllocTemp();
1377 // Doing an OR is a quick way to check if both registers are zero. This will set the flags.
1378 OpRegRegReg(kOpOr, t_reg, reg.GetLow(), reg.GetHigh());
1379 // The temp is no longer needed so free it at this time.
1380 FreeTemp(t_reg);
1381 }
Razvan A Lupusoru090dd442013-12-20 14:35:03 -08001382
1383 // In case of zero, throw ArithmeticException.
Mingyao Yange643a172014-04-08 11:02:52 -07001384 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385}
1386
Mingyao Yang80365d92014-04-18 12:10:58 -07001387void X86Mir2Lir::GenArrayBoundsCheck(RegStorage index,
1388 RegStorage array_base,
1389 int len_offset) {
1390 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1391 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001392 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1393 RegStorage index_in, RegStorage array_base_in, int32_t len_offset_in)
1394 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
1395 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001396 }
1397
1398 void Compile() OVERRIDE {
1399 m2l_->ResetRegPool();
1400 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001401 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001402
1403 RegStorage new_index = index_;
1404 // Move index out of kArg1, either directly to kArg0, or to kArg2.
Serguei Katkov4c7cc152014-06-24 00:50:02 +07001405 // TODO: clean-up to check not a number but with type
Andreas Gampeccc60262014-07-04 18:02:38 -07001406 if (index_ == m2l_->TargetReg(kArg1, kNotWide)) {
1407 if (array_base_ == m2l_->TargetReg(kArg0, kRef)) {
1408 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kNotWide), index_);
1409 new_index = m2l_->TargetReg(kArg2, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001410 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001411 m2l_->OpRegCopy(m2l_->TargetReg(kArg0, kNotWide), index_);
1412 new_index = m2l_->TargetReg(kArg0, kNotWide);
Mingyao Yang80365d92014-04-18 12:10:58 -07001413 }
1414 }
1415 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001416 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1417 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1418 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, new_index,
1419 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001420 }
1421
1422 private:
1423 const RegStorage index_;
1424 const RegStorage array_base_;
1425 const int32_t len_offset_;
1426 };
1427
1428 OpRegMem(kOpCmp, index, array_base, len_offset);
Dave Allison69dfe512014-07-11 17:11:58 +00001429 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001430 LIR* branch = OpCondBranch(kCondUge, nullptr);
1431 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1432 index, array_base, len_offset));
1433}
1434
1435void X86Mir2Lir::GenArrayBoundsCheck(int32_t index,
1436 RegStorage array_base,
1437 int32_t len_offset) {
1438 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
1439 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001440 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in,
1441 int32_t index_in, RegStorage array_base_in, int32_t len_offset_in)
1442 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
1443 index_(index_in), array_base_(array_base_in), len_offset_(len_offset_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001444 }
1445
1446 void Compile() OVERRIDE {
1447 m2l_->ResetRegPool();
1448 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001449 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -07001450
1451 // Load array length to kArg1.
Andreas Gampe98430592014-07-27 19:44:50 -07001452 X86Mir2Lir* x86_m2l = static_cast<X86Mir2Lir*>(m2l_);
1453 x86_m2l->OpRegMem(kOpMov, m2l_->TargetReg(kArg1, kNotWide), array_base_, len_offset_);
1454 x86_m2l->LoadConstant(m2l_->TargetReg(kArg0, kNotWide), index_);
1455 x86_m2l->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, m2l_->TargetReg(kArg0, kNotWide),
1456 m2l_->TargetReg(kArg1, kNotWide), true);
Mingyao Yang80365d92014-04-18 12:10:58 -07001457 }
1458
1459 private:
1460 const int32_t index_;
1461 const RegStorage array_base_;
1462 const int32_t len_offset_;
1463 };
1464
1465 NewLIR3(IS_SIMM8(index) ? kX86Cmp32MI8 : kX86Cmp32MI, array_base.GetReg(), len_offset, index);
Dave Allison69dfe512014-07-11 17:11:58 +00001466 MarkPossibleNullPointerException(0);
Mingyao Yang80365d92014-04-18 12:10:58 -07001467 LIR* branch = OpCondBranch(kCondLs, nullptr);
1468 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch,
1469 index, array_base, len_offset));
1470}
1471
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001473LIR* X86Mir2Lir::OpTestSuspend(LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -07001474 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001475 OpTlsCmp(Thread::ThreadFlagsOffset<8>(), 0);
1476 } else {
1477 OpTlsCmp(Thread::ThreadFlagsOffset<4>(), 0);
1478 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 return OpCondBranch((target == NULL) ? kCondNe : kCondEq, target);
1480}
1481
1482// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001483LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484 OpRegImm(kOpSub, reg, 1);
Yixin Shoua0dac3e2014-01-23 05:01:22 -08001485 return OpCondBranch(c_code, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486}
1487
buzbee11b63d12013-08-27 07:34:17 -07001488bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001489 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001490 UNUSED(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 LOG(FATAL) << "Unexpected use of smallLiteralDive in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001492 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493}
1494
Ian Rogerse2143c02014-03-28 08:47:16 -07001495bool X86Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001496 UNUSED(rl_src, rl_dest, lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001497 LOG(FATAL) << "Unexpected use of easyMultiply in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001498 UNREACHABLE();
Ian Rogerse2143c02014-03-28 08:47:16 -07001499}
1500
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001501LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001502 UNUSED(cond, guide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 LOG(FATAL) << "Unexpected use of OpIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001504 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505}
1506
Dave Allison3da67a52014-04-02 17:03:45 -07001507void X86Mir2Lir::OpEndIT(LIR* it) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001508 UNUSED(it);
Dave Allison3da67a52014-04-02 17:03:45 -07001509 LOG(FATAL) << "Unexpected use of OpEndIT in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001510 UNREACHABLE();
Dave Allison3da67a52014-04-02 17:03:45 -07001511}
1512
buzbee2700f7e2014-03-07 09:46:20 -08001513void X86Mir2Lir::GenImulRegImm(RegStorage dest, RegStorage src, int val) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001514 switch (val) {
1515 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001516 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001517 break;
1518 case 1:
1519 OpRegCopy(dest, src);
1520 break;
1521 default:
1522 OpRegRegImm(kOpMul, dest, src, val);
1523 break;
1524 }
1525}
1526
buzbee2700f7e2014-03-07 09:46:20 -08001527void X86Mir2Lir::GenImulMemImm(RegStorage dest, int sreg, int displacement, int val) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001528 UNUSED(sreg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001529 // All memory accesses below reference dalvik regs.
1530 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1531
Mark Mendell4708dcd2014-01-22 09:05:18 -08001532 LIR *m;
1533 switch (val) {
1534 case 0:
buzbee2700f7e2014-03-07 09:46:20 -08001535 NewLIR2(kX86Xor32RR, dest.GetReg(), dest.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001536 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001537 case 1: {
1538 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
1539 LoadBaseDisp(rs_rSP, displacement, dest, k32, kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001540 break;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001541 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001542 default:
buzbee091cc402014-03-31 10:14:40 -07001543 m = NewLIR4(IS_SIMM8(val) ? kX86Imul32RMI8 : kX86Imul32RMI, dest.GetReg(),
Ian Rogersb28c1c02014-11-08 11:21:21 -08001544 rs_rX86_SP_32.GetReg(), displacement, val);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001545 AnnotateDalvikRegAccess(m, displacement >> 2, true /* is_load */, true /* is_64bit */);
1546 break;
1547 }
1548}
1549
Andreas Gampec76c6142014-08-04 16:30:03 -07001550void X86Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001551 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001552 if (!cu_->target64) {
1553 // Some x86 32b ops are fallback.
1554 switch (opcode) {
1555 case Instruction::NOT_LONG:
1556 case Instruction::DIV_LONG:
1557 case Instruction::DIV_LONG_2ADDR:
1558 case Instruction::REM_LONG:
1559 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001560 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001561 return;
1562
1563 default:
1564 // Everything else we can handle.
1565 break;
1566 }
1567 }
1568
1569 switch (opcode) {
1570 case Instruction::NOT_LONG:
1571 GenNotLong(rl_dest, rl_src2);
1572 return;
1573
1574 case Instruction::ADD_LONG:
1575 case Instruction::ADD_LONG_2ADDR:
1576 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1577 return;
1578
1579 case Instruction::SUB_LONG:
1580 case Instruction::SUB_LONG_2ADDR:
1581 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, false);
1582 return;
1583
1584 case Instruction::MUL_LONG:
1585 case Instruction::MUL_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001586 GenMulLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001587 return;
1588
1589 case Instruction::DIV_LONG:
1590 case Instruction::DIV_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001591 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001592 return;
1593
1594 case Instruction::REM_LONG:
1595 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001596 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001597 return;
1598
1599 case Instruction::AND_LONG_2ADDR:
1600 case Instruction::AND_LONG:
1601 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1602 return;
1603
1604 case Instruction::OR_LONG:
1605 case Instruction::OR_LONG_2ADDR:
1606 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1607 return;
1608
1609 case Instruction::XOR_LONG:
1610 case Instruction::XOR_LONG_2ADDR:
1611 GenLongArith(rl_dest, rl_src1, rl_src2, opcode, true);
1612 return;
1613
1614 case Instruction::NEG_LONG:
1615 GenNegLong(rl_dest, rl_src2);
1616 return;
1617
1618 default:
1619 LOG(FATAL) << "Invalid long arith op";
1620 return;
1621 }
1622}
1623
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001624bool X86Mir2Lir::GenMulLongConst(RegLocation rl_dest, RegLocation rl_src1, int64_t val, int flags) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001625 // All memory accesses below reference dalvik regs.
1626 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1627
Andreas Gampec76c6142014-08-04 16:30:03 -07001628 if (val == 0) {
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001629 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampec76c6142014-08-04 16:30:03 -07001630 if (cu_->target64) {
1631 OpRegReg(kOpXor, rl_result.reg, rl_result.reg);
Alexei Zavjalovd8191d02014-06-11 18:26:40 +07001632 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001633 OpRegReg(kOpXor, rl_result.reg.GetLow(), rl_result.reg.GetLow());
1634 OpRegReg(kOpXor, rl_result.reg.GetHigh(), rl_result.reg.GetHigh());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001635 }
Andreas Gampec76c6142014-08-04 16:30:03 -07001636 StoreValueWide(rl_dest, rl_result);
1637 return true;
1638 } else if (val == 1) {
1639 StoreValueWide(rl_dest, rl_src1);
1640 return true;
1641 } else if (val == 2) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001642 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src1, rl_src1, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001643 return true;
1644 } else if (IsPowerOfTwo(val)) {
Andreas Gampe7e499922015-01-06 08:28:12 -08001645 int shift_amount = CTZ(val);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001646 if (!PartiallyIntersects(rl_src1, rl_dest)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001647 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1648 RegLocation rl_result = GenShiftImmOpLong(Instruction::SHL_LONG, rl_dest, rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001649 shift_amount, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001650 StoreValueWide(rl_dest, rl_result);
1651 return true;
1652 }
1653 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001654
Andreas Gampec76c6142014-08-04 16:30:03 -07001655 // Okay, on 32b just bite the bullet and do it, still better than the general case.
1656 if (!cu_->target64) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08001657 int32_t val_lo = Low32Bits(val);
1658 int32_t val_hi = High32Bits(val);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001659 // Prepare for explicit register usage.
1660 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001661 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001662 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1663 int displacement = SRegOffset(rl_src1.s_reg_low);
1664
1665 // ECX <- 1H * 2L
1666 // EAX <- 1L * 2H
1667 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001668 GenImulRegImm(rs_r1, rl_src1.reg.GetHigh(), val_lo);
1669 GenImulRegImm(rs_r0, rl_src1.reg.GetLow(), val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001670 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001671 GenImulMemImm(rs_r1, GetSRegHi(rl_src1.s_reg_low), displacement + HIWORD_OFFSET, val_lo);
1672 GenImulMemImm(rs_r0, rl_src1.s_reg_low, displacement + LOWORD_OFFSET, val_hi);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001673 }
1674
1675 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001676 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001677
1678 // EAX <- 2L
buzbee2700f7e2014-03-07 09:46:20 -08001679 LoadConstantNoClobber(rs_r0, val_lo);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001680
1681 // EDX:EAX <- 2L * 1L (double precision)
1682 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001683 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001684 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001685 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001686 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1687 true /* is_load */, true /* is_64bit */);
1688 }
1689
1690 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001691 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001692
1693 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001694 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
1695 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001696 StoreValueWide(rl_dest, rl_result);
Andreas Gampec76c6142014-08-04 16:30:03 -07001697 return true;
1698 }
1699 return false;
1700}
1701
1702void X86Mir2Lir::GenMulLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001703 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001704 if (rl_src1.is_const) {
1705 std::swap(rl_src1, rl_src2);
1706 }
1707
1708 if (rl_src2.is_const) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001709 if (GenMulLongConst(rl_dest, rl_src1, mir_graph_->ConstantValueWide(rl_src2), flags)) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001710 return;
1711 }
1712 }
1713
1714 // All memory accesses below reference dalvik regs.
1715 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1716
1717 if (cu_->target64) {
1718 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1719 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1720 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1721 if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1722 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1723 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
1724 } else if (rl_result.reg.GetReg() != rl_src1.reg.GetReg() &&
1725 rl_result.reg.GetReg() == rl_src2.reg.GetReg()) {
1726 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
1727 } else if (rl_result.reg.GetReg() == rl_src1.reg.GetReg() &&
1728 rl_result.reg.GetReg() != rl_src2.reg.GetReg()) {
1729 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1730 } else {
1731 OpRegCopy(rl_result.reg, rl_src1.reg);
1732 NewLIR2(kX86Imul64RR, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1733 }
1734 StoreValueWide(rl_dest, rl_result);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001735 return;
1736 }
1737
Andreas Gampec76c6142014-08-04 16:30:03 -07001738 // Not multiplying by a constant. Do it the hard way
Mark Mendellde99bba2014-02-14 12:15:02 -08001739 // Check for V*V. We can eliminate a multiply in that case, as 2L*1H == 2H*1L.
1740 bool is_square = mir_graph_->SRegToVReg(rl_src1.s_reg_low) ==
1741 mir_graph_->SRegToVReg(rl_src2.s_reg_low);
1742
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07001743 // Prepare for explicit register usage.
1744 ExplicitTempRegisterLock(this, 3, &rs_r0, &rs_r1, &rs_r2);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001745 rl_src1 = UpdateLocWideTyped(rl_src1);
1746 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001747
1748 // At this point, the VRs are in their home locations.
1749 bool src1_in_reg = rl_src1.location == kLocPhysReg;
1750 bool src2_in_reg = rl_src2.location == kLocPhysReg;
Ian Rogersb28c1c02014-11-08 11:21:21 -08001751 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Mark Mendell4708dcd2014-01-22 09:05:18 -08001752
1753 // ECX <- 1H
1754 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001755 NewLIR2(kX86Mov32RR, rs_r1.GetReg(), rl_src1.reg.GetHighReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001756 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001757 LoadBaseDisp(rs_rSP, SRegOffset(rl_src1.s_reg_low) + HIWORD_OFFSET, rs_r1, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001758 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001759 }
1760
Mark Mendellde99bba2014-02-14 12:15:02 -08001761 if (is_square) {
1762 // Take advantage of the fact that the values are the same.
1763 // ECX <- ECX * 2L (1H * 2L)
1764 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001765 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001766 } else {
1767 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001768 LIR* m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001769 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001770 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1771 true /* is_load */, true /* is_64bit */);
1772 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001773
Mark Mendellde99bba2014-02-14 12:15:02 -08001774 // ECX <- 2*ECX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001775 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001776 } else {
Mark Mendellde99bba2014-02-14 12:15:02 -08001777 // EAX <- 2H
1778 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001779 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetHighReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001780 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001781 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + HIWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001782 kNotVolatile);
Mark Mendellde99bba2014-02-14 12:15:02 -08001783 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001784
Mark Mendellde99bba2014-02-14 12:15:02 -08001785 // EAX <- EAX * 1L (2H * 1L)
1786 if (src1_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001787 NewLIR2(kX86Imul32RR, rs_r0.GetReg(), rl_src1.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001788 } else {
1789 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001790 LIR *m = NewLIR3(kX86Imul32RM, rs_r0.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001791 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001792 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1793 true /* is_load */, true /* is_64bit */);
1794 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001795
Mark Mendellde99bba2014-02-14 12:15:02 -08001796 // ECX <- ECX * 2L (1H * 2L)
1797 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001798 NewLIR2(kX86Imul32RR, rs_r1.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001799 } else {
1800 int displacement = SRegOffset(rl_src2.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001801 LIR *m = NewLIR3(kX86Imul32RM, rs_r1.GetReg(), rs_rX86_SP_32.GetReg(),
buzbee091cc402014-03-31 10:14:40 -07001802 displacement + LOWORD_OFFSET);
Mark Mendellde99bba2014-02-14 12:15:02 -08001803 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1804 true /* is_load */, true /* is_64bit */);
1805 }
1806
1807 // ECX <- ECX + EAX (2H * 1L) + (1H * 2L)
buzbee091cc402014-03-31 10:14:40 -07001808 NewLIR2(kX86Add32RR, rs_r1.GetReg(), rs_r0.GetReg());
Mark Mendellde99bba2014-02-14 12:15:02 -08001809 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08001810
1811 // EAX <- 2L
1812 if (src2_in_reg) {
buzbee091cc402014-03-31 10:14:40 -07001813 NewLIR2(kX86Mov32RR, rs_r0.GetReg(), rl_src2.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001814 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001815 LoadBaseDisp(rs_rSP, SRegOffset(rl_src2.s_reg_low) + LOWORD_OFFSET, rs_r0, k32,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001816 kNotVolatile);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001817 }
1818
1819 // EDX:EAX <- 2L * 1L (double precision)
1820 if (src1_in_reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001821 NewLIR1(kX86Mul32DaR, rl_src1.reg.GetLowReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001822 } else {
1823 int displacement = SRegOffset(rl_src1.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001824 LIR *m = NewLIR2(kX86Mul32DaM, rs_rX86_SP_32.GetReg(), displacement + LOWORD_OFFSET);
Mark Mendell4708dcd2014-01-22 09:05:18 -08001825 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
1826 true /* is_load */, true /* is_64bit */);
1827 }
1828
1829 // EDX <- EDX + ECX (add high words)
buzbee091cc402014-03-31 10:14:40 -07001830 NewLIR2(kX86Add32RR, rs_r2.GetReg(), rs_r1.GetReg());
Mark Mendell4708dcd2014-01-22 09:05:18 -08001831
1832 // Result is EDX:EAX
buzbee091cc402014-03-31 10:14:40 -07001833 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
buzbee2700f7e2014-03-07 09:46:20 -08001834 RegStorage::MakeRegPair(rs_r0, rs_r2), INVALID_SREG, INVALID_SREG};
Mark Mendell4708dcd2014-01-22 09:05:18 -08001835 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001836}
Mark Mendelle02d48f2014-01-15 11:19:23 -08001837
1838void X86Mir2Lir::GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src,
1839 Instruction::Code op) {
1840 DCHECK_EQ(rl_dest.location, kLocPhysReg);
1841 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
1842 if (rl_src.location == kLocPhysReg) {
1843 // Both operands are in registers.
Serguei Katkovab5545f2014-03-25 10:51:15 +07001844 // But we must ensure that rl_src is in pair
Elena Sayapinadd644502014-07-01 18:39:52 +07001845 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001846 NewLIR2(x86op, rl_dest.reg.GetReg(), rl_src.reg.GetReg());
1847 } else {
1848 rl_src = LoadValueWide(rl_src, kCoreReg);
1849 if (rl_dest.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1850 // The registers are the same, so we would clobber it before the use.
1851 RegStorage temp_reg = AllocTemp();
1852 OpRegCopy(temp_reg, rl_dest.reg);
1853 rl_src.reg.SetHighReg(temp_reg.GetReg());
1854 }
1855 NewLIR2(x86op, rl_dest.reg.GetLowReg(), rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08001856
Chao-ying Fua0147762014-06-06 18:38:49 -07001857 x86op = GetOpcode(op, rl_dest, rl_src, true);
1858 NewLIR2(x86op, rl_dest.reg.GetHighReg(), rl_src.reg.GetHighReg());
Chao-ying Fua0147762014-06-06 18:38:49 -07001859 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08001860 return;
1861 }
1862
1863 // RHS is in memory.
1864 DCHECK((rl_src.location == kLocDalvikFrame) ||
1865 (rl_src.location == kLocCompilerTemp));
Ian Rogersb28c1c02014-11-08 11:21:21 -08001866 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08001867 int displacement = SRegOffset(rl_src.s_reg_low);
1868
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001869 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -07001870 LIR *lir = NewLIR3(x86op, cu_->target64 ? rl_dest.reg.GetReg() : rl_dest.reg.GetLowReg(),
1871 r_base, displacement + LOWORD_OFFSET);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001872 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
1873 true /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07001874 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001875 x86op = GetOpcode(op, rl_dest, rl_src, true);
1876 lir = NewLIR3(x86op, rl_dest.reg.GetHighReg(), r_base, displacement + HIWORD_OFFSET);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001877 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1878 true /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07001879 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880}
1881
Mark Mendelle02d48f2014-01-15 11:19:23 -08001882void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001883 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001884 if (rl_dest.location == kLocPhysReg) {
1885 // Ensure we are in a register pair
1886 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1887
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001888 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001889 GenLongRegOrMemOp(rl_result, rl_src, op);
1890 StoreFinalValueWide(rl_dest, rl_result);
1891 return;
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001892 } else if (!cu_->target64 && Intersects(rl_src, rl_dest)) {
1893 // Handle the case when src and dest are intersect.
1894 rl_src = LoadValueWide(rl_src, kCoreReg);
1895 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001896 rl_src = UpdateLocWideTyped(rl_src);
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001897 GenLongRegOrMemOp(rl_result, rl_src, op);
1898 StoreFinalValueWide(rl_dest, rl_result);
1899 return;
Mark Mendelle02d48f2014-01-15 11:19:23 -08001900 }
1901
1902 // It wasn't in registers, so it better be in memory.
1903 DCHECK((rl_dest.location == kLocDalvikFrame) ||
1904 (rl_dest.location == kLocCompilerTemp));
1905 rl_src = LoadValueWide(rl_src, kCoreReg);
1906
1907 // Operate directly into memory.
1908 X86OpCode x86op = GetOpcode(op, rl_dest, rl_src, false);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001909 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08001910 int displacement = SRegOffset(rl_dest.s_reg_low);
1911
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001912 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07001913 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET,
Elena Sayapinadd644502014-07-01 18:39:52 +07001914 cu_->target64 ? rl_src.reg.GetReg() : rl_src.reg.GetLowReg());
Mark Mendelle02d48f2014-01-15 11:19:23 -08001915 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07001916 true /* is_load */, true /* is64bit */);
1917 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08001918 false /* is_load */, true /* is64bit */);
Elena Sayapinadd644502014-07-01 18:39:52 +07001919 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001920 x86op = GetOpcode(op, rl_dest, rl_src, true);
1921 lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, rl_src.reg.GetHighReg());
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07001922 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1923 true /* is_load */, true /* is64bit */);
1924 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
1925 false /* is_load */, true /* is64bit */);
Chao-ying Fua0147762014-06-06 18:38:49 -07001926 }
nikolay serdjuk6b9356c2014-11-13 18:15:23 +06001927
1928 int v_src_reg = mir_graph_->SRegToVReg(rl_src.s_reg_low);
1929 int v_dst_reg = mir_graph_->SRegToVReg(rl_dest.s_reg_low);
1930
1931 // If the left operand is in memory and the right operand is in a register
1932 // and both belong to the same dalvik register then we should clobber the
1933 // right one because it doesn't hold valid data anymore.
1934 if (v_src_reg == v_dst_reg) {
1935 Clobber(rl_src.reg);
1936 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001937}
1938
Mark Mendelle02d48f2014-01-15 11:19:23 -08001939void X86Mir2Lir::GenLongArith(RegLocation rl_dest, RegLocation rl_src1,
1940 RegLocation rl_src2, Instruction::Code op,
1941 bool is_commutative) {
1942 // Is this really a 2 operand operation?
1943 switch (op) {
1944 case Instruction::ADD_LONG_2ADDR:
1945 case Instruction::SUB_LONG_2ADDR:
1946 case Instruction::AND_LONG_2ADDR:
1947 case Instruction::OR_LONG_2ADDR:
1948 case Instruction::XOR_LONG_2ADDR:
Mark Mendelle87f9b52014-04-30 14:13:18 -04001949 if (GenerateTwoOperandInstructions()) {
1950 GenLongArith(rl_dest, rl_src2, op);
1951 return;
1952 }
1953 break;
1954
Mark Mendelle02d48f2014-01-15 11:19:23 -08001955 default:
1956 break;
1957 }
1958
1959 if (rl_dest.location == kLocPhysReg) {
1960 RegLocation rl_result = LoadValueWide(rl_src1, kCoreReg);
1961
1962 // We are about to clobber the LHS, so it needs to be a temp.
1963 rl_result = ForceTempWide(rl_result);
1964
1965 // Perform the operation using the RHS.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001966 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001967 GenLongRegOrMemOp(rl_result, rl_src2, op);
1968
1969 // And now record that the result is in the temp.
1970 StoreFinalValueWide(rl_dest, rl_result);
1971 return;
1972 }
1973
1974 // It wasn't in registers, so it better be in memory.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001975 DCHECK((rl_dest.location == kLocDalvikFrame) || (rl_dest.location == kLocCompilerTemp));
1976 rl_src1 = UpdateLocWideTyped(rl_src1);
1977 rl_src2 = UpdateLocWideTyped(rl_src2);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001978
1979 // Get one of the source operands into temporary register.
1980 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +07001981 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07001982 if (IsTemp(rl_src1.reg)) {
1983 GenLongRegOrMemOp(rl_src1, rl_src2, op);
1984 } else if (is_commutative) {
1985 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1986 // We need at least one of them to be a temporary.
1987 if (!IsTemp(rl_src2.reg)) {
1988 rl_src1 = ForceTempWide(rl_src1);
1989 GenLongRegOrMemOp(rl_src1, rl_src2, op);
1990 } else {
1991 GenLongRegOrMemOp(rl_src2, rl_src1, op);
1992 StoreFinalValueWide(rl_dest, rl_src2);
1993 return;
1994 }
1995 } else {
1996 // Need LHS to be the temp.
Mark Mendelle02d48f2014-01-15 11:19:23 -08001997 rl_src1 = ForceTempWide(rl_src1);
Yevgeny Rouban91b6ffa2014-03-07 14:35:44 +07001998 GenLongRegOrMemOp(rl_src1, rl_src2, op);
Mark Mendelle02d48f2014-01-15 11:19:23 -08001999 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002000 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002001 if (IsTemp(rl_src1.reg.GetLow()) && IsTemp(rl_src1.reg.GetHigh())) {
2002 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2003 } else if (is_commutative) {
2004 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2005 // We need at least one of them to be a temporary.
2006 if (!(IsTemp(rl_src2.reg.GetLow()) && IsTemp(rl_src2.reg.GetHigh()))) {
2007 rl_src1 = ForceTempWide(rl_src1);
2008 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2009 } else {
2010 GenLongRegOrMemOp(rl_src2, rl_src1, op);
2011 StoreFinalValueWide(rl_dest, rl_src2);
2012 return;
2013 }
2014 } else {
2015 // Need LHS to be the temp.
2016 rl_src1 = ForceTempWide(rl_src1);
2017 GenLongRegOrMemOp(rl_src1, rl_src2, op);
2018 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002019 }
2020
2021 StoreFinalValueWide(rl_dest, rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022}
2023
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002024void X86Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002025 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002026 rl_src = LoadValueWide(rl_src, kCoreReg);
2027 RegLocation rl_result;
2028 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2029 OpRegCopy(rl_result.reg, rl_src.reg);
2030 OpReg(kOpNot, rl_result.reg);
2031 StoreValueWide(rl_dest, rl_result);
2032 } else {
2033 LOG(FATAL) << "Unexpected use GenNotLong()";
2034 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002035}
2036
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002037void X86Mir2Lir::GenDivRemLongLit(RegLocation rl_dest, RegLocation rl_src,
2038 int64_t imm, bool is_div) {
2039 if (imm == 0) {
2040 GenDivZeroException();
2041 } else if (imm == 1) {
2042 if (is_div) {
2043 // x / 1 == x.
2044 StoreValueWide(rl_dest, rl_src);
2045 } else {
2046 // x % 1 == 0.
2047 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2048 LoadConstantWide(rl_result.reg, 0);
2049 StoreValueWide(rl_dest, rl_result);
2050 }
2051 } else if (imm == -1) { // handle 0x8000000000000000 / -1 special case.
2052 if (is_div) {
2053 rl_src = LoadValueWide(rl_src, kCoreReg);
2054 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2055 RegStorage rs_temp = AllocTempWide();
2056
2057 OpRegCopy(rl_result.reg, rl_src.reg);
2058 LoadConstantWide(rs_temp, 0x8000000000000000);
2059
2060 // If x == MIN_LONG, return MIN_LONG.
2061 OpRegReg(kOpCmp, rl_src.reg, rs_temp);
2062 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
2063
2064 // For x != MIN_LONG, x / -1 == -x.
2065 OpReg(kOpNeg, rl_result.reg);
2066
2067 minint_branch->target = NewLIR0(kPseudoTargetLabel);
2068 FreeTemp(rs_temp);
2069 StoreValueWide(rl_dest, rl_result);
2070 } else {
2071 // x % -1 == 0.
2072 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2073 LoadConstantWide(rl_result.reg, 0);
2074 StoreValueWide(rl_dest, rl_result);
2075 }
2076 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
2077 // Division using shifting.
2078 rl_src = LoadValueWide(rl_src, kCoreReg);
2079 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2080 if (IsSameReg(rl_result.reg, rl_src.reg)) {
2081 RegStorage rs_temp = AllocTypedTempWide(false, kCoreReg);
2082 rl_result.reg.SetReg(rs_temp.GetReg());
2083 }
2084 LoadConstantWide(rl_result.reg, std::abs(imm) - 1);
2085 OpRegReg(kOpAdd, rl_result.reg, rl_src.reg);
2086 NewLIR2(kX86Test64RR, rl_src.reg.GetReg(), rl_src.reg.GetReg());
2087 OpCondRegReg(kOpCmov, kCondPl, rl_result.reg, rl_src.reg);
Andreas Gampe7e499922015-01-06 08:28:12 -08002088 int shift_amount = CTZ(imm);
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002089 OpRegImm(kOpAsr, rl_result.reg, shift_amount);
2090 if (imm < 0) {
2091 OpReg(kOpNeg, rl_result.reg);
2092 }
2093 StoreValueWide(rl_dest, rl_result);
2094 } else {
2095 CHECK(imm <= -2 || imm >= 2);
2096
2097 FlushReg(rs_r0q);
2098 Clobber(rs_r0q);
2099 LockTemp(rs_r0q);
2100 FlushReg(rs_r2q);
2101 Clobber(rs_r2q);
2102 LockTemp(rs_r2q);
2103
Mark Mendell3a91f442014-09-02 12:44:24 -04002104 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1,
2105 is_div ? rs_r2q : rs_r0q, INVALID_SREG, INVALID_SREG};
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002106
2107 // Use H.S.Warren's Hacker's Delight Chapter 10 and
2108 // T,Grablund, P.L.Montogomery's Division by invariant integers using multiplication.
2109 int64_t magic;
2110 int shift;
2111 CalculateMagicAndShift(imm, magic, shift, true /* is_long */);
2112
2113 /*
2114 * For imm >= 2,
2115 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n > 0
2116 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1, while n < 0.
2117 * For imm <= -2,
2118 * int(n/imm) = ceil(n/imm) = floor(M*n/2^S) +1 , while n > 0
2119 * int(n/imm) = floor(n/imm) = floor(M*n/2^S), while n < 0.
2120 * We implement this algorithm in the following way:
2121 * 1. multiply magic number m and numerator n, get the higher 64bit result in RDX
2122 * 2. if imm > 0 and magic < 0, add numerator to RDX
2123 * if imm < 0 and magic > 0, sub numerator from RDX
2124 * 3. if S !=0, SAR S bits for RDX
2125 * 4. add 1 to RDX if RDX < 0
2126 * 5. Thus, RDX is the quotient
2127 */
2128
Mark Mendell3a91f442014-09-02 12:44:24 -04002129 // RAX = magic.
2130 LoadConstantWide(rs_r0q, magic);
2131
2132 // Multiply by numerator.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002133 RegStorage numerator_reg;
2134 if (!is_div || (imm > 0 && magic < 0) || (imm < 0 && magic > 0)) {
2135 // We will need the value later.
2136 rl_src = LoadValueWide(rl_src, kCoreReg);
2137 numerator_reg = rl_src.reg;
Mark Mendell3a91f442014-09-02 12:44:24 -04002138
2139 // RDX:RAX = magic * numerator.
2140 NewLIR1(kX86Imul64DaR, numerator_reg.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002141 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002142 // Only need this once. Multiply directly from the value.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002143 rl_src = UpdateLocWideTyped(rl_src);
Mark Mendell3a91f442014-09-02 12:44:24 -04002144 if (rl_src.location != kLocPhysReg) {
2145 // Okay, we can do this from memory.
2146 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2147 int displacement = SRegOffset(rl_src.s_reg_low);
2148 // RDX:RAX = magic * numerator.
Ian Rogersb28c1c02014-11-08 11:21:21 -08002149 LIR *m = NewLIR2(kX86Imul64DaM, rs_rX86_SP_32.GetReg(), displacement);
Mark Mendell3a91f442014-09-02 12:44:24 -04002150 AnnotateDalvikRegAccess(m, displacement >> 2,
2151 true /* is_load */, true /* is_64bit */);
2152 } else {
2153 // RDX:RAX = magic * numerator.
2154 NewLIR1(kX86Imul64DaR, rl_src.reg.GetReg());
2155 }
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002156 }
2157
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002158 if (imm > 0 && magic < 0) {
2159 // Add numerator to RDX.
2160 DCHECK(numerator_reg.Valid());
2161 OpRegReg(kOpAdd, rs_r2q, numerator_reg);
2162 } else if (imm < 0 && magic > 0) {
2163 DCHECK(numerator_reg.Valid());
2164 OpRegReg(kOpSub, rs_r2q, numerator_reg);
2165 }
2166
2167 // Do we need the shift?
2168 if (shift != 0) {
2169 // Shift RDX by 'shift' bits.
2170 OpRegImm(kOpAsr, rs_r2q, shift);
2171 }
2172
2173 // Move RDX to RAX.
2174 OpRegCopyWide(rs_r0q, rs_r2q);
2175
2176 // Move sign bit to bit 0, zeroing the rest.
2177 OpRegImm(kOpLsr, rs_r2q, 63);
2178
2179 // RDX = RDX + RAX.
2180 OpRegReg(kOpAdd, rs_r2q, rs_r0q);
2181
2182 // Quotient is in RDX.
2183 if (!is_div) {
2184 // We need to compute the remainder.
2185 // Remainder is divisor - (quotient * imm).
2186 DCHECK(numerator_reg.Valid());
2187 OpRegCopyWide(rs_r0q, numerator_reg);
2188
2189 // Imul doesn't support 64-bit imms.
2190 if (imm > std::numeric_limits<int32_t>::max() ||
2191 imm < std::numeric_limits<int32_t>::min()) {
2192 RegStorage rs_temp = AllocTempWide();
2193 LoadConstantWide(rs_temp, imm);
2194
2195 // RAX = numerator * imm.
2196 NewLIR2(kX86Imul64RR, rs_r2q.GetReg(), rs_temp.GetReg());
2197
2198 FreeTemp(rs_temp);
2199 } else {
2200 // RAX = numerator * imm.
2201 int short_imm = static_cast<int>(imm);
2202 NewLIR3(kX86Imul64RRI, rs_r2q.GetReg(), rs_r2q.GetReg(), short_imm);
2203 }
2204
Mark Mendell3a91f442014-09-02 12:44:24 -04002205 // RAX -= RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002206 OpRegReg(kOpSub, rs_r0q, rs_r2q);
2207
Mark Mendell3a91f442014-09-02 12:44:24 -04002208 // Result in RAX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002209 } else {
Mark Mendell3a91f442014-09-02 12:44:24 -04002210 // Result in RDX.
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002211 }
2212 StoreValueWide(rl_dest, rl_result);
2213 FreeTemp(rs_r0q);
2214 FreeTemp(rs_r2q);
2215 }
2216}
2217
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002218void X86Mir2Lir::GenDivRemLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002219 RegLocation rl_src2, bool is_div, int flags) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002220 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002221 LOG(FATAL) << "Unexpected use GenDivRemLong()";
2222 return;
2223 }
2224
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002225 if (rl_src2.is_const) {
2226 DCHECK(rl_src2.wide);
2227 int64_t imm = mir_graph_->ConstantValueWide(rl_src2);
2228 GenDivRemLongLit(rl_dest, rl_src1, imm, is_div);
2229 return;
2230 }
2231
Chao-ying Fua0147762014-06-06 18:38:49 -07002232 // We have to use fixed registers, so flush all the temps.
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002233 // Prepare for explicit register usage.
2234 ExplicitTempRegisterLock(this, 4, &rs_r0q, &rs_r1q, &rs_r2q, &rs_r6q);
Chao-ying Fua0147762014-06-06 18:38:49 -07002235
2236 // Load LHS into RAX.
2237 LoadValueDirectWideFixed(rl_src1, rs_r0q);
2238
2239 // Load RHS into RCX.
2240 LoadValueDirectWideFixed(rl_src2, rs_r1q);
2241
2242 // Copy LHS sign bit into RDX.
2243 NewLIR0(kx86Cqo64Da);
2244
2245 // Handle division by zero case.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002246 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2247 GenDivZeroCheckWide(rs_r1q);
2248 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002249
2250 // Have to catch 0x8000000000000000/-1 case, or we will get an exception!
2251 NewLIR2(kX86Cmp64RI8, rs_r1q.GetReg(), -1);
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002252 LIR* minus_one_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002253
2254 // RHS is -1.
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002255 LoadConstantWide(rs_r6q, 0x8000000000000000);
2256 NewLIR2(kX86Cmp64RR, rs_r0q.GetReg(), rs_r6q.GetReg());
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002257 LIR *minint_branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
Chao-ying Fua0147762014-06-06 18:38:49 -07002258
2259 // In 0x8000000000000000/-1 case.
2260 if (!is_div) {
2261 // For DIV, RAX is already right. For REM, we need RDX 0.
2262 NewLIR2(kX86Xor64RR, rs_r2q.GetReg(), rs_r2q.GetReg());
2263 }
2264 LIR* done = NewLIR1(kX86Jmp8, 0);
2265
2266 // Expected case.
2267 minus_one_branch->target = NewLIR0(kPseudoTargetLabel);
2268 minint_branch->target = minus_one_branch->target;
2269 NewLIR1(kX86Idivmod64DaR, rs_r1q.GetReg());
2270 done->target = NewLIR0(kPseudoTargetLabel);
2271
2272 // Result is in RAX for div and RDX for rem.
2273 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, rs_r0q, INVALID_SREG, INVALID_SREG};
2274 if (!is_div) {
2275 rl_result.reg.SetReg(r2q);
2276 }
2277
2278 StoreValueWide(rl_dest, rl_result);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002279}
2280
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002281void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002282 rl_src = LoadValueWide(rl_src, kCoreReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002283 RegLocation rl_result;
Elena Sayapinadd644502014-07-01 18:39:52 +07002284 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002285 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2286 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
2287 } else {
2288 rl_result = ForceTempWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002289 OpRegReg(kOpNeg, rl_result.reg.GetLow(), rl_result.reg.GetLow()); // rLow = -rLow
2290 OpRegImm(kOpAdc, rl_result.reg.GetHigh(), 0); // rHigh = rHigh + CF
2291 OpRegReg(kOpNeg, rl_result.reg.GetHigh(), rl_result.reg.GetHigh()); // rHigh = -rHigh
Mark Mendelle02d48f2014-01-15 11:19:23 -08002292 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002293 StoreValueWide(rl_dest, rl_result);
2294}
2295
buzbee091cc402014-03-31 10:14:40 -07002296void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<4> thread_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002297 DCHECK_EQ(kX86, cu_->instruction_set);
2298 X86OpCode opcode = kX86Bkpt;
2299 switch (op) {
2300 case kOpCmp: opcode = kX86Cmp32RT; break;
2301 case kOpMov: opcode = kX86Mov32RT; break;
2302 default:
2303 LOG(FATAL) << "Bad opcode: " << op;
2304 break;
2305 }
2306 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
2307}
2308
2309void X86Mir2Lir::OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<8> thread_offset) {
2310 DCHECK_EQ(kX86_64, cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002311 X86OpCode opcode = kX86Bkpt;
Elena Sayapinadd644502014-07-01 18:39:52 +07002312 if (cu_->target64 && r_dest.Is64BitSolo()) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +07002313 switch (op) {
2314 case kOpCmp: opcode = kX86Cmp64RT; break;
2315 case kOpMov: opcode = kX86Mov64RT; break;
2316 default:
2317 LOG(FATAL) << "Bad opcode(OpRegThreadMem 64): " << op;
2318 break;
2319 }
2320 } else {
2321 switch (op) {
2322 case kOpCmp: opcode = kX86Cmp32RT; break;
2323 case kOpMov: opcode = kX86Mov32RT; break;
2324 default:
2325 LOG(FATAL) << "Bad opcode: " << op;
2326 break;
2327 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002328 }
buzbee091cc402014-03-31 10:14:40 -07002329 NewLIR2(opcode, r_dest.GetReg(), thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002330}
2331
2332/*
2333 * Generate array load
2334 */
2335void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002336 RegLocation rl_index, RegLocation rl_dest, int scale) {
Mark Mendellca541342014-10-15 16:59:49 -04002337 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002338 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002339 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002340 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002341
Mark Mendell343adb52013-12-18 06:02:17 -08002342 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07002343 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002344 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2345 } else {
2346 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2347 }
2348
Mark Mendell343adb52013-12-18 06:02:17 -08002349 bool constant_index = rl_index.is_const;
2350 int32_t constant_index_value = 0;
2351 if (!constant_index) {
2352 rl_index = LoadValue(rl_index, kCoreReg);
2353 } else {
2354 constant_index_value = mir_graph_->ConstantValue(rl_index);
2355 // If index is constant, just fold it into the data offset
2356 data_offset += constant_index_value << scale;
2357 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002358 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002359 }
2360
Brian Carlstrom7940e442013-07-12 13:46:57 -07002361 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002362 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002363
2364 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002365 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002366 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002367 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002368 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002369 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002370 }
Mark Mendell343adb52013-12-18 06:02:17 -08002371 rl_result = EvalLoc(rl_dest, reg_class, true);
Vladimir Marko3bf7c602014-05-07 14:55:43 +01002372 LoadBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_result.reg, size);
buzbee695d13a2014-04-19 13:32:20 -07002373 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002374 StoreValueWide(rl_dest, rl_result);
2375 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002376 StoreValue(rl_dest, rl_result);
2377 }
2378}
2379
2380/*
2381 * Generate array store
2382 *
2383 */
2384void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07002385 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Mark Mendellca541342014-10-15 16:59:49 -04002386 RegisterClass reg_class = RegClassForFieldLoadStore(size, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002387 int len_offset = mirror::Array::LengthOffset().Int32Value();
2388 int data_offset;
2389
buzbee695d13a2014-04-19 13:32:20 -07002390 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002391 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
2392 } else {
2393 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
2394 }
2395
buzbeea0cd2d72014-06-01 09:33:49 -07002396 rl_array = LoadValue(rl_array, kRefReg);
Mark Mendell343adb52013-12-18 06:02:17 -08002397 bool constant_index = rl_index.is_const;
2398 int32_t constant_index_value = 0;
2399 if (!constant_index) {
2400 rl_index = LoadValue(rl_index, kCoreReg);
2401 } else {
2402 // If index is constant, just fold it into the data offset
2403 constant_index_value = mir_graph_->ConstantValue(rl_index);
2404 data_offset += constant_index_value << scale;
2405 // treat as non array below
buzbee2700f7e2014-03-07 09:46:20 -08002406 rl_index.reg = RegStorage::InvalidReg();
Mark Mendell343adb52013-12-18 06:02:17 -08002407 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002408
2409 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08002410 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002411
2412 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -08002413 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07002414 GenArrayBoundsCheck(constant_index_value, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002415 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07002416 GenArrayBoundsCheck(rl_index.reg, rl_array.reg, len_offset);
Mark Mendell343adb52013-12-18 06:02:17 -08002417 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002418 }
buzbee695d13a2014-04-19 13:32:20 -07002419 if ((size == k64) || (size == kDouble)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002420 rl_src = LoadValueWide(rl_src, reg_class);
2421 } else {
2422 rl_src = LoadValue(rl_src, reg_class);
2423 }
2424 // If the src reg can't be byte accessed, move it to a temp first.
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002425 if ((size == kSignedByte || size == kUnsignedByte) && !IsByteRegister(rl_src.reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08002426 RegStorage temp = AllocTemp();
2427 OpRegCopy(temp, rl_src.reg);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002428 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, temp, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002429 } else {
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07002430 StoreBaseIndexedDisp(rl_array.reg, rl_index.reg, scale, data_offset, rl_src.reg, size, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002431 }
Ian Rogersa9a82542013-10-04 11:17:26 -07002432 if (card_mark) {
Ian Rogers773aab12013-10-14 13:50:10 -07002433 // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
Mark Mendell343adb52013-12-18 06:02:17 -08002434 if (!constant_index) {
buzbee091cc402014-03-31 10:14:40 -07002435 FreeTemp(rl_index.reg);
Mark Mendell343adb52013-12-18 06:02:17 -08002436 }
Vladimir Marko743b98c2014-11-24 19:45:41 +00002437 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002438 }
2439}
2440
Mark Mendell4708dcd2014-01-22 09:05:18 -08002441RegLocation X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002442 RegLocation rl_src, int shift_amount, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002443 UNUSED(flags);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002444 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Elena Sayapinadd644502014-07-01 18:39:52 +07002445 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002446 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
2447 switch (opcode) {
2448 case Instruction::SHL_LONG:
2449 case Instruction::SHL_LONG_2ADDR:
2450 op = kOpLsl;
2451 break;
2452 case Instruction::SHR_LONG:
2453 case Instruction::SHR_LONG_2ADDR:
2454 op = kOpAsr;
2455 break;
2456 case Instruction::USHR_LONG:
2457 case Instruction::USHR_LONG_2ADDR:
2458 op = kOpLsr;
2459 break;
2460 default:
2461 LOG(FATAL) << "Unexpected case";
2462 }
2463 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
2464 } else {
2465 switch (opcode) {
2466 case Instruction::SHL_LONG:
2467 case Instruction::SHL_LONG_2ADDR:
2468 DCHECK_NE(shift_amount, 1); // Prevent a double store from happening.
2469 if (shift_amount == 32) {
2470 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2471 LoadConstant(rl_result.reg.GetLow(), 0);
2472 } else if (shift_amount > 31) {
2473 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetLow());
2474 NewLIR2(kX86Sal32RI, rl_result.reg.GetHighReg(), shift_amount - 32);
2475 LoadConstant(rl_result.reg.GetLow(), 0);
2476 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002477 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002478 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2479 NewLIR3(kX86Shld32RRI, rl_result.reg.GetHighReg(), rl_result.reg.GetLowReg(),
2480 shift_amount);
2481 NewLIR2(kX86Sal32RI, rl_result.reg.GetLowReg(), shift_amount);
2482 }
2483 break;
2484 case Instruction::SHR_LONG:
2485 case Instruction::SHR_LONG_2ADDR:
2486 if (shift_amount == 32) {
2487 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2488 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2489 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2490 } else if (shift_amount > 31) {
2491 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2492 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2493 NewLIR2(kX86Sar32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2494 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), 31);
2495 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002496 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002497 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2498 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2499 shift_amount);
2500 NewLIR2(kX86Sar32RI, rl_result.reg.GetHighReg(), shift_amount);
2501 }
2502 break;
2503 case Instruction::USHR_LONG:
2504 case Instruction::USHR_LONG_2ADDR:
2505 if (shift_amount == 32) {
2506 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2507 LoadConstant(rl_result.reg.GetHigh(), 0);
2508 } else if (shift_amount > 31) {
2509 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
2510 NewLIR2(kX86Shr32RI, rl_result.reg.GetLowReg(), shift_amount - 32);
2511 LoadConstant(rl_result.reg.GetHigh(), 0);
2512 } else {
Mark Mendellb9b9d662014-06-16 13:03:42 -04002513 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetLow());
Chao-ying Fua0147762014-06-06 18:38:49 -07002514 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg.GetHigh());
2515 NewLIR3(kX86Shrd32RRI, rl_result.reg.GetLowReg(), rl_result.reg.GetHighReg(),
2516 shift_amount);
2517 NewLIR2(kX86Shr32RI, rl_result.reg.GetHighReg(), shift_amount);
2518 }
2519 break;
2520 default:
2521 LOG(FATAL) << "Unexpected case";
2522 }
Mark Mendell4708dcd2014-01-22 09:05:18 -08002523 }
2524 return rl_result;
2525}
2526
Brian Carlstrom7940e442013-07-12 13:46:57 -07002527void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002528 RegLocation rl_src, RegLocation rl_shift, int flags) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002529 // Per spec, we only care about low 6 bits of shift amount.
2530 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
2531 if (shift_amount == 0) {
2532 rl_src = LoadValueWide(rl_src, kCoreReg);
2533 StoreValueWide(rl_dest, rl_src);
2534 return;
2535 } else if (shift_amount == 1 &&
2536 (opcode == Instruction::SHL_LONG || opcode == Instruction::SHL_LONG_2ADDR)) {
2537 // Need to handle this here to avoid calling StoreValueWide twice.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002538 GenArithOpLong(Instruction::ADD_LONG, rl_dest, rl_src, rl_src, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002539 return;
2540 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07002541 if (PartiallyIntersects(rl_src, rl_dest)) {
Mark Mendell4708dcd2014-01-22 09:05:18 -08002542 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
2543 return;
2544 }
2545 rl_src = LoadValueWide(rl_src, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002546 RegLocation rl_result = GenShiftImmOpLong(opcode, rl_dest, rl_src, shift_amount, flags);
Mark Mendell4708dcd2014-01-22 09:05:18 -08002547 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002548}
2549
2550void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002551 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
2552 int flags) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002553 bool isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002554 switch (opcode) {
2555 case Instruction::ADD_LONG:
2556 case Instruction::AND_LONG:
2557 case Instruction::OR_LONG:
2558 case Instruction::XOR_LONG:
2559 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002560 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002561 } else {
2562 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002563 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002564 }
2565 break;
2566 case Instruction::SUB_LONG:
2567 case Instruction::SUB_LONG_2ADDR:
2568 if (rl_src2.is_const) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002569 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002570 } else {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002571 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002572 isConstSuccess = true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002573 }
2574 break;
2575 case Instruction::ADD_LONG_2ADDR:
2576 case Instruction::OR_LONG_2ADDR:
2577 case Instruction::XOR_LONG_2ADDR:
2578 case Instruction::AND_LONG_2ADDR:
2579 if (rl_src2.is_const) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04002580 if (GenerateTwoOperandInstructions()) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002581 isConstSuccess = GenLongImm(rl_dest, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002582 } else {
Chao-ying Fua0147762014-06-06 18:38:49 -07002583 isConstSuccess = GenLongLongImm(rl_dest, rl_src1, rl_src2, opcode);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002584 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002585 } else {
2586 DCHECK(rl_src1.is_const);
Chao-ying Fua0147762014-06-06 18:38:49 -07002587 isConstSuccess = GenLongLongImm(rl_dest, rl_src2, rl_src1, opcode);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002588 }
2589 break;
2590 default:
Chao-ying Fua0147762014-06-06 18:38:49 -07002591 isConstSuccess = false;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002592 break;
2593 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002594
2595 if (!isConstSuccess) {
2596 // Default - bail to non-const handler.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002597 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Chao-ying Fua0147762014-06-06 18:38:49 -07002598 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002599}
2600
2601bool X86Mir2Lir::IsNoOp(Instruction::Code op, int32_t value) {
2602 switch (op) {
2603 case Instruction::AND_LONG_2ADDR:
2604 case Instruction::AND_LONG:
2605 return value == -1;
2606 case Instruction::OR_LONG:
2607 case Instruction::OR_LONG_2ADDR:
2608 case Instruction::XOR_LONG:
2609 case Instruction::XOR_LONG_2ADDR:
2610 return value == 0;
2611 default:
2612 return false;
2613 }
2614}
2615
2616X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
2617 bool is_high_op) {
2618 bool rhs_in_mem = rhs.location != kLocPhysReg;
2619 bool dest_in_mem = dest.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002620 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002621 DCHECK(!rhs_in_mem || !dest_in_mem);
2622 switch (op) {
2623 case Instruction::ADD_LONG:
2624 case Instruction::ADD_LONG_2ADDR:
2625 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002626 return is64Bit ? kX86Add64MR : is_high_op ? kX86Adc32MR : kX86Add32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002627 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002628 return is64Bit ? kX86Add64RM : is_high_op ? kX86Adc32RM : kX86Add32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002629 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002630 return is64Bit ? kX86Add64RR : is_high_op ? kX86Adc32RR : kX86Add32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002631 case Instruction::SUB_LONG:
2632 case Instruction::SUB_LONG_2ADDR:
2633 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002634 return is64Bit ? kX86Sub64MR : is_high_op ? kX86Sbb32MR : kX86Sub32MR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002635 } else if (rhs_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002636 return is64Bit ? kX86Sub64RM : is_high_op ? kX86Sbb32RM : kX86Sub32RM;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002637 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002638 return is64Bit ? kX86Sub64RR : is_high_op ? kX86Sbb32RR : kX86Sub32RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002639 case Instruction::AND_LONG_2ADDR:
2640 case Instruction::AND_LONG:
2641 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002642 return is64Bit ? kX86And64MR : kX86And32MR;
2643 }
2644 if (is64Bit) {
2645 return rhs_in_mem ? kX86And64RM : kX86And64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002646 }
2647 return rhs_in_mem ? kX86And32RM : kX86And32RR;
2648 case Instruction::OR_LONG:
2649 case Instruction::OR_LONG_2ADDR:
2650 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002651 return is64Bit ? kX86Or64MR : kX86Or32MR;
2652 }
2653 if (is64Bit) {
2654 return rhs_in_mem ? kX86Or64RM : kX86Or64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002655 }
2656 return rhs_in_mem ? kX86Or32RM : kX86Or32RR;
2657 case Instruction::XOR_LONG:
2658 case Instruction::XOR_LONG_2ADDR:
2659 if (dest_in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002660 return is64Bit ? kX86Xor64MR : kX86Xor32MR;
2661 }
2662 if (is64Bit) {
2663 return rhs_in_mem ? kX86Xor64RM : kX86Xor64RR;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002664 }
2665 return rhs_in_mem ? kX86Xor32RM : kX86Xor32RR;
2666 default:
2667 LOG(FATAL) << "Unexpected opcode: " << op;
2668 return kX86Add32RR;
2669 }
2670}
2671
2672X86OpCode X86Mir2Lir::GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op,
2673 int32_t value) {
2674 bool in_mem = loc.location != kLocPhysReg;
Elena Sayapinadd644502014-07-01 18:39:52 +07002675 bool is64Bit = cu_->target64;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002676 bool byte_imm = IS_SIMM8(value);
buzbee091cc402014-03-31 10:14:40 -07002677 DCHECK(in_mem || !loc.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002678 switch (op) {
2679 case Instruction::ADD_LONG:
2680 case Instruction::ADD_LONG_2ADDR:
2681 if (byte_imm) {
2682 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002683 return is64Bit ? kX86Add64MI8 : is_high_op ? kX86Adc32MI8 : kX86Add32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002684 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002685 return is64Bit ? kX86Add64RI8 : is_high_op ? kX86Adc32RI8 : kX86Add32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002686 }
2687 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002688 return is64Bit ? kX86Add64MI : is_high_op ? kX86Adc32MI : kX86Add32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002689 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002690 return is64Bit ? kX86Add64RI : is_high_op ? kX86Adc32RI : kX86Add32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002691 case Instruction::SUB_LONG:
2692 case Instruction::SUB_LONG_2ADDR:
2693 if (byte_imm) {
2694 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002695 return is64Bit ? kX86Sub64MI8 : is_high_op ? kX86Sbb32MI8 : kX86Sub32MI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002696 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002697 return is64Bit ? kX86Sub64RI8 : is_high_op ? kX86Sbb32RI8 : kX86Sub32RI8;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002698 }
2699 if (in_mem) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002700 return is64Bit ? kX86Sub64MI : is_high_op ? kX86Sbb32MI : kX86Sub32MI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002701 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002702 return is64Bit ? kX86Sub64RI : is_high_op ? kX86Sbb32RI : kX86Sub32RI;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002703 case Instruction::AND_LONG_2ADDR:
2704 case Instruction::AND_LONG:
2705 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002706 if (is64Bit) {
2707 return in_mem ? kX86And64MI8 : kX86And64RI8;
2708 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002709 return in_mem ? kX86And32MI8 : kX86And32RI8;
2710 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002711 if (is64Bit) {
2712 return in_mem ? kX86And64MI : kX86And64RI;
2713 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002714 return in_mem ? kX86And32MI : kX86And32RI;
2715 case Instruction::OR_LONG:
2716 case Instruction::OR_LONG_2ADDR:
2717 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002718 if (is64Bit) {
2719 return in_mem ? kX86Or64MI8 : kX86Or64RI8;
2720 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002721 return in_mem ? kX86Or32MI8 : kX86Or32RI8;
2722 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002723 if (is64Bit) {
2724 return in_mem ? kX86Or64MI : kX86Or64RI;
2725 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002726 return in_mem ? kX86Or32MI : kX86Or32RI;
2727 case Instruction::XOR_LONG:
2728 case Instruction::XOR_LONG_2ADDR:
2729 if (byte_imm) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002730 if (is64Bit) {
2731 return in_mem ? kX86Xor64MI8 : kX86Xor64RI8;
2732 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002733 return in_mem ? kX86Xor32MI8 : kX86Xor32RI8;
2734 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07002735 if (is64Bit) {
2736 return in_mem ? kX86Xor64MI : kX86Xor64RI;
2737 }
Mark Mendelle02d48f2014-01-15 11:19:23 -08002738 return in_mem ? kX86Xor32MI : kX86Xor32RI;
2739 default:
2740 LOG(FATAL) << "Unexpected opcode: " << op;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002741 UNREACHABLE();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002742 }
2743}
2744
Chao-ying Fua0147762014-06-06 18:38:49 -07002745bool X86Mir2Lir::GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002746 DCHECK(rl_src.is_const);
2747 int64_t val = mir_graph_->ConstantValueWide(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07002748
Elena Sayapinadd644502014-07-01 18:39:52 +07002749 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002750 // We can do with imm only if it fits 32 bit
2751 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2752 return false;
2753 }
2754
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002755 rl_dest = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002756
2757 if ((rl_dest.location == kLocDalvikFrame) ||
2758 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002759 int r_base = rs_rX86_SP_32.GetReg();
Chao-ying Fua0147762014-06-06 18:38:49 -07002760 int displacement = SRegOffset(rl_dest.s_reg_low);
2761
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002762 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002763 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
2764 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val);
2765 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2766 true /* is_load */, true /* is64bit */);
2767 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
2768 false /* is_load */, true /* is64bit */);
2769 return true;
2770 }
2771
2772 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2773 DCHECK_EQ(rl_result.location, kLocPhysReg);
2774 DCHECK(!rl_result.reg.IsFloat());
2775
2776 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2777 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2778
2779 StoreValueWide(rl_dest, rl_result);
2780 return true;
2781 }
2782
Mark Mendelle02d48f2014-01-15 11:19:23 -08002783 int32_t val_lo = Low32Bits(val);
2784 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002785 rl_dest = UpdateLocWideTyped(rl_dest);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002786
2787 // Can we just do this into memory?
2788 if ((rl_dest.location == kLocDalvikFrame) ||
2789 (rl_dest.location == kLocCompilerTemp)) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002790 int r_base = rs_rX86_SP_32.GetReg();
Mark Mendelle02d48f2014-01-15 11:19:23 -08002791 int displacement = SRegOffset(rl_dest.s_reg_low);
2792
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002793 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002794 if (!IsNoOp(op, val_lo)) {
2795 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002796 LIR *lir = NewLIR3(x86op, r_base, displacement + LOWORD_OFFSET, val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002797 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002798 true /* is_load */, true /* is64bit */);
2799 AnnotateDalvikRegAccess(lir, (displacement + LOWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002800 false /* is_load */, true /* is64bit */);
2801 }
2802 if (!IsNoOp(op, val_hi)) {
2803 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
buzbee2700f7e2014-03-07 09:46:20 -08002804 LIR *lir = NewLIR3(x86op, r_base, displacement + HIWORD_OFFSET, val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002805 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Serguei Katkov217fe732014-03-27 14:41:56 +07002806 true /* is_load */, true /* is64bit */);
2807 AnnotateDalvikRegAccess(lir, (displacement + HIWORD_OFFSET) >> 2,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002808 false /* is_load */, true /* is64bit */);
2809 }
Chao-ying Fua0147762014-06-06 18:38:49 -07002810 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002811 }
2812
2813 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2814 DCHECK_EQ(rl_result.location, kLocPhysReg);
buzbee091cc402014-03-31 10:14:40 -07002815 DCHECK(!rl_result.reg.IsFloat());
Mark Mendelle02d48f2014-01-15 11:19:23 -08002816
2817 if (!IsNoOp(op, val_lo)) {
2818 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002819 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002820 }
2821 if (!IsNoOp(op, val_hi)) {
2822 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002823 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002824 }
2825 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002826 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002827}
2828
Chao-ying Fua0147762014-06-06 18:38:49 -07002829bool X86Mir2Lir::GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1,
Mark Mendelle02d48f2014-01-15 11:19:23 -08002830 RegLocation rl_src2, Instruction::Code op) {
2831 DCHECK(rl_src2.is_const);
2832 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Chao-ying Fua0147762014-06-06 18:38:49 -07002833
Elena Sayapinadd644502014-07-01 18:39:52 +07002834 if (cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07002835 // We can do with imm only if it fits 32 bit
2836 if (val != (static_cast<int64_t>(static_cast<int32_t>(val)))) {
2837 return false;
2838 }
2839 if (rl_dest.location == kLocPhysReg &&
2840 rl_src1.location == kLocPhysReg && !rl_dest.reg.IsFloat()) {
2841 X86OpCode x86op = GetOpcode(op, rl_dest, false, val);
Dmitry Petrochenko3157f9a2014-06-18 19:11:41 +07002842 OpRegCopy(rl_dest.reg, rl_src1.reg);
Chao-ying Fua0147762014-06-06 18:38:49 -07002843 NewLIR2(x86op, rl_dest.reg.GetReg(), val);
2844 StoreFinalValueWide(rl_dest, rl_dest);
2845 return true;
2846 }
2847
2848 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2849 // We need the values to be in a temporary
2850 RegLocation rl_result = ForceTempWide(rl_src1);
2851
2852 X86OpCode x86op = GetOpcode(op, rl_result, false, val);
2853 NewLIR2(x86op, rl_result.reg.GetReg(), val);
2854
2855 StoreFinalValueWide(rl_dest, rl_result);
2856 return true;
2857 }
2858
Mark Mendelle02d48f2014-01-15 11:19:23 -08002859 int32_t val_lo = Low32Bits(val);
2860 int32_t val_hi = High32Bits(val);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002861 rl_dest = UpdateLocWideTyped(rl_dest);
2862 rl_src1 = UpdateLocWideTyped(rl_src1);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002863
2864 // Can we do this directly into the destination registers?
2865 if (rl_dest.location == kLocPhysReg && rl_src1.location == kLocPhysReg &&
buzbee2700f7e2014-03-07 09:46:20 -08002866 rl_dest.reg.GetLowReg() == rl_src1.reg.GetLowReg() &&
buzbee091cc402014-03-31 10:14:40 -07002867 rl_dest.reg.GetHighReg() == rl_src1.reg.GetHighReg() && !rl_dest.reg.IsFloat()) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08002868 if (!IsNoOp(op, val_lo)) {
2869 X86OpCode x86op = GetOpcode(op, rl_dest, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002870 NewLIR2(x86op, rl_dest.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002871 }
2872 if (!IsNoOp(op, val_hi)) {
2873 X86OpCode x86op = GetOpcode(op, rl_dest, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002874 NewLIR2(x86op, rl_dest.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002875 }
Maxim Kazantsev653f2bf2014-02-13 15:11:17 +07002876
2877 StoreFinalValueWide(rl_dest, rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07002878 return true;
Mark Mendelle02d48f2014-01-15 11:19:23 -08002879 }
2880
2881 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
2882 DCHECK_EQ(rl_src1.location, kLocPhysReg);
2883
2884 // We need the values to be in a temporary
2885 RegLocation rl_result = ForceTempWide(rl_src1);
2886 if (!IsNoOp(op, val_lo)) {
2887 X86OpCode x86op = GetOpcode(op, rl_result, false, val_lo);
buzbee2700f7e2014-03-07 09:46:20 -08002888 NewLIR2(x86op, rl_result.reg.GetLowReg(), val_lo);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002889 }
2890 if (!IsNoOp(op, val_hi)) {
2891 X86OpCode x86op = GetOpcode(op, rl_result, true, val_hi);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002892 NewLIR2(x86op, rl_result.reg.GetHighReg(), val_hi);
Mark Mendelle02d48f2014-01-15 11:19:23 -08002893 }
2894
2895 StoreFinalValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07002896 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002897}
2898
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002899// For final classes there are no sub-classes to check and so we can answer the instance-of
2900// question with simple comparisons. Use compares to memory and SETEQ to optimize for x86.
2901void X86Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx,
2902 RegLocation rl_dest, RegLocation rl_src) {
buzbeea0cd2d72014-06-01 09:33:49 -07002903 RegLocation object = LoadValue(rl_src, kRefReg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002904 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002905 RegStorage result_reg = rl_result.reg;
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002906
Chao-ying Fu7e399fd2014-06-10 18:11:11 -07002907 // For 32-bit, SETcc only works with EAX..EDX.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002908 RegStorage object_32reg = object.reg.Is64Bit() ? As32BitReg(object.reg) : object.reg;
Dmitry Petrochenko407f5c12014-07-01 01:21:38 +07002909 if (result_reg.GetRegNum() == object_32reg.GetRegNum() || !IsByteRegister(result_reg)) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04002910 result_reg = AllocateByteRegister();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002911 }
2912
2913 // Assume that there is no match.
2914 LoadConstant(result_reg, 0);
buzbee2700f7e2014-03-07 09:46:20 -08002915 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002916
Mark Mendellade54a22014-06-09 12:49:55 -04002917 // We will use this register to compare to memory below.
2918 // References are 32 bit in memory, and 64 bit in registers (in 64 bit mode).
2919 // For this reason, force allocation of a 32 bit register to use, so that the
2920 // compare to memory will be done using a 32 bit comparision.
2921 // The LoadRefDisp(s) below will work normally, even in 64 bit mode.
2922 RegStorage check_class = AllocTemp();
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002923
2924 // If Method* is already in a register, we can save a copy.
2925 RegLocation rl_method = mir_graph_->GetMethodLoc();
Andreas Gampeccc60262014-07-04 18:02:38 -07002926 int32_t offset_of_type = mirror::Array::DataOffset(
2927 sizeof(mirror::HeapReference<mirror::Class*>)).Int32Value() +
2928 (sizeof(mirror::HeapReference<mirror::Class*>) * type_idx);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002929
2930 if (rl_method.location == kLocPhysReg) {
2931 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07002932 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002933 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002934 } else {
buzbee695d13a2014-04-19 13:32:20 -07002935 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002936 check_class, kNotVolatile);
2937 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002938 }
2939 } else {
2940 LoadCurrMethodDirect(check_class);
2941 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07002942 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002943 check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002944 } else {
buzbee695d13a2014-04-19 13:32:20 -07002945 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00002946 check_class, kNotVolatile);
2947 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002948 }
2949 }
2950
2951 // Compare the computed class to the class in the object.
2952 DCHECK_EQ(object.location, kLocPhysReg);
buzbee2700f7e2014-03-07 09:46:20 -08002953 OpRegMem(kOpCmp, check_class, object.reg, mirror::Object::ClassOffset().Int32Value());
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002954
2955 // Set the low byte of the result to 0 or 1 from the compare condition code.
buzbee2700f7e2014-03-07 09:46:20 -08002956 NewLIR2(kX86Set8R, result_reg.GetReg(), kX86CondEq);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002957
2958 LIR* target = NewLIR0(kPseudoTargetLabel);
2959 null_branchover->target = target;
2960 FreeTemp(check_class);
2961 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08002962 OpRegCopy(rl_result.reg, result_reg);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08002963 FreeTemp(result_reg);
2964 }
2965 StoreValue(rl_dest, rl_result);
2966}
2967
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002968void X86Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002969 RegLocation rl_lhs, RegLocation rl_rhs, int flags) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002970 OpKind op = kOpBkpt;
2971 bool is_div_rem = false;
2972 bool unary = false;
2973 bool shift_op = false;
2974 bool is_two_addr = false;
2975 RegLocation rl_result;
2976 switch (opcode) {
2977 case Instruction::NEG_INT:
2978 op = kOpNeg;
2979 unary = true;
2980 break;
2981 case Instruction::NOT_INT:
2982 op = kOpMvn;
2983 unary = true;
2984 break;
2985 case Instruction::ADD_INT_2ADDR:
2986 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002987 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002988 case Instruction::ADD_INT:
2989 op = kOpAdd;
2990 break;
2991 case Instruction::SUB_INT_2ADDR:
2992 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002993 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08002994 case Instruction::SUB_INT:
2995 op = kOpSub;
2996 break;
2997 case Instruction::MUL_INT_2ADDR:
2998 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002999 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003000 case Instruction::MUL_INT:
3001 op = kOpMul;
3002 break;
3003 case Instruction::DIV_INT_2ADDR:
3004 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003005 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003006 case Instruction::DIV_INT:
3007 op = kOpDiv;
3008 is_div_rem = true;
3009 break;
3010 /* NOTE: returns in kArg1 */
3011 case Instruction::REM_INT_2ADDR:
3012 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003013 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003014 case Instruction::REM_INT:
3015 op = kOpRem;
3016 is_div_rem = true;
3017 break;
3018 case Instruction::AND_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::AND_INT:
3022 op = kOpAnd;
3023 break;
3024 case Instruction::OR_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::OR_INT:
3028 op = kOpOr;
3029 break;
3030 case Instruction::XOR_INT_2ADDR:
3031 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003032 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003033 case Instruction::XOR_INT:
3034 op = kOpXor;
3035 break;
3036 case Instruction::SHL_INT_2ADDR:
3037 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003038 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003039 case Instruction::SHL_INT:
3040 shift_op = true;
3041 op = kOpLsl;
3042 break;
3043 case Instruction::SHR_INT_2ADDR:
3044 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003045 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003046 case Instruction::SHR_INT:
3047 shift_op = true;
3048 op = kOpAsr;
3049 break;
3050 case Instruction::USHR_INT_2ADDR:
3051 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003052 FALLTHROUGH_INTENDED;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003053 case Instruction::USHR_INT:
3054 shift_op = true;
3055 op = kOpLsr;
3056 break;
3057 default:
3058 LOG(FATAL) << "Invalid word arith op: " << opcode;
3059 }
3060
Mark Mendelle87f9b52014-04-30 14:13:18 -04003061 // Can we convert to a two address instruction?
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003062 if (!is_two_addr &&
3063 (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3064 mir_graph_->SRegToVReg(rl_lhs.s_reg_low))) {
Mark Mendelle87f9b52014-04-30 14:13:18 -04003065 is_two_addr = true;
3066 }
3067
3068 if (!GenerateTwoOperandInstructions()) {
3069 is_two_addr = false;
3070 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003071
3072 // Get the div/rem stuff out of the way.
3073 if (is_div_rem) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07003074 rl_result = GenDivRem(rl_dest, rl_lhs, rl_rhs, op == kOpDiv, flags);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003075 StoreValue(rl_dest, rl_result);
3076 return;
3077 }
3078
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003079 // If we generate any memory access below, it will reference a dalvik reg.
3080 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
3081
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003082 if (unary) {
3083 rl_lhs = LoadValue(rl_lhs, kCoreReg);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003084 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003085 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003086 OpRegReg(op, rl_result.reg, rl_lhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003087 } else {
3088 if (shift_op) {
3089 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003090 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003091 LoadValueDirectFixed(rl_rhs, t_reg);
3092 if (is_two_addr) {
3093 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003094 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003095 if (rl_result.location != kLocPhysReg) {
3096 // Okay, we can do this into memory
buzbee2700f7e2014-03-07 09:46:20 -08003097 OpMemReg(op, rl_result, t_reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003098 FreeTemp(t_reg);
3099 return;
buzbee091cc402014-03-31 10:14:40 -07003100 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003101 // Can do this directly into the result register
buzbee2700f7e2014-03-07 09:46:20 -08003102 OpRegReg(op, rl_result.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003103 FreeTemp(t_reg);
3104 StoreFinalValue(rl_dest, rl_result);
3105 return;
3106 }
3107 }
3108 // Three address form, or we can't do directly.
3109 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3110 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003111 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, t_reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003112 FreeTemp(t_reg);
3113 } else {
3114 // Multiply is 3 operand only (sort of).
3115 if (is_two_addr && op != kOpMul) {
3116 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003117 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003118 if (rl_result.location == kLocPhysReg) {
Serguei Katkov366f8ae2014-04-15 16:55:26 +07003119 // Ensure res is in a core reg
3120 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003121 // Can we do this from memory directly?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003122 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003123 if (rl_rhs.location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08003124 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003125 StoreFinalValue(rl_dest, rl_result);
3126 return;
buzbee091cc402014-03-31 10:14:40 -07003127 } else if (!rl_rhs.reg.IsFloat()) {
buzbee2700f7e2014-03-07 09:46:20 -08003128 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003129 StoreFinalValue(rl_dest, rl_result);
3130 return;
3131 }
3132 }
3133 rl_rhs = LoadValue(rl_rhs, kCoreReg);
Serguei Katkovd293fb42014-05-19 15:45:42 +07003134 // It might happen rl_rhs and rl_dest are the same VR
3135 // in this case rl_dest is in reg after LoadValue while
3136 // rl_result is not updated yet, so do this
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003137 rl_result = UpdateLocTyped(rl_dest);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003138 if (rl_result.location != kLocPhysReg) {
3139 // Okay, we can do this into memory.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00003140 OpMemReg(op, rl_result, rl_rhs.reg.GetReg());
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003141 return;
buzbee091cc402014-03-31 10:14:40 -07003142 } else if (!rl_result.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003143 // Can do this directly into the result register.
buzbee2700f7e2014-03-07 09:46:20 -08003144 OpRegReg(op, rl_result.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003145 StoreFinalValue(rl_dest, rl_result);
3146 return;
3147 } else {
3148 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3149 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003150 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003151 }
3152 } else {
3153 // Try to use reg/memory instructions.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003154 rl_lhs = UpdateLocTyped(rl_lhs);
3155 rl_rhs = UpdateLocTyped(rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003156 // We can't optimize with FP registers.
3157 if (!IsOperationSafeWithoutTemps(rl_lhs, rl_rhs)) {
3158 // Something is difficult, so fall back to the standard case.
3159 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3160 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3161 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003162 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003163 } else {
3164 // We can optimize by moving to result and using memory operands.
3165 if (rl_rhs.location != kLocPhysReg) {
3166 // Force LHS into result.
Serguei Katkov66da1362014-03-14 13:33:33 +07003167 // We should be careful with order here
3168 // If rl_dest and rl_lhs points to the same VR we should load first
3169 // If the are different we should find a register first for dest
Chao-ying Fua0147762014-06-06 18:38:49 -07003170 if (mir_graph_->SRegToVReg(rl_dest.s_reg_low) ==
3171 mir_graph_->SRegToVReg(rl_lhs.s_reg_low)) {
Serguei Katkov66da1362014-03-14 13:33:33 +07003172 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3173 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendelle87f9b52014-04-30 14:13:18 -04003174 // No-op if these are the same.
3175 OpRegCopy(rl_result.reg, rl_lhs.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003176 } else {
3177 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003178 LoadValueDirect(rl_lhs, rl_result.reg);
Serguei Katkov66da1362014-03-14 13:33:33 +07003179 }
buzbee2700f7e2014-03-07 09:46:20 -08003180 OpRegMem(op, rl_result.reg, rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003181 } else if (rl_lhs.location != kLocPhysReg) {
3182 // RHS is in a register; LHS is in memory.
3183 if (op != kOpSub) {
3184 // Force RHS into result and operate on memory.
3185 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003186 OpRegCopy(rl_result.reg, rl_rhs.reg);
3187 OpRegMem(op, rl_result.reg, rl_lhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003188 } else {
3189 // Subtraction isn't commutative.
3190 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3191 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3192 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003193 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003194 }
3195 } else {
3196 // Both are in registers.
3197 rl_lhs = LoadValue(rl_lhs, kCoreReg);
3198 rl_rhs = LoadValue(rl_rhs, kCoreReg);
3199 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08003200 OpRegRegReg(op, rl_result.reg, rl_lhs.reg, rl_rhs.reg);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003201 }
3202 }
3203 }
3204 }
3205 }
3206 StoreValue(rl_dest, rl_result);
3207}
3208
3209bool X86Mir2Lir::IsOperationSafeWithoutTemps(RegLocation rl_lhs, RegLocation rl_rhs) {
3210 // If we have non-core registers, then we can't do good things.
buzbee091cc402014-03-31 10:14:40 -07003211 if (rl_lhs.location == kLocPhysReg && rl_lhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003212 return false;
3213 }
buzbee091cc402014-03-31 10:14:40 -07003214 if (rl_rhs.location == kLocPhysReg && rl_rhs.reg.IsFloat()) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08003215 return false;
3216 }
3217
3218 // Everything will be fine :-).
3219 return true;
3220}
Chao-ying Fua0147762014-06-06 18:38:49 -07003221
3222void X86Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003223 if (!cu_->target64) {
Chao-ying Fua0147762014-06-06 18:38:49 -07003224 Mir2Lir::GenIntToLong(rl_dest, rl_src);
3225 return;
3226 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003227 rl_src = UpdateLocTyped(rl_src);
Chao-ying Fua0147762014-06-06 18:38:49 -07003228 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
3229 if (rl_src.location == kLocPhysReg) {
3230 NewLIR2(kX86MovsxdRR, rl_result.reg.GetReg(), rl_src.reg.GetReg());
3231 } else {
3232 int displacement = SRegOffset(rl_src.s_reg_low);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003233 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08003234 LIR *m = NewLIR3(kX86MovsxdRM, rl_result.reg.GetReg(), rs_rX86_SP_32.GetReg(),
Chao-ying Fua0147762014-06-06 18:38:49 -07003235 displacement + LOWORD_OFFSET);
3236 AnnotateDalvikRegAccess(m, (displacement + LOWORD_OFFSET) >> 2,
3237 true /* is_load */, true /* is_64bit */);
3238 }
3239 StoreValueWide(rl_dest, rl_result);
3240}
3241
Yevgeny Rouban6af82062014-11-26 18:11:54 +06003242void X86Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
3243 rl_src = UpdateLocWide(rl_src);
3244 rl_src = NarrowRegLoc(rl_src);
3245 StoreValue(rl_dest, rl_src);
3246
3247 if (cu_->target64) {
3248 // if src and dest are in the same phys reg then StoreValue generates
3249 // no operation but we need explicit 32-bit mov R, R to clear
3250 // the higher 32-bits
3251 rl_dest = UpdateLoc(rl_dest);
3252 if (rl_src.location == kLocPhysReg && rl_dest.location == kLocPhysReg
3253 && IsSameReg(rl_src.reg, rl_dest.reg)) {
3254 LIR* copy_lir = OpRegCopyNoInsert(rl_dest.reg, rl_dest.reg);
3255 // remove nop flag set by OpRegCopyNoInsert if src == dest
3256 copy_lir->flags.is_nop = false;
3257 AppendLIR(copy_lir);
3258 }
3259 }
3260}
3261
Chao-ying Fua0147762014-06-06 18:38:49 -07003262void X86Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
3263 RegLocation rl_src1, RegLocation rl_shift) {
Elena Sayapinadd644502014-07-01 18:39:52 +07003264 if (!cu_->target64) {
Yixin Shouf40f8902014-08-14 14:10:32 -04003265 // Long shift operations in 32-bit. Use shld or shrd to create a 32-bit register filled from
3266 // the other half, shift the other half, if the shift amount is less than 32 we're done,
3267 // otherwise move one register to the other and place zero or sign bits in the other.
3268 LIR* branch;
3269 FlushAllRegs();
3270 LockCallTemps();
3271 LoadValueDirectFixed(rl_shift, rs_rCX);
3272 RegStorage r_tmp = RegStorage::MakeRegPair(rs_rAX, rs_rDX);
3273 LoadValueDirectWideFixed(rl_src1, r_tmp);
3274 switch (opcode) {
3275 case Instruction::SHL_LONG:
3276 case Instruction::SHL_LONG_2ADDR:
3277 NewLIR3(kX86Shld32RRC, r_tmp.GetHighReg(), r_tmp.GetLowReg(), rs_rCX.GetReg());
3278 NewLIR2(kX86Sal32RC, r_tmp.GetLowReg(), rs_rCX.GetReg());
3279 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3280 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3281 OpRegCopy(r_tmp.GetHigh(), r_tmp.GetLow());
3282 LoadConstant(r_tmp.GetLow(), 0);
3283 branch->target = NewLIR0(kPseudoTargetLabel);
3284 break;
3285 case Instruction::SHR_LONG:
3286 case Instruction::SHR_LONG_2ADDR:
3287 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(), rs_rCX.GetReg());
3288 NewLIR2(kX86Sar32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3289 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3290 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3291 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3292 NewLIR2(kX86Sar32RI, r_tmp.GetHighReg(), 31);
3293 branch->target = NewLIR0(kPseudoTargetLabel);
3294 break;
3295 case Instruction::USHR_LONG:
3296 case Instruction::USHR_LONG_2ADDR:
3297 NewLIR3(kX86Shrd32RRC, r_tmp.GetLowReg(), r_tmp.GetHighReg(),
3298 rs_rCX.GetReg());
3299 NewLIR2(kX86Shr32RC, r_tmp.GetHighReg(), rs_rCX.GetReg());
3300 NewLIR2(kX86Test8RI, rs_rCX.GetReg(), 32);
3301 branch = NewLIR2(kX86Jcc8, 0, kX86CondZ);
3302 OpRegCopy(r_tmp.GetLow(), r_tmp.GetHigh());
3303 LoadConstant(r_tmp.GetHigh(), 0);
3304 branch->target = NewLIR0(kPseudoTargetLabel);
3305 break;
3306 default:
3307 LOG(FATAL) << "Unexpected case: " << opcode;
3308 return;
3309 }
3310 RegLocation rl_result = LocCReturnWide();
3311 StoreValueWide(rl_dest, rl_result);
Chao-ying Fua0147762014-06-06 18:38:49 -07003312 return;
3313 }
3314
3315 bool is_two_addr = false;
3316 OpKind op = kOpBkpt;
3317 RegLocation rl_result;
3318
3319 switch (opcode) {
3320 case Instruction::SHL_LONG_2ADDR:
3321 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003322 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003323 case Instruction::SHL_LONG:
3324 op = kOpLsl;
3325 break;
3326 case Instruction::SHR_LONG_2ADDR:
3327 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003328 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003329 case Instruction::SHR_LONG:
3330 op = kOpAsr;
3331 break;
3332 case Instruction::USHR_LONG_2ADDR:
3333 is_two_addr = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003334 FALLTHROUGH_INTENDED;
Chao-ying Fua0147762014-06-06 18:38:49 -07003335 case Instruction::USHR_LONG:
3336 op = kOpLsr;
3337 break;
3338 default:
3339 op = kOpBkpt;
3340 }
3341
3342 // X86 doesn't require masking and must use ECX.
Andreas Gampeccc60262014-07-04 18:02:38 -07003343 RegStorage t_reg = TargetReg(kCount, kNotWide); // rCX
Chao-ying Fua0147762014-06-06 18:38:49 -07003344 LoadValueDirectFixed(rl_shift, t_reg);
3345 if (is_two_addr) {
3346 // Can we do this directly into memory?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003347 rl_result = UpdateLocWideTyped(rl_dest);
Chao-ying Fua0147762014-06-06 18:38:49 -07003348 if (rl_result.location != kLocPhysReg) {
3349 // Okay, we can do this into memory
Vladimir Marko8dea81c2014-06-06 14:50:36 +01003350 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua0147762014-06-06 18:38:49 -07003351 OpMemReg(op, rl_result, t_reg.GetReg());
3352 } else if (!rl_result.reg.IsFloat()) {
3353 // Can do this directly into the result register
3354 OpRegReg(op, rl_result.reg, t_reg);
3355 StoreFinalValueWide(rl_dest, rl_result);
3356 }
3357 } else {
3358 // Three address form, or we can't do directly.
3359 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
3360 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
3361 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
3362 StoreFinalValueWide(rl_dest, rl_result);
3363 }
3364
3365 FreeTemp(t_reg);
3366}
3367
Brian Carlstrom7940e442013-07-12 13:46:57 -07003368} // namespace art