blob: cb9a24a33666a3ad23ce54b20c63586928bd4f00 [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"
18#include "dex/quick/mir_to_lir-inl.h"
Mark Mendell67c39c42014-01-31 17:28:00 -080019#include "dex/dataflow_iterator-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "x86_lir.h"
Yixin Shou7071c8d2014-03-05 06:07:48 -050021#include "dex/quick/dex_file_method_inliner.h"
22#include "dex/quick/dex_file_to_method_inliner_map.h"
buzbeeb5860fb2014-06-21 15:31:01 -070023#include "dex/reg_storage_eq.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
25namespace art {
26
27/* This file contains codegen for the X86 ISA */
28
buzbee2700f7e2014-03-07 09:46:20 -080029LIR* X86Mir2Lir::OpFpRegCopy(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070030 int opcode;
31 /* must be both DOUBLE or both not DOUBLE */
buzbee091cc402014-03-31 10:14:40 -070032 DCHECK(r_dest.IsFloat() || r_src.IsFloat());
33 DCHECK_EQ(r_dest.IsDouble(), r_src.IsDouble());
34 if (r_dest.IsDouble()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070035 opcode = kX86MovsdRR;
36 } else {
buzbee091cc402014-03-31 10:14:40 -070037 if (r_dest.IsSingle()) {
38 if (r_src.IsSingle()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 opcode = kX86MovssRR;
40 } else { // Fpr <- Gpr
41 opcode = kX86MovdxrRR;
42 }
43 } else { // Gpr <- Fpr
buzbee091cc402014-03-31 10:14:40 -070044 DCHECK(r_src.IsSingle()) << "Raw: 0x" << std::hex << r_src.GetRawBits();
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 opcode = kX86MovdrxRR;
46 }
47 }
48 DCHECK_NE((EncodingMap[opcode].flags & IS_BINARY_OP), 0ULL);
buzbee2700f7e2014-03-07 09:46:20 -080049 LIR* res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070050 if (r_dest == r_src) {
51 res->flags.is_nop = true;
52 }
53 return res;
54}
55
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070056bool X86Mir2Lir::InexpensiveConstantInt(int32_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070057 UNUSED(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -070058 return true;
59}
60
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070061bool X86Mir2Lir::InexpensiveConstantFloat(int32_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070062 return value == 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -070063}
64
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070065bool X86Mir2Lir::InexpensiveConstantLong(int64_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070066 UNUSED(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 return true;
68}
69
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070070bool X86Mir2Lir::InexpensiveConstantDouble(int64_t value) {
Mark Mendell67c39c42014-01-31 17:28:00 -080071 return value == 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -070072}
73
74/*
75 * Load a immediate using a shortcut if possible; otherwise
76 * grab from the per-translation literal pool. If target is
77 * a high register, build constant into a low register and copy.
78 *
79 * No additional register clobbering operation performed. Use this version when
80 * 1) r_dest is freshly returned from AllocTemp or
81 * 2) The codegen is under fixed register usage
82 */
buzbee2700f7e2014-03-07 09:46:20 -080083LIR* X86Mir2Lir::LoadConstantNoClobber(RegStorage r_dest, int value) {
84 RegStorage r_dest_save = r_dest;
buzbee091cc402014-03-31 10:14:40 -070085 if (r_dest.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 if (value == 0) {
buzbee2700f7e2014-03-07 09:46:20 -080087 return NewLIR2(kX86XorpsRR, r_dest.GetReg(), r_dest.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 r_dest = AllocTemp();
90 }
91
92 LIR *res;
93 if (value == 0) {
buzbee2700f7e2014-03-07 09:46:20 -080094 res = NewLIR2(kX86Xor32RR, r_dest.GetReg(), r_dest.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 } else {
96 // Note, there is no byte immediate form of a 32 bit immediate move.
Chao-ying Fue0ccdc02014-06-06 17:32:37 -070097 // 64-bit immediate is not supported by LIR structure
98 res = NewLIR2(kX86Mov32RI, r_dest.GetReg(), value);
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 }
100
buzbee091cc402014-03-31 10:14:40 -0700101 if (r_dest_save.IsFloat()) {
buzbee2700f7e2014-03-07 09:46:20 -0800102 NewLIR2(kX86MovdxrRR, r_dest_save.GetReg(), r_dest.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103 FreeTemp(r_dest);
104 }
105
106 return res;
107}
108
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700109LIR* X86Mir2Lir::OpUnconditionalBranch(LIR* target) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700110 LIR* res = NewLIR1(kX86Jmp8, 0 /* offset to be patched during assembly*/);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 res->target = target;
112 return res;
113}
114
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700115LIR* X86Mir2Lir::OpCondBranch(ConditionCode cc, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 LIR* branch = NewLIR2(kX86Jcc8, 0 /* offset to be patched */,
117 X86ConditionEncoding(cc));
118 branch->target = target;
119 return branch;
120}
121
buzbee2700f7e2014-03-07 09:46:20 -0800122LIR* X86Mir2Lir::OpReg(OpKind op, RegStorage r_dest_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 X86OpCode opcode = kX86Bkpt;
124 switch (op) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700125 case kOpNeg: opcode = r_dest_src.Is64Bit() ? kX86Neg64R : kX86Neg32R; break;
126 case kOpNot: opcode = r_dest_src.Is64Bit() ? kX86Not64R : kX86Not32R; break;
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700127 case kOpRev: opcode = r_dest_src.Is64Bit() ? kX86Bswap64R : kX86Bswap32R; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 case kOpBlx: opcode = kX86CallR; break;
129 default:
130 LOG(FATAL) << "Bad case in OpReg " << op;
131 }
buzbee2700f7e2014-03-07 09:46:20 -0800132 return NewLIR1(opcode, r_dest_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133}
134
buzbee2700f7e2014-03-07 09:46:20 -0800135LIR* X86Mir2Lir::OpRegImm(OpKind op, RegStorage r_dest_src1, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 X86OpCode opcode = kX86Bkpt;
137 bool byte_imm = IS_SIMM8(value);
buzbee091cc402014-03-31 10:14:40 -0700138 DCHECK(!r_dest_src1.IsFloat());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700139 if (r_dest_src1.Is64Bit()) {
140 switch (op) {
141 case kOpAdd: opcode = byte_imm ? kX86Add64RI8 : kX86Add64RI; break;
142 case kOpSub: opcode = byte_imm ? kX86Sub64RI8 : kX86Sub64RI; break;
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700143 case kOpLsl: opcode = kX86Sal64RI; break;
144 case kOpLsr: opcode = kX86Shr64RI; break;
145 case kOpAsr: opcode = kX86Sar64RI; break;
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700146 case kOpCmp: opcode = byte_imm ? kX86Cmp64RI8 : kX86Cmp64RI; break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700147 default:
148 LOG(FATAL) << "Bad case in OpRegImm (64-bit) " << op;
149 }
150 } else {
151 switch (op) {
152 case kOpLsl: opcode = kX86Sal32RI; break;
153 case kOpLsr: opcode = kX86Shr32RI; break;
154 case kOpAsr: opcode = kX86Sar32RI; break;
155 case kOpAdd: opcode = byte_imm ? kX86Add32RI8 : kX86Add32RI; break;
156 case kOpOr: opcode = byte_imm ? kX86Or32RI8 : kX86Or32RI; break;
157 case kOpAdc: opcode = byte_imm ? kX86Adc32RI8 : kX86Adc32RI; break;
158 // case kOpSbb: opcode = kX86Sbb32RI; break;
159 case kOpAnd: opcode = byte_imm ? kX86And32RI8 : kX86And32RI; break;
160 case kOpSub: opcode = byte_imm ? kX86Sub32RI8 : kX86Sub32RI; break;
161 case kOpXor: opcode = byte_imm ? kX86Xor32RI8 : kX86Xor32RI; break;
162 case kOpCmp: opcode = byte_imm ? kX86Cmp32RI8 : kX86Cmp32RI; break;
163 case kOpMov:
164 /*
165 * Moving the constant zero into register can be specialized as an xor of the register.
166 * However, that sets eflags while the move does not. For that reason here, always do
167 * the move and if caller is flexible, they should be calling LoadConstantNoClobber instead.
168 */
169 opcode = kX86Mov32RI;
170 break;
171 case kOpMul:
172 opcode = byte_imm ? kX86Imul32RRI8 : kX86Imul32RRI;
173 return NewLIR3(opcode, r_dest_src1.GetReg(), r_dest_src1.GetReg(), value);
Mark Mendelle87f9b52014-04-30 14:13:18 -0400174 case kOp2Byte:
175 opcode = kX86Mov32RI;
176 value = static_cast<int8_t>(value);
177 break;
178 case kOp2Short:
179 opcode = kX86Mov32RI;
180 value = static_cast<int16_t>(value);
181 break;
182 case kOp2Char:
183 opcode = kX86Mov32RI;
184 value = static_cast<uint16_t>(value);
185 break;
186 case kOpNeg:
187 opcode = kX86Mov32RI;
188 value = -value;
189 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700190 default:
191 LOG(FATAL) << "Bad case in OpRegImm " << op;
192 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193 }
buzbee2700f7e2014-03-07 09:46:20 -0800194 return NewLIR2(opcode, r_dest_src1.GetReg(), value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195}
196
buzbee2700f7e2014-03-07 09:46:20 -0800197LIR* X86Mir2Lir::OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700198 bool is64Bit = r_dest_src1.Is64Bit();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 X86OpCode opcode = kX86Nop;
200 bool src2_must_be_cx = false;
201 switch (op) {
202 // X86 unary opcodes
203 case kOpMvn:
204 OpRegCopy(r_dest_src1, r_src2);
205 return OpReg(kOpNot, r_dest_src1);
206 case kOpNeg:
207 OpRegCopy(r_dest_src1, r_src2);
208 return OpReg(kOpNeg, r_dest_src1);
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100209 case kOpRev:
210 OpRegCopy(r_dest_src1, r_src2);
211 return OpReg(kOpRev, r_dest_src1);
212 case kOpRevsh:
213 OpRegCopy(r_dest_src1, r_src2);
214 OpReg(kOpRev, r_dest_src1);
215 return OpRegImm(kOpAsr, r_dest_src1, 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 // X86 binary opcodes
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700217 case kOpSub: opcode = is64Bit ? kX86Sub64RR : kX86Sub32RR; break;
218 case kOpSbc: opcode = is64Bit ? kX86Sbb64RR : kX86Sbb32RR; break;
219 case kOpLsl: opcode = is64Bit ? kX86Sal64RC : kX86Sal32RC; src2_must_be_cx = true; break;
220 case kOpLsr: opcode = is64Bit ? kX86Shr64RC : kX86Shr32RC; src2_must_be_cx = true; break;
221 case kOpAsr: opcode = is64Bit ? kX86Sar64RC : kX86Sar32RC; src2_must_be_cx = true; break;
222 case kOpMov: opcode = is64Bit ? kX86Mov64RR : kX86Mov32RR; break;
223 case kOpCmp: opcode = is64Bit ? kX86Cmp64RR : kX86Cmp32RR; break;
224 case kOpAdd: opcode = is64Bit ? kX86Add64RR : kX86Add32RR; break;
225 case kOpAdc: opcode = is64Bit ? kX86Adc64RR : kX86Adc32RR; break;
226 case kOpAnd: opcode = is64Bit ? kX86And64RR : kX86And32RR; break;
227 case kOpOr: opcode = is64Bit ? kX86Or64RR : kX86Or32RR; break;
228 case kOpXor: opcode = is64Bit ? kX86Xor64RR : kX86Xor32RR; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 case kOp2Byte:
buzbee091cc402014-03-31 10:14:40 -0700230 // TODO: there are several instances of this check. A utility function perhaps?
231 // TODO: Similar to Arm's reg < 8 check. Perhaps add attribute checks to RegStorage?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 // Use shifts instead of a byte operand if the source can't be byte accessed.
buzbee091cc402014-03-31 10:14:40 -0700233 if (r_src2.GetRegNum() >= rs_rX86_SP.GetRegNum()) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700234 NewLIR2(is64Bit ? kX86Mov64RR : kX86Mov32RR, r_dest_src1.GetReg(), r_src2.GetReg());
235 NewLIR2(is64Bit ? kX86Sal64RI : kX86Sal32RI, r_dest_src1.GetReg(), is64Bit ? 56 : 24);
236 return NewLIR2(is64Bit ? kX86Sar64RI : kX86Sar32RI, r_dest_src1.GetReg(),
237 is64Bit ? 56 : 24);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700239 opcode = is64Bit ? kX86Bkpt : kX86Movsx8RR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 }
241 break;
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700242 case kOp2Short: opcode = is64Bit ? kX86Bkpt : kX86Movsx16RR; break;
243 case kOp2Char: opcode = is64Bit ? kX86Bkpt : kX86Movzx16RR; break;
244 case kOpMul: opcode = is64Bit ? kX86Bkpt : kX86Imul32RR; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 default:
246 LOG(FATAL) << "Bad case in OpRegReg " << op;
247 break;
248 }
buzbee091cc402014-03-31 10:14:40 -0700249 CHECK(!src2_must_be_cx || r_src2.GetReg() == rs_rCX.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -0800250 return NewLIR2(opcode, r_dest_src1.GetReg(), r_src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251}
252
buzbee2700f7e2014-03-07 09:46:20 -0800253LIR* X86Mir2Lir::OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset, MoveType move_type) {
buzbee091cc402014-03-31 10:14:40 -0700254 DCHECK(!r_base.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800255 X86OpCode opcode = kX86Nop;
buzbee2700f7e2014-03-07 09:46:20 -0800256 int dest = r_dest.IsPair() ? r_dest.GetLowReg() : r_dest.GetReg();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800257 switch (move_type) {
258 case kMov8GP:
buzbee091cc402014-03-31 10:14:40 -0700259 CHECK(!r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800260 opcode = kX86Mov8RM;
261 break;
262 case kMov16GP:
buzbee091cc402014-03-31 10:14:40 -0700263 CHECK(!r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800264 opcode = kX86Mov16RM;
265 break;
266 case kMov32GP:
buzbee091cc402014-03-31 10:14:40 -0700267 CHECK(!r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800268 opcode = kX86Mov32RM;
269 break;
270 case kMov32FP:
buzbee091cc402014-03-31 10:14:40 -0700271 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800272 opcode = kX86MovssRM;
273 break;
274 case kMov64FP:
buzbee091cc402014-03-31 10:14:40 -0700275 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800276 opcode = kX86MovsdRM;
277 break;
278 case kMovU128FP:
buzbee091cc402014-03-31 10:14:40 -0700279 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800280 opcode = kX86MovupsRM;
281 break;
282 case kMovA128FP:
buzbee091cc402014-03-31 10:14:40 -0700283 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800284 opcode = kX86MovapsRM;
285 break;
286 case kMovLo128FP:
buzbee091cc402014-03-31 10:14:40 -0700287 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800288 opcode = kX86MovlpsRM;
289 break;
290 case kMovHi128FP:
buzbee091cc402014-03-31 10:14:40 -0700291 CHECK(r_dest.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800292 opcode = kX86MovhpsRM;
293 break;
294 case kMov64GP:
295 case kMovLo64FP:
296 case kMovHi64FP:
297 default:
298 LOG(FATAL) << "Bad case in OpMovRegMem";
299 break;
300 }
301
buzbee2700f7e2014-03-07 09:46:20 -0800302 return NewLIR3(opcode, dest, r_base.GetReg(), offset);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800303}
304
buzbee2700f7e2014-03-07 09:46:20 -0800305LIR* X86Mir2Lir::OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type) {
buzbee091cc402014-03-31 10:14:40 -0700306 DCHECK(!r_base.IsFloat());
buzbee2700f7e2014-03-07 09:46:20 -0800307 int src = r_src.IsPair() ? r_src.GetLowReg() : r_src.GetReg();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800308
309 X86OpCode opcode = kX86Nop;
310 switch (move_type) {
311 case kMov8GP:
buzbee091cc402014-03-31 10:14:40 -0700312 CHECK(!r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800313 opcode = kX86Mov8MR;
314 break;
315 case kMov16GP:
buzbee091cc402014-03-31 10:14:40 -0700316 CHECK(!r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800317 opcode = kX86Mov16MR;
318 break;
319 case kMov32GP:
buzbee091cc402014-03-31 10:14:40 -0700320 CHECK(!r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800321 opcode = kX86Mov32MR;
322 break;
323 case kMov32FP:
buzbee091cc402014-03-31 10:14:40 -0700324 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800325 opcode = kX86MovssMR;
326 break;
327 case kMov64FP:
buzbee091cc402014-03-31 10:14:40 -0700328 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800329 opcode = kX86MovsdMR;
330 break;
331 case kMovU128FP:
buzbee091cc402014-03-31 10:14:40 -0700332 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800333 opcode = kX86MovupsMR;
334 break;
335 case kMovA128FP:
buzbee091cc402014-03-31 10:14:40 -0700336 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800337 opcode = kX86MovapsMR;
338 break;
339 case kMovLo128FP:
buzbee091cc402014-03-31 10:14:40 -0700340 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800341 opcode = kX86MovlpsMR;
342 break;
343 case kMovHi128FP:
buzbee091cc402014-03-31 10:14:40 -0700344 CHECK(r_src.IsFloat());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800345 opcode = kX86MovhpsMR;
346 break;
347 case kMov64GP:
348 case kMovLo64FP:
349 case kMovHi64FP:
350 default:
351 LOG(FATAL) << "Bad case in OpMovMemReg";
352 break;
353 }
354
buzbee2700f7e2014-03-07 09:46:20 -0800355 return NewLIR3(opcode, r_base.GetReg(), offset, src);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800356}
357
buzbee2700f7e2014-03-07 09:46:20 -0800358LIR* X86Mir2Lir::OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src) {
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800359 // The only conditional reg to reg operation supported is Cmov
360 DCHECK_EQ(op, kOpCmov);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +0700361 DCHECK_EQ(r_dest.Is64Bit(), r_src.Is64Bit());
362 return NewLIR3(r_dest.Is64Bit() ? kX86Cmov64RRC : kX86Cmov32RRC, r_dest.GetReg(),
363 r_src.GetReg(), X86ConditionEncoding(cc));
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800364}
365
buzbee2700f7e2014-03-07 09:46:20 -0800366LIR* X86Mir2Lir::OpRegMem(OpKind op, RegStorage r_dest, RegStorage r_base, int offset) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700367 bool is64Bit = r_dest.Is64Bit();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 X86OpCode opcode = kX86Nop;
369 switch (op) {
370 // X86 binary opcodes
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700371 case kOpSub: opcode = is64Bit ? kX86Sub64RM : kX86Sub32RM; break;
372 case kOpMov: opcode = is64Bit ? kX86Mov64RM : kX86Mov32RM; break;
373 case kOpCmp: opcode = is64Bit ? kX86Cmp64RM : kX86Cmp32RM; break;
374 case kOpAdd: opcode = is64Bit ? kX86Add64RM : kX86Add32RM; break;
375 case kOpAnd: opcode = is64Bit ? kX86And64RM : kX86And32RM; break;
376 case kOpOr: opcode = is64Bit ? kX86Or64RM : kX86Or32RM; break;
377 case kOpXor: opcode = is64Bit ? kX86Xor64RM : kX86Xor32RM; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 case kOp2Byte: opcode = kX86Movsx8RM; break;
379 case kOp2Short: opcode = kX86Movsx16RM; break;
380 case kOp2Char: opcode = kX86Movzx16RM; break;
381 case kOpMul:
382 default:
383 LOG(FATAL) << "Bad case in OpRegMem " << op;
384 break;
385 }
buzbee2700f7e2014-03-07 09:46:20 -0800386 LIR *l = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), offset);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100387 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
388 DCHECK(r_base == rs_rX86_SP);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800389 AnnotateDalvikRegAccess(l, offset >> 2, true /* is_load */, false /* is_64bit */);
390 }
391 return l;
392}
393
394LIR* X86Mir2Lir::OpMemReg(OpKind op, RegLocation rl_dest, int r_value) {
395 DCHECK_NE(rl_dest.location, kLocPhysReg);
396 int displacement = SRegOffset(rl_dest.s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700397 bool is64Bit = rl_dest.wide != 0;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800398 X86OpCode opcode = kX86Nop;
399 switch (op) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700400 case kOpSub: opcode = is64Bit ? kX86Sub64MR : kX86Sub32MR; break;
401 case kOpMov: opcode = is64Bit ? kX86Mov64MR : kX86Mov32MR; break;
402 case kOpCmp: opcode = is64Bit ? kX86Cmp64MR : kX86Cmp32MR; break;
403 case kOpAdd: opcode = is64Bit ? kX86Add64MR : kX86Add32MR; break;
404 case kOpAnd: opcode = is64Bit ? kX86And64MR : kX86And32MR; break;
405 case kOpOr: opcode = is64Bit ? kX86Or64MR : kX86Or32MR; break;
406 case kOpXor: opcode = is64Bit ? kX86Xor64MR : kX86Xor32MR; break;
407 case kOpLsl: opcode = is64Bit ? kX86Sal64MC : kX86Sal32MC; break;
408 case kOpLsr: opcode = is64Bit ? kX86Shr64MC : kX86Shr32MC; break;
409 case kOpAsr: opcode = is64Bit ? kX86Sar64MC : kX86Sar32MC; break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800410 default:
411 LOG(FATAL) << "Bad case in OpMemReg " << op;
412 break;
413 }
buzbee091cc402014-03-31 10:14:40 -0700414 LIR *l = NewLIR3(opcode, rs_rX86_SP.GetReg(), displacement, r_value);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100415 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
416 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is64Bit /* is_64bit */);
417 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is64Bit /* is_64bit */);
418 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800419 return l;
420}
421
buzbee2700f7e2014-03-07 09:46:20 -0800422LIR* X86Mir2Lir::OpRegMem(OpKind op, RegStorage r_dest, RegLocation rl_value) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800423 DCHECK_NE(rl_value.location, kLocPhysReg);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700424 bool is64Bit = r_dest.Is64Bit();
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800425 int displacement = SRegOffset(rl_value.s_reg_low);
426 X86OpCode opcode = kX86Nop;
427 switch (op) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700428 case kOpSub: opcode = is64Bit ? kX86Sub64RM : kX86Sub32RM; break;
429 case kOpMov: opcode = is64Bit ? kX86Mov64RM : kX86Mov32RM; break;
430 case kOpCmp: opcode = is64Bit ? kX86Cmp64RM : kX86Cmp32RM; break;
431 case kOpAdd: opcode = is64Bit ? kX86Add64RM : kX86Add32RM; break;
432 case kOpAnd: opcode = is64Bit ? kX86And64RM : kX86And32RM; break;
433 case kOpOr: opcode = is64Bit ? kX86Or64RM : kX86Or32RM; break;
434 case kOpXor: opcode = is64Bit ? kX86Xor64RM : kX86Xor32RM; break;
435 case kOpMul: opcode = is64Bit ? kX86Bkpt : kX86Imul32RM; break;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800436 default:
437 LOG(FATAL) << "Bad case in OpRegMem " << op;
438 break;
439 }
buzbee091cc402014-03-31 10:14:40 -0700440 LIR *l = NewLIR3(opcode, r_dest.GetReg(), rs_rX86_SP.GetReg(), displacement);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100441 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
442 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is64Bit /* is_64bit */);
443 }
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800444 return l;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445}
446
buzbee2700f7e2014-03-07 09:46:20 -0800447LIR* X86Mir2Lir::OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1,
448 RegStorage r_src2) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700449 bool is64Bit = r_dest.Is64Bit();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 if (r_dest != r_src1 && r_dest != r_src2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700451 if (op == kOpAdd) { // lea special case, except can't encode rbp as base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 if (r_src1 == r_src2) {
453 OpRegCopy(r_dest, r_src1);
454 return OpRegImm(kOpLsl, r_dest, 1);
buzbee2700f7e2014-03-07 09:46:20 -0800455 } else if (r_src1 != rs_rBP) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700456 return NewLIR5(is64Bit ? kX86Lea64RA : kX86Lea32RA, r_dest.GetReg(),
457 r_src1.GetReg() /* base */, r_src2.GetReg() /* index */,
458 0 /* scale */, 0 /* disp */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700460 return NewLIR5(is64Bit ? kX86Lea64RA : kX86Lea32RA, r_dest.GetReg(),
461 r_src2.GetReg() /* base */, r_src1.GetReg() /* index */,
462 0 /* scale */, 0 /* disp */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 }
464 } else {
465 OpRegCopy(r_dest, r_src1);
466 return OpRegReg(op, r_dest, r_src2);
467 }
468 } else if (r_dest == r_src1) {
469 return OpRegReg(op, r_dest, r_src2);
470 } else { // r_dest == r_src2
471 switch (op) {
472 case kOpSub: // non-commutative
473 OpReg(kOpNeg, r_dest);
474 op = kOpAdd;
475 break;
476 case kOpSbc:
477 case kOpLsl: case kOpLsr: case kOpAsr: case kOpRor: {
buzbee2700f7e2014-03-07 09:46:20 -0800478 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 OpRegCopy(t_reg, r_src1);
480 OpRegReg(op, t_reg, r_src2);
buzbee7a11ab02014-04-28 20:02:38 -0700481 LIR* res = OpRegCopyNoInsert(r_dest, t_reg);
482 AppendLIR(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 FreeTemp(t_reg);
484 return res;
485 }
486 case kOpAdd: // commutative
487 case kOpOr:
488 case kOpAdc:
489 case kOpAnd:
490 case kOpXor:
491 break;
492 default:
493 LOG(FATAL) << "Bad case in OpRegRegReg " << op;
494 }
495 return OpRegReg(op, r_dest, r_src1);
496 }
497}
498
buzbee2700f7e2014-03-07 09:46:20 -0800499LIR* X86Mir2Lir::OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src, int value) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700500 if (op == kOpMul && !cu_->target64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 X86OpCode opcode = IS_SIMM8(value) ? kX86Imul32RRI8 : kX86Imul32RRI;
buzbee2700f7e2014-03-07 09:46:20 -0800502 return NewLIR3(opcode, r_dest.GetReg(), r_src.GetReg(), value);
Elena Sayapinadd644502014-07-01 18:39:52 +0700503 } else if (op == kOpAnd && !cu_->target64) {
buzbee091cc402014-03-31 10:14:40 -0700504 if (value == 0xFF && r_src.Low4()) {
buzbee2700f7e2014-03-07 09:46:20 -0800505 return NewLIR2(kX86Movzx8RR, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 } else if (value == 0xFFFF) {
buzbee2700f7e2014-03-07 09:46:20 -0800507 return NewLIR2(kX86Movzx16RR, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 }
509 }
510 if (r_dest != r_src) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700511 if (false && op == kOpLsl && value >= 0 && value <= 3) { // lea shift special case
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 // TODO: fix bug in LEA encoding when disp == 0
buzbee2700f7e2014-03-07 09:46:20 -0800513 return NewLIR5(kX86Lea32RA, r_dest.GetReg(), r5sib_no_base /* base */,
514 r_src.GetReg() /* index */, value /* scale */, 0 /* disp */);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700515 } else if (op == kOpAdd) { // lea add special case
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700516 return NewLIR5(r_dest.Is64Bit() ? kX86Lea64RA : kX86Lea32RA, r_dest.GetReg(),
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700517 r_src.GetReg() /* base */, rs_rX86_SP.GetReg()/*r4sib_no_index*/ /* index */,
518 0 /* scale */, value /* disp */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 }
520 OpRegCopy(r_dest, r_src);
521 }
522 return OpRegImm(op, r_dest, value);
523}
524
Ian Rogersdd7624d2014-03-14 17:43:00 -0700525LIR* X86Mir2Lir::OpThreadMem(OpKind op, ThreadOffset<4> thread_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700526 DCHECK_EQ(kX86, cu_->instruction_set);
527 X86OpCode opcode = kX86Bkpt;
528 switch (op) {
529 case kOpBlx: opcode = kX86CallT; break;
530 case kOpBx: opcode = kX86JmpT; break;
531 default:
532 LOG(FATAL) << "Bad opcode: " << op;
533 break;
534 }
535 return NewLIR1(opcode, thread_offset.Int32Value());
536}
537
538LIR* X86Mir2Lir::OpThreadMem(OpKind op, ThreadOffset<8> thread_offset) {
539 DCHECK_EQ(kX86_64, cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 X86OpCode opcode = kX86Bkpt;
541 switch (op) {
542 case kOpBlx: opcode = kX86CallT; break;
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700543 case kOpBx: opcode = kX86JmpT; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 default:
545 LOG(FATAL) << "Bad opcode: " << op;
546 break;
547 }
Ian Rogers468532e2013-08-05 10:56:33 -0700548 return NewLIR1(opcode, thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549}
550
buzbee2700f7e2014-03-07 09:46:20 -0800551LIR* X86Mir2Lir::OpMem(OpKind op, RegStorage r_base, int disp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 X86OpCode opcode = kX86Bkpt;
553 switch (op) {
554 case kOpBlx: opcode = kX86CallM; break;
555 default:
556 LOG(FATAL) << "Bad opcode: " << op;
557 break;
558 }
buzbee2700f7e2014-03-07 09:46:20 -0800559 return NewLIR2(opcode, r_base.GetReg(), disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560}
561
buzbee2700f7e2014-03-07 09:46:20 -0800562LIR* X86Mir2Lir::LoadConstantWide(RegStorage r_dest, int64_t value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 int32_t val_lo = Low32Bits(value);
564 int32_t val_hi = High32Bits(value);
buzbee2700f7e2014-03-07 09:46:20 -0800565 int32_t low_reg_val = r_dest.IsPair() ? r_dest.GetLowReg() : r_dest.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 LIR *res;
Mark Mendelle87f9b52014-04-30 14:13:18 -0400567 bool is_fp = r_dest.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800568 // TODO: clean this up once we fully recognize 64-bit storage containers.
569 if (is_fp) {
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +0700570 DCHECK(r_dest.IsDouble());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 if (value == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800572 return NewLIR2(kX86XorpsRR, low_reg_val, low_reg_val);
Mark Mendell67c39c42014-01-31 17:28:00 -0800573 } else if (base_of_code_ != nullptr) {
574 // We will load the value from the literal area.
575 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
576 if (data_target == NULL) {
577 data_target = AddWideData(&literal_list_, val_lo, val_hi);
578 }
579
580 // Address the start of the method
581 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700582 if (rl_method.wide) {
583 rl_method = LoadValueWide(rl_method, kCoreReg);
584 } else {
585 rl_method = LoadValue(rl_method, kCoreReg);
586 }
Mark Mendell67c39c42014-01-31 17:28:00 -0800587
588 // Load the proper value from the literal area.
589 // We don't know the proper offset for the value, so pick one that will force
590 // 4 byte offset. We will fix this up in the assembler later to have the right
591 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100592 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Mark Mendell0c524512014-05-27 15:52:21 -0400593 res = LoadBaseDisp(rl_method.reg, 256 /* bogus */, RegStorage::FloatSolo64(low_reg_val),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000594 kDouble, kNotVolatile);
Mark Mendell67c39c42014-01-31 17:28:00 -0800595 res->target = data_target;
596 res->flags.fixup = kFixupLoad;
Mark Mendell55d0eac2014-02-06 11:02:52 -0800597 store_method_addr_used_ = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 } else {
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +0700599 if (r_dest.IsPair()) {
600 if (val_lo == 0) {
601 res = NewLIR2(kX86XorpsRR, low_reg_val, low_reg_val);
602 } else {
603 res = LoadConstantNoClobber(RegStorage::FloatSolo32(low_reg_val), val_lo);
604 }
605 if (val_hi != 0) {
606 RegStorage r_dest_hi = AllocTempDouble();
607 LoadConstantNoClobber(r_dest_hi, val_hi);
608 NewLIR2(kX86PunpckldqRR, low_reg_val, r_dest_hi.GetReg());
609 FreeTemp(r_dest_hi);
610 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 } else {
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +0700612 RegStorage r_temp = AllocTypedTempWide(false, kCoreReg);
613 res = LoadConstantWide(r_temp, value);
614 OpRegCopyWide(r_dest, r_temp);
615 FreeTemp(r_temp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 }
617 }
618 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700619 if (r_dest.IsPair()) {
620 res = LoadConstantNoClobber(r_dest.GetLow(), val_lo);
621 LoadConstantNoClobber(r_dest.GetHigh(), val_hi);
622 } else {
Yixin Shou5192cbb2014-07-01 13:48:17 -0400623 if (value == 0) {
Serguei Katkov1c557032014-06-23 13:23:38 +0700624 res = NewLIR2(kX86Xor64RR, r_dest.GetReg(), r_dest.GetReg());
Yixin Shou5192cbb2014-07-01 13:48:17 -0400625 } else if (value >= INT_MIN && value <= INT_MAX) {
626 res = NewLIR2(kX86Mov64RI32, r_dest.GetReg(), val_lo);
627 } else {
628 res = NewLIR3(kX86Mov64RI64, r_dest.GetReg(), val_hi, val_lo);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700629 }
630 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 }
632 return res;
633}
634
buzbee2700f7e2014-03-07 09:46:20 -0800635LIR* X86Mir2Lir::LoadBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale,
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100636 int displacement, RegStorage r_dest, OpSize size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 LIR *load = NULL;
638 LIR *load2 = NULL;
buzbee2700f7e2014-03-07 09:46:20 -0800639 bool is_array = r_index.Valid();
buzbee091cc402014-03-31 10:14:40 -0700640 bool pair = r_dest.IsPair();
641 bool is64bit = ((size == k64) || (size == kDouble));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 X86OpCode opcode = kX86Nop;
643 switch (size) {
buzbee695d13a2014-04-19 13:32:20 -0700644 case k64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 case kDouble:
buzbee091cc402014-03-31 10:14:40 -0700646 if (r_dest.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 opcode = is_array ? kX86MovsdRA : kX86MovsdRM;
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700648 } else if (!pair) {
649 opcode = is_array ? kX86Mov64RA : kX86Mov64RM;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 opcode = is_array ? kX86Mov32RA : kX86Mov32RM;
652 }
653 // TODO: double store is to unaligned address
654 DCHECK_EQ((displacement & 0x3), 0);
655 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700656 case kWord:
Elena Sayapinadd644502014-07-01 18:39:52 +0700657 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700658 opcode = is_array ? kX86Mov64RA : kX86Mov64RM;
659 CHECK_EQ(is_array, false);
660 CHECK_EQ(r_dest.IsFloat(), false);
661 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700662 }
663 FALLTHROUGH_INTENDED; // else fall-through to k32 case
buzbee695d13a2014-04-19 13:32:20 -0700664 case k32:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 case kSingle:
buzbee695d13a2014-04-19 13:32:20 -0700666 case kReference: // TODO: update for reference decompression on 64-bit targets.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 opcode = is_array ? kX86Mov32RA : kX86Mov32RM;
buzbee091cc402014-03-31 10:14:40 -0700668 if (r_dest.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 opcode = is_array ? kX86MovssRA : kX86MovssRM;
buzbee091cc402014-03-31 10:14:40 -0700670 DCHECK(r_dest.IsFloat());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 }
672 DCHECK_EQ((displacement & 0x3), 0);
673 break;
674 case kUnsignedHalf:
675 opcode = is_array ? kX86Movzx16RA : kX86Movzx16RM;
676 DCHECK_EQ((displacement & 0x1), 0);
677 break;
678 case kSignedHalf:
679 opcode = is_array ? kX86Movsx16RA : kX86Movsx16RM;
680 DCHECK_EQ((displacement & 0x1), 0);
681 break;
682 case kUnsignedByte:
683 opcode = is_array ? kX86Movzx8RA : kX86Movzx8RM;
684 break;
685 case kSignedByte:
686 opcode = is_array ? kX86Movsx8RA : kX86Movsx8RM;
687 break;
688 default:
689 LOG(FATAL) << "Bad case in LoadBaseIndexedDispBody";
690 }
691
692 if (!is_array) {
693 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800694 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), displacement + LOWORD_OFFSET);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 } else {
buzbee091cc402014-03-31 10:14:40 -0700696 DCHECK(!r_dest.IsFloat()); // Make sure we're not still using a pair here.
697 if (r_base == r_dest.GetLow()) {
Dave Allison69dfe512014-07-11 17:11:58 +0000698 load = NewLIR3(opcode, r_dest.GetHighReg(), r_base.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 displacement + HIWORD_OFFSET);
Dave Allison69dfe512014-07-11 17:11:58 +0000700 load2 = NewLIR3(opcode, r_dest.GetLowReg(), r_base.GetReg(), displacement + LOWORD_OFFSET);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 } else {
buzbee091cc402014-03-31 10:14:40 -0700702 load = NewLIR3(opcode, r_dest.GetLowReg(), r_base.GetReg(), displacement + LOWORD_OFFSET);
703 load2 = NewLIR3(opcode, r_dest.GetHighReg(), r_base.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 displacement + HIWORD_OFFSET);
705 }
706 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100707 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
708 DCHECK(r_base == rs_rX86_SP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 AnnotateDalvikRegAccess(load, (displacement + (pair ? LOWORD_OFFSET : 0)) >> 2,
710 true /* is_load */, is64bit);
711 if (pair) {
712 AnnotateDalvikRegAccess(load2, (displacement + HIWORD_OFFSET) >> 2,
713 true /* is_load */, is64bit);
714 }
715 }
716 } else {
717 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800718 load = NewLIR5(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(), scale,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 displacement + LOWORD_OFFSET);
720 } else {
buzbee091cc402014-03-31 10:14:40 -0700721 DCHECK(!r_dest.IsFloat()); // Make sure we're not still using a pair here.
722 if (r_base == r_dest.GetLow()) {
723 if (r_dest.GetHigh() == r_index) {
Mark Mendellae427c32014-01-24 09:17:22 -0800724 // We can't use either register for the first load.
buzbee2700f7e2014-03-07 09:46:20 -0800725 RegStorage temp = AllocTemp();
Dave Allison69dfe512014-07-11 17:11:58 +0000726 load = NewLIR5(opcode, temp.GetReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800727 displacement + HIWORD_OFFSET);
Dave Allison69dfe512014-07-11 17:11:58 +0000728 load2 = NewLIR5(opcode, r_dest.GetLowReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800729 displacement + LOWORD_OFFSET);
buzbee091cc402014-03-31 10:14:40 -0700730 OpRegCopy(r_dest.GetHigh(), temp);
Mark Mendellae427c32014-01-24 09:17:22 -0800731 FreeTemp(temp);
732 } else {
Dave Allison69dfe512014-07-11 17:11:58 +0000733 load = NewLIR5(opcode, r_dest.GetHighReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800734 displacement + HIWORD_OFFSET);
Dave Allison69dfe512014-07-11 17:11:58 +0000735 load2 = NewLIR5(opcode, r_dest.GetLowReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800736 displacement + LOWORD_OFFSET);
737 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 } else {
buzbee091cc402014-03-31 10:14:40 -0700739 if (r_dest.GetLow() == r_index) {
Mark Mendellae427c32014-01-24 09:17:22 -0800740 // We can't use either register for the first load.
buzbee2700f7e2014-03-07 09:46:20 -0800741 RegStorage temp = AllocTemp();
742 load = NewLIR5(opcode, temp.GetReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800743 displacement + LOWORD_OFFSET);
buzbee091cc402014-03-31 10:14:40 -0700744 load2 = NewLIR5(opcode, r_dest.GetHighReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800745 displacement + HIWORD_OFFSET);
buzbee091cc402014-03-31 10:14:40 -0700746 OpRegCopy(r_dest.GetLow(), temp);
Mark Mendellae427c32014-01-24 09:17:22 -0800747 FreeTemp(temp);
748 } else {
buzbee091cc402014-03-31 10:14:40 -0700749 load = NewLIR5(opcode, r_dest.GetLowReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800750 displacement + LOWORD_OFFSET);
buzbee091cc402014-03-31 10:14:40 -0700751 load2 = NewLIR5(opcode, r_dest.GetHighReg(), r_base.GetReg(), r_index.GetReg(), scale,
Mark Mendellae427c32014-01-24 09:17:22 -0800752 displacement + HIWORD_OFFSET);
753 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 }
755 }
756 }
757
Dave Allison69dfe512014-07-11 17:11:58 +0000758 // Always return first load generated as this might cause a fault if base is nullptr.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 return load;
760}
761
762/* Load value from base + scaled index. */
buzbee2700f7e2014-03-07 09:46:20 -0800763LIR* X86Mir2Lir::LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
764 int scale, OpSize size) {
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100765 return LoadBaseIndexedDisp(r_base, r_index, scale, 0, r_dest, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766}
767
Andreas Gampe3c12c512014-06-24 18:46:29 +0000768LIR* X86Mir2Lir::LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
769 OpSize size, VolatileKind is_volatile) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100770 // LoadBaseDisp() will emit correct insn for atomic load on x86
771 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Vladimir Marko674744e2014-04-24 15:18:26 +0100772
Andreas Gampe3c12c512014-06-24 18:46:29 +0000773 LIR* load = LoadBaseIndexedDisp(r_base, RegStorage::InvalidReg(), 0, displacement, r_dest,
774 size);
775
776 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700777 GenMemBarrier(kLoadAny); // Only a scheduling barrier.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000778 }
779
780 return load;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781}
782
buzbee2700f7e2014-03-07 09:46:20 -0800783LIR* X86Mir2Lir::StoreBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale,
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700784 int displacement, RegStorage r_src, OpSize size,
785 int opt_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 LIR *store = NULL;
787 LIR *store2 = NULL;
buzbee2700f7e2014-03-07 09:46:20 -0800788 bool is_array = r_index.Valid();
buzbee091cc402014-03-31 10:14:40 -0700789 bool pair = r_src.IsPair();
790 bool is64bit = (size == k64) || (size == kDouble);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700791 bool consider_non_temporal = false;
792
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 X86OpCode opcode = kX86Nop;
794 switch (size) {
buzbee695d13a2014-04-19 13:32:20 -0700795 case k64:
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700796 consider_non_temporal = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700797 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 case kDouble:
buzbee091cc402014-03-31 10:14:40 -0700799 if (r_src.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 opcode = is_array ? kX86MovsdAR : kX86MovsdMR;
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700801 } else if (!pair) {
802 opcode = is_array ? kX86Mov64AR : kX86Mov64MR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 } else {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700804 opcode = is_array ? kX86Mov32AR : kX86Mov32MR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 }
806 // TODO: double store is to unaligned address
807 DCHECK_EQ((displacement & 0x3), 0);
808 break;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700809 case kWord:
Elena Sayapinadd644502014-07-01 18:39:52 +0700810 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700811 opcode = is_array ? kX86Mov64AR : kX86Mov64MR;
812 CHECK_EQ(is_array, false);
813 CHECK_EQ(r_src.IsFloat(), false);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700814 consider_non_temporal = true;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700815 break;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700816 }
817 FALLTHROUGH_INTENDED; // else fall-through to k32 case
buzbee695d13a2014-04-19 13:32:20 -0700818 case k32:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 case kSingle:
buzbee695d13a2014-04-19 13:32:20 -0700820 case kReference:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 opcode = is_array ? kX86Mov32AR : kX86Mov32MR;
buzbee091cc402014-03-31 10:14:40 -0700822 if (r_src.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 opcode = is_array ? kX86MovssAR : kX86MovssMR;
buzbee091cc402014-03-31 10:14:40 -0700824 DCHECK(r_src.IsSingle());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 }
826 DCHECK_EQ((displacement & 0x3), 0);
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700827 consider_non_temporal = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700828 break;
829 case kUnsignedHalf:
830 case kSignedHalf:
831 opcode = is_array ? kX86Mov16AR : kX86Mov16MR;
832 DCHECK_EQ((displacement & 0x1), 0);
833 break;
834 case kUnsignedByte:
835 case kSignedByte:
836 opcode = is_array ? kX86Mov8AR : kX86Mov8MR;
837 break;
838 default:
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000839 LOG(FATAL) << "Bad case in StoreBaseIndexedDispBody";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 }
841
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700842 // Handle non temporal hint here.
843 if (consider_non_temporal && ((opt_flags & MIR_STORE_NON_TEMPORAL) != 0)) {
844 switch (opcode) {
845 // We currently only handle 32/64 bit moves here.
846 case kX86Mov64AR:
847 opcode = kX86Movnti64AR;
848 break;
849 case kX86Mov64MR:
850 opcode = kX86Movnti64MR;
851 break;
852 case kX86Mov32AR:
853 opcode = kX86Movnti32AR;
854 break;
855 case kX86Mov32MR:
856 opcode = kX86Movnti32MR;
857 break;
858 default:
859 // Do nothing here.
860 break;
861 }
862 }
863
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 if (!is_array) {
865 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800866 store = NewLIR3(opcode, r_base.GetReg(), displacement + LOWORD_OFFSET, r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 } else {
buzbee091cc402014-03-31 10:14:40 -0700868 DCHECK(!r_src.IsFloat()); // Make sure we're not still using a pair here.
869 store = NewLIR3(opcode, r_base.GetReg(), displacement + LOWORD_OFFSET, r_src.GetLowReg());
870 store2 = NewLIR3(opcode, r_base.GetReg(), displacement + HIWORD_OFFSET, r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100872 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
873 DCHECK(r_base == rs_rX86_SP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 AnnotateDalvikRegAccess(store, (displacement + (pair ? LOWORD_OFFSET : 0)) >> 2,
875 false /* is_load */, is64bit);
876 if (pair) {
877 AnnotateDalvikRegAccess(store2, (displacement + HIWORD_OFFSET) >> 2,
878 false /* is_load */, is64bit);
879 }
880 }
881 } else {
882 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800883 store = NewLIR5(opcode, r_base.GetReg(), r_index.GetReg(), scale,
884 displacement + LOWORD_OFFSET, r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 } else {
buzbee091cc402014-03-31 10:14:40 -0700886 DCHECK(!r_src.IsFloat()); // Make sure we're not still using a pair here.
buzbee2700f7e2014-03-07 09:46:20 -0800887 store = NewLIR5(opcode, r_base.GetReg(), r_index.GetReg(), scale,
buzbee091cc402014-03-31 10:14:40 -0700888 displacement + LOWORD_OFFSET, r_src.GetLowReg());
buzbee2700f7e2014-03-07 09:46:20 -0800889 store2 = NewLIR5(opcode, r_base.GetReg(), r_index.GetReg(), scale,
buzbee091cc402014-03-31 10:14:40 -0700890 displacement + HIWORD_OFFSET, r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 }
892 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 return store;
894}
895
896/* store value base base + scaled index. */
buzbee2700f7e2014-03-07 09:46:20 -0800897LIR* X86Mir2Lir::StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000898 int scale, OpSize size) {
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100899 return StoreBaseIndexedDisp(r_base, r_index, scale, 0, r_src, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900}
901
Andreas Gampe3c12c512014-06-24 18:46:29 +0000902LIR* X86Mir2Lir::StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src, OpSize size,
903 VolatileKind is_volatile) {
904 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700905 GenMemBarrier(kAnyStore); // Only a scheduling barrier.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000906 }
907
Vladimir Marko674744e2014-04-24 15:18:26 +0100908 // StoreBaseDisp() will emit correct insn for atomic store on x86
909 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Fred Shih37f05ef2014-07-16 18:38:08 -0700910 // x86 only allows registers EAX-EDX to be used as byte registers, if the input src is not
911 // valid, allocate a temp.
912 bool allocated_temp = false;
913 if (size == kUnsignedByte || size == kSignedByte) {
914 if (!cu_->target64 && !r_src.Low4()) {
915 RegStorage r_input = r_src;
916 r_src = AllocateByteRegister();
917 OpRegCopy(r_src, r_input);
918 allocated_temp = true;
919 }
920 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100921
Andreas Gampe3c12c512014-06-24 18:46:29 +0000922 LIR* store = StoreBaseIndexedDisp(r_base, RegStorage::InvalidReg(), 0, displacement, r_src, size);
923
924 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700925 // A volatile load might follow the volatile store so insert a StoreLoad barrier.
926 // This does require a fence, even on x86.
927 GenMemBarrier(kAnyAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000928 }
929
Fred Shih37f05ef2014-07-16 18:38:08 -0700930 if (allocated_temp) {
931 FreeTemp(r_src);
932 }
933
Andreas Gampe3c12c512014-06-24 18:46:29 +0000934 return store;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935}
936
buzbee2700f7e2014-03-07 09:46:20 -0800937LIR* X86Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Dave Allison69dfe512014-07-11 17:11:58 +0000938 int offset, int check_value, LIR* target, LIR** compare) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700939 UNUSED(temp_reg); // Comparison performed directly with memory.
940 LIR* inst = NewLIR3(IS_SIMM8(check_value) ? kX86Cmp32MI8 : kX86Cmp32MI, base_reg.GetReg(),
941 offset, check_value);
942 if (compare != nullptr) {
943 *compare = inst;
944 }
945 LIR* branch = OpCondBranch(cond, target);
946 return branch;
Mark Mendell766e9292014-01-27 07:55:47 -0800947}
948
Mark Mendell67c39c42014-01-31 17:28:00 -0800949void X86Mir2Lir::AnalyzeMIR() {
950 // Assume we don't need a pointer to the base of the code.
951 cu_->NewTimingSplit("X86 MIR Analysis");
952 store_method_addr_ = false;
953
954 // Walk the MIR looking for interesting items.
955 PreOrderDfsIterator iter(mir_graph_);
956 BasicBlock* curr_bb = iter.Next();
957 while (curr_bb != NULL) {
958 AnalyzeBB(curr_bb);
959 curr_bb = iter.Next();
960 }
961
962 // Did we need a pointer to the method code?
963 if (store_method_addr_) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700964 base_of_code_ = mir_graph_->GetNewCompilerTemp(kCompilerTempBackend, cu_->target64 == true);
965 DCHECK(base_of_code_ != nullptr);
Mark Mendell67c39c42014-01-31 17:28:00 -0800966 } else {
967 base_of_code_ = nullptr;
968 }
969}
970
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700971void X86Mir2Lir::AnalyzeBB(BasicBlock* bb) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800972 if (bb->block_type == kDead) {
973 // Ignore dead blocks
974 return;
975 }
976
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700977 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800978 int opcode = mir->dalvikInsn.opcode;
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700979 if (MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800980 AnalyzeExtendedMIR(opcode, bb, mir);
981 } else {
982 AnalyzeMIR(opcode, bb, mir);
983 }
984 }
985}
986
987
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700988void X86Mir2Lir::AnalyzeExtendedMIR(int opcode, BasicBlock* bb, MIR* mir) {
Mark Mendell67c39c42014-01-31 17:28:00 -0800989 switch (opcode) {
990 // Instructions referencing doubles.
991 case kMirOpFusedCmplDouble:
992 case kMirOpFusedCmpgDouble:
993 AnalyzeFPInstruction(opcode, bb, mir);
994 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -0400995 case kMirOpConstVector:
996 store_method_addr_ = true;
997 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700998 case kMirOpPackedMultiply:
999 case kMirOpPackedShiftLeft:
1000 case kMirOpPackedSignedShiftRight:
1001 case kMirOpPackedUnsignedShiftRight: {
1002 // Byte emulation requires constants from the literal pool.
1003 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1004 if (opsize == kSignedByte || opsize == kUnsignedByte) {
1005 store_method_addr_ = true;
1006 }
1007 break;
1008 }
Mark Mendell67c39c42014-01-31 17:28:00 -08001009 default:
1010 // Ignore the rest.
1011 break;
1012 }
1013}
1014
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001015void X86Mir2Lir::AnalyzeMIR(int opcode, BasicBlock* bb, MIR* mir) {
Mark Mendell67c39c42014-01-31 17:28:00 -08001016 // Looking for
1017 // - Do we need a pointer to the code (used for packed switches and double lits)?
1018
1019 switch (opcode) {
1020 // Instructions referencing doubles.
1021 case Instruction::CMPL_DOUBLE:
1022 case Instruction::CMPG_DOUBLE:
1023 case Instruction::NEG_DOUBLE:
1024 case Instruction::ADD_DOUBLE:
1025 case Instruction::SUB_DOUBLE:
1026 case Instruction::MUL_DOUBLE:
1027 case Instruction::DIV_DOUBLE:
1028 case Instruction::REM_DOUBLE:
1029 case Instruction::ADD_DOUBLE_2ADDR:
1030 case Instruction::SUB_DOUBLE_2ADDR:
1031 case Instruction::MUL_DOUBLE_2ADDR:
1032 case Instruction::DIV_DOUBLE_2ADDR:
1033 case Instruction::REM_DOUBLE_2ADDR:
1034 AnalyzeFPInstruction(opcode, bb, mir);
1035 break;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001036
Mark Mendell67c39c42014-01-31 17:28:00 -08001037 // Packed switches and array fills need a pointer to the base of the method.
1038 case Instruction::FILL_ARRAY_DATA:
1039 case Instruction::PACKED_SWITCH:
1040 store_method_addr_ = true;
1041 break;
Yixin Shou7071c8d2014-03-05 06:07:48 -05001042 case Instruction::INVOKE_STATIC:
Razvan A Lupusorue5beb182014-08-14 13:49:57 +08001043 case Instruction::INVOKE_STATIC_RANGE:
Yixin Shou7071c8d2014-03-05 06:07:48 -05001044 AnalyzeInvokeStatic(opcode, bb, mir);
1045 break;
Mark Mendell67c39c42014-01-31 17:28:00 -08001046 default:
1047 // Other instructions are not interesting yet.
1048 break;
1049 }
1050}
1051
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001052void X86Mir2Lir::AnalyzeFPInstruction(int opcode, BasicBlock* bb, MIR* mir) {
1053 UNUSED(bb);
Mark Mendell67c39c42014-01-31 17:28:00 -08001054 // Look at all the uses, and see if they are double constants.
Jean Christophe Beylercc794c32014-05-02 09:34:13 -07001055 uint64_t attrs = MIRGraph::GetDataFlowAttributes(static_cast<Instruction::Code>(opcode));
Mark Mendell67c39c42014-01-31 17:28:00 -08001056 int next_sreg = 0;
1057 if (attrs & DF_UA) {
1058 if (attrs & DF_A_WIDE) {
1059 AnalyzeDoubleUse(mir_graph_->GetSrcWide(mir, next_sreg));
1060 next_sreg += 2;
1061 } else {
1062 next_sreg++;
1063 }
1064 }
1065 if (attrs & DF_UB) {
1066 if (attrs & DF_B_WIDE) {
1067 AnalyzeDoubleUse(mir_graph_->GetSrcWide(mir, next_sreg));
1068 next_sreg += 2;
1069 } else {
1070 next_sreg++;
1071 }
1072 }
1073 if (attrs & DF_UC) {
1074 if (attrs & DF_C_WIDE) {
1075 AnalyzeDoubleUse(mir_graph_->GetSrcWide(mir, next_sreg));
1076 }
1077 }
1078}
1079
1080void X86Mir2Lir::AnalyzeDoubleUse(RegLocation use) {
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +07001081 // If this is a double literal, we will want it in the literal pool on 32b platforms.
1082 if (use.is_const && !cu_->target64) {
Mark Mendell67c39c42014-01-31 17:28:00 -08001083 store_method_addr_ = true;
1084 }
1085}
1086
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001087RegLocation X86Mir2Lir::UpdateLocTyped(RegLocation loc) {
buzbee30adc732014-05-09 15:10:18 -07001088 loc = UpdateLoc(loc);
1089 if ((loc.location == kLocPhysReg) && (loc.fp != loc.reg.IsFloat())) {
1090 if (GetRegInfo(loc.reg)->IsTemp()) {
1091 Clobber(loc.reg);
1092 FreeTemp(loc.reg);
1093 loc.reg = RegStorage::InvalidReg();
1094 loc.location = kLocDalvikFrame;
1095 }
1096 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001097 DCHECK(CheckCorePoolSanity());
buzbee30adc732014-05-09 15:10:18 -07001098 return loc;
1099}
1100
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001101RegLocation X86Mir2Lir::UpdateLocWideTyped(RegLocation loc) {
buzbee30adc732014-05-09 15:10:18 -07001102 loc = UpdateLocWide(loc);
1103 if ((loc.location == kLocPhysReg) && (loc.fp != loc.reg.IsFloat())) {
1104 if (GetRegInfo(loc.reg)->IsTemp()) {
1105 Clobber(loc.reg);
1106 FreeTemp(loc.reg);
1107 loc.reg = RegStorage::InvalidReg();
1108 loc.location = kLocDalvikFrame;
1109 }
1110 }
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001111 DCHECK(CheckCorePoolSanity());
buzbee30adc732014-05-09 15:10:18 -07001112 return loc;
1113}
Yixin Shou7071c8d2014-03-05 06:07:48 -05001114
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001115void X86Mir2Lir::AnalyzeInvokeStatic(int opcode, BasicBlock* bb, MIR* mir) {
1116 UNUSED(opcode, bb);
Alexei Zavjalov0e63ce12014-07-10 18:34:23 +07001117 // For now this is only actual for x86-32.
1118 if (cu_->target64) {
1119 return;
1120 }
1121
Yixin Shou7071c8d2014-03-05 06:07:48 -05001122 uint32_t index = mir->dalvikInsn.vB;
Vladimir Markoff0ac472014-10-02 17:24:53 +01001123 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1124 DexFileMethodInliner* method_inliner =
1125 cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file);
1126 InlineMethod method;
1127 if (method_inliner->IsIntrinsic(index, &method)) {
1128 switch (method.opcode) {
1129 case kIntrinsicAbsDouble:
1130 case kIntrinsicMinMaxDouble:
1131 store_method_addr_ = true;
1132 break;
1133 default:
1134 break;
Yixin Shou7071c8d2014-03-05 06:07:48 -05001135 }
1136 }
1137}
Andreas Gampe98430592014-07-27 19:44:50 -07001138
1139LIR* X86Mir2Lir::InvokeTrampoline(OpKind op, RegStorage r_tgt, QuickEntrypointEnum trampoline) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001140 UNUSED(r_tgt); // Call to absolute memory location doesn't need a temporary target register.
Andreas Gampe98430592014-07-27 19:44:50 -07001141 if (cu_->target64) {
1142 return OpThreadMem(op, GetThreadOffset<8>(trampoline));
1143 } else {
1144 return OpThreadMem(op, GetThreadOffset<4>(trampoline));
1145 }
1146}
1147
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148} // namespace art