blob: ffe6702bb855ab6b1e1184d4246909819edfe0f8 [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
Nicolas Geoffrayf3e2cc42014-02-18 18:37:26 +000017#include <string>
18#include <inttypes.h>
19
Andreas Gampe53c913b2014-08-12 23:19:23 -070020#include "backend_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "codegen_x86.h"
22#include "dex/compiler_internals.h"
23#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070024#include "dex/reg_storage_eq.h"
Mark Mendelle19c91f2014-02-25 08:19:08 -080025#include "mirror/array.h"
26#include "mirror/string.h"
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -070027#include "oat.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "x86_lir.h"
Tong Shen547cdfd2014-08-05 01:54:19 -070029#include "utils/dwarf_cfi.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030
Brian Carlstrom7940e442013-07-12 13:46:57 -070031namespace art {
32
Vladimir Marko089142c2014-06-05 10:57:05 +010033static constexpr RegStorage core_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070034 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
35};
Vladimir Marko089142c2014-06-05 10:57:05 +010036static constexpr RegStorage core_regs_arr_64[] = {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070037 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
buzbee091cc402014-03-31 10:14:40 -070038 rs_r8, rs_r9, rs_r10, rs_r11, rs_r12, rs_r13, rs_r14, rs_r15
Brian Carlstrom7940e442013-07-12 13:46:57 -070039};
Vladimir Marko089142c2014-06-05 10:57:05 +010040static constexpr RegStorage core_regs_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070041 rs_r0q, rs_r1q, rs_r2q, rs_r3q, rs_rX86_SP_64, rs_r5q, rs_r6q, rs_r7q,
Dmitry Petrochenkoa20468c2014-04-30 13:40:19 +070042 rs_r8q, rs_r9q, rs_r10q, rs_r11q, rs_r12q, rs_r13q, rs_r14q, rs_r15q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070043};
Vladimir Marko089142c2014-06-05 10:57:05 +010044static constexpr RegStorage sp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070045 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
46};
Vladimir Marko089142c2014-06-05 10:57:05 +010047static constexpr RegStorage sp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070048 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
buzbee091cc402014-03-31 10:14:40 -070049 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070050};
Vladimir Marko089142c2014-06-05 10:57:05 +010051static constexpr RegStorage dp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070052 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
53};
Vladimir Marko089142c2014-06-05 10:57:05 +010054static constexpr RegStorage dp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070055 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
buzbee091cc402014-03-31 10:14:40 -070056 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070057};
Serguei Katkovc3801912014-07-08 17:21:53 +070058static constexpr RegStorage xp_regs_arr_32[] = {
59 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
60};
61static constexpr RegStorage xp_regs_arr_64[] = {
62 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
63 rs_xr8, rs_xr9, rs_xr10, rs_xr11, rs_xr12, rs_xr13, rs_xr14, rs_xr15
64};
Vladimir Marko089142c2014-06-05 10:57:05 +010065static constexpr RegStorage reserved_regs_arr_32[] = {rs_rX86_SP_32};
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070066static constexpr RegStorage reserved_regs_arr_64[] = {rs_rX86_SP_32};
Vladimir Marko089142c2014-06-05 10:57:05 +010067static constexpr RegStorage reserved_regs_arr_64q[] = {rs_rX86_SP_64};
68static constexpr RegStorage core_temps_arr_32[] = {rs_rAX, rs_rCX, rs_rDX, rs_rBX};
69static constexpr RegStorage core_temps_arr_64[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070070 rs_rAX, rs_rCX, rs_rDX, rs_rSI, rs_rDI,
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070071 rs_r8, rs_r9, rs_r10, rs_r11
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070072};
Serguei Katkovc3801912014-07-08 17:21:53 +070073
74// How to add register to be available for promotion:
75// 1) Remove register from array defining temp
76// 2) Update ClobberCallerSave
77// 3) Update JNI compiler ABI:
78// 3.1) add reg in JniCallingConvention method
79// 3.2) update CoreSpillMask/FpSpillMask
80// 4) Update entrypoints
81// 4.1) Update constants in asm_support_x86_64.h for new frame size
82// 4.2) Remove entry in SmashCallerSaves
83// 4.3) Update jni_entrypoints to spill/unspill new callee save reg
84// 4.4) Update quick_entrypoints to spill/unspill new callee save reg
85// 5) Update runtime ABI
86// 5.1) Update quick_method_frame_info with new required spills
87// 5.2) Update QuickArgumentVisitor with new offsets to gprs and xmms
88// Note that you cannot use register corresponding to incoming args
89// according to ABI and QCG needs one additional XMM temp for
90// bulk copy in preparation to call.
Vladimir Marko089142c2014-06-05 10:57:05 +010091static constexpr RegStorage core_temps_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070092 rs_r0q, rs_r1q, rs_r2q, rs_r6q, rs_r7q,
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070093 rs_r8q, rs_r9q, rs_r10q, rs_r11q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070094};
Vladimir Marko089142c2014-06-05 10:57:05 +010095static constexpr RegStorage sp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070096 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
97};
Vladimir Marko089142c2014-06-05 10:57:05 +010098static constexpr RegStorage sp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070099 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700100 rs_fr8, rs_fr9, rs_fr10, rs_fr11
buzbee091cc402014-03-31 10:14:40 -0700101};
Vladimir Marko089142c2014-06-05 10:57:05 +0100102static constexpr RegStorage dp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700103 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
104};
Vladimir Marko089142c2014-06-05 10:57:05 +0100105static constexpr RegStorage dp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -0700106 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700107 rs_dr8, rs_dr9, rs_dr10, rs_dr11
buzbee091cc402014-03-31 10:14:40 -0700108};
109
Vladimir Marko089142c2014-06-05 10:57:05 +0100110static constexpr RegStorage xp_temps_arr_32[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400111 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
112};
Vladimir Marko089142c2014-06-05 10:57:05 +0100113static constexpr RegStorage xp_temps_arr_64[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400114 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700115 rs_xr8, rs_xr9, rs_xr10, rs_xr11
Mark Mendellfe945782014-05-22 09:52:36 -0400116};
117
Vladimir Marko089142c2014-06-05 10:57:05 +0100118static constexpr ArrayRef<const RegStorage> empty_pool;
119static constexpr ArrayRef<const RegStorage> core_regs_32(core_regs_arr_32);
120static constexpr ArrayRef<const RegStorage> core_regs_64(core_regs_arr_64);
121static constexpr ArrayRef<const RegStorage> core_regs_64q(core_regs_arr_64q);
122static constexpr ArrayRef<const RegStorage> sp_regs_32(sp_regs_arr_32);
123static constexpr ArrayRef<const RegStorage> sp_regs_64(sp_regs_arr_64);
124static constexpr ArrayRef<const RegStorage> dp_regs_32(dp_regs_arr_32);
125static constexpr ArrayRef<const RegStorage> dp_regs_64(dp_regs_arr_64);
Serguei Katkovc3801912014-07-08 17:21:53 +0700126static constexpr ArrayRef<const RegStorage> xp_regs_32(xp_regs_arr_32);
127static constexpr ArrayRef<const RegStorage> xp_regs_64(xp_regs_arr_64);
Vladimir Marko089142c2014-06-05 10:57:05 +0100128static constexpr ArrayRef<const RegStorage> reserved_regs_32(reserved_regs_arr_32);
129static constexpr ArrayRef<const RegStorage> reserved_regs_64(reserved_regs_arr_64);
130static constexpr ArrayRef<const RegStorage> reserved_regs_64q(reserved_regs_arr_64q);
131static constexpr ArrayRef<const RegStorage> core_temps_32(core_temps_arr_32);
132static constexpr ArrayRef<const RegStorage> core_temps_64(core_temps_arr_64);
133static constexpr ArrayRef<const RegStorage> core_temps_64q(core_temps_arr_64q);
134static constexpr ArrayRef<const RegStorage> sp_temps_32(sp_temps_arr_32);
135static constexpr ArrayRef<const RegStorage> sp_temps_64(sp_temps_arr_64);
136static constexpr ArrayRef<const RegStorage> dp_temps_32(dp_temps_arr_32);
137static constexpr ArrayRef<const RegStorage> dp_temps_64(dp_temps_arr_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700138
Vladimir Marko089142c2014-06-05 10:57:05 +0100139static constexpr ArrayRef<const RegStorage> xp_temps_32(xp_temps_arr_32);
140static constexpr ArrayRef<const RegStorage> xp_temps_64(xp_temps_arr_64);
Mark Mendellfe945782014-05-22 09:52:36 -0400141
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700142RegStorage rs_rX86_SP;
143
144X86NativeRegisterPool rX86_ARG0;
145X86NativeRegisterPool rX86_ARG1;
146X86NativeRegisterPool rX86_ARG2;
147X86NativeRegisterPool rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700148X86NativeRegisterPool rX86_ARG4;
149X86NativeRegisterPool rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700150X86NativeRegisterPool rX86_FARG0;
151X86NativeRegisterPool rX86_FARG1;
152X86NativeRegisterPool rX86_FARG2;
153X86NativeRegisterPool rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700154X86NativeRegisterPool rX86_FARG4;
155X86NativeRegisterPool rX86_FARG5;
156X86NativeRegisterPool rX86_FARG6;
157X86NativeRegisterPool rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700158X86NativeRegisterPool rX86_RET0;
159X86NativeRegisterPool rX86_RET1;
160X86NativeRegisterPool rX86_INVOKE_TGT;
161X86NativeRegisterPool rX86_COUNT;
162
163RegStorage rs_rX86_ARG0;
164RegStorage rs_rX86_ARG1;
165RegStorage rs_rX86_ARG2;
166RegStorage rs_rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700167RegStorage rs_rX86_ARG4;
168RegStorage rs_rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700169RegStorage rs_rX86_FARG0;
170RegStorage rs_rX86_FARG1;
171RegStorage rs_rX86_FARG2;
172RegStorage rs_rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700173RegStorage rs_rX86_FARG4;
174RegStorage rs_rX86_FARG5;
175RegStorage rs_rX86_FARG6;
176RegStorage rs_rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700177RegStorage rs_rX86_RET0;
178RegStorage rs_rX86_RET1;
179RegStorage rs_rX86_INVOKE_TGT;
180RegStorage rs_rX86_COUNT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700182RegLocation X86Mir2Lir::LocCReturn() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000183 return x86_loc_c_return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184}
185
buzbeea0cd2d72014-06-01 09:33:49 -0700186RegLocation X86Mir2Lir::LocCReturnRef() {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700187 return cu_->target64 ? x86_64_loc_c_return_ref : x86_loc_c_return_ref;
buzbeea0cd2d72014-06-01 09:33:49 -0700188}
189
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700190RegLocation X86Mir2Lir::LocCReturnWide() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700191 return cu_->target64 ? x86_64_loc_c_return_wide : x86_loc_c_return_wide;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192}
193
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700194RegLocation X86Mir2Lir::LocCReturnFloat() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000195 return x86_loc_c_return_float;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196}
197
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700198RegLocation X86Mir2Lir::LocCReturnDouble() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000199 return x86_loc_c_return_double;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200}
201
Chao-ying Fua77ee512014-07-01 17:43:41 -0700202// Return a target-dependent special register for 32-bit.
203RegStorage X86Mir2Lir::TargetReg32(SpecialTargetRegister reg) {
buzbee091cc402014-03-31 10:14:40 -0700204 RegStorage res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 switch (reg) {
buzbee091cc402014-03-31 10:14:40 -0700206 case kSelf: res_reg = RegStorage::InvalidReg(); break;
207 case kSuspend: res_reg = RegStorage::InvalidReg(); break;
208 case kLr: res_reg = RegStorage::InvalidReg(); break;
209 case kPc: res_reg = RegStorage::InvalidReg(); break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700210 case kSp: res_reg = rs_rX86_SP_32; break; // This must be the concrete one, as _SP is target-
211 // specific size.
buzbee091cc402014-03-31 10:14:40 -0700212 case kArg0: res_reg = rs_rX86_ARG0; break;
213 case kArg1: res_reg = rs_rX86_ARG1; break;
214 case kArg2: res_reg = rs_rX86_ARG2; break;
215 case kArg3: res_reg = rs_rX86_ARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700216 case kArg4: res_reg = rs_rX86_ARG4; break;
217 case kArg5: res_reg = rs_rX86_ARG5; break;
buzbee091cc402014-03-31 10:14:40 -0700218 case kFArg0: res_reg = rs_rX86_FARG0; break;
219 case kFArg1: res_reg = rs_rX86_FARG1; break;
220 case kFArg2: res_reg = rs_rX86_FARG2; break;
221 case kFArg3: res_reg = rs_rX86_FARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700222 case kFArg4: res_reg = rs_rX86_FARG4; break;
223 case kFArg5: res_reg = rs_rX86_FARG5; break;
224 case kFArg6: res_reg = rs_rX86_FARG6; break;
225 case kFArg7: res_reg = rs_rX86_FARG7; break;
buzbee091cc402014-03-31 10:14:40 -0700226 case kRet0: res_reg = rs_rX86_RET0; break;
227 case kRet1: res_reg = rs_rX86_RET1; break;
228 case kInvokeTgt: res_reg = rs_rX86_INVOKE_TGT; break;
229 case kHiddenArg: res_reg = rs_rAX; break;
Elena Sayapinadd644502014-07-01 18:39:52 +0700230 case kHiddenFpArg: DCHECK(!cu_->target64); res_reg = rs_fr0; break;
buzbee091cc402014-03-31 10:14:40 -0700231 case kCount: res_reg = rs_rX86_COUNT; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700232 default: res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 }
buzbee091cc402014-03-31 10:14:40 -0700234 return res_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235}
236
Chao-ying Fua77ee512014-07-01 17:43:41 -0700237RegStorage X86Mir2Lir::TargetReg(SpecialTargetRegister reg) {
238 LOG(FATAL) << "Do not use this function!!!";
239 return RegStorage::InvalidReg();
240}
241
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242/*
243 * Decode the register id.
244 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100245ResourceMask X86Mir2Lir::GetRegMaskCommon(const RegStorage& reg) const {
246 /* Double registers in x86 are just a single FP register. This is always just a single bit. */
247 return ResourceMask::Bit(
248 /* FP register starts at bit position 16 */
249 ((reg.IsFloat() || reg.StorageSize() > 8) ? kX86FPReg0 : 0) + reg.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250}
251
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100252ResourceMask X86Mir2Lir::GetPCUseDefEncoding() const {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100253 return kEncodeNone;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254}
255
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100256void X86Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags,
257 ResourceMask* use_mask, ResourceMask* def_mask) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700258 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbeeb48819d2013-09-14 16:15:25 -0700259 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260
261 // X86-specific resource map setup here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 if (flags & REG_USE_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100263 use_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 }
265
266 if (flags & REG_DEF_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100267 def_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 }
269
270 if (flags & REG_DEFA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100271 SetupRegMask(def_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 }
273
274 if (flags & REG_DEFD) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100275 SetupRegMask(def_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 }
277 if (flags & REG_USEA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100278 SetupRegMask(use_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 }
280
281 if (flags & REG_USEC) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100282 SetupRegMask(use_mask, rs_rCX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 }
284
285 if (flags & REG_USED) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100286 SetupRegMask(use_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 }
Vladimir Marko70b797d2013-12-03 15:25:24 +0000288
289 if (flags & REG_USEB) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100290 SetupRegMask(use_mask, rs_rBX.GetReg());
Vladimir Marko70b797d2013-12-03 15:25:24 +0000291 }
Mark Mendell4028a6c2014-02-19 20:06:20 -0800292
293 // Fixup hard to describe instruction: Uses rAX, rCX, rDI; sets rDI.
294 if (lir->opcode == kX86RepneScasw) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100295 SetupRegMask(use_mask, rs_rAX.GetReg());
296 SetupRegMask(use_mask, rs_rCX.GetReg());
297 SetupRegMask(use_mask, rs_rDI.GetReg());
298 SetupRegMask(def_mask, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800299 }
Serguei Katkove90501d2014-03-12 15:56:54 +0700300
301 if (flags & USE_FP_STACK) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100302 use_mask->SetBit(kX86FPStack);
303 def_mask->SetBit(kX86FPStack);
Serguei Katkove90501d2014-03-12 15:56:54 +0700304 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305}
306
307/* For dumping instructions */
308static const char* x86RegName[] = {
309 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
310 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
311};
312
313static const char* x86CondName[] = {
314 "O",
315 "NO",
316 "B/NAE/C",
317 "NB/AE/NC",
318 "Z/EQ",
319 "NZ/NE",
320 "BE/NA",
321 "NBE/A",
322 "S",
323 "NS",
324 "P/PE",
325 "NP/PO",
326 "L/NGE",
327 "NL/GE",
328 "LE/NG",
329 "NLE/G"
330};
331
332/*
333 * Interpret a format string and build a string no longer than size
334 * See format key in Assemble.cc.
335 */
336std::string X86Mir2Lir::BuildInsnString(const char *fmt, LIR *lir, unsigned char* base_addr) {
337 std::string buf;
338 size_t i = 0;
339 size_t fmt_len = strlen(fmt);
340 while (i < fmt_len) {
341 if (fmt[i] != '!') {
342 buf += fmt[i];
343 i++;
344 } else {
345 i++;
346 DCHECK_LT(i, fmt_len);
347 char operand_number_ch = fmt[i];
348 i++;
349 if (operand_number_ch == '!') {
350 buf += "!";
351 } else {
352 int operand_number = operand_number_ch - '0';
353 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
354 DCHECK_LT(i, fmt_len);
355 int operand = lir->operands[operand_number];
356 switch (fmt[i]) {
357 case 'c':
358 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
359 buf += x86CondName[operand];
360 break;
361 case 'd':
362 buf += StringPrintf("%d", operand);
363 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400364 case 'q': {
365 int64_t value = static_cast<int64_t>(static_cast<int64_t>(operand) << 32 |
366 static_cast<uint32_t>(lir->operands[operand_number+1]));
367 buf +=StringPrintf("%" PRId64, value);
Haitao Fenge70f1792014-08-09 08:31:02 +0800368 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400369 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 case 'p': {
buzbee0d829482013-10-11 15:24:55 -0700371 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(operand));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 buf += StringPrintf("0x%08x", tab_rec->offset);
373 break;
374 }
375 case 'r':
buzbee091cc402014-03-31 10:14:40 -0700376 if (RegStorage::IsFloat(operand)) {
377 int fp_reg = RegStorage::RegNum(operand);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 buf += StringPrintf("xmm%d", fp_reg);
379 } else {
buzbee091cc402014-03-31 10:14:40 -0700380 int reg_num = RegStorage::RegNum(operand);
381 DCHECK_LT(static_cast<size_t>(reg_num), sizeof(x86RegName));
382 buf += x86RegName[reg_num];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 }
384 break;
385 case 't':
Ian Rogers107c31e2014-01-23 20:55:29 -0800386 buf += StringPrintf("0x%08" PRIxPTR " (L%p)",
387 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + operand,
388 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 break;
390 default:
391 buf += StringPrintf("DecodeError '%c'", fmt[i]);
392 break;
393 }
394 i++;
395 }
396 }
397 }
398 return buf;
399}
400
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100401void X86Mir2Lir::DumpResourceMask(LIR *x86LIR, const ResourceMask& mask, const char *prefix) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 char buf[256];
403 buf[0] = 0;
404
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100405 if (mask.Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 strcpy(buf, "all");
407 } else {
408 char num[8];
409 int i;
410
411 for (i = 0; i < kX86RegEnd; i++) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100412 if (mask.HasBit(i)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800413 snprintf(num, arraysize(num), "%d ", i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 strcat(buf, num);
415 }
416 }
417
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100418 if (mask.HasBit(ResourceMask::kCCode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 strcat(buf, "cc ");
420 }
421 /* Memory bits */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100422 if (x86LIR && (mask.HasBit(ResourceMask::kDalvikReg))) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800423 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
424 DECODE_ALIAS_INFO_REG(x86LIR->flags.alias_info),
425 (DECODE_ALIAS_INFO_WIDE(x86LIR->flags.alias_info)) ? "(+1)" : "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100427 if (mask.HasBit(ResourceMask::kLiteral)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 strcat(buf, "lit ");
429 }
430
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100431 if (mask.HasBit(ResourceMask::kHeapRef)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 strcat(buf, "heap ");
433 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100434 if (mask.HasBit(ResourceMask::kMustNotAlias)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 strcat(buf, "noalias ");
436 }
437 }
438 if (buf[0]) {
439 LOG(INFO) << prefix << ": " << buf;
440 }
441}
442
443void X86Mir2Lir::AdjustSpillMask() {
444 // Adjustment for LR spilling, x86 has no LR so nothing to do here
buzbee091cc402014-03-31 10:14:40 -0700445 core_spill_mask_ |= (1 << rs_rRET.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 num_core_spills_++;
447}
448
Mark Mendelle87f9b52014-04-30 14:13:18 -0400449RegStorage X86Mir2Lir::AllocateByteRegister() {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700450 RegStorage reg = AllocTypedTemp(false, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +0700451 if (!cu_->target64) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700452 DCHECK_LT(reg.GetRegNum(), rs_rX86_SP.GetRegNum());
453 }
454 return reg;
455}
456
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700457RegStorage X86Mir2Lir::Get128BitRegister(RegStorage reg) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700458 return GetRegInfo(reg)->Master()->GetReg();
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700459}
460
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700461bool X86Mir2Lir::IsByteRegister(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700462 return cu_->target64 || reg.GetRegNum() < rs_rX86_SP.GetRegNum();
Mark Mendelle87f9b52014-04-30 14:13:18 -0400463}
464
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465/* Clobber all regs that might be used by an external C call */
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000466void X86Mir2Lir::ClobberCallerSave() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700467 if (cu_->target64) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700468 Clobber(rs_rAX);
469 Clobber(rs_rCX);
470 Clobber(rs_rDX);
471 Clobber(rs_rSI);
472 Clobber(rs_rDI);
473
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700474 Clobber(rs_r8);
475 Clobber(rs_r9);
476 Clobber(rs_r10);
477 Clobber(rs_r11);
478
479 Clobber(rs_fr8);
480 Clobber(rs_fr9);
481 Clobber(rs_fr10);
482 Clobber(rs_fr11);
Serguei Katkovc3801912014-07-08 17:21:53 +0700483 } else {
484 Clobber(rs_rAX);
485 Clobber(rs_rCX);
486 Clobber(rs_rDX);
487 Clobber(rs_rBX);
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700488 }
Serguei Katkovc3801912014-07-08 17:21:53 +0700489
490 Clobber(rs_fr0);
491 Clobber(rs_fr1);
492 Clobber(rs_fr2);
493 Clobber(rs_fr3);
494 Clobber(rs_fr4);
495 Clobber(rs_fr5);
496 Clobber(rs_fr6);
497 Clobber(rs_fr7);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498}
499
500RegLocation X86Mir2Lir::GetReturnWideAlt() {
501 RegLocation res = LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -0700502 DCHECK(res.reg.GetLowReg() == rs_rAX.GetReg());
503 DCHECK(res.reg.GetHighReg() == rs_rDX.GetReg());
504 Clobber(rs_rAX);
505 Clobber(rs_rDX);
506 MarkInUse(rs_rAX);
507 MarkInUse(rs_rDX);
508 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 return res;
510}
511
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700512RegLocation X86Mir2Lir::GetReturnAlt() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 RegLocation res = LocCReturn();
buzbee091cc402014-03-31 10:14:40 -0700514 res.reg.SetReg(rs_rDX.GetReg());
515 Clobber(rs_rDX);
516 MarkInUse(rs_rDX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 return res;
518}
519
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700521void X86Mir2Lir::LockCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700522 LockTemp(rs_rX86_ARG0);
523 LockTemp(rs_rX86_ARG1);
524 LockTemp(rs_rX86_ARG2);
525 LockTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700526 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700527 LockTemp(rs_rX86_ARG4);
528 LockTemp(rs_rX86_ARG5);
529 LockTemp(rs_rX86_FARG0);
530 LockTemp(rs_rX86_FARG1);
531 LockTemp(rs_rX86_FARG2);
532 LockTemp(rs_rX86_FARG3);
533 LockTemp(rs_rX86_FARG4);
534 LockTemp(rs_rX86_FARG5);
535 LockTemp(rs_rX86_FARG6);
536 LockTemp(rs_rX86_FARG7);
537 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538}
539
540/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700541void X86Mir2Lir::FreeCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700542 FreeTemp(rs_rX86_ARG0);
543 FreeTemp(rs_rX86_ARG1);
544 FreeTemp(rs_rX86_ARG2);
545 FreeTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700546 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700547 FreeTemp(rs_rX86_ARG4);
548 FreeTemp(rs_rX86_ARG5);
549 FreeTemp(rs_rX86_FARG0);
550 FreeTemp(rs_rX86_FARG1);
551 FreeTemp(rs_rX86_FARG2);
552 FreeTemp(rs_rX86_FARG3);
553 FreeTemp(rs_rX86_FARG4);
554 FreeTemp(rs_rX86_FARG5);
555 FreeTemp(rs_rX86_FARG6);
556 FreeTemp(rs_rX86_FARG7);
557 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558}
559
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800560bool X86Mir2Lir::ProvidesFullMemoryBarrier(X86OpCode opcode) {
561 switch (opcode) {
562 case kX86LockCmpxchgMR:
563 case kX86LockCmpxchgAR:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700564 case kX86LockCmpxchg64M:
565 case kX86LockCmpxchg64A:
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800566 case kX86XchgMR:
567 case kX86Mfence:
568 // Atomic memory instructions provide full barrier.
569 return true;
570 default:
571 break;
572 }
573
574 // Conservative if cannot prove it provides full barrier.
575 return false;
576}
577
Andreas Gampeb14329f2014-05-15 11:16:06 -0700578bool X86Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800580 // Start off with using the last LIR as the barrier. If it is not enough, then we will update it.
581 LIR* mem_barrier = last_lir_insn_;
582
Andreas Gampeb14329f2014-05-15 11:16:06 -0700583 bool ret = false;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800584 /*
Hans Boehm48f5c472014-06-27 14:50:10 -0700585 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
586 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
587 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800588 */
Hans Boehm48f5c472014-06-27 14:50:10 -0700589 if (barrier_kind == kAnyAny) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800590 // If no LIR exists already that can be used a barrier, then generate an mfence.
591 if (mem_barrier == nullptr) {
592 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700593 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800594 }
595
596 // If last instruction does not provide full barrier, then insert an mfence.
597 if (ProvidesFullMemoryBarrier(static_cast<X86OpCode>(mem_barrier->opcode)) == false) {
598 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700599 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800600 }
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700601 } else if (barrier_kind == kNTStoreStore) {
602 mem_barrier = NewLIR0(kX86Sfence);
603 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800604 }
605
606 // Now ensure that a scheduling barrier is in place.
607 if (mem_barrier == nullptr) {
608 GenBarrier();
609 } else {
610 // Mark as a scheduling barrier.
611 DCHECK(!mem_barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100612 mem_barrier->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800613 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700614 return ret;
615#else
616 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617#endif
618}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000619
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620void X86Mir2Lir::CompilerInitializeRegAlloc() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700621 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700622 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_64, core_regs_64q, sp_regs_64,
623 dp_regs_64, reserved_regs_64, reserved_regs_64q,
624 core_temps_64, core_temps_64q, sp_temps_64, dp_temps_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700625 } else {
buzbeeb01bf152014-05-13 15:59:07 -0700626 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
627 dp_regs_32, reserved_regs_32, empty_pool,
628 core_temps_32, empty_pool, sp_temps_32, dp_temps_32);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700629 }
buzbee091cc402014-03-31 10:14:40 -0700630
631 // Target-specific adjustments.
632
Mark Mendellfe945782014-05-22 09:52:36 -0400633 // Add in XMM registers.
Serguei Katkovc3801912014-07-08 17:21:53 +0700634 const ArrayRef<const RegStorage> *xp_regs = cu_->target64 ? &xp_regs_64 : &xp_regs_32;
635 for (RegStorage reg : *xp_regs) {
Mark Mendellfe945782014-05-22 09:52:36 -0400636 RegisterInfo* info = new (arena_) RegisterInfo(reg, GetRegMaskCommon(reg));
637 reginfo_map_.Put(reg.GetReg(), info);
Serguei Katkovc3801912014-07-08 17:21:53 +0700638 }
639 const ArrayRef<const RegStorage> *xp_temps = cu_->target64 ? &xp_temps_64 : &xp_temps_32;
640 for (RegStorage reg : *xp_temps) {
641 RegisterInfo* xp_reg_info = GetRegInfo(reg);
642 xp_reg_info->SetIsTemp(true);
Mark Mendellfe945782014-05-22 09:52:36 -0400643 }
644
buzbee091cc402014-03-31 10:14:40 -0700645 // Alias single precision xmm to double xmms.
646 // TODO: as needed, add larger vector sizes - alias all to the largest.
647 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
648 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
649 int sp_reg_num = info->GetReg().GetRegNum();
Mark Mendellfe945782014-05-22 09:52:36 -0400650 RegStorage xp_reg = RegStorage::Solo128(sp_reg_num);
651 RegisterInfo* xp_reg_info = GetRegInfo(xp_reg);
652 // 128-bit xmm vector register's master storage should refer to itself.
653 DCHECK_EQ(xp_reg_info, xp_reg_info->Master());
654
655 // Redirect 32-bit vector's master storage to 128-bit vector.
656 info->SetMaster(xp_reg_info);
657
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700658 RegStorage dp_reg = RegStorage::FloatSolo64(sp_reg_num);
buzbee091cc402014-03-31 10:14:40 -0700659 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
Mark Mendellfe945782014-05-22 09:52:36 -0400660 // Redirect 64-bit vector's master storage to 128-bit vector.
661 dp_reg_info->SetMaster(xp_reg_info);
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700662 // Singles should show a single 32-bit mask bit, at first referring to the low half.
663 DCHECK_EQ(info->StorageMask(), 0x1U);
664 }
665
Elena Sayapinadd644502014-07-01 18:39:52 +0700666 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700667 // Alias 32bit W registers to corresponding 64bit X registers.
668 GrowableArray<RegisterInfo*>::Iterator w_it(&reg_pool_->core_regs_);
669 for (RegisterInfo* info = w_it.Next(); info != nullptr; info = w_it.Next()) {
670 int x_reg_num = info->GetReg().GetRegNum();
671 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
672 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
673 // 64bit X register's master storage should refer to itself.
674 DCHECK_EQ(x_reg_info, x_reg_info->Master());
675 // Redirect 32bit W master storage to 64bit X.
676 info->SetMaster(x_reg_info);
677 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
678 DCHECK_EQ(info->StorageMask(), 0x1U);
679 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 }
buzbee091cc402014-03-31 10:14:40 -0700681
682 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
683 // TODO: adjust for x86/hard float calling convention.
684 reg_pool_->next_core_reg_ = 2;
685 reg_pool_->next_sp_reg_ = 2;
686 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687}
688
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700689int X86Mir2Lir::VectorRegisterSize() {
690 return 128;
691}
692
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700693int X86Mir2Lir::NumReservableVectorRegisters(bool long_or_fp) {
694 int num_vector_temps = cu_->target64 ? xp_temps_64.size() : xp_temps_32.size();
695
696 // Leave a few temps for use by backend as scratch.
697 return long_or_fp ? num_vector_temps - 2 : num_vector_temps - 1;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700698}
699
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700void X86Mir2Lir::SpillCoreRegs() {
701 if (num_core_spills_ == 0) {
702 return;
703 }
704 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700705 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700706 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700707 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 for (int reg = 0; mask; mask >>= 1, reg++) {
709 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700710 StoreBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
711 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700712 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 }
714 }
715}
716
717void X86Mir2Lir::UnSpillCoreRegs() {
718 if (num_core_spills_ == 0) {
719 return;
720 }
721 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700722 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700723 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700724 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 for (int reg = 0; mask; mask >>= 1, reg++) {
726 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700727 LoadBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
728 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700729 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 }
731 }
732}
733
Serguei Katkovc3801912014-07-08 17:21:53 +0700734void X86Mir2Lir::SpillFPRegs() {
735 if (num_fp_spills_ == 0) {
736 return;
737 }
738 uint32_t mask = fp_spill_mask_;
739 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
740 for (int reg = 0; mask; mask >>= 1, reg++) {
741 if (mask & 0x1) {
742 StoreBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
743 k64, kNotVolatile);
744 offset += sizeof(double);
745 }
746 }
747}
748void X86Mir2Lir::UnSpillFPRegs() {
749 if (num_fp_spills_ == 0) {
750 return;
751 }
752 uint32_t mask = fp_spill_mask_;
753 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
754 for (int reg = 0; mask; mask >>= 1, reg++) {
755 if (mask & 0x1) {
756 LoadBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
757 k64, kNotVolatile);
758 offset += sizeof(double);
759 }
760 }
761}
762
763
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700764bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
766}
767
Vladimir Marko674744e2014-04-24 15:18:26 +0100768RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700769 // X86_64 can handle any size.
Elena Sayapinadd644502014-07-01 18:39:52 +0700770 if (cu_->target64) {
Chao-ying Fu06839f82014-08-14 15:59:17 -0700771 return RegClassBySize(size);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700772 }
773
Vladimir Marko674744e2014-04-24 15:18:26 +0100774 if (UNLIKELY(is_volatile)) {
775 // On x86, atomic 64-bit load/store requires an fp register.
776 // Smaller aligned load/store is atomic for both core and fp registers.
777 if (size == k64 || size == kDouble) {
778 return kFPReg;
779 }
780 }
781 return RegClassBySize(size);
782}
783
Elena Sayapinadd644502014-07-01 18:39:52 +0700784X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800785 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700786 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800787 method_address_insns_(arena, 100, kGrowableArrayMisc),
788 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800789 call_method_insns_(arena, 100, kGrowableArrayMisc),
Elena Sayapinadd644502014-07-01 18:39:52 +0700790 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400791 const_vectors_(nullptr) {
792 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700793 if (kIsDebugBuild) {
794 for (int i = 0; i < kX86Last; i++) {
795 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
796 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400797 << " is wrong: expecting " << i << ", seeing "
798 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700799 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 }
801 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700802 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700803 rs_rX86_SP = rs_rX86_SP_64;
804
805 rs_rX86_ARG0 = rs_rDI;
806 rs_rX86_ARG1 = rs_rSI;
807 rs_rX86_ARG2 = rs_rDX;
808 rs_rX86_ARG3 = rs_rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700809 rs_rX86_ARG4 = rs_r8;
810 rs_rX86_ARG5 = rs_r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700811 rs_rX86_FARG0 = rs_fr0;
812 rs_rX86_FARG1 = rs_fr1;
813 rs_rX86_FARG2 = rs_fr2;
814 rs_rX86_FARG3 = rs_fr3;
815 rs_rX86_FARG4 = rs_fr4;
816 rs_rX86_FARG5 = rs_fr5;
817 rs_rX86_FARG6 = rs_fr6;
818 rs_rX86_FARG7 = rs_fr7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700819 rX86_ARG0 = rDI;
820 rX86_ARG1 = rSI;
821 rX86_ARG2 = rDX;
822 rX86_ARG3 = rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700823 rX86_ARG4 = r8;
824 rX86_ARG5 = r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700825 rX86_FARG0 = fr0;
826 rX86_FARG1 = fr1;
827 rX86_FARG2 = fr2;
828 rX86_FARG3 = fr3;
829 rX86_FARG4 = fr4;
830 rX86_FARG5 = fr5;
831 rX86_FARG6 = fr6;
832 rX86_FARG7 = fr7;
Mark Mendell55884bc2014-06-10 10:21:29 -0400833 rs_rX86_INVOKE_TGT = rs_rDI;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700834 } else {
835 rs_rX86_SP = rs_rX86_SP_32;
836
837 rs_rX86_ARG0 = rs_rAX;
838 rs_rX86_ARG1 = rs_rCX;
839 rs_rX86_ARG2 = rs_rDX;
840 rs_rX86_ARG3 = rs_rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700841 rs_rX86_ARG4 = RegStorage::InvalidReg();
842 rs_rX86_ARG5 = RegStorage::InvalidReg();
843 rs_rX86_FARG0 = rs_rAX;
844 rs_rX86_FARG1 = rs_rCX;
845 rs_rX86_FARG2 = rs_rDX;
846 rs_rX86_FARG3 = rs_rBX;
847 rs_rX86_FARG4 = RegStorage::InvalidReg();
848 rs_rX86_FARG5 = RegStorage::InvalidReg();
849 rs_rX86_FARG6 = RegStorage::InvalidReg();
850 rs_rX86_FARG7 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700851 rX86_ARG0 = rAX;
852 rX86_ARG1 = rCX;
853 rX86_ARG2 = rDX;
854 rX86_ARG3 = rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700855 rX86_FARG0 = rAX;
856 rX86_FARG1 = rCX;
857 rX86_FARG2 = rDX;
858 rX86_FARG3 = rBX;
Mark Mendell55884bc2014-06-10 10:21:29 -0400859 rs_rX86_INVOKE_TGT = rs_rAX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700860 // TODO(64): Initialize with invalid reg
861// rX86_ARG4 = RegStorage::InvalidReg();
862// rX86_ARG5 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700863 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700864 rs_rX86_RET0 = rs_rAX;
865 rs_rX86_RET1 = rs_rDX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700866 rs_rX86_COUNT = rs_rCX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700867 rX86_RET0 = rAX;
868 rX86_RET1 = rDX;
869 rX86_INVOKE_TGT = rAX;
870 rX86_COUNT = rCX;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871}
872
873Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
874 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700875 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876}
877
Andreas Gampe98430592014-07-27 19:44:50 -0700878// Not used in x86(-64)
879RegStorage X86Mir2Lir::LoadHelper(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700880 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
881 return RegStorage::InvalidReg();
882}
883
Dave Allisonb373e092014-02-20 16:06:36 -0800884LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
Dave Allison69dfe512014-07-11 17:11:58 +0000885 // First load the pointer in fs:[suspend-trigger] into eax
886 // Then use a test instruction to indirect via that address.
Dave Allisondfd3b472014-07-16 16:04:32 -0700887 if (cu_->target64) {
888 NewLIR2(kX86Mov64RT, rs_rAX.GetReg(),
889 Thread::ThreadSuspendTriggerOffset<8>().Int32Value());
890 } else {
891 NewLIR2(kX86Mov32RT, rs_rAX.GetReg(),
892 Thread::ThreadSuspendTriggerOffset<4>().Int32Value());
893 }
Dave Allison69dfe512014-07-11 17:11:58 +0000894 return NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rAX.GetReg(), 0);
Dave Allisonb373e092014-02-20 16:06:36 -0800895}
896
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700897uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700898 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 return X86Mir2Lir::EncodingMap[opcode].flags;
900}
901
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700902const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700903 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 return X86Mir2Lir::EncodingMap[opcode].name;
905}
906
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700907const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700908 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 return X86Mir2Lir::EncodingMap[opcode].fmt;
910}
911
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000912void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
913 // Can we do this directly to memory?
914 rl_dest = UpdateLocWide(rl_dest);
915 if ((rl_dest.location == kLocDalvikFrame) ||
916 (rl_dest.location == kLocCompilerTemp)) {
917 int32_t val_lo = Low32Bits(value);
918 int32_t val_hi = High32Bits(value);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700919 int r_base = rs_rX86_SP.GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000920 int displacement = SRegOffset(rl_dest.s_reg_low);
921
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100922 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800923 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000924 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
925 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800926 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000927 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
928 false /* is_load */, true /* is64bit */);
929 return;
930 }
931
932 // Just use the standard code to do the generation.
933 Mir2Lir::GenConstWide(rl_dest, value);
934}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800935
936// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
937void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
938 LOG(INFO) << "location: " << loc.location << ','
939 << (loc.wide ? " w" : " ")
940 << (loc.defined ? " D" : " ")
941 << (loc.is_const ? " c" : " ")
942 << (loc.fp ? " F" : " ")
943 << (loc.core ? " C" : " ")
944 << (loc.ref ? " r" : " ")
945 << (loc.high_word ? " h" : " ")
946 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800947 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000948 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800949 << ", s_reg: " << loc.s_reg_low
950 << ", orig: " << loc.orig_sreg;
951}
952
Mark Mendell67c39c42014-01-31 17:28:00 -0800953void X86Mir2Lir::Materialize() {
954 // A good place to put the analysis before starting.
955 AnalyzeMIR();
956
957 // Now continue with regular code generation.
958 Mir2Lir::Materialize();
959}
960
Jeff Hao49161ce2014-03-12 11:05:25 -0700961void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800962 SpecialTargetRegister symbolic_reg) {
963 /*
964 * For x86, just generate a 32 bit move immediate instruction, that will be filled
965 * in at 'link time'. For now, put a unique value based on target to ensure that
966 * code deduplication works.
967 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700968 int target_method_idx = target_method.dex_method_index;
969 const DexFile* target_dex_file = target_method.dex_file;
970 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
971 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800972
Jeff Hao49161ce2014-03-12 11:05:25 -0700973 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700974 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
975 TargetReg(symbolic_reg, kNotWide).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700976 static_cast<int>(target_method_id_ptr), target_method_idx,
977 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800978 AppendLIR(move);
979 method_address_insns_.Insert(move);
980}
981
Fred Shihe7f82e22014-08-06 10:46:37 -0700982void X86Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
983 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800984 /*
985 * For x86, just generate a 32 bit move immediate instruction, that will be filled
986 * in at 'link time'. For now, put a unique value based on target to ensure that
987 * code deduplication works.
988 */
Fred Shihe7f82e22014-08-06 10:46:37 -0700989 const DexFile::TypeId& id = dex_file.GetTypeId(type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800990 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
991
992 // Generate the move instruction with the unique pointer and save index and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700993 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
994 TargetReg(symbolic_reg, kNotWide).GetReg(),
Fred Shihe7f82e22014-08-06 10:46:37 -0700995 static_cast<int>(ptr), type_idx,
996 WrapPointer(const_cast<DexFile*>(&dex_file)));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800997 AppendLIR(move);
998 class_type_address_insns_.Insert(move);
999}
1000
Jeff Hao49161ce2014-03-12 11:05:25 -07001001LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001002 /*
1003 * For x86, just generate a 32 bit call relative instruction, that will be filled
1004 * in at 'link time'. For now, put a unique value based on target to ensure that
1005 * code deduplication works.
1006 */
Jeff Hao49161ce2014-03-12 11:05:25 -07001007 int target_method_idx = target_method.dex_method_index;
1008 const DexFile* target_dex_file = target_method.dex_file;
1009 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
1010 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001011
Jeff Hao49161ce2014-03-12 11:05:25 -07001012 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
1013 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
1014 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001015 AppendLIR(call);
1016 call_method_insns_.Insert(call);
1017 return call;
1018}
1019
1020void X86Mir2Lir::InstallLiteralPools() {
1021 // These are handled differently for x86.
1022 DCHECK(code_literal_list_ == nullptr);
1023 DCHECK(method_literal_list_ == nullptr);
1024 DCHECK(class_literal_list_ == nullptr);
1025
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001026
Mark Mendelld65c51a2014-04-29 16:55:20 -04001027 if (const_vectors_ != nullptr) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001028 // Vector literals must be 16-byte aligned. The header that is placed
1029 // in the code section causes misalignment so we take it into account.
1030 // Otherwise, we are sure that for x86 method is aligned to 16.
1031 DCHECK_EQ(GetInstructionSetAlignment(cu_->instruction_set), 16u);
1032 uint32_t bytes_to_fill = (0x10 - ((code_buffer_.size() + sizeof(OatQuickMethodHeader)) & 0xF)) & 0xF;
1033 while (bytes_to_fill > 0) {
1034 code_buffer_.push_back(0);
1035 bytes_to_fill--;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001036 }
1037
Mark Mendelld65c51a2014-04-29 16:55:20 -04001038 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001039 PushWord(&code_buffer_, p->operands[0]);
1040 PushWord(&code_buffer_, p->operands[1]);
1041 PushWord(&code_buffer_, p->operands[2]);
1042 PushWord(&code_buffer_, p->operands[3]);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001043 }
1044 }
1045
Mark Mendell55d0eac2014-02-06 11:02:52 -08001046 // Handle the fixups for methods.
1047 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
1048 LIR* p = method_address_insns_.Get(i);
1049 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001050 uint32_t target_method_idx = p->operands[2];
1051 const DexFile* target_dex_file =
1052 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001053
1054 // The offset to patch is the last 4 bytes of the instruction.
1055 int patch_offset = p->offset + p->flags.size - 4;
1056 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
1057 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -07001058 target_method_idx, target_dex_file,
1059 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001060 patch_offset);
1061 }
1062
1063 // Handle the fixups for class types.
1064 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
1065 LIR* p = class_type_address_insns_.Get(i);
1066 DCHECK_EQ(p->opcode, kX86Mov32RI);
Fred Shihe7f82e22014-08-06 10:46:37 -07001067
1068 const DexFile* class_dex_file =
1069 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Jeff Hao49161ce2014-03-12 11:05:25 -07001070 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001071
1072 // The offset to patch is the last 4 bytes of the instruction.
1073 int patch_offset = p->offset + p->flags.size - 4;
1074 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -07001075 cu_->method_idx, target_method_idx, class_dex_file,
1076 patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001077 }
1078
1079 // And now the PC-relative calls to methods.
1080 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
1081 LIR* p = call_method_insns_.Get(i);
1082 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001083 uint32_t target_method_idx = p->operands[1];
1084 const DexFile* target_dex_file =
1085 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001086
1087 // The offset to patch is the last 4 bytes of the instruction.
1088 int patch_offset = p->offset + p->flags.size - 4;
1089 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001090 cu_->method_idx, cu_->invoke_type,
1091 target_method_idx, target_dex_file,
1092 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001093 patch_offset, -4 /* offset */);
1094 }
1095
1096 // And do the normal processing.
1097 Mir2Lir::InstallLiteralPools();
1098}
1099
DaniilSokolov70c4f062014-06-24 17:34:00 -07001100bool X86Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
DaniilSokolov70c4f062014-06-24 17:34:00 -07001101 RegLocation rl_src = info->args[0];
1102 RegLocation rl_srcPos = info->args[1];
1103 RegLocation rl_dst = info->args[2];
1104 RegLocation rl_dstPos = info->args[3];
1105 RegLocation rl_length = info->args[4];
1106 if (rl_srcPos.is_const && (mir_graph_->ConstantValue(rl_srcPos) < 0)) {
1107 return false;
1108 }
1109 if (rl_dstPos.is_const && (mir_graph_->ConstantValue(rl_dstPos) < 0)) {
1110 return false;
1111 }
1112 ClobberCallerSave();
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001113 LockCallTemps(); // Using fixed registers.
1114 RegStorage tmp_reg = cu_->target64 ? rs_r11 : rs_rBX;
1115 LoadValueDirectFixed(rl_src, rs_rAX);
1116 LoadValueDirectFixed(rl_dst, rs_rCX);
1117 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_rAX, rs_rCX, nullptr);
1118 LIR* src_null_branch = OpCmpImmBranch(kCondEq, rs_rAX, 0, nullptr);
1119 LIR* dst_null_branch = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1120 LoadValueDirectFixed(rl_length, rs_rDX);
1121 // If the length of the copy is > 128 characters (256 bytes) or negative then go slow path.
1122 LIR* len_too_big = OpCmpImmBranch(kCondHi, rs_rDX, 128, nullptr);
1123 LoadValueDirectFixed(rl_src, rs_rAX);
1124 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001125 LIR* src_bad_len = nullptr;
1126 LIR* srcPos_negative = nullptr;
1127 if (!rl_srcPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001128 LoadValueDirectFixed(rl_srcPos, tmp_reg);
1129 srcPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1130 OpRegReg(kOpAdd, tmp_reg, rs_rDX);
1131 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001132 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001133 int32_t pos_val = mir_graph_->ConstantValue(rl_srcPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001134 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001135 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001136 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001137 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1138 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001139 }
1140 }
1141 LIR* dstPos_negative = nullptr;
1142 LIR* dst_bad_len = nullptr;
1143 LoadValueDirectFixed(rl_dst, rs_rAX);
1144 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1145 if (!rl_dstPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001146 LoadValueDirectFixed(rl_dstPos, tmp_reg);
1147 dstPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1148 OpRegRegReg(kOpAdd, tmp_reg, tmp_reg, rs_rDX);
1149 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001150 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001151 int32_t pos_val = mir_graph_->ConstantValue(rl_dstPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001152 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001153 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001154 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001155 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1156 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001157 }
1158 }
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001159 // Everything is checked now.
1160 LoadValueDirectFixed(rl_src, rs_rAX);
1161 LoadValueDirectFixed(rl_dst, tmp_reg);
1162 LoadValueDirectFixed(rl_srcPos, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001163 NewLIR5(kX86Lea32RA, rs_rAX.GetReg(), rs_rAX.GetReg(),
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001164 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value());
1165 // RAX now holds the address of the first src element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001166
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001167 LoadValueDirectFixed(rl_dstPos, rs_rCX);
1168 NewLIR5(kX86Lea32RA, tmp_reg.GetReg(), tmp_reg.GetReg(),
1169 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value() );
1170 // RBX now holds the address of the first dst element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001171
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001172 // Check if the number of elements to be copied is odd or even. If odd
DaniilSokolov70c4f062014-06-24 17:34:00 -07001173 // then copy the first element (so that the remaining number of elements
1174 // is even).
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001175 LoadValueDirectFixed(rl_length, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001176 OpRegImm(kOpAnd, rs_rCX, 1);
1177 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1178 OpRegImm(kOpSub, rs_rDX, 1);
1179 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001180 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001181
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001182 // Since the remaining number of elements is even, we will copy by
DaniilSokolov70c4f062014-06-24 17:34:00 -07001183 // two elements at a time.
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001184 LIR* beginLoop = NewLIR0(kPseudoTargetLabel);
1185 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_rDX, 0, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001186 OpRegImm(kOpSub, rs_rDX, 2);
1187 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001188 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001189 OpUnconditionalBranch(beginLoop);
1190 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1191 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1192 LIR *return_point = NewLIR0(kPseudoTargetLabel);
1193 jmp_to_ret->target = return_point;
1194 jmp_to_begin_loop->target = beginLoop;
1195 src_dst_same->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001196 len_too_big->target = check_failed;
1197 src_null_branch->target = check_failed;
1198 if (srcPos_negative != nullptr)
1199 srcPos_negative ->target = check_failed;
1200 if (src_bad_len != nullptr)
1201 src_bad_len->target = check_failed;
1202 dst_null_branch->target = check_failed;
1203 if (dstPos_negative != nullptr)
1204 dstPos_negative->target = check_failed;
1205 if (dst_bad_len != nullptr)
1206 dst_bad_len->target = check_failed;
1207 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
1208 return true;
1209}
1210
1211
Mark Mendell4028a6c2014-02-19 20:06:20 -08001212/*
1213 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1214 * otherwise bails to standard library code.
1215 */
1216bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001217 RegLocation rl_obj = info->args[0];
1218 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001219 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001220 // RBX is promotable in 64-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001221 RegStorage rs_tmp = cu_->target64 ? rs_r11 : rs_rBX;
1222 int start_value = -1;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001223
1224 uint32_t char_value =
1225 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1226
1227 if (char_value > 0xFFFF) {
1228 // We have to punt to the real String.indexOf.
1229 return false;
1230 }
1231
1232 // Okay, we are commited to inlining this.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001233 // EAX: 16 bit character being searched.
1234 // ECX: count: number of words to be searched.
1235 // EDI: String being searched.
1236 // EDX: temporary during execution.
1237 // EBX or R11: temporary during execution (depending on mode).
1238 // REP SCASW: search instruction.
1239
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001240 FlushAllRegs();
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001241
buzbeea0cd2d72014-06-01 09:33:49 -07001242 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001243 RegLocation rl_dest = InlineTarget(info);
1244
1245 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001246 LoadValueDirectFixed(rl_obj, rs_rDX);
1247 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001248 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001249
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001250 LIR *slowpath_branch = nullptr, *length_compare = nullptr;
1251
1252 // We need the value in EAX.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001253 if (rl_char.is_const) {
buzbee2700f7e2014-03-07 09:46:20 -08001254 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001255 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001256 // Does the character fit in 16 bits? Compare it at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001257 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001258 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001259 }
1260
1261 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001262 // Location of reference to data array within the String object.
1263 int value_offset = mirror::String::ValueOffset().Int32Value();
1264 // Location of count within the String object.
1265 int count_offset = mirror::String::CountOffset().Int32Value();
1266 // Starting offset within data array.
1267 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1268 // Start of char data with array_.
1269 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001270
Dave Allison69dfe512014-07-11 17:11:58 +00001271 // Compute the number of words to search in to rCX.
1272 Load32Disp(rs_rDX, count_offset, rs_rCX);
1273
Dave Allisondfd3b472014-07-16 16:04:32 -07001274 // Possible signal here due to null pointer dereference.
1275 // Note that the signal handler will expect the top word of
1276 // the stack to be the ArtMethod*. If the PUSH edi instruction
1277 // below is ahead of the load above then this will not be true
1278 // and the signal handler will not work.
1279 MarkPossibleNullPointerException(0);
Dave Allison69dfe512014-07-11 17:11:58 +00001280
Dave Allisondfd3b472014-07-16 16:04:32 -07001281 if (!cu_->target64) {
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001282 // EDI is promotable in 32-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001283 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1284 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001285
Mark Mendell4028a6c2014-02-19 20:06:20 -08001286 if (zero_based) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001287 // Start index is not present.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001288 // We have to handle an empty string. Use special instruction JECXZ.
1289 length_compare = NewLIR0(kX86Jecxz8);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001290
1291 // Copy the number of words to search in a temporary register.
1292 // We will use the register at the end to calculate result.
1293 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001294 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001295 // Start index is present.
buzbeea44d4f52014-03-05 11:26:39 -08001296 rl_start = info->args[2];
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001297
Mark Mendell4028a6c2014-02-19 20:06:20 -08001298 // We have to offset by the start index.
1299 if (rl_start.is_const) {
1300 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1301 start_value = std::max(start_value, 0);
1302
1303 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001304 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001305 OpRegImm(kOpMov, rs_rDI, start_value);
1306
1307 // Copy the number of words to search in a temporary register.
1308 // We will use the register at the end to calculate result.
1309 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001310
1311 if (start_value != 0) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001312 // Decrease the number of words to search by the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001313 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001314 }
1315 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001316 // Handle "start index < 0" case.
1317 if (!cu_->target64 && rl_start.location != kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001318 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001319 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko74de63b2014-08-19 15:00:34 +01001320 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1321 Load32Disp(rs_rX86_SP, displacement, rs_rDI);
1322 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1323 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1324 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - 1;
1325 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001326 } else {
1327 LoadValueDirectFixed(rl_start, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001328 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001329 OpRegReg(kOpXor, rs_tmp, rs_tmp);
1330 OpRegReg(kOpCmp, rs_rDI, rs_tmp);
1331 OpCondRegReg(kOpCmov, kCondLt, rs_rDI, rs_tmp);
1332
1333 // The length of the string should be greater than the start index.
1334 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rDI, nullptr);
1335
1336 // Copy the number of words to search in a temporary register.
1337 // We will use the register at the end to calculate result.
1338 OpRegReg(kOpMov, rs_tmp, rs_rCX);
1339
1340 // Decrease the number of words to search by the start index.
1341 OpRegReg(kOpSub, rs_rCX, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001342 }
1343 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001344
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001345 // Load the address of the string into EDI.
1346 // In case of start index we have to add the address to existing value in EDI.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001347 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001348 if (zero_based || (!zero_based && rl_start.is_const && start_value == 0)) {
1349 Load32Disp(rs_rDX, offset_offset, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001350 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001351 OpRegMem(kOpAdd, rs_rDI, rs_rDX, offset_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001352 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001353 OpRegImm(kOpLsl, rs_rDI, 1);
1354 OpRegMem(kOpAdd, rs_rDI, rs_rDX, value_offset);
1355 OpRegImm(kOpAdd, rs_rDI, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001356
1357 // EDI now contains the start of the string to be searched.
1358 // We are all prepared to do the search for the character.
1359 NewLIR0(kX86RepneScasw);
1360
1361 // Did we find a match?
1362 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1363
1364 // yes, we matched. Compute the index of the result.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001365 OpRegReg(kOpSub, rs_tmp, rs_rCX);
1366 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_tmp.GetReg(), -1);
1367
Mark Mendell4028a6c2014-02-19 20:06:20 -08001368 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1369
1370 // Failed to match; return -1.
1371 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1372 length_compare->target = not_found;
1373 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001374 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001375
1376 // And join up at the end.
1377 all_done->target = NewLIR0(kPseudoTargetLabel);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001378
1379 if (!cu_->target64)
1380 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001381
1382 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001383 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001384 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001385 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001386 }
1387
1388 StoreValue(rl_dest, rl_return);
1389 return true;
1390}
1391
Tong Shen35e1e6a2014-07-30 09:31:22 -07001392static bool ARTRegIDToDWARFRegID(bool is_x86_64, int art_reg_id, int* dwarf_reg_id) {
1393 if (is_x86_64) {
1394 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001395 case 3 : *dwarf_reg_id = 3; return true; // %rbx
Tong Shen35e1e6a2014-07-30 09:31:22 -07001396 // This is the only discrepancy between ART & DWARF register numbering.
Andreas Gampebda27222014-07-30 23:21:36 -07001397 case 5 : *dwarf_reg_id = 6; return true; // %rbp
1398 case 12: *dwarf_reg_id = 12; return true; // %r12
1399 case 13: *dwarf_reg_id = 13; return true; // %r13
1400 case 14: *dwarf_reg_id = 14; return true; // %r14
1401 case 15: *dwarf_reg_id = 15; return true; // %r15
1402 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001403 }
1404 } else {
1405 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001406 case 5: *dwarf_reg_id = 5; return true; // %ebp
1407 case 6: *dwarf_reg_id = 6; return true; // %esi
1408 case 7: *dwarf_reg_id = 7; return true; // %edi
1409 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001410 }
1411 }
1412}
1413
Tong Shen547cdfd2014-08-05 01:54:19 -07001414std::vector<uint8_t>* X86Mir2Lir::ReturnFrameDescriptionEntry() {
1415 std::vector<uint8_t>* cfi_info = new std::vector<uint8_t>;
Mark Mendellae9fd932014-02-10 16:14:35 -08001416
1417 // Generate the FDE for the method.
1418 DCHECK_NE(data_offset_, 0U);
1419
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001420 WriteFDEHeader(cfi_info, cu_->target64);
1421 WriteFDEAddressRange(cfi_info, data_offset_, cu_->target64);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001422
Mark Mendellae9fd932014-02-10 16:14:35 -08001423 // The instructions in the FDE.
1424 if (stack_decrement_ != nullptr) {
1425 // Advance LOC to just past the stack decrement.
1426 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001427 DW_CFA_advance_loc(cfi_info, pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001428
1429 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
Tong Shen547cdfd2014-08-05 01:54:19 -07001430 DW_CFA_def_cfa_offset(cfi_info, frame_size_);
Mark Mendellae9fd932014-02-10 16:14:35 -08001431
Tong Shen35e1e6a2014-07-30 09:31:22 -07001432 // Handle register spills
1433 const uint32_t kSpillInstLen = (cu_->target64) ? 5 : 4;
1434 const int kDataAlignmentFactor = (cu_->target64) ? -8 : -4;
1435 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
1436 int offset = -(GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
1437 for (int reg = 0; mask; mask >>= 1, reg++) {
1438 if (mask & 0x1) {
1439 pc += kSpillInstLen;
1440
1441 // Advance LOC to pass this instruction
Tong Shen547cdfd2014-08-05 01:54:19 -07001442 DW_CFA_advance_loc(cfi_info, kSpillInstLen);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001443
1444 int dwarf_reg_id;
1445 if (ARTRegIDToDWARFRegID(cu_->target64, reg, &dwarf_reg_id)) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001446 // DW_CFA_offset_extended_sf reg offset
1447 DW_CFA_offset_extended_sf(cfi_info, dwarf_reg_id, offset / kDataAlignmentFactor);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001448 }
1449
1450 offset += GetInstructionSetPointerSize(cu_->instruction_set);
1451 }
1452 }
1453
Mark Mendellae9fd932014-02-10 16:14:35 -08001454 // We continue with that stack until the epilogue.
1455 if (stack_increment_ != nullptr) {
1456 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001457 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001458
1459 // We probably have code snippets after the epilogue, so save the
1460 // current state: DW_CFA_remember_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001461 DW_CFA_remember_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001462
Tong Shen35e1e6a2014-07-30 09:31:22 -07001463 // We have now popped the stack: DW_CFA_def_cfa_offset 4/8.
1464 // There is only the return PC on the stack now.
Tong Shen547cdfd2014-08-05 01:54:19 -07001465 DW_CFA_def_cfa_offset(cfi_info, GetInstructionSetPointerSize(cu_->instruction_set));
Mark Mendellae9fd932014-02-10 16:14:35 -08001466
1467 // Everything after that is the same as before the epilogue.
1468 // Stack bump was followed by RET instruction.
1469 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1470 if (post_ret_insn != nullptr) {
1471 pc = new_pc;
1472 new_pc = post_ret_insn->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001473 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001474 // Restore the state: DW_CFA_restore_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001475 DW_CFA_restore_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001476 }
1477 }
1478 }
1479
Tong Shen547cdfd2014-08-05 01:54:19 -07001480 PadCFI(cfi_info);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001481 WriteCFILength(cfi_info, cu_->target64);
Mark Mendellae9fd932014-02-10 16:14:35 -08001482
Mark Mendellae9fd932014-02-10 16:14:35 -08001483 return cfi_info;
1484}
1485
Mark Mendelld65c51a2014-04-29 16:55:20 -04001486void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1487 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001488 case kMirOpReserveVectorRegisters:
1489 ReserveVectorRegisters(mir);
1490 break;
1491 case kMirOpReturnVectorRegisters:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001492 ReturnVectorRegisters(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001493 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001494 case kMirOpConstVector:
1495 GenConst128(bb, mir);
1496 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001497 case kMirOpMoveVector:
1498 GenMoveVector(bb, mir);
1499 break;
1500 case kMirOpPackedMultiply:
1501 GenMultiplyVector(bb, mir);
1502 break;
1503 case kMirOpPackedAddition:
1504 GenAddVector(bb, mir);
1505 break;
1506 case kMirOpPackedSubtract:
1507 GenSubtractVector(bb, mir);
1508 break;
1509 case kMirOpPackedShiftLeft:
1510 GenShiftLeftVector(bb, mir);
1511 break;
1512 case kMirOpPackedSignedShiftRight:
1513 GenSignedShiftRightVector(bb, mir);
1514 break;
1515 case kMirOpPackedUnsignedShiftRight:
1516 GenUnsignedShiftRightVector(bb, mir);
1517 break;
1518 case kMirOpPackedAnd:
1519 GenAndVector(bb, mir);
1520 break;
1521 case kMirOpPackedOr:
1522 GenOrVector(bb, mir);
1523 break;
1524 case kMirOpPackedXor:
1525 GenXorVector(bb, mir);
1526 break;
1527 case kMirOpPackedAddReduce:
1528 GenAddReduceVector(bb, mir);
1529 break;
1530 case kMirOpPackedReduce:
1531 GenReduceVector(bb, mir);
1532 break;
1533 case kMirOpPackedSet:
1534 GenSetVector(bb, mir);
1535 break;
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07001536 case kMirOpMemBarrier:
1537 GenMemBarrier(static_cast<MemBarrierKind>(mir->dalvikInsn.vA));
1538 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001539 case kMirOpPackedArrayGet:
1540 GenPackedArrayGet(bb, mir);
1541 break;
1542 case kMirOpPackedArrayPut:
1543 GenPackedArrayPut(bb, mir);
1544 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001545 default:
1546 break;
1547 }
1548}
1549
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001550void X86Mir2Lir::ReserveVectorRegisters(MIR* mir) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001551 for (uint32_t i = mir->dalvikInsn.vA; i <= mir->dalvikInsn.vB; i++) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001552 RegStorage xp_reg = RegStorage::Solo128(i);
1553 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1554 Clobber(xp_reg);
1555
1556 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1557 info != nullptr;
1558 info = info->GetAliasChain()) {
1559 if (info->GetReg().IsSingle()) {
1560 reg_pool_->sp_regs_.Delete(info);
1561 } else {
1562 reg_pool_->dp_regs_.Delete(info);
1563 }
1564 }
1565 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001566}
1567
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001568void X86Mir2Lir::ReturnVectorRegisters(MIR* mir) {
1569 for (uint32_t i = mir->dalvikInsn.vA; i <= mir->dalvikInsn.vB; i++) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001570 RegStorage xp_reg = RegStorage::Solo128(i);
1571 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1572
1573 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1574 info != nullptr;
1575 info = info->GetAliasChain()) {
1576 if (info->GetReg().IsSingle()) {
1577 reg_pool_->sp_regs_.Insert(info);
1578 } else {
1579 reg_pool_->dp_regs_.Insert(info);
1580 }
1581 }
1582 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001583}
1584
Mark Mendelld65c51a2014-04-29 16:55:20 -04001585void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001586 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001587 Clobber(rs_dest);
1588
Mark Mendelld65c51a2014-04-29 16:55:20 -04001589 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001590 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001591 // Check for all 0 case.
1592 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1593 NewLIR2(kX86XorpsRR, reg, reg);
1594 return;
1595 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001596
1597 // Append the mov const vector to reg opcode.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001598 AppendOpcodeWithConst(kX86MovdqaRM, reg, mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001599}
1600
1601void X86Mir2Lir::AppendOpcodeWithConst(X86OpCode opcode, int reg, MIR* mir) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001602 // The literal pool needs position independent logic.
1603 store_method_addr_used_ = true;
1604
1605 // To deal with correct memory ordering, reverse order of constants.
1606 int32_t constants[4];
1607 constants[3] = mir->dalvikInsn.arg[0];
1608 constants[2] = mir->dalvikInsn.arg[1];
1609 constants[1] = mir->dalvikInsn.arg[2];
1610 constants[0] = mir->dalvikInsn.arg[3];
1611
1612 // Search if there is already a constant in pool with this value.
1613 LIR *data_target = ScanVectorLiteral(constants);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001614 if (data_target == nullptr) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001615 data_target = AddVectorLiteral(constants);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001616 }
1617
1618 // Address the start of the method.
1619 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001620 if (rl_method.wide) {
1621 rl_method = LoadValueWide(rl_method, kCoreReg);
1622 } else {
1623 rl_method = LoadValue(rl_method, kCoreReg);
1624 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001625
1626 // Load the proper value from the literal area.
1627 // We don't know the proper offset for the value, so pick one that will force
1628 // 4 byte offset. We will fix this up in the assembler later to have the right
1629 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001630 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001631 LIR *load = NewLIR3(opcode, reg, rl_method.reg.GetReg(), 256 /* bogus */);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001632 load->flags.fixup = kFixupLoad;
1633 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001634}
1635
Mark Mendellfe945782014-05-22 09:52:36 -04001636void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1637 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001638 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1639 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001640 Clobber(rs_dest);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001641 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001642 NewLIR2(kX86MovdqaRR, rs_dest.GetReg(), rs_src.GetReg());
Mark Mendellfe945782014-05-22 09:52:36 -04001643}
1644
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001645void X86Mir2Lir::GenMultiplyVectorSignedByte(RegStorage rs_dest_src1, RegStorage rs_src2) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001646 /*
1647 * Emulate the behavior of a kSignedByte by separating out the 16 values in the two XMM
1648 * and multiplying 8 at a time before recombining back into one XMM register.
1649 *
1650 * let xmm1, xmm2 be real srcs (keep low bits of 16bit lanes)
1651 * xmm3 is tmp (operate on high bits of 16bit lanes)
1652 *
1653 * xmm3 = xmm1
1654 * xmm1 = xmm1 .* xmm2
1655 * xmm1 = xmm1 & 0x00ff00ff00ff00ff00ff00ff00ff00ff // xmm1 now has low bits
1656 * xmm3 = xmm3 .>> 8
1657 * xmm2 = xmm2 & 0xff00ff00ff00ff00ff00ff00ff00ff00
1658 * xmm2 = xmm2 .* xmm3 // xmm2 now has high bits
1659 * xmm1 = xmm1 | xmm2 // combine results
1660 */
1661
1662 // Copy xmm1.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001663 RegStorage rs_src1_high_tmp = Get128BitRegister(AllocTempDouble());
1664 RegStorage rs_dest_high_tmp = Get128BitRegister(AllocTempDouble());
1665 NewLIR2(kX86MovdqaRR, rs_src1_high_tmp.GetReg(), rs_src2.GetReg());
1666 NewLIR2(kX86MovdqaRR, rs_dest_high_tmp.GetReg(), rs_dest_src1.GetReg());
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001667
1668 // Multiply low bits.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001669 // x7 *= x3
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001670 NewLIR2(kX86PmullwRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1671
1672 // xmm1 now has low bits.
1673 AndMaskVectorRegister(rs_dest_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
1674
1675 // Prepare high bits for multiplication.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001676 NewLIR2(kX86PsrlwRI, rs_src1_high_tmp.GetReg(), 0x8);
1677 AndMaskVectorRegister(rs_dest_high_tmp, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001678
1679 // Multiply high bits and xmm2 now has high bits.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001680 NewLIR2(kX86PmullwRR, rs_src1_high_tmp.GetReg(), rs_dest_high_tmp.GetReg());
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001681
1682 // Combine back into dest XMM register.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001683 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src1_high_tmp.GetReg());
1684}
1685
1686void X86Mir2Lir::GenMultiplyVectorLong(RegStorage rs_dest_src1, RegStorage rs_src2) {
1687 /*
1688 * We need to emulate the packed long multiply.
1689 * For kMirOpPackedMultiply xmm1, xmm0:
1690 * - xmm1 is src/dest
1691 * - xmm0 is src
1692 * - Get xmm2 and xmm3 as temp
1693 * - Idea is to multiply the lower 32 of each operand with the higher 32 of the other.
1694 * - Then add the two results.
1695 * - Move it to the upper 32 of the destination
1696 * - Then multiply the lower 32-bits of the operands and add the result to the destination.
1697 *
1698 * (op dest src )
1699 * movdqa %xmm2, %xmm1
1700 * movdqa %xmm3, %xmm0
1701 * psrlq %xmm3, $0x20
1702 * pmuludq %xmm3, %xmm2
1703 * psrlq %xmm1, $0x20
1704 * pmuludq %xmm1, %xmm0
1705 * paddq %xmm1, %xmm3
1706 * psllq %xmm1, $0x20
1707 * pmuludq %xmm2, %xmm0
1708 * paddq %xmm1, %xmm2
1709 *
1710 * When both the operands are the same, then we need to calculate the lower-32 * higher-32
1711 * calculation only once. Thus we don't need the xmm3 temp above. That sequence becomes:
1712 *
1713 * (op dest src )
1714 * movdqa %xmm2, %xmm1
1715 * psrlq %xmm1, $0x20
1716 * pmuludq %xmm1, %xmm0
1717 * paddq %xmm1, %xmm1
1718 * psllq %xmm1, $0x20
1719 * pmuludq %xmm2, %xmm0
1720 * paddq %xmm1, %xmm2
1721 *
1722 */
1723
1724 bool both_operands_same = (rs_dest_src1.GetReg() == rs_src2.GetReg());
1725
1726 RegStorage rs_tmp_vector_1;
1727 RegStorage rs_tmp_vector_2;
1728 rs_tmp_vector_1 = Get128BitRegister(AllocTempDouble());
1729 NewLIR2(kX86MovdqaRR, rs_tmp_vector_1.GetReg(), rs_dest_src1.GetReg());
1730
1731 if (both_operands_same == false) {
1732 rs_tmp_vector_2 = Get128BitRegister(AllocTempDouble());
1733 NewLIR2(kX86MovdqaRR, rs_tmp_vector_2.GetReg(), rs_src2.GetReg());
1734 NewLIR2(kX86PsrlqRI, rs_tmp_vector_2.GetReg(), 0x20);
1735 NewLIR2(kX86PmuludqRR, rs_tmp_vector_2.GetReg(), rs_tmp_vector_1.GetReg());
1736 }
1737
1738 NewLIR2(kX86PsrlqRI, rs_dest_src1.GetReg(), 0x20);
1739 NewLIR2(kX86PmuludqRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1740
1741 if (both_operands_same == false) {
1742 NewLIR2(kX86PaddqRR, rs_dest_src1.GetReg(), rs_tmp_vector_2.GetReg());
1743 } else {
1744 NewLIR2(kX86PaddqRR, rs_dest_src1.GetReg(), rs_dest_src1.GetReg());
1745 }
1746
1747 NewLIR2(kX86PsllqRI, rs_dest_src1.GetReg(), 0x20);
1748 NewLIR2(kX86PmuludqRR, rs_tmp_vector_1.GetReg(), rs_src2.GetReg());
1749 NewLIR2(kX86PaddqRR, rs_dest_src1.GetReg(), rs_tmp_vector_1.GetReg());
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001750}
1751
Mark Mendellfe945782014-05-22 09:52:36 -04001752void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001753 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1754 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1755 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001756 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001757 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001758 int opcode = 0;
1759 switch (opsize) {
1760 case k32:
1761 opcode = kX86PmulldRR;
1762 break;
1763 case kSignedHalf:
1764 opcode = kX86PmullwRR;
1765 break;
1766 case kSingle:
1767 opcode = kX86MulpsRR;
1768 break;
1769 case kDouble:
1770 opcode = kX86MulpdRR;
1771 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001772 case kSignedByte:
1773 // HW doesn't support 16x16 byte multiplication so emulate it.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001774 GenMultiplyVectorSignedByte(rs_dest_src1, rs_src2);
1775 return;
1776 case k64:
1777 GenMultiplyVectorLong(rs_dest_src1, rs_src2);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001778 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001779 default:
1780 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1781 break;
1782 }
1783 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1784}
1785
1786void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001787 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1788 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1789 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001790 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001791 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001792 int opcode = 0;
1793 switch (opsize) {
1794 case k32:
1795 opcode = kX86PadddRR;
1796 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001797 case k64:
1798 opcode = kX86PaddqRR;
1799 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001800 case kSignedHalf:
1801 case kUnsignedHalf:
1802 opcode = kX86PaddwRR;
1803 break;
1804 case kUnsignedByte:
1805 case kSignedByte:
1806 opcode = kX86PaddbRR;
1807 break;
1808 case kSingle:
1809 opcode = kX86AddpsRR;
1810 break;
1811 case kDouble:
1812 opcode = kX86AddpdRR;
1813 break;
1814 default:
1815 LOG(FATAL) << "Unsupported vector addition " << opsize;
1816 break;
1817 }
1818 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1819}
1820
1821void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001822 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1823 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1824 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001825 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001826 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001827 int opcode = 0;
1828 switch (opsize) {
1829 case k32:
1830 opcode = kX86PsubdRR;
1831 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001832 case k64:
1833 opcode = kX86PsubqRR;
1834 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001835 case kSignedHalf:
1836 case kUnsignedHalf:
1837 opcode = kX86PsubwRR;
1838 break;
1839 case kUnsignedByte:
1840 case kSignedByte:
1841 opcode = kX86PsubbRR;
1842 break;
1843 case kSingle:
1844 opcode = kX86SubpsRR;
1845 break;
1846 case kDouble:
1847 opcode = kX86SubpdRR;
1848 break;
1849 default:
1850 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1851 break;
1852 }
1853 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1854}
1855
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001856void X86Mir2Lir::GenShiftByteVector(BasicBlock *bb, MIR *mir) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001857 // Destination does not need clobbered because it has already been as part
1858 // of the general packed shift handler (caller of this method).
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001859 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001860
1861 int opcode = 0;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001862 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1863 case kMirOpPackedShiftLeft:
1864 opcode = kX86PsllwRI;
1865 break;
1866 case kMirOpPackedSignedShiftRight:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001867 case kMirOpPackedUnsignedShiftRight:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001868 // TODO Add support for emulated byte shifts.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001869 default:
1870 LOG(FATAL) << "Unsupported shift operation on byte vector " << opcode;
1871 break;
1872 }
1873
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001874 // Clear xmm register and return if shift more than byte length.
1875 int imm = mir->dalvikInsn.vB;
1876 if (imm >= 8) {
1877 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_dest_src1.GetReg());
1878 return;
1879 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001880
1881 // Shift lower values.
1882 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1883
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001884 /*
1885 * The above shift will shift the whole word, but that means
1886 * both the bytes will shift as well. To emulate a byte level
1887 * shift, we can just throw away the lower (8 - N) bits of the
1888 * upper byte, and we are done.
1889 */
1890 uint8_t byte_mask = 0xFF << imm;
1891 uint32_t int_mask = byte_mask;
1892 int_mask = int_mask << 8 | byte_mask;
1893 int_mask = int_mask << 8 | byte_mask;
1894 int_mask = int_mask << 8 | byte_mask;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001895
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001896 // And the destination with the mask
1897 AndMaskVectorRegister(rs_dest_src1, int_mask, int_mask, int_mask, int_mask);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001898}
1899
Mark Mendellfe945782014-05-22 09:52:36 -04001900void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001901 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1902 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1903 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001904 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001905 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001906 int opcode = 0;
1907 switch (opsize) {
1908 case k32:
1909 opcode = kX86PslldRI;
1910 break;
1911 case k64:
1912 opcode = kX86PsllqRI;
1913 break;
1914 case kSignedHalf:
1915 case kUnsignedHalf:
1916 opcode = kX86PsllwRI;
1917 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001918 case kSignedByte:
1919 case kUnsignedByte:
1920 GenShiftByteVector(bb, mir);
1921 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001922 default:
1923 LOG(FATAL) << "Unsupported vector shift left " << opsize;
1924 break;
1925 }
1926 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1927}
1928
1929void X86Mir2Lir::GenSignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001930 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1931 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1932 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001933 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001934 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001935 int opcode = 0;
1936 switch (opsize) {
1937 case k32:
1938 opcode = kX86PsradRI;
1939 break;
1940 case kSignedHalf:
1941 case kUnsignedHalf:
1942 opcode = kX86PsrawRI;
1943 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001944 case kSignedByte:
1945 case kUnsignedByte:
1946 GenShiftByteVector(bb, mir);
1947 return;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001948 case k64:
1949 // TODO Implement emulated shift algorithm.
Mark Mendellfe945782014-05-22 09:52:36 -04001950 default:
1951 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
1952 break;
1953 }
1954 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1955}
1956
1957void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001958 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1959 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1960 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001961 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001962 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001963 int opcode = 0;
1964 switch (opsize) {
1965 case k32:
1966 opcode = kX86PsrldRI;
1967 break;
1968 case k64:
1969 opcode = kX86PsrlqRI;
1970 break;
1971 case kSignedHalf:
1972 case kUnsignedHalf:
1973 opcode = kX86PsrlwRI;
1974 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001975 case kSignedByte:
1976 case kUnsignedByte:
1977 GenShiftByteVector(bb, mir);
1978 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001979 default:
1980 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1981 break;
1982 }
1983 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1984}
1985
1986void X86Mir2Lir::GenAndVector(BasicBlock *bb, MIR *mir) {
1987 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001988 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1989 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001990 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001991 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001992 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1993}
1994
1995void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
1996 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001997 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1998 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001999 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002000 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04002001 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
2002}
2003
2004void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
2005 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002006 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2007 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002008 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002009 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04002010 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
2011}
2012
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002013void X86Mir2Lir::AndMaskVectorRegister(RegStorage rs_src1, uint32_t m1, uint32_t m2, uint32_t m3, uint32_t m4) {
2014 MaskVectorRegister(kX86PandRM, rs_src1, m1, m2, m3, m4);
2015}
2016
2017void X86Mir2Lir::MaskVectorRegister(X86OpCode opcode, RegStorage rs_src1, uint32_t m0, uint32_t m1, uint32_t m2, uint32_t m3) {
2018 // Create temporary MIR as container for 128-bit binary mask.
2019 MIR const_mir;
2020 MIR* const_mirp = &const_mir;
2021 const_mirp->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpConstVector);
2022 const_mirp->dalvikInsn.arg[0] = m0;
2023 const_mirp->dalvikInsn.arg[1] = m1;
2024 const_mirp->dalvikInsn.arg[2] = m2;
2025 const_mirp->dalvikInsn.arg[3] = m3;
2026
2027 // Mask vector with const from literal pool.
2028 AppendOpcodeWithConst(opcode, rs_src1.GetReg(), const_mirp);
2029}
2030
Mark Mendellfe945782014-05-22 09:52:36 -04002031void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002032 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002033 RegStorage vector_src = RegStorage::Solo128(mir->dalvikInsn.vB);
2034 bool is_wide = opsize == k64 || opsize == kDouble;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002035
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002036 // Get the location of the virtual register. Since this bytecode is overloaded
2037 // for different types (and sizes), we need different logic for each path.
2038 // The design of bytecode uses same VR for source and destination.
2039 RegLocation rl_src, rl_dest, rl_result;
2040 if (is_wide) {
2041 rl_src = mir_graph_->GetSrcWide(mir, 0);
2042 rl_dest = mir_graph_->GetDestWide(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002043 } else {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002044 rl_src = mir_graph_->GetSrc(mir, 0);
2045 rl_dest = mir_graph_->GetDest(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002046 }
2047
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002048 // We need a temp for byte and short values
2049 RegStorage temp;
2050
2051 // There is a different path depending on type and size.
2052 if (opsize == kSingle) {
2053 // Handle float case.
2054 // TODO Add support for fast math (not value safe) and do horizontal add in that case.
2055
2056 rl_src = LoadValue(rl_src, kFPReg);
2057 rl_result = EvalLoc(rl_dest, kFPReg, true);
2058
2059 // Since we are doing an add-reduce, we move the reg holding the VR
2060 // into the result so we include it in result.
2061 OpRegCopy(rl_result.reg, rl_src.reg);
2062 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), vector_src.GetReg());
2063
2064 // Since FP must keep order of operation for value safety, we shift to low
2065 // 32-bits and add to result.
2066 for (int i = 0; i < 3; i++) {
2067 NewLIR3(kX86ShufpsRRI, vector_src.GetReg(), vector_src.GetReg(), 0x39);
2068 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), vector_src.GetReg());
2069 }
2070
2071 StoreValue(rl_dest, rl_result);
2072 } else if (opsize == kDouble) {
2073 // Handle double case.
2074 rl_src = LoadValueWide(rl_src, kFPReg);
2075 rl_result = EvalLocWide(rl_dest, kFPReg, true);
2076 LOG(FATAL) << "Unsupported vector add reduce for double.";
2077 } else if (opsize == k64) {
2078 /*
2079 * Handle long case:
2080 * 1) Reduce the vector register to lower half (with addition).
2081 * 1-1) Get an xmm temp and fill it with vector register.
2082 * 1-2) Shift the xmm temp by 8-bytes.
2083 * 1-3) Add the xmm temp to vector register that is being reduced.
2084 * 2) Allocate temp GP / GP pair.
2085 * 2-1) In 64-bit case, use movq to move result to a 64-bit GP.
2086 * 2-2) In 32-bit case, use movd twice to move to 32-bit GP pair.
2087 * 3) Finish the add reduction by doing what add-long/2addr does,
2088 * but instead of having a VR as one of the sources, we have our temp GP.
2089 */
2090 RegStorage rs_tmp_vector = Get128BitRegister(AllocTempDouble());
2091 NewLIR2(kX86MovdqaRR, rs_tmp_vector.GetReg(), vector_src.GetReg());
2092 NewLIR2(kX86PsrldqRI, rs_tmp_vector.GetReg(), 8);
2093 NewLIR2(kX86PaddqRR, vector_src.GetReg(), rs_tmp_vector.GetReg());
2094 FreeTemp(rs_tmp_vector);
2095
2096 // We would like to be able to reuse the add-long implementation, so set up a fake
2097 // register location to pass it.
2098 RegLocation temp_loc = mir_graph_->GetBadLoc();
2099 temp_loc.core = 1;
2100 temp_loc.wide = 1;
2101 temp_loc.location = kLocPhysReg;
2102 temp_loc.reg = AllocTempWide();
2103
2104 if (cu_->target64) {
2105 DCHECK(!temp_loc.reg.IsPair());
2106 NewLIR2(kX86MovqrxRR, temp_loc.reg.GetReg(), vector_src.GetReg());
2107 } else {
2108 NewLIR2(kX86MovdrxRR, temp_loc.reg.GetLowReg(), vector_src.GetReg());
2109 NewLIR2(kX86PsrlqRI, vector_src.GetReg(), 0x20);
2110 NewLIR2(kX86MovdrxRR, temp_loc.reg.GetHighReg(), vector_src.GetReg());
2111 }
2112
2113 GenArithOpLong(Instruction::ADD_LONG_2ADDR, rl_dest, temp_loc, temp_loc);
2114 } else if (opsize == kSignedByte || opsize == kUnsignedByte) {
2115 RegStorage rs_tmp = Get128BitRegister(AllocTempDouble());
2116 NewLIR2(kX86PxorRR, rs_tmp.GetReg(), rs_tmp.GetReg());
2117 NewLIR2(kX86PsadbwRR, vector_src.GetReg(), rs_tmp.GetReg());
2118 NewLIR3(kX86PshufdRRI, rs_tmp.GetReg(), vector_src.GetReg(), 0x4e);
2119 NewLIR2(kX86PaddbRR, vector_src.GetReg(), rs_tmp.GetReg());
2120 // Move to a GPR
2121 temp = AllocTemp();
2122 NewLIR2(kX86MovdrxRR, temp.GetReg(), vector_src.GetReg());
2123 } else {
2124 // Handle and the int and short cases together
2125
2126 // Initialize as if we were handling int case. Below we update
2127 // the opcode if handling byte or short.
2128 int vec_bytes = (mir->dalvikInsn.vC & 0xFFFF) / 8;
2129 int vec_unit_size;
2130 int horizontal_add_opcode;
2131 int extract_opcode;
2132
2133 if (opsize == kSignedHalf || opsize == kUnsignedHalf) {
2134 extract_opcode = kX86PextrwRRI;
2135 horizontal_add_opcode = kX86PhaddwRR;
2136 vec_unit_size = 2;
2137 } else if (opsize == k32) {
2138 vec_unit_size = 4;
2139 horizontal_add_opcode = kX86PhadddRR;
2140 extract_opcode = kX86PextrdRRI;
2141 } else {
2142 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2143 return;
2144 }
2145
2146 int elems = vec_bytes / vec_unit_size;
2147
2148 while (elems > 1) {
2149 NewLIR2(horizontal_add_opcode, vector_src.GetReg(), vector_src.GetReg());
2150 elems >>= 1;
2151 }
2152
2153 // Handle this as arithmetic unary case.
2154 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2155
2156 // Extract to a GP register because this is integral typed.
2157 temp = AllocTemp();
2158 NewLIR3(extract_opcode, temp.GetReg(), vector_src.GetReg(), 0);
2159 }
2160
2161 if (opsize != k64 && opsize != kSingle && opsize != kDouble) {
2162 // The logic below looks very similar to the handling of ADD_INT_2ADDR
2163 // except the rhs is not a VR but a physical register allocated above.
2164 // No load of source VR is done because it assumes that rl_result will
2165 // share physical register / memory location.
2166 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2167 if (rl_result.location == kLocPhysReg) {
2168 // Ensure res is in a core reg.
2169 rl_result = EvalLoc(rl_dest, kCoreReg, true);
2170 OpRegReg(kOpAdd, rl_result.reg, temp);
2171 StoreFinalValue(rl_dest, rl_result);
2172 } else {
2173 // Do the addition directly to memory.
2174 OpMemReg(kOpAdd, rl_result, temp.GetReg());
2175 }
2176 }
Mark Mendellfe945782014-05-22 09:52:36 -04002177}
2178
2179void X86Mir2Lir::GenReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002180 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2181 RegLocation rl_dest = mir_graph_->GetDest(mir);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002182 RegStorage vector_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002183 int extract_index = mir->dalvikInsn.arg[0];
2184 int extr_opcode = 0;
2185 RegLocation rl_result;
2186 bool is_wide = false;
2187
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002188 // There is a different path depending on type and size.
2189 if (opsize == kSingle) {
2190 // Handle float case.
2191 // TODO Add support for fast math (not value safe) and do horizontal add in that case.
Mark Mendellfe945782014-05-22 09:52:36 -04002192
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002193 rl_result = EvalLoc(rl_dest, kFPReg, true);
2194 NewLIR2(kX86PxorRR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
2195 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), vector_src.GetReg());
2196
2197 // Since FP must keep order of operation for value safety, we shift to low
2198 // 32-bits and add to result.
2199 for (int i = 0; i < 3; i++) {
2200 NewLIR3(kX86ShufpsRRI, vector_src.GetReg(), vector_src.GetReg(), 0x39);
2201 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), vector_src.GetReg());
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002202 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002203
2204 StoreValue(rl_dest, rl_result);
2205 } else if (opsize == kDouble) {
2206 // TODO Handle double case.
2207 LOG(FATAL) << "Unsupported add reduce for double.";
2208 } else if (opsize == k64) {
2209 /*
2210 * Handle long case:
2211 * 1) Reduce the vector register to lower half (with addition).
2212 * 1-1) Get an xmm temp and fill it with vector register.
2213 * 1-2) Shift the xmm temp by 8-bytes.
2214 * 1-3) Add the xmm temp to vector register that is being reduced.
2215 * 2) Evaluate destination to a GP / GP pair.
2216 * 2-1) In 64-bit case, use movq to move result to a 64-bit GP.
2217 * 2-2) In 32-bit case, use movd twice to move to 32-bit GP pair.
2218 * 3) Store the result to the final destination.
2219 */
2220 RegStorage rs_tmp_vector = Get128BitRegister(AllocTempDouble());
2221 NewLIR2(kX86MovdqaRR, rs_tmp_vector.GetReg(), vector_src.GetReg());
2222 NewLIR2(kX86PsrldqRI, rs_tmp_vector.GetReg(), 8);
2223 NewLIR2(kX86PaddqRR, vector_src.GetReg(), rs_tmp_vector.GetReg());
2224 FreeTemp(rs_tmp_vector);
2225
2226 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2227 if (cu_->target64) {
2228 DCHECK(!rl_result.reg.IsPair());
2229 NewLIR2(kX86MovqrxRR, rl_result.reg.GetReg(), vector_src.GetReg());
2230 } else {
2231 NewLIR2(kX86MovdrxRR, rl_result.reg.GetLowReg(), vector_src.GetReg());
2232 NewLIR2(kX86PsrlqRI, vector_src.GetReg(), 0x20);
2233 NewLIR2(kX86MovdrxRR, rl_result.reg.GetHighReg(), vector_src.GetReg());
2234 }
2235
2236 StoreValueWide(rl_dest, rl_result);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002237 } else {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002238 // Handle the rest of integral types now.
2239 switch (opsize) {
2240 case k32:
2241 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2242 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrdMRI : kX86PextrdRRI;
2243 break;
2244 case kSignedHalf:
2245 case kUnsignedHalf:
2246 rl_result= UpdateLocTyped(rl_dest, kCoreReg);
2247 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrwMRI : kX86PextrwRRI;
2248 break;
2249 default:
2250 LOG(FATAL) << "Unsupported vector reduce " << opsize;
2251 return;
2252 }
2253
2254 if (rl_result.location == kLocPhysReg) {
2255 NewLIR3(extr_opcode, rl_result.reg.GetReg(), vector_src.GetReg(), extract_index);
2256 if (is_wide == true) {
2257 StoreFinalValue(rl_dest, rl_result);
2258 } else {
2259 StoreFinalValueWide(rl_dest, rl_result);
2260 }
2261 } else {
2262 int displacement = SRegOffset(rl_result.s_reg_low);
2263 LIR *l = NewLIR3(extr_opcode, rs_rX86_SP.GetReg(), displacement, vector_src.GetReg());
2264 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is_wide /* is_64bit */);
2265 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is_wide /* is_64bit */);
2266 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002267 }
Mark Mendellfe945782014-05-22 09:52:36 -04002268}
2269
2270void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002271 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2272 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2273 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002274 Clobber(rs_dest);
2275 int op_shuffle = 0, op_shuffle_high = 0, op_mov = kX86MovdxrRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002276 RegisterClass reg_type = kCoreReg;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002277 bool is_wide = false;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002278
Mark Mendellfe945782014-05-22 09:52:36 -04002279 switch (opsize) {
2280 case k32:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002281 op_shuffle = kX86PshufdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002282 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002283 case kSingle:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002284 op_shuffle = kX86PshufdRRI;
2285 op_mov = kX86MovdqaRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002286 reg_type = kFPReg;
2287 break;
2288 case k64:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002289 op_shuffle = kX86PunpcklqdqRR;
2290 op_mov = kX86MovqrxRR;
2291 is_wide = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002292 break;
2293 case kSignedByte:
2294 case kUnsignedByte:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002295 // We will have the source loaded up in a
2296 // double-word before we use this shuffle
2297 op_shuffle = kX86PshufdRRI;
2298 break;
Mark Mendellfe945782014-05-22 09:52:36 -04002299 case kSignedHalf:
2300 case kUnsignedHalf:
2301 // Handles low quadword.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002302 op_shuffle = kX86PshuflwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002303 // Handles upper quadword.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002304 op_shuffle_high = kX86PshufdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002305 break;
2306 default:
2307 LOG(FATAL) << "Unsupported vector set " << opsize;
2308 break;
2309 }
2310
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002311 // Load the value from the VR into a physical register.
2312 RegLocation rl_src;
2313 if (!is_wide) {
2314 rl_src = mir_graph_->GetSrc(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002315 rl_src = LoadValue(rl_src, reg_type);
2316 } else {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002317 rl_src = mir_graph_->GetSrcWide(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002318 rl_src = LoadValueWide(rl_src, reg_type);
2319 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002320 RegStorage reg_to_shuffle = rl_src.reg;
Mark Mendellfe945782014-05-22 09:52:36 -04002321
2322 // Load the value into the XMM register.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002323 if (!cu_->target64 && opsize == k64) {
2324 // Logic assumes that longs are loaded in GP register pairs.
2325 NewLIR2(kX86MovdxrRR, rs_dest.GetReg(), reg_to_shuffle.GetLowReg());
2326 RegStorage r_tmp = AllocTempDouble();
2327 NewLIR2(kX86MovdxrRR, r_tmp.GetReg(), reg_to_shuffle.GetHighReg());
2328 NewLIR2(kX86PunpckldqRR, rs_dest.GetReg(), r_tmp.GetReg());
2329 FreeTemp(r_tmp);
2330 } else {
2331 NewLIR2(op_mov, rs_dest.GetReg(), reg_to_shuffle.GetReg());
2332 }
2333
2334 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2335 // In the byte case, first duplicate it to be a word
2336 // Then duplicate it to be a double-word
2337 NewLIR2(kX86PunpcklbwRR, rs_dest.GetReg(), rs_dest.GetReg());
2338 NewLIR2(kX86PunpcklwdRR, rs_dest.GetReg(), rs_dest.GetReg());
2339 }
Mark Mendellfe945782014-05-22 09:52:36 -04002340
2341 // Now shuffle the value across the destination.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002342 if (op_shuffle == kX86PunpcklqdqRR) {
2343 NewLIR2(op_shuffle, rs_dest.GetReg(), rs_dest.GetReg());
2344 } else {
2345 NewLIR3(op_shuffle, rs_dest.GetReg(), rs_dest.GetReg(), 0);
2346 }
Mark Mendellfe945782014-05-22 09:52:36 -04002347
2348 // And then repeat as needed.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002349 if (op_shuffle_high != 0) {
2350 NewLIR3(op_shuffle_high, rs_dest.GetReg(), rs_dest.GetReg(), 0);
Mark Mendellfe945782014-05-22 09:52:36 -04002351 }
2352}
2353
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002354void X86Mir2Lir::GenPackedArrayGet(BasicBlock *bb, MIR *mir) {
2355 UNIMPLEMENTED(FATAL) << "Extended opcode kMirOpPackedArrayGet not supported.";
2356}
2357
2358void X86Mir2Lir::GenPackedArrayPut(BasicBlock *bb, MIR *mir) {
2359 UNIMPLEMENTED(FATAL) << "Extended opcode kMirOpPackedArrayPut not supported.";
2360}
2361
2362LIR* X86Mir2Lir::ScanVectorLiteral(int32_t* constants) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04002363 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002364 if (constants[0] == p->operands[0] && constants[1] == p->operands[1] &&
2365 constants[2] == p->operands[2] && constants[3] == p->operands[3]) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04002366 return p;
2367 }
2368 }
2369 return nullptr;
2370}
2371
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002372LIR* X86Mir2Lir::AddVectorLiteral(int32_t* constants) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04002373 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002374 new_value->operands[0] = constants[0];
2375 new_value->operands[1] = constants[1];
2376 new_value->operands[2] = constants[2];
2377 new_value->operands[3] = constants[3];
Mark Mendelld65c51a2014-04-29 16:55:20 -04002378 new_value->next = const_vectors_;
2379 if (const_vectors_ == nullptr) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002380 estimated_native_code_size_ += 12; // Maximum needed to align to 16 byte boundary.
Mark Mendelld65c51a2014-04-29 16:55:20 -04002381 }
2382 estimated_native_code_size_ += 16; // Space for one vector.
2383 const_vectors_ = new_value;
2384 return new_value;
2385}
2386
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002387// ------------ ABI support: mapping of args to physical registers -------------
Andreas Gampeccc60262014-07-04 18:02:38 -07002388RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide,
2389 bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002390 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
Andreas Gampeccc60262014-07-04 18:02:38 -07002391 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) /
2392 sizeof(SpecialTargetRegister);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002393 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
Andreas Gampeccc60262014-07-04 18:02:38 -07002394 kFArg4, kFArg5, kFArg6, kFArg7};
2395 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) /
2396 sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002397
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002398 if (is_double_or_float) {
2399 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002400 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide ? kWide : kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002401 }
2402 } else {
2403 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002404 return ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++],
2405 is_ref ? kRef : (is_wide ? kWide : kNotWide));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002406 }
2407 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07002408 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002409}
2410
2411RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
2412 DCHECK(IsInitialized());
2413 auto res = mapping_.find(in_position);
2414 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
2415}
2416
Andreas Gampeccc60262014-07-04 18:02:38 -07002417void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count,
2418 InToRegStorageMapper* mapper) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002419 DCHECK(mapper != nullptr);
2420 max_mapped_in_ = -1;
2421 is_there_stack_mapped_ = false;
2422 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07002423 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
2424 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002425 if (reg.Valid()) {
2426 mapping_[in_position] = reg;
2427 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07002428 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002429 // We covered 2 args, so skip the next one
2430 in_position++;
2431 }
2432 } else {
2433 is_there_stack_mapped_ = true;
2434 }
2435 }
2436 initialized_ = true;
2437}
2438
2439RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002440 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002441 return GetCoreArgMappingToPhysicalReg(arg_num);
2442 }
2443
2444 if (!in_to_reg_storage_mapping_.IsInitialized()) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002445 int start_vreg = cu_->mir_graph->GetFirstInVR();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002446 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
2447
Chao-ying Fua77ee512014-07-01 17:43:41 -07002448 InToRegStorageX86_64Mapper mapper(this);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002449 in_to_reg_storage_mapping_.Initialize(arg_locs, mir_graph_->GetNumOfInVRs(), &mapper);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002450 }
2451 return in_to_reg_storage_mapping_.Get(arg_num);
2452}
2453
2454RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
2455 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
2456 // Not used for 64-bit, TODO: Move X86_32 to the same framework
2457 switch (core_arg_num) {
2458 case 0:
2459 return rs_rX86_ARG1;
2460 case 1:
2461 return rs_rX86_ARG2;
2462 case 2:
2463 return rs_rX86_ARG3;
2464 default:
2465 return RegStorage::InvalidReg();
2466 }
2467}
2468
2469// ---------End of ABI support: mapping of args to physical registers -------------
2470
2471/*
2472 * If there are any ins passed in registers that have not been promoted
2473 * to a callee-save register, flush them to the frame. Perform initial
2474 * assignment of promoted arguments.
2475 *
2476 * ArgLocs is an array of location records describing the incoming arguments
2477 * with one location record per word of argument.
2478 */
2479void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002480 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002481 /*
2482 * Dummy up a RegLocation for the incoming Method*
2483 * It will attempt to keep kArg0 live (or copy it to home location
2484 * if promoted).
2485 */
2486
2487 RegLocation rl_src = rl_method;
2488 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -07002489 rl_src.reg = TargetReg(kArg0, kRef);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002490 rl_src.home = false;
2491 MarkLive(rl_src);
2492 StoreValue(rl_method, rl_src);
2493 // If Method* has been promoted, explicitly flush
2494 if (rl_method.location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002495 StoreRefDisp(rs_rX86_SP, 0, As32BitReg(TargetReg(kArg0, kRef)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002496 }
2497
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002498 if (mir_graph_->GetNumOfInVRs() == 0) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002499 return;
2500 }
2501
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002502 int start_vreg = cu_->mir_graph->GetFirstInVR();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002503 /*
2504 * Copy incoming arguments to their proper home locations.
2505 * NOTE: an older version of dx had an issue in which
2506 * it would reuse static method argument registers.
2507 * This could result in the same Dalvik virtual register
2508 * being promoted to both core and fp regs. To account for this,
2509 * we only copy to the corresponding promoted physical register
2510 * if it matches the type of the SSA name for the incoming
2511 * argument. It is also possible that long and double arguments
2512 * end up half-promoted. In those cases, we must flush the promoted
2513 * half to memory as well.
2514 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002515 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002516 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002517 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002518 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002519
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002520 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002521 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002522 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002523
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002524 // We have already updated the arg location with promoted info
2525 // so we can be based on it.
2526 if (t_loc->location == kLocPhysReg) {
2527 // Just copy it.
2528 OpRegCopy(t_loc->reg, reg);
2529 } else {
2530 // Needs flush.
2531 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002532 StoreRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002533 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002534 StoreBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002535 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002536 }
2537 }
2538 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002539 // If arriving in frame & promoted.
2540 if (t_loc->location == kLocPhysReg) {
2541 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002542 LoadRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002543 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002544 LoadBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002545 t_loc->wide ? k64 : k32, kNotVolatile);
2546 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002547 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002548 }
2549 if (t_loc->wide) {
2550 // Increment i to skip the next one.
2551 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002552 }
2553 }
2554}
2555
2556/*
2557 * Load up to 5 arguments, the first three of which will be in
2558 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2559 * and as part of the load sequence, it must be replaced with
2560 * the target method pointer. Note, this may also be called
2561 * for "range" variants if the number of arguments is 5 or fewer.
2562 */
2563int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2564 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2565 const MethodReference& target_method,
2566 uint32_t vtable_idx, uintptr_t direct_code,
2567 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002568 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002569 return Mir2Lir::GenDalvikArgsNoRange(info,
2570 call_state, pcrLabel, next_call_insn,
2571 target_method,
2572 vtable_idx, direct_code,
2573 direct_method, type, skip_this);
2574 }
2575 return GenDalvikArgsRange(info,
2576 call_state, pcrLabel, next_call_insn,
2577 target_method,
2578 vtable_idx, direct_code,
2579 direct_method, type, skip_this);
2580}
2581
2582/*
2583 * May have 0+ arguments (also used for jumbo). Note that
2584 * source virtual registers may be in physical registers, so may
2585 * need to be flushed to home location before copying. This
2586 * applies to arg3 and above (see below).
2587 *
2588 * Two general strategies:
2589 * If < 20 arguments
2590 * Pass args 3-18 using vldm/vstm block copy
2591 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2592 * If 20+ arguments
2593 * Pass args arg19+ using memcpy block copy
2594 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2595 *
2596 */
2597int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2598 LIR** pcrLabel, NextCallInsn next_call_insn,
2599 const MethodReference& target_method,
2600 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2601 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002602 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002603 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2604 pcrLabel, next_call_insn,
2605 target_method,
2606 vtable_idx, direct_code, direct_method,
2607 type, skip_this);
2608 }
2609
2610 /* If no arguments, just return */
2611 if (info->num_arg_words == 0)
2612 return call_state;
2613
2614 const int start_index = skip_this ? 1 : 0;
2615
Chao-ying Fua77ee512014-07-01 17:43:41 -07002616 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002617 InToRegStorageMapping in_to_reg_storage_mapping;
2618 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2619 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2620 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
Serguei Katkov8e3acdd2014-07-15 12:01:00 +07002621 info->args[last_mapped_in].wide ? 2 : 1;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002622 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2623
2624 // Fisrt of all, check whether it make sense to use bulk copying
2625 // Optimization is aplicable only for range case
2626 // TODO: make a constant instead of 2
2627 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2628 // Scan the rest of the args - if in phys_reg flush to memory
2629 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2630 RegLocation loc = info->args[next_arg];
2631 if (loc.wide) {
2632 loc = UpdateLocWide(loc);
2633 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002634 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002635 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002636 }
2637 next_arg += 2;
2638 } else {
2639 loc = UpdateLoc(loc);
2640 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002641 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002642 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002643 }
2644 next_arg++;
2645 }
2646 }
2647
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002648 // The rest can be copied together
2649 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
Andreas Gampeccc60262014-07-04 18:02:38 -07002650 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped,
2651 cu_->instruction_set);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002652
2653 int current_src_offset = start_offset;
2654 int current_dest_offset = outs_offset;
2655
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002656 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2657 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002658 while (regs_left_to_pass_via_stack > 0) {
2659 // This is based on the knowledge that the stack itself is 16-byte aligned.
2660 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2661 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2662 size_t bytes_to_move;
2663
2664 /*
2665 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2666 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2667 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2668 * We do this because we could potentially do a smaller move to align.
2669 */
2670 if (regs_left_to_pass_via_stack == 4 ||
2671 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2672 // Moving 128-bits via xmm register.
2673 bytes_to_move = sizeof(uint32_t) * 4;
2674
2675 // Allocate a free xmm temp. Since we are working through the calling sequence,
2676 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2677 // there are no free registers.
2678 RegStorage temp = AllocTempDouble();
2679
2680 LIR* ld1 = nullptr;
2681 LIR* ld2 = nullptr;
2682 LIR* st1 = nullptr;
2683 LIR* st2 = nullptr;
2684
2685 /*
2686 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2687 * do an aligned move. If we have 8-byte alignment, then do the move in two
2688 * parts. This approach prevents possible cache line splits. Finally, fall back
2689 * to doing an unaligned move. In most cases we likely won't split the cache
2690 * line but we cannot prove it and thus take a conservative approach.
2691 */
2692 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2693 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2694
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002695 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002696 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002697 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002698 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002699 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovLo128FP);
2700 ld2 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002701 kMovHi128FP);
2702 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002703 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002704 }
2705
2706 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002707 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002708 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002709 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovLo128FP);
2710 st2 = OpMovMemReg(rs_rX86_SP, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002711 temp, kMovHi128FP);
2712 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002713 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002714 }
2715
2716 // TODO If we could keep track of aliasing information for memory accesses that are wider
2717 // than 64-bit, we wouldn't need to set up a barrier.
2718 if (ld1 != nullptr) {
2719 if (ld2 != nullptr) {
2720 // For 64-bit load we can actually set up the aliasing information.
2721 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2722 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2723 } else {
2724 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002725 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002726 }
2727 }
2728 if (st1 != nullptr) {
2729 if (st2 != nullptr) {
2730 // For 64-bit store we can actually set up the aliasing information.
2731 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2732 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2733 } else {
2734 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002735 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002736 }
2737 }
2738
2739 // Free the temporary used for the data movement.
2740 FreeTemp(temp);
2741 } else {
2742 // Moving 32-bits via general purpose register.
2743 bytes_to_move = sizeof(uint32_t);
2744
2745 // Instead of allocating a new temp, simply reuse one of the registers being used
2746 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07002747 RegStorage temp = TargetReg(kArg3, kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002748
2749 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002750 Load32Disp(rs_rX86_SP, current_src_offset, temp);
2751 Store32Disp(rs_rX86_SP, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002752 }
2753
2754 current_src_offset += bytes_to_move;
2755 current_dest_offset += bytes_to_move;
2756 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2757 }
2758 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2759 }
2760
2761 // Now handle rest not registers if they are
2762 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002763 RegStorage regSingle = TargetReg(kArg2, kNotWide);
2764 RegStorage regWide = TargetReg(kArg3, kWide);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002765 for (int i = start_index;
2766 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002767 RegLocation rl_arg = info->args[i];
2768 rl_arg = UpdateRawLoc(rl_arg);
2769 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2770 if (!reg.Valid()) {
2771 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2772
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002773 {
2774 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2775 if (rl_arg.wide) {
2776 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002777 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002778 } else {
2779 LoadValueDirectWideFixed(rl_arg, regWide);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002780 StoreBaseDisp(rs_rX86_SP, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002781 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002782 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002783 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002784 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002785 } else {
2786 LoadValueDirectFixed(rl_arg, regSingle);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002787 StoreBaseDisp(rs_rX86_SP, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002788 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002789 }
2790 }
2791 call_state = next_call_insn(cu_, info, call_state, target_method,
2792 vtable_idx, direct_code, direct_method, type);
2793 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002794 if (rl_arg.wide) {
2795 i++;
2796 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002797 }
2798 }
2799
2800 // Finish with mapped registers
2801 for (int i = start_index; i <= last_mapped_in; i++) {
2802 RegLocation rl_arg = info->args[i];
2803 rl_arg = UpdateRawLoc(rl_arg);
2804 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2805 if (reg.Valid()) {
2806 if (rl_arg.wide) {
2807 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002808 } else {
2809 LoadValueDirectFixed(rl_arg, reg);
2810 }
2811 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2812 direct_code, direct_method, type);
2813 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002814 if (rl_arg.wide) {
2815 i++;
2816 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002817 }
2818
2819 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2820 direct_code, direct_method, type);
2821 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00002822 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002823 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002824 } else {
2825 *pcrLabel = nullptr;
2826 // In lieu of generating a check for kArg1 being null, we need to
2827 // perform a load when doing implicit checks.
2828 RegStorage tmp = AllocTemp();
Andreas Gampeccc60262014-07-04 18:02:38 -07002829 Load32Disp(TargetReg(kArg1, kRef), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002830 MarkPossibleNullPointerException(info->opt_flags);
2831 FreeTemp(tmp);
2832 }
2833 }
2834 return call_state;
2835}
2836
Andreas Gampe98430592014-07-27 19:44:50 -07002837bool X86Mir2Lir::GenInlinedCharAt(CallInfo* info) {
2838 // Location of reference to data array
2839 int value_offset = mirror::String::ValueOffset().Int32Value();
2840 // Location of count
2841 int count_offset = mirror::String::CountOffset().Int32Value();
2842 // Starting offset within data array
2843 int offset_offset = mirror::String::OffsetOffset().Int32Value();
2844 // Start of char data with array_
2845 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
2846
2847 RegLocation rl_obj = info->args[0];
2848 RegLocation rl_idx = info->args[1];
2849 rl_obj = LoadValue(rl_obj, kRefReg);
2850 // X86 wants to avoid putting a constant index into a register.
2851 if (!rl_idx.is_const) {
2852 rl_idx = LoadValue(rl_idx, kCoreReg);
2853 }
2854 RegStorage reg_max;
2855 GenNullCheck(rl_obj.reg, info->opt_flags);
2856 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
2857 LIR* range_check_branch = nullptr;
2858 RegStorage reg_off;
2859 RegStorage reg_ptr;
2860 if (range_check) {
2861 // On x86, we can compare to memory directly
2862 // Set up a launch pad to allow retry in case of bounds violation */
2863 if (rl_idx.is_const) {
2864 LIR* comparison;
2865 range_check_branch = OpCmpMemImmBranch(
2866 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
2867 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr, &comparison);
2868 MarkPossibleNullPointerExceptionAfter(0, comparison);
2869 } else {
2870 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
2871 MarkPossibleNullPointerException(0);
2872 range_check_branch = OpCondBranch(kCondUge, nullptr);
2873 }
2874 }
2875 reg_off = AllocTemp();
2876 reg_ptr = AllocTempRef();
2877 Load32Disp(rl_obj.reg, offset_offset, reg_off);
2878 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
2879 if (rl_idx.is_const) {
2880 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
2881 } else {
2882 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
2883 }
2884 FreeTemp(rl_obj.reg);
2885 if (rl_idx.location == kLocPhysReg) {
2886 FreeTemp(rl_idx.reg);
2887 }
2888 RegLocation rl_dest = InlineTarget(info);
2889 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
2890 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
2891 FreeTemp(reg_off);
2892 FreeTemp(reg_ptr);
2893 StoreValue(rl_dest, rl_result);
2894 if (range_check) {
2895 DCHECK(range_check_branch != nullptr);
2896 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
2897 AddIntrinsicSlowPath(info, range_check_branch);
2898 }
2899 return true;
2900}
2901
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002902bool X86Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
2903 RegLocation rl_dest = InlineTarget(info);
2904
2905 // Early exit if the result is unused.
2906 if (rl_dest.orig_sreg < 0) {
2907 return true;
2908 }
2909
2910 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
2911
2912 if (cu_->target64) {
2913 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<8>());
2914 } else {
2915 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<4>());
2916 }
2917
2918 StoreValue(rl_dest, rl_result);
2919 return true;
2920}
2921
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002922} // namespace art