blob: 893b98a49d37785693cd1b8a2bf60216f1c2d81d [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#include "codegen_x86.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080018
19#include "base/logging.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "dex/quick/mir_to_lir-inl.h"
Mark Mendell67c39c42014-01-31 17:28:00 -080021#include "dex/dataflow_iterator-inl.h"
Yixin Shou7071c8d2014-03-05 06:07:48 -050022#include "dex/quick/dex_file_method_inliner.h"
23#include "dex/quick/dex_file_to_method_inliner_map.h"
buzbeeb5860fb2014-06-21 15:31:01 -070024#include "dex/reg_storage_eq.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "driver/compiler_driver.h"
26#include "x86_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027
28namespace art {
29
30/* This file contains codegen for the X86 ISA */
31
buzbee2700f7e2014-03-07 09:46:20 -080032LIR* X86Mir2Lir::OpFpRegCopy(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070033 int opcode;
34 /* must be both DOUBLE or both not DOUBLE */
buzbee091cc402014-03-31 10:14:40 -070035 DCHECK(r_dest.IsFloat() || r_src.IsFloat());
36 DCHECK_EQ(r_dest.IsDouble(), r_src.IsDouble());
37 if (r_dest.IsDouble()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070038 opcode = kX86MovsdRR;
39 } else {
buzbee091cc402014-03-31 10:14:40 -070040 if (r_dest.IsSingle()) {
41 if (r_src.IsSingle()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 opcode = kX86MovssRR;
43 } else { // Fpr <- Gpr
44 opcode = kX86MovdxrRR;
45 }
46 } else { // Gpr <- Fpr
buzbee091cc402014-03-31 10:14:40 -070047 DCHECK(r_src.IsSingle()) << "Raw: 0x" << std::hex << r_src.GetRawBits();
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 opcode = kX86MovdrxRR;
49 }
50 }
51 DCHECK_NE((EncodingMap[opcode].flags & IS_BINARY_OP), 0ULL);
buzbee2700f7e2014-03-07 09:46:20 -080052 LIR* res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 if (r_dest == r_src) {
54 res->flags.is_nop = true;
55 }
56 return res;
57}
58
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070059bool X86Mir2Lir::InexpensiveConstantInt(int32_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070060 UNUSED(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 return true;
62}
63
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070064bool X86Mir2Lir::InexpensiveConstantFloat(int32_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070065 return value == 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -070066}
67
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070068bool X86Mir2Lir::InexpensiveConstantLong(int64_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070069 UNUSED(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 return true;
71}
72
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070073bool X86Mir2Lir::InexpensiveConstantDouble(int64_t value) {
Mark Mendell67c39c42014-01-31 17:28:00 -080074 return value == 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -070075}
76
77/*
78 * Load a immediate using a shortcut if possible; otherwise
79 * grab from the per-translation literal pool. If target is
80 * a high register, build constant into a low register and copy.
81 *
82 * No additional register clobbering operation performed. Use this version when
83 * 1) r_dest is freshly returned from AllocTemp or
84 * 2) The codegen is under fixed register usage
85 */
buzbee2700f7e2014-03-07 09:46:20 -080086LIR* X86Mir2Lir::LoadConstantNoClobber(RegStorage r_dest, int value) {
87 RegStorage r_dest_save = r_dest;
buzbee091cc402014-03-31 10:14:40 -070088 if (r_dest.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 if (value == 0) {
buzbee2700f7e2014-03-07 09:46:20 -080090 return NewLIR2(kX86XorpsRR, r_dest.GetReg(), r_dest.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 r_dest = AllocTemp();
93 }
94
95 LIR *res;
96 if (value == 0) {
buzbee2700f7e2014-03-07 09:46:20 -080097 res = NewLIR2(kX86Xor32RR, r_dest.GetReg(), r_dest.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 } else {
99 // Note, there is no byte immediate form of a 32 bit immediate move.
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700100 // 64-bit immediate is not supported by LIR structure
101 res = NewLIR2(kX86Mov32RI, r_dest.GetReg(), value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 }
103
buzbee091cc402014-03-31 10:14:40 -0700104 if (r_dest_save.IsFloat()) {
buzbee2700f7e2014-03-07 09:46:20 -0800105 NewLIR2(kX86MovdxrRR, r_dest_save.GetReg(), r_dest.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 FreeTemp(r_dest);
107 }
108
109 return res;
110}
111
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700112LIR* X86Mir2Lir::OpUnconditionalBranch(LIR* target) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700113 LIR* res = NewLIR1(kX86Jmp8, 0 /* offset to be patched during assembly*/);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 res->target = target;
115 return res;
116}
117
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700118LIR* X86Mir2Lir::OpCondBranch(ConditionCode cc, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 LIR* branch = NewLIR2(kX86Jcc8, 0 /* offset to be patched */,
120 X86ConditionEncoding(cc));
121 branch->target = target;
122 return branch;
123}
124
buzbee2700f7e2014-03-07 09:46:20 -0800125LIR* X86Mir2Lir::OpReg(OpKind op, RegStorage r_dest_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 X86OpCode opcode = kX86Bkpt;
127 switch (op) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700128 case kOpNeg: opcode = r_dest_src.Is64Bit() ? kX86Neg64R : kX86Neg32R; break;
129 case kOpNot: opcode = r_dest_src.Is64Bit() ? kX86Not64R : kX86Not32R; break;
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700130 case kOpRev: opcode = r_dest_src.Is64Bit() ? kX86Bswap64R : kX86Bswap32R; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 case kOpBlx: opcode = kX86CallR; break;
132 default:
133 LOG(FATAL) << "Bad case in OpReg " << op;
134 }
buzbee2700f7e2014-03-07 09:46:20 -0800135 return NewLIR1(opcode, r_dest_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136}
137
buzbee2700f7e2014-03-07 09:46:20 -0800138LIR* X86Mir2Lir::OpRegImm(OpKind op, RegStorage r_dest_src1, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 X86OpCode opcode = kX86Bkpt;
140 bool byte_imm = IS_SIMM8(value);
buzbee091cc402014-03-31 10:14:40 -0700141 DCHECK(!r_dest_src1.IsFloat());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700142 if (r_dest_src1.Is64Bit()) {
143 switch (op) {
144 case kOpAdd: opcode = byte_imm ? kX86Add64RI8 : kX86Add64RI; break;
145 case kOpSub: opcode = byte_imm ? kX86Sub64RI8 : kX86Sub64RI; break;
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700146 case kOpLsl: opcode = kX86Sal64RI; break;
147 case kOpLsr: opcode = kX86Shr64RI; break;
148 case kOpAsr: opcode = kX86Sar64RI; break;
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700149 case kOpCmp: opcode = byte_imm ? kX86Cmp64RI8 : kX86Cmp64RI; break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700150 default:
151 LOG(FATAL) << "Bad case in OpRegImm (64-bit) " << op;
152 }
153 } else {
154 switch (op) {
155 case kOpLsl: opcode = kX86Sal32RI; break;
156 case kOpLsr: opcode = kX86Shr32RI; break;
157 case kOpAsr: opcode = kX86Sar32RI; break;
158 case kOpAdd: opcode = byte_imm ? kX86Add32RI8 : kX86Add32RI; break;
159 case kOpOr: opcode = byte_imm ? kX86Or32RI8 : kX86Or32RI; break;
160 case kOpAdc: opcode = byte_imm ? kX86Adc32RI8 : kX86Adc32RI; break;
161 // case kOpSbb: opcode = kX86Sbb32RI; break;
162 case kOpAnd: opcode = byte_imm ? kX86And32RI8 : kX86And32RI; break;
163 case kOpSub: opcode = byte_imm ? kX86Sub32RI8 : kX86Sub32RI; break;
164 case kOpXor: opcode = byte_imm ? kX86Xor32RI8 : kX86Xor32RI; break;
165 case kOpCmp: opcode = byte_imm ? kX86Cmp32RI8 : kX86Cmp32RI; break;
166 case kOpMov:
167 /*
168 * Moving the constant zero into register can be specialized as an xor of the register.
169 * However, that sets eflags while the move does not. For that reason here, always do
170 * the move and if caller is flexible, they should be calling LoadConstantNoClobber instead.
171 */
172 opcode = kX86Mov32RI;
173 break;
174 case kOpMul:
175 opcode = byte_imm ? kX86Imul32RRI8 : kX86Imul32RRI;
176 return NewLIR3(opcode, r_dest_src1.GetReg(), r_dest_src1.GetReg(), value);
Mark Mendelle87f9b52014-04-30 14:13:18 -0400177 case kOp2Byte:
178 opcode = kX86Mov32RI;
179 value = static_cast<int8_t>(value);
180 break;
181 case kOp2Short:
182 opcode = kX86Mov32RI;
183 value = static_cast<int16_t>(value);
184 break;
185 case kOp2Char:
186 opcode = kX86Mov32RI;
187 value = static_cast<uint16_t>(value);
188 break;
189 case kOpNeg:
190 opcode = kX86Mov32RI;
191 value = -value;
192 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700193 default:
194 LOG(FATAL) << "Bad case in OpRegImm " << op;
195 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 }
buzbee2700f7e2014-03-07 09:46:20 -0800197 return NewLIR2(opcode, r_dest_src1.GetReg(), value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198}
199
buzbee2700f7e2014-03-07 09:46:20 -0800200LIR* X86Mir2Lir::OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700201 bool is64Bit = r_dest_src1.Is64Bit();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 X86OpCode opcode = kX86Nop;
203 bool src2_must_be_cx = false;
204 switch (op) {
205 // X86 unary opcodes
206 case kOpMvn:
207 OpRegCopy(r_dest_src1, r_src2);
208 return OpReg(kOpNot, r_dest_src1);
209 case kOpNeg:
210 OpRegCopy(r_dest_src1, r_src2);
211 return OpReg(kOpNeg, r_dest_src1);
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100212 case kOpRev:
213 OpRegCopy(r_dest_src1, r_src2);
214 return OpReg(kOpRev, r_dest_src1);
215 case kOpRevsh:
216 OpRegCopy(r_dest_src1, r_src2);
217 OpReg(kOpRev, r_dest_src1);
218 return OpRegImm(kOpAsr, r_dest_src1, 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 // X86 binary opcodes
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700220 case kOpSub: opcode = is64Bit ? kX86Sub64RR : kX86Sub32RR; break;
221 case kOpSbc: opcode = is64Bit ? kX86Sbb64RR : kX86Sbb32RR; break;
222 case kOpLsl: opcode = is64Bit ? kX86Sal64RC : kX86Sal32RC; src2_must_be_cx = true; break;
223 case kOpLsr: opcode = is64Bit ? kX86Shr64RC : kX86Shr32RC; src2_must_be_cx = true; break;
224 case kOpAsr: opcode = is64Bit ? kX86Sar64RC : kX86Sar32RC; src2_must_be_cx = true; break;
225 case kOpMov: opcode = is64Bit ? kX86Mov64RR : kX86Mov32RR; break;
226 case kOpCmp: opcode = is64Bit ? kX86Cmp64RR : kX86Cmp32RR; break;
227 case kOpAdd: opcode = is64Bit ? kX86Add64RR : kX86Add32RR; break;
228 case kOpAdc: opcode = is64Bit ? kX86Adc64RR : kX86Adc32RR; break;
229 case kOpAnd: opcode = is64Bit ? kX86And64RR : kX86And32RR; break;
230 case kOpOr: opcode = is64Bit ? kX86Or64RR : kX86Or32RR; break;
231 case kOpXor: opcode = is64Bit ? kX86Xor64RR : kX86Xor32RR; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 case kOp2Byte:
buzbee091cc402014-03-31 10:14:40 -0700233 // TODO: there are several instances of this check. A utility function perhaps?
234 // TODO: Similar to Arm's reg < 8 check. Perhaps add attribute checks to RegStorage?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 // Use shifts instead of a byte operand if the source can't be byte accessed.
Ian Rogersb28c1c02014-11-08 11:21:21 -0800236 if (r_src2.GetRegNum() >= rs_rX86_SP_32.GetRegNum()) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700237 NewLIR2(is64Bit ? kX86Mov64RR : kX86Mov32RR, r_dest_src1.GetReg(), r_src2.GetReg());
238 NewLIR2(is64Bit ? kX86Sal64RI : kX86Sal32RI, r_dest_src1.GetReg(), is64Bit ? 56 : 24);
239 return NewLIR2(is64Bit ? kX86Sar64RI : kX86Sar32RI, r_dest_src1.GetReg(),
240 is64Bit ? 56 : 24);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700242 opcode = is64Bit ? kX86Bkpt : kX86Movsx8RR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 }
244 break;
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700245 case kOp2Short: opcode = is64Bit ? kX86Bkpt : kX86Movsx16RR; break;
246 case kOp2Char: opcode = is64Bit ? kX86Bkpt : kX86Movzx16RR; break;
247 case kOpMul: opcode = is64Bit ? kX86Bkpt : kX86Imul32RR; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 default:
249 LOG(FATAL) << "Bad case in OpRegReg " << op;
250 break;
251 }
buzbee091cc402014-03-31 10:14:40 -0700252 CHECK(!src2_must_be_cx || r_src2.GetReg() == rs_rCX.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -0800253 return NewLIR2(opcode, r_dest_src1.GetReg(), r_src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254}
255
buzbee2700f7e2014-03-07 09:46:20 -0800256LIR* X86Mir2Lir::OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset, MoveType move_type) {
buzbee091cc402014-03-31 10:14:40 -0700257 DCHECK(!r_base.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800258 X86OpCode opcode = kX86Nop;
buzbee2700f7e2014-03-07 09:46:20 -0800259 int dest = r_dest.IsPair() ? r_dest.GetLowReg() : r_dest.GetReg();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800260 switch (move_type) {
261 case kMov8GP:
buzbee091cc402014-03-31 10:14:40 -0700262 CHECK(!r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800263 opcode = kX86Mov8RM;
264 break;
265 case kMov16GP:
buzbee091cc402014-03-31 10:14:40 -0700266 CHECK(!r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800267 opcode = kX86Mov16RM;
268 break;
269 case kMov32GP:
buzbee091cc402014-03-31 10:14:40 -0700270 CHECK(!r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800271 opcode = kX86Mov32RM;
272 break;
273 case kMov32FP:
buzbee091cc402014-03-31 10:14:40 -0700274 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800275 opcode = kX86MovssRM;
276 break;
277 case kMov64FP:
buzbee091cc402014-03-31 10:14:40 -0700278 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800279 opcode = kX86MovsdRM;
280 break;
281 case kMovU128FP:
buzbee091cc402014-03-31 10:14:40 -0700282 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800283 opcode = kX86MovupsRM;
284 break;
285 case kMovA128FP:
buzbee091cc402014-03-31 10:14:40 -0700286 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800287 opcode = kX86MovapsRM;
288 break;
289 case kMovLo128FP:
buzbee091cc402014-03-31 10:14:40 -0700290 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800291 opcode = kX86MovlpsRM;
292 break;
293 case kMovHi128FP:
buzbee091cc402014-03-31 10:14:40 -0700294 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800295 opcode = kX86MovhpsRM;
296 break;
297 case kMov64GP:
298 case kMovLo64FP:
299 case kMovHi64FP:
300 default:
301 LOG(FATAL) << "Bad case in OpMovRegMem";
302 break;
303 }
304
buzbee2700f7e2014-03-07 09:46:20 -0800305 return NewLIR3(opcode, dest, r_base.GetReg(), offset);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800306}
307
buzbee2700f7e2014-03-07 09:46:20 -0800308LIR* X86Mir2Lir::OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type) {
buzbee091cc402014-03-31 10:14:40 -0700309 DCHECK(!r_base.IsFloat());
buzbee2700f7e2014-03-07 09:46:20 -0800310 int src = r_src.IsPair() ? r_src.GetLowReg() : r_src.GetReg();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800311
312 X86OpCode opcode = kX86Nop;
313 switch (move_type) {
314 case kMov8GP:
buzbee091cc402014-03-31 10:14:40 -0700315 CHECK(!r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800316 opcode = kX86Mov8MR;
317 break;
318 case kMov16GP:
buzbee091cc402014-03-31 10:14:40 -0700319 CHECK(!r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800320 opcode = kX86Mov16MR;
321 break;
322 case kMov32GP:
buzbee091cc402014-03-31 10:14:40 -0700323 CHECK(!r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800324 opcode = kX86Mov32MR;
325 break;
326 case kMov32FP:
buzbee091cc402014-03-31 10:14:40 -0700327 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800328 opcode = kX86MovssMR;
329 break;
330 case kMov64FP:
buzbee091cc402014-03-31 10:14:40 -0700331 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800332 opcode = kX86MovsdMR;
333 break;
334 case kMovU128FP:
buzbee091cc402014-03-31 10:14:40 -0700335 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800336 opcode = kX86MovupsMR;
337 break;
338 case kMovA128FP:
buzbee091cc402014-03-31 10:14:40 -0700339 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800340 opcode = kX86MovapsMR;
341 break;
342 case kMovLo128FP:
buzbee091cc402014-03-31 10:14:40 -0700343 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800344 opcode = kX86MovlpsMR;
345 break;
346 case kMovHi128FP:
buzbee091cc402014-03-31 10:14:40 -0700347 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800348 opcode = kX86MovhpsMR;
349 break;
350 case kMov64GP:
351 case kMovLo64FP:
352 case kMovHi64FP:
353 default:
354 LOG(FATAL) << "Bad case in OpMovMemReg";
355 break;
356 }
357
buzbee2700f7e2014-03-07 09:46:20 -0800358 return NewLIR3(opcode, r_base.GetReg(), offset, src);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800359}
360
buzbee2700f7e2014-03-07 09:46:20 -0800361LIR* X86Mir2Lir::OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800362 // The only conditional reg to reg operation supported is Cmov
363 DCHECK_EQ(op, kOpCmov);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700364 DCHECK_EQ(r_dest.Is64Bit(), r_src.Is64Bit());
365 return NewLIR3(r_dest.Is64Bit() ? kX86Cmov64RRC : kX86Cmov32RRC, r_dest.GetReg(),
366 r_src.GetReg(), X86ConditionEncoding(cc));
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800367}
368
buzbee2700f7e2014-03-07 09:46:20 -0800369LIR* X86Mir2Lir::OpRegMem(OpKind op, RegStorage r_dest, RegStorage r_base, int offset) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700370 bool is64Bit = r_dest.Is64Bit();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 X86OpCode opcode = kX86Nop;
372 switch (op) {
373 // X86 binary opcodes
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700374 case kOpSub: opcode = is64Bit ? kX86Sub64RM : kX86Sub32RM; break;
375 case kOpMov: opcode = is64Bit ? kX86Mov64RM : kX86Mov32RM; break;
376 case kOpCmp: opcode = is64Bit ? kX86Cmp64RM : kX86Cmp32RM; break;
377 case kOpAdd: opcode = is64Bit ? kX86Add64RM : kX86Add32RM; break;
378 case kOpAnd: opcode = is64Bit ? kX86And64RM : kX86And32RM; break;
379 case kOpOr: opcode = is64Bit ? kX86Or64RM : kX86Or32RM; break;
380 case kOpXor: opcode = is64Bit ? kX86Xor64RM : kX86Xor32RM; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 case kOp2Byte: opcode = kX86Movsx8RM; break;
382 case kOp2Short: opcode = kX86Movsx16RM; break;
383 case kOp2Char: opcode = kX86Movzx16RM; break;
384 case kOpMul:
385 default:
386 LOG(FATAL) << "Bad case in OpRegMem " << op;
387 break;
388 }
buzbee2700f7e2014-03-07 09:46:20 -0800389 LIR *l = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), offset);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100390 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800391 DCHECK_EQ(r_base, cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800392 AnnotateDalvikRegAccess(l, offset >> 2, true /* is_load */, false /* is_64bit */);
393 }
394 return l;
395}
396
397LIR* X86Mir2Lir::OpMemReg(OpKind op, RegLocation rl_dest, int r_value) {
398 DCHECK_NE(rl_dest.location, kLocPhysReg);
399 int displacement = SRegOffset(rl_dest.s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700400 bool is64Bit = rl_dest.wide != 0;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800401 X86OpCode opcode = kX86Nop;
402 switch (op) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700403 case kOpSub: opcode = is64Bit ? kX86Sub64MR : kX86Sub32MR; break;
404 case kOpMov: opcode = is64Bit ? kX86Mov64MR : kX86Mov32MR; break;
405 case kOpCmp: opcode = is64Bit ? kX86Cmp64MR : kX86Cmp32MR; break;
406 case kOpAdd: opcode = is64Bit ? kX86Add64MR : kX86Add32MR; break;
407 case kOpAnd: opcode = is64Bit ? kX86And64MR : kX86And32MR; break;
408 case kOpOr: opcode = is64Bit ? kX86Or64MR : kX86Or32MR; break;
409 case kOpXor: opcode = is64Bit ? kX86Xor64MR : kX86Xor32MR; break;
410 case kOpLsl: opcode = is64Bit ? kX86Sal64MC : kX86Sal32MC; break;
411 case kOpLsr: opcode = is64Bit ? kX86Shr64MC : kX86Shr32MC; break;
412 case kOpAsr: opcode = is64Bit ? kX86Sar64MC : kX86Sar32MC; break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800413 default:
414 LOG(FATAL) << "Bad case in OpMemReg " << op;
415 break;
416 }
Ian Rogersb28c1c02014-11-08 11:21:21 -0800417 LIR *l = NewLIR3(opcode, rs_rX86_SP_32.GetReg(), displacement, r_value);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100418 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
419 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is64Bit /* is_64bit */);
420 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is64Bit /* is_64bit */);
421 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800422 return l;
423}
424
buzbee2700f7e2014-03-07 09:46:20 -0800425LIR* X86Mir2Lir::OpRegMem(OpKind op, RegStorage r_dest, RegLocation rl_value) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800426 DCHECK_NE(rl_value.location, kLocPhysReg);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700427 bool is64Bit = r_dest.Is64Bit();
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800428 int displacement = SRegOffset(rl_value.s_reg_low);
429 X86OpCode opcode = kX86Nop;
430 switch (op) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700431 case kOpSub: opcode = is64Bit ? kX86Sub64RM : kX86Sub32RM; break;
432 case kOpMov: opcode = is64Bit ? kX86Mov64RM : kX86Mov32RM; break;
433 case kOpCmp: opcode = is64Bit ? kX86Cmp64RM : kX86Cmp32RM; break;
434 case kOpAdd: opcode = is64Bit ? kX86Add64RM : kX86Add32RM; break;
435 case kOpAnd: opcode = is64Bit ? kX86And64RM : kX86And32RM; break;
436 case kOpOr: opcode = is64Bit ? kX86Or64RM : kX86Or32RM; break;
437 case kOpXor: opcode = is64Bit ? kX86Xor64RM : kX86Xor32RM; break;
438 case kOpMul: opcode = is64Bit ? kX86Bkpt : kX86Imul32RM; break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800439 default:
440 LOG(FATAL) << "Bad case in OpRegMem " << op;
441 break;
442 }
Ian Rogersb28c1c02014-11-08 11:21:21 -0800443 LIR *l = NewLIR3(opcode, r_dest.GetReg(), rs_rX86_SP_32.GetReg(), displacement);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100444 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
445 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is64Bit /* is_64bit */);
446 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800447 return l;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448}
449
buzbee2700f7e2014-03-07 09:46:20 -0800450LIR* X86Mir2Lir::OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1,
451 RegStorage r_src2) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700452 bool is64Bit = r_dest.Is64Bit();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 if (r_dest != r_src1 && r_dest != r_src2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700454 if (op == kOpAdd) { // lea special case, except can't encode rbp as base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 if (r_src1 == r_src2) {
456 OpRegCopy(r_dest, r_src1);
457 return OpRegImm(kOpLsl, r_dest, 1);
buzbee2700f7e2014-03-07 09:46:20 -0800458 } else if (r_src1 != rs_rBP) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700459 return NewLIR5(is64Bit ? kX86Lea64RA : kX86Lea32RA, r_dest.GetReg(),
460 r_src1.GetReg() /* base */, r_src2.GetReg() /* index */,
461 0 /* scale */, 0 /* disp */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700463 return NewLIR5(is64Bit ? kX86Lea64RA : kX86Lea32RA, r_dest.GetReg(),
464 r_src2.GetReg() /* base */, r_src1.GetReg() /* index */,
465 0 /* scale */, 0 /* disp */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 }
467 } else {
468 OpRegCopy(r_dest, r_src1);
469 return OpRegReg(op, r_dest, r_src2);
470 }
471 } else if (r_dest == r_src1) {
472 return OpRegReg(op, r_dest, r_src2);
473 } else { // r_dest == r_src2
474 switch (op) {
475 case kOpSub: // non-commutative
476 OpReg(kOpNeg, r_dest);
477 op = kOpAdd;
478 break;
479 case kOpSbc:
480 case kOpLsl: case kOpLsr: case kOpAsr: case kOpRor: {
buzbee2700f7e2014-03-07 09:46:20 -0800481 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 OpRegCopy(t_reg, r_src1);
483 OpRegReg(op, t_reg, r_src2);
buzbee7a11ab02014-04-28 20:02:38 -0700484 LIR* res = OpRegCopyNoInsert(r_dest, t_reg);
485 AppendLIR(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 FreeTemp(t_reg);
487 return res;
488 }
489 case kOpAdd: // commutative
490 case kOpOr:
491 case kOpAdc:
492 case kOpAnd:
493 case kOpXor:
Pavel Vyssotski4ee71b22014-11-18 11:51:24 +0600494 case kOpMul:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 break;
496 default:
497 LOG(FATAL) << "Bad case in OpRegRegReg " << op;
498 }
499 return OpRegReg(op, r_dest, r_src1);
500 }
501}
502
buzbee2700f7e2014-03-07 09:46:20 -0800503LIR* X86Mir2Lir::OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src, int value) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700504 if (op == kOpMul && !cu_->target64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 X86OpCode opcode = IS_SIMM8(value) ? kX86Imul32RRI8 : kX86Imul32RRI;
buzbee2700f7e2014-03-07 09:46:20 -0800506 return NewLIR3(opcode, r_dest.GetReg(), r_src.GetReg(), value);
Elena Sayapinadd644502014-07-01 18:39:52 +0700507 } else if (op == kOpAnd && !cu_->target64) {
buzbee091cc402014-03-31 10:14:40 -0700508 if (value == 0xFF && r_src.Low4()) {
buzbee2700f7e2014-03-07 09:46:20 -0800509 return NewLIR2(kX86Movzx8RR, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 } else if (value == 0xFFFF) {
buzbee2700f7e2014-03-07 09:46:20 -0800511 return NewLIR2(kX86Movzx16RR, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 }
513 }
514 if (r_dest != r_src) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800515 if ((false) && op == kOpLsl && value >= 0 && value <= 3) { // lea shift special case
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 // TODO: fix bug in LEA encoding when disp == 0
buzbee2700f7e2014-03-07 09:46:20 -0800517 return NewLIR5(kX86Lea32RA, r_dest.GetReg(), r5sib_no_base /* base */,
518 r_src.GetReg() /* index */, value /* scale */, 0 /* disp */);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700519 } else if (op == kOpAdd) { // lea add special case
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700520 return NewLIR5(r_dest.Is64Bit() ? kX86Lea64RA : kX86Lea32RA, r_dest.GetReg(),
Ian Rogersb28c1c02014-11-08 11:21:21 -0800521 r_src.GetReg() /* base */, rs_rX86_SP_32.GetReg()/*r4sib_no_index*/ /* index */,
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700522 0 /* scale */, value /* disp */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 }
524 OpRegCopy(r_dest, r_src);
525 }
526 return OpRegImm(op, r_dest, value);
527}
528
Ian Rogersdd7624d2014-03-14 17:43:00 -0700529LIR* X86Mir2Lir::OpThreadMem(OpKind op, ThreadOffset<4> thread_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700530 DCHECK_EQ(kX86, cu_->instruction_set);
531 X86OpCode opcode = kX86Bkpt;
532 switch (op) {
533 case kOpBlx: opcode = kX86CallT; break;
534 case kOpBx: opcode = kX86JmpT; break;
535 default:
536 LOG(FATAL) << "Bad opcode: " << op;
537 break;
538 }
539 return NewLIR1(opcode, thread_offset.Int32Value());
540}
541
542LIR* X86Mir2Lir::OpThreadMem(OpKind op, ThreadOffset<8> thread_offset) {
543 DCHECK_EQ(kX86_64, cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 X86OpCode opcode = kX86Bkpt;
545 switch (op) {
546 case kOpBlx: opcode = kX86CallT; break;
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700547 case kOpBx: opcode = kX86JmpT; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 default:
549 LOG(FATAL) << "Bad opcode: " << op;
550 break;
551 }
Ian Rogers468532e2013-08-05 10:56:33 -0700552 return NewLIR1(opcode, thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553}
554
buzbee2700f7e2014-03-07 09:46:20 -0800555LIR* X86Mir2Lir::OpMem(OpKind op, RegStorage r_base, int disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 X86OpCode opcode = kX86Bkpt;
557 switch (op) {
558 case kOpBlx: opcode = kX86CallM; break;
559 default:
560 LOG(FATAL) << "Bad opcode: " << op;
561 break;
562 }
buzbee2700f7e2014-03-07 09:46:20 -0800563 return NewLIR2(opcode, r_base.GetReg(), disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564}
565
buzbee2700f7e2014-03-07 09:46:20 -0800566LIR* X86Mir2Lir::LoadConstantWide(RegStorage r_dest, int64_t value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 int32_t val_lo = Low32Bits(value);
568 int32_t val_hi = High32Bits(value);
buzbee2700f7e2014-03-07 09:46:20 -0800569 int32_t low_reg_val = r_dest.IsPair() ? r_dest.GetLowReg() : r_dest.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 LIR *res;
Mark Mendelle87f9b52014-04-30 14:13:18 -0400571 bool is_fp = r_dest.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800572 // TODO: clean this up once we fully recognize 64-bit storage containers.
573 if (is_fp) {
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +0700574 DCHECK(r_dest.IsDouble());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 if (value == 0) {
Mark Mendell27dee8b2014-12-01 19:06:12 -0500576 return NewLIR2(kX86XorpdRR, low_reg_val, low_reg_val);
577 } else if (base_of_code_ != nullptr || cu_->target64) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800578 // We will load the value from the literal area.
579 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
580 if (data_target == NULL) {
581 data_target = AddWideData(&literal_list_, val_lo, val_hi);
582 }
583
Mark Mendell67c39c42014-01-31 17:28:00 -0800584 // Load the proper value from the literal area.
Mark Mendell27dee8b2014-12-01 19:06:12 -0500585 // We don't know the proper offset for the value, so pick one that
586 // will force 4 byte offset. We will fix this up in the assembler
587 // later to have the right value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100588 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Mark Mendell27dee8b2014-12-01 19:06:12 -0500589 if (cu_->target64) {
590 res = NewLIR3(kX86MovsdRM, low_reg_val, kRIPReg, 256 /* bogus */);
591 } else {
592 // Address the start of the method.
593 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
594 if (rl_method.wide) {
595 rl_method = LoadValueWide(rl_method, kCoreReg);
596 } else {
597 rl_method = LoadValue(rl_method, kCoreReg);
598 }
599
600 res = LoadBaseDisp(rl_method.reg, 256 /* bogus */, RegStorage::FloatSolo64(low_reg_val),
601 kDouble, kNotVolatile);
602 store_method_addr_used_ = true;
603 }
Mark Mendell67c39c42014-01-31 17:28:00 -0800604 res->target = data_target;
605 res->flags.fixup = kFixupLoad;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 } else {
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +0700607 if (r_dest.IsPair()) {
608 if (val_lo == 0) {
609 res = NewLIR2(kX86XorpsRR, low_reg_val, low_reg_val);
610 } else {
611 res = LoadConstantNoClobber(RegStorage::FloatSolo32(low_reg_val), val_lo);
612 }
613 if (val_hi != 0) {
614 RegStorage r_dest_hi = AllocTempDouble();
615 LoadConstantNoClobber(r_dest_hi, val_hi);
616 NewLIR2(kX86PunpckldqRR, low_reg_val, r_dest_hi.GetReg());
617 FreeTemp(r_dest_hi);
618 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 } else {
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +0700620 RegStorage r_temp = AllocTypedTempWide(false, kCoreReg);
621 res = LoadConstantWide(r_temp, value);
622 OpRegCopyWide(r_dest, r_temp);
623 FreeTemp(r_temp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 }
625 }
626 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700627 if (r_dest.IsPair()) {
628 res = LoadConstantNoClobber(r_dest.GetLow(), val_lo);
629 LoadConstantNoClobber(r_dest.GetHigh(), val_hi);
630 } else {
Yixin Shou5192cbb2014-07-01 13:48:17 -0400631 if (value == 0) {
Serguei Katkov1c557032014-06-23 13:23:38 +0700632 res = NewLIR2(kX86Xor64RR, r_dest.GetReg(), r_dest.GetReg());
Yixin Shou5192cbb2014-07-01 13:48:17 -0400633 } else if (value >= INT_MIN && value <= INT_MAX) {
634 res = NewLIR2(kX86Mov64RI32, r_dest.GetReg(), val_lo);
635 } else {
636 res = NewLIR3(kX86Mov64RI64, r_dest.GetReg(), val_hi, val_lo);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700637 }
638 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
640 return res;
641}
642
buzbee2700f7e2014-03-07 09:46:20 -0800643LIR* X86Mir2Lir::LoadBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale,
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100644 int displacement, RegStorage r_dest, OpSize size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 LIR *load = NULL;
646 LIR *load2 = NULL;
buzbee2700f7e2014-03-07 09:46:20 -0800647 bool is_array = r_index.Valid();
buzbee091cc402014-03-31 10:14:40 -0700648 bool pair = r_dest.IsPair();
649 bool is64bit = ((size == k64) || (size == kDouble));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 X86OpCode opcode = kX86Nop;
651 switch (size) {
buzbee695d13a2014-04-19 13:32:20 -0700652 case k64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 case kDouble:
buzbee091cc402014-03-31 10:14:40 -0700654 if (r_dest.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 opcode = is_array ? kX86MovsdRA : kX86MovsdRM;
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700656 } else if (!pair) {
657 opcode = is_array ? kX86Mov64RA : kX86Mov64RM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 opcode = is_array ? kX86Mov32RA : kX86Mov32RM;
660 }
661 // TODO: double store is to unaligned address
662 DCHECK_EQ((displacement & 0x3), 0);
663 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700664 case kWord:
Elena Sayapinadd644502014-07-01 18:39:52 +0700665 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700666 opcode = is_array ? kX86Mov64RA : kX86Mov64RM;
667 CHECK_EQ(is_array, false);
668 CHECK_EQ(r_dest.IsFloat(), false);
669 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700670 }
671 FALLTHROUGH_INTENDED; // else fall-through to k32 case
buzbee695d13a2014-04-19 13:32:20 -0700672 case k32:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 case kSingle:
buzbee695d13a2014-04-19 13:32:20 -0700674 case kReference: // TODO: update for reference decompression on 64-bit targets.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 opcode = is_array ? kX86Mov32RA : kX86Mov32RM;
buzbee091cc402014-03-31 10:14:40 -0700676 if (r_dest.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 opcode = is_array ? kX86MovssRA : kX86MovssRM;
buzbee091cc402014-03-31 10:14:40 -0700678 DCHECK(r_dest.IsFloat());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 }
680 DCHECK_EQ((displacement & 0x3), 0);
681 break;
682 case kUnsignedHalf:
683 opcode = is_array ? kX86Movzx16RA : kX86Movzx16RM;
684 DCHECK_EQ((displacement & 0x1), 0);
685 break;
686 case kSignedHalf:
687 opcode = is_array ? kX86Movsx16RA : kX86Movsx16RM;
688 DCHECK_EQ((displacement & 0x1), 0);
689 break;
690 case kUnsignedByte:
691 opcode = is_array ? kX86Movzx8RA : kX86Movzx8RM;
692 break;
693 case kSignedByte:
694 opcode = is_array ? kX86Movsx8RA : kX86Movsx8RM;
695 break;
696 default:
697 LOG(FATAL) << "Bad case in LoadBaseIndexedDispBody";
698 }
699
700 if (!is_array) {
701 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800702 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), displacement + LOWORD_OFFSET);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 } else {
buzbee091cc402014-03-31 10:14:40 -0700704 DCHECK(!r_dest.IsFloat()); // Make sure we're not still using a pair here.
705 if (r_base == r_dest.GetLow()) {
Dave Allison69dfe512014-07-11 17:11:58 +0000706 load = NewLIR3(opcode, r_dest.GetHighReg(), r_base.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 displacement + HIWORD_OFFSET);
Dave Allison69dfe512014-07-11 17:11:58 +0000708 load2 = NewLIR3(opcode, r_dest.GetLowReg(), r_base.GetReg(), displacement + LOWORD_OFFSET);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 } else {
buzbee091cc402014-03-31 10:14:40 -0700710 load = NewLIR3(opcode, r_dest.GetLowReg(), r_base.GetReg(), displacement + LOWORD_OFFSET);
711 load2 = NewLIR3(opcode, r_dest.GetHighReg(), r_base.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 displacement + HIWORD_OFFSET);
713 }
714 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100715 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800716 DCHECK_EQ(r_base, cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 AnnotateDalvikRegAccess(load, (displacement + (pair ? LOWORD_OFFSET : 0)) >> 2,
718 true /* is_load */, is64bit);
719 if (pair) {
720 AnnotateDalvikRegAccess(load2, (displacement + HIWORD_OFFSET) >> 2,
721 true /* is_load */, is64bit);
722 }
723 }
724 } else {
725 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800726 load = NewLIR5(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(), scale,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 displacement + LOWORD_OFFSET);
728 } else {
buzbee091cc402014-03-31 10:14:40 -0700729 DCHECK(!r_dest.IsFloat()); // Make sure we're not still using a pair here.
730 if (r_base == r_dest.GetLow()) {
731 if (r_dest.GetHigh() == r_index) {
Mark Mendellae427c32014-01-24 09:17:22 -0800732 // We can't use either register for the first load.
buzbee2700f7e2014-03-07 09:46:20 -0800733 RegStorage temp = AllocTemp();
Dave Allison69dfe512014-07-11 17:11:58 +0000734 load = NewLIR5(opcode, temp.GetReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800735 displacement + HIWORD_OFFSET);
Dave Allison69dfe512014-07-11 17:11:58 +0000736 load2 = NewLIR5(opcode, r_dest.GetLowReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800737 displacement + LOWORD_OFFSET);
buzbee091cc402014-03-31 10:14:40 -0700738 OpRegCopy(r_dest.GetHigh(), temp);
Mark Mendellae427c32014-01-24 09:17:22 -0800739 FreeTemp(temp);
740 } else {
Dave Allison69dfe512014-07-11 17:11:58 +0000741 load = NewLIR5(opcode, r_dest.GetHighReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800742 displacement + HIWORD_OFFSET);
Dave Allison69dfe512014-07-11 17:11:58 +0000743 load2 = NewLIR5(opcode, r_dest.GetLowReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800744 displacement + LOWORD_OFFSET);
745 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 } else {
buzbee091cc402014-03-31 10:14:40 -0700747 if (r_dest.GetLow() == r_index) {
Mark Mendellae427c32014-01-24 09:17:22 -0800748 // We can't use either register for the first load.
buzbee2700f7e2014-03-07 09:46:20 -0800749 RegStorage temp = AllocTemp();
750 load = NewLIR5(opcode, temp.GetReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800751 displacement + LOWORD_OFFSET);
buzbee091cc402014-03-31 10:14:40 -0700752 load2 = NewLIR5(opcode, r_dest.GetHighReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800753 displacement + HIWORD_OFFSET);
buzbee091cc402014-03-31 10:14:40 -0700754 OpRegCopy(r_dest.GetLow(), temp);
Mark Mendellae427c32014-01-24 09:17:22 -0800755 FreeTemp(temp);
756 } else {
buzbee091cc402014-03-31 10:14:40 -0700757 load = NewLIR5(opcode, r_dest.GetLowReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800758 displacement + LOWORD_OFFSET);
buzbee091cc402014-03-31 10:14:40 -0700759 load2 = NewLIR5(opcode, r_dest.GetHighReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800760 displacement + HIWORD_OFFSET);
761 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 }
763 }
764 }
765
Dave Allison69dfe512014-07-11 17:11:58 +0000766 // Always return first load generated as this might cause a fault if base is nullptr.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 return load;
768}
769
770/* Load value from base + scaled index. */
buzbee2700f7e2014-03-07 09:46:20 -0800771LIR* X86Mir2Lir::LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
772 int scale, OpSize size) {
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100773 return LoadBaseIndexedDisp(r_base, r_index, scale, 0, r_dest, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774}
775
Andreas Gampe3c12c512014-06-24 18:46:29 +0000776LIR* X86Mir2Lir::LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
777 OpSize size, VolatileKind is_volatile) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100778 // LoadBaseDisp() will emit correct insn for atomic load on x86
779 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Vladimir Marko674744e2014-04-24 15:18:26 +0100780
Andreas Gampe3c12c512014-06-24 18:46:29 +0000781 LIR* load = LoadBaseIndexedDisp(r_base, RegStorage::InvalidReg(), 0, displacement, r_dest,
782 size);
783
784 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700785 GenMemBarrier(kLoadAny); // Only a scheduling barrier.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000786 }
787
788 return load;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789}
790
buzbee2700f7e2014-03-07 09:46:20 -0800791LIR* X86Mir2Lir::StoreBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale,
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700792 int displacement, RegStorage r_src, OpSize size,
793 int opt_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 LIR *store = NULL;
795 LIR *store2 = NULL;
buzbee2700f7e2014-03-07 09:46:20 -0800796 bool is_array = r_index.Valid();
buzbee091cc402014-03-31 10:14:40 -0700797 bool pair = r_src.IsPair();
798 bool is64bit = (size == k64) || (size == kDouble);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700799 bool consider_non_temporal = false;
800
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 X86OpCode opcode = kX86Nop;
802 switch (size) {
buzbee695d13a2014-04-19 13:32:20 -0700803 case k64:
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700804 consider_non_temporal = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700805 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 case kDouble:
buzbee091cc402014-03-31 10:14:40 -0700807 if (r_src.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 opcode = is_array ? kX86MovsdAR : kX86MovsdMR;
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700809 } else if (!pair) {
810 opcode = is_array ? kX86Mov64AR : kX86Mov64MR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700812 opcode = is_array ? kX86Mov32AR : kX86Mov32MR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813 }
814 // TODO: double store is to unaligned address
815 DCHECK_EQ((displacement & 0x3), 0);
816 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700817 case kWord:
Elena Sayapinadd644502014-07-01 18:39:52 +0700818 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700819 opcode = is_array ? kX86Mov64AR : kX86Mov64MR;
820 CHECK_EQ(is_array, false);
821 CHECK_EQ(r_src.IsFloat(), false);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700822 consider_non_temporal = true;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700823 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700824 }
825 FALLTHROUGH_INTENDED; // else fall-through to k32 case
buzbee695d13a2014-04-19 13:32:20 -0700826 case k32:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 case kSingle:
buzbee695d13a2014-04-19 13:32:20 -0700828 case kReference:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 opcode = is_array ? kX86Mov32AR : kX86Mov32MR;
buzbee091cc402014-03-31 10:14:40 -0700830 if (r_src.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 opcode = is_array ? kX86MovssAR : kX86MovssMR;
buzbee091cc402014-03-31 10:14:40 -0700832 DCHECK(r_src.IsSingle());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 }
834 DCHECK_EQ((displacement & 0x3), 0);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700835 consider_non_temporal = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 break;
837 case kUnsignedHalf:
838 case kSignedHalf:
839 opcode = is_array ? kX86Mov16AR : kX86Mov16MR;
840 DCHECK_EQ((displacement & 0x1), 0);
841 break;
842 case kUnsignedByte:
843 case kSignedByte:
844 opcode = is_array ? kX86Mov8AR : kX86Mov8MR;
845 break;
846 default:
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000847 LOG(FATAL) << "Bad case in StoreBaseIndexedDispBody";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 }
849
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700850 // Handle non temporal hint here.
851 if (consider_non_temporal && ((opt_flags & MIR_STORE_NON_TEMPORAL) != 0)) {
852 switch (opcode) {
853 // We currently only handle 32/64 bit moves here.
854 case kX86Mov64AR:
855 opcode = kX86Movnti64AR;
856 break;
857 case kX86Mov64MR:
858 opcode = kX86Movnti64MR;
859 break;
860 case kX86Mov32AR:
861 opcode = kX86Movnti32AR;
862 break;
863 case kX86Mov32MR:
864 opcode = kX86Movnti32MR;
865 break;
866 default:
867 // Do nothing here.
868 break;
869 }
870 }
871
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 if (!is_array) {
873 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800874 store = NewLIR3(opcode, r_base.GetReg(), displacement + LOWORD_OFFSET, r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 } else {
buzbee091cc402014-03-31 10:14:40 -0700876 DCHECK(!r_src.IsFloat()); // Make sure we're not still using a pair here.
877 store = NewLIR3(opcode, r_base.GetReg(), displacement + LOWORD_OFFSET, r_src.GetLowReg());
878 store2 = NewLIR3(opcode, r_base.GetReg(), displacement + HIWORD_OFFSET, r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100880 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800881 DCHECK_EQ(r_base, cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 AnnotateDalvikRegAccess(store, (displacement + (pair ? LOWORD_OFFSET : 0)) >> 2,
883 false /* is_load */, is64bit);
884 if (pair) {
885 AnnotateDalvikRegAccess(store2, (displacement + HIWORD_OFFSET) >> 2,
886 false /* is_load */, is64bit);
887 }
888 }
889 } else {
890 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800891 store = NewLIR5(opcode, r_base.GetReg(), r_index.GetReg(), scale,
892 displacement + LOWORD_OFFSET, r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 } else {
buzbee091cc402014-03-31 10:14:40 -0700894 DCHECK(!r_src.IsFloat()); // Make sure we're not still using a pair here.
buzbee2700f7e2014-03-07 09:46:20 -0800895 store = NewLIR5(opcode, r_base.GetReg(), r_index.GetReg(), scale,
buzbee091cc402014-03-31 10:14:40 -0700896 displacement + LOWORD_OFFSET, r_src.GetLowReg());
buzbee2700f7e2014-03-07 09:46:20 -0800897 store2 = NewLIR5(opcode, r_base.GetReg(), r_index.GetReg(), scale,
buzbee091cc402014-03-31 10:14:40 -0700898 displacement + HIWORD_OFFSET, r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 }
900 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 return store;
902}
903
904/* store value base base + scaled index. */
buzbee2700f7e2014-03-07 09:46:20 -0800905LIR* X86Mir2Lir::StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000906 int scale, OpSize size) {
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100907 return StoreBaseIndexedDisp(r_base, r_index, scale, 0, r_src, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908}
909
Andreas Gampe3c12c512014-06-24 18:46:29 +0000910LIR* X86Mir2Lir::StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src, OpSize size,
911 VolatileKind is_volatile) {
912 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700913 GenMemBarrier(kAnyStore); // Only a scheduling barrier.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000914 }
915
Vladimir Marko674744e2014-04-24 15:18:26 +0100916 // StoreBaseDisp() will emit correct insn for atomic store on x86
917 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Fred Shih37f05ef2014-07-16 18:38:08 -0700918 // x86 only allows registers EAX-EDX to be used as byte registers, if the input src is not
919 // valid, allocate a temp.
920 bool allocated_temp = false;
921 if (size == kUnsignedByte || size == kSignedByte) {
922 if (!cu_->target64 && !r_src.Low4()) {
923 RegStorage r_input = r_src;
924 r_src = AllocateByteRegister();
925 OpRegCopy(r_src, r_input);
926 allocated_temp = true;
927 }
928 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100929
Andreas Gampe3c12c512014-06-24 18:46:29 +0000930 LIR* store = StoreBaseIndexedDisp(r_base, RegStorage::InvalidReg(), 0, displacement, r_src, size);
931
932 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700933 // A volatile load might follow the volatile store so insert a StoreLoad barrier.
934 // This does require a fence, even on x86.
935 GenMemBarrier(kAnyAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000936 }
937
Fred Shih37f05ef2014-07-16 18:38:08 -0700938 if (allocated_temp) {
939 FreeTemp(r_src);
940 }
941
Andreas Gampe3c12c512014-06-24 18:46:29 +0000942 return store;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700943}
944
buzbee2700f7e2014-03-07 09:46:20 -0800945LIR* X86Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +0000946 int offset, int check_value, LIR* target, LIR** compare) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700947 UNUSED(temp_reg); // Comparison performed directly with memory.
948 LIR* inst = NewLIR3(IS_SIMM8(check_value) ? kX86Cmp32MI8 : kX86Cmp32MI, base_reg.GetReg(),
949 offset, check_value);
950 if (compare != nullptr) {
951 *compare = inst;
952 }
953 LIR* branch = OpCondBranch(cond, target);
954 return branch;
Mark Mendell766e9292014-01-27 07:55:47 -0800955}
956
Mark Mendell67c39c42014-01-31 17:28:00 -0800957void X86Mir2Lir::AnalyzeMIR() {
958 // Assume we don't need a pointer to the base of the code.
959 cu_->NewTimingSplit("X86 MIR Analysis");
960 store_method_addr_ = false;
961
962 // Walk the MIR looking for interesting items.
963 PreOrderDfsIterator iter(mir_graph_);
964 BasicBlock* curr_bb = iter.Next();
965 while (curr_bb != NULL) {
966 AnalyzeBB(curr_bb);
967 curr_bb = iter.Next();
968 }
969
Mark Mendell27dee8b2014-12-01 19:06:12 -0500970 // Did we need a pointer to the method code? Not in 64 bit mode.
971 base_of_code_ = nullptr;
972
973 // store_method_addr_ must be false for x86_64, since RIP addressing is used.
974 CHECK(!(cu_->target64 && store_method_addr_));
Mark Mendell67c39c42014-01-31 17:28:00 -0800975 if (store_method_addr_) {
Mark Mendell27dee8b2014-12-01 19:06:12 -0500976 base_of_code_ = mir_graph_->GetNewCompilerTemp(kCompilerTempBackend, false);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700977 DCHECK(base_of_code_ != nullptr);
Mark Mendell67c39c42014-01-31 17:28:00 -0800978 }
979}
980
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700981void X86Mir2Lir::AnalyzeBB(BasicBlock* bb) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800982 if (bb->block_type == kDead) {
983 // Ignore dead blocks
984 return;
985 }
986
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700987 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800988 int opcode = mir->dalvikInsn.opcode;
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700989 if (MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800990 AnalyzeExtendedMIR(opcode, bb, mir);
991 } else {
992 AnalyzeMIR(opcode, bb, mir);
993 }
994 }
995}
996
997
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700998void X86Mir2Lir::AnalyzeExtendedMIR(int opcode, BasicBlock* bb, MIR* mir) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800999 switch (opcode) {
1000 // Instructions referencing doubles.
1001 case kMirOpFusedCmplDouble:
1002 case kMirOpFusedCmpgDouble:
1003 AnalyzeFPInstruction(opcode, bb, mir);
1004 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001005 case kMirOpConstVector:
Mark Mendell27dee8b2014-12-01 19:06:12 -05001006 if (!cu_->target64) {
1007 store_method_addr_ = true;
1008 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001009 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001010 case kMirOpPackedMultiply:
1011 case kMirOpPackedShiftLeft:
1012 case kMirOpPackedSignedShiftRight:
Mark Mendell27dee8b2014-12-01 19:06:12 -05001013 case kMirOpPackedUnsignedShiftRight:
1014 if (!cu_->target64) {
1015 // Byte emulation requires constants from the literal pool.
1016 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1017 if (opsize == kSignedByte || opsize == kUnsignedByte) {
1018 store_method_addr_ = true;
1019 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001020 }
1021 break;
Mark Mendell67c39c42014-01-31 17:28:00 -08001022 default:
1023 // Ignore the rest.
1024 break;
1025 }
1026}
1027
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001028void X86Mir2Lir::AnalyzeMIR(int opcode, BasicBlock* bb, MIR* mir) {
Mark Mendell67c39c42014-01-31 17:28:00 -08001029 // Looking for
1030 // - Do we need a pointer to the code (used for packed switches and double lits)?
Mark Mendell27dee8b2014-12-01 19:06:12 -05001031 // 64 bit uses RIP addressing instead.
Mark Mendell67c39c42014-01-31 17:28:00 -08001032
1033 switch (opcode) {
1034 // Instructions referencing doubles.
1035 case Instruction::CMPL_DOUBLE:
1036 case Instruction::CMPG_DOUBLE:
1037 case Instruction::NEG_DOUBLE:
1038 case Instruction::ADD_DOUBLE:
1039 case Instruction::SUB_DOUBLE:
1040 case Instruction::MUL_DOUBLE:
1041 case Instruction::DIV_DOUBLE:
1042 case Instruction::REM_DOUBLE:
1043 case Instruction::ADD_DOUBLE_2ADDR:
1044 case Instruction::SUB_DOUBLE_2ADDR:
1045 case Instruction::MUL_DOUBLE_2ADDR:
1046 case Instruction::DIV_DOUBLE_2ADDR:
1047 case Instruction::REM_DOUBLE_2ADDR:
1048 AnalyzeFPInstruction(opcode, bb, mir);
1049 break;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001050
Mark Mendell67c39c42014-01-31 17:28:00 -08001051 // Packed switches and array fills need a pointer to the base of the method.
1052 case Instruction::FILL_ARRAY_DATA:
1053 case Instruction::PACKED_SWITCH:
Mark Mendell27dee8b2014-12-01 19:06:12 -05001054 if (!cu_->target64) {
1055 store_method_addr_ = true;
1056 }
Mark Mendell67c39c42014-01-31 17:28:00 -08001057 break;
Yixin Shou7071c8d2014-03-05 06:07:48 -05001058 case Instruction::INVOKE_STATIC:
Razvan A Lupusorue5beb182014-08-14 13:49:57 +08001059 case Instruction::INVOKE_STATIC_RANGE:
Yixin Shou7071c8d2014-03-05 06:07:48 -05001060 AnalyzeInvokeStatic(opcode, bb, mir);
1061 break;
Mark Mendell67c39c42014-01-31 17:28:00 -08001062 default:
1063 // Other instructions are not interesting yet.
1064 break;
1065 }
1066}
1067
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001068void X86Mir2Lir::AnalyzeFPInstruction(int opcode, BasicBlock* bb, MIR* mir) {
1069 UNUSED(bb);
Mark Mendell67c39c42014-01-31 17:28:00 -08001070 // Look at all the uses, and see if they are double constants.
Jean Christophe Beylercc794c32014-05-02 09:34:13 -07001071 uint64_t attrs = MIRGraph::GetDataFlowAttributes(static_cast<Instruction::Code>(opcode));
Mark Mendell67c39c42014-01-31 17:28:00 -08001072 int next_sreg = 0;
1073 if (attrs & DF_UA) {
1074 if (attrs & DF_A_WIDE) {
1075 AnalyzeDoubleUse(mir_graph_->GetSrcWide(mir, next_sreg));
1076 next_sreg += 2;
1077 } else {
1078 next_sreg++;
1079 }
1080 }
1081 if (attrs & DF_UB) {
1082 if (attrs & DF_B_WIDE) {
1083 AnalyzeDoubleUse(mir_graph_->GetSrcWide(mir, next_sreg));
1084 next_sreg += 2;
1085 } else {
1086 next_sreg++;
1087 }
1088 }
1089 if (attrs & DF_UC) {
1090 if (attrs & DF_C_WIDE) {
1091 AnalyzeDoubleUse(mir_graph_->GetSrcWide(mir, next_sreg));
1092 }
1093 }
1094}
1095
1096void X86Mir2Lir::AnalyzeDoubleUse(RegLocation use) {
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +07001097 // If this is a double literal, we will want it in the literal pool on 32b platforms.
1098 if (use.is_const && !cu_->target64) {
Mark Mendell67c39c42014-01-31 17:28:00 -08001099 store_method_addr_ = true;
1100 }
1101}
1102
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001103RegLocation X86Mir2Lir::UpdateLocTyped(RegLocation loc) {
buzbee30adc732014-05-09 15:10:18 -07001104 loc = UpdateLoc(loc);
1105 if ((loc.location == kLocPhysReg) && (loc.fp != loc.reg.IsFloat())) {
1106 if (GetRegInfo(loc.reg)->IsTemp()) {
1107 Clobber(loc.reg);
1108 FreeTemp(loc.reg);
1109 loc.reg = RegStorage::InvalidReg();
1110 loc.location = kLocDalvikFrame;
1111 }
1112 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001113 DCHECK(CheckCorePoolSanity());
buzbee30adc732014-05-09 15:10:18 -07001114 return loc;
1115}
1116
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001117RegLocation X86Mir2Lir::UpdateLocWideTyped(RegLocation loc) {
buzbee30adc732014-05-09 15:10:18 -07001118 loc = UpdateLocWide(loc);
1119 if ((loc.location == kLocPhysReg) && (loc.fp != loc.reg.IsFloat())) {
1120 if (GetRegInfo(loc.reg)->IsTemp()) {
1121 Clobber(loc.reg);
1122 FreeTemp(loc.reg);
1123 loc.reg = RegStorage::InvalidReg();
1124 loc.location = kLocDalvikFrame;
1125 }
1126 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001127 DCHECK(CheckCorePoolSanity());
buzbee30adc732014-05-09 15:10:18 -07001128 return loc;
1129}
Yixin Shou7071c8d2014-03-05 06:07:48 -05001130
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001131void X86Mir2Lir::AnalyzeInvokeStatic(int opcode, BasicBlock* bb, MIR* mir) {
1132 UNUSED(opcode, bb);
Mark Mendell27dee8b2014-12-01 19:06:12 -05001133
1134 // 64 bit RIP addressing doesn't need store_method_addr_ set.
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +07001135 if (cu_->target64) {
1136 return;
1137 }
1138
Yixin Shou7071c8d2014-03-05 06:07:48 -05001139 uint32_t index = mir->dalvikInsn.vB;
Vladimir Markoff0ac472014-10-02 17:24:53 +01001140 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1141 DexFileMethodInliner* method_inliner =
1142 cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file);
1143 InlineMethod method;
1144 if (method_inliner->IsIntrinsic(index, &method)) {
1145 switch (method.opcode) {
1146 case kIntrinsicAbsDouble:
1147 case kIntrinsicMinMaxDouble:
1148 store_method_addr_ = true;
1149 break;
1150 default:
1151 break;
Yixin Shou7071c8d2014-03-05 06:07:48 -05001152 }
1153 }
1154}
Andreas Gampe98430592014-07-27 19:44:50 -07001155
1156LIR* X86Mir2Lir::InvokeTrampoline(OpKind op, RegStorage r_tgt, QuickEntrypointEnum trampoline) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001157 UNUSED(r_tgt); // Call to absolute memory location doesn't need a temporary target register.
Andreas Gampe98430592014-07-27 19:44:50 -07001158 if (cu_->target64) {
1159 return OpThreadMem(op, GetThreadOffset<8>(trampoline));
1160 } else {
1161 return OpThreadMem(op, GetThreadOffset<4>(trampoline));
1162 }
1163}
1164
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165} // namespace art