blob: 604f4bf516218334bce0cb2b03a8422fcc9e0a22 [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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "x86_lir.h"
Tong Shen547cdfd2014-08-05 01:54:19 -070028#include "utils/dwarf_cfi.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029
Brian Carlstrom7940e442013-07-12 13:46:57 -070030namespace art {
31
Vladimir Marko089142c2014-06-05 10:57:05 +010032static constexpr RegStorage core_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070033 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
34};
Vladimir Marko089142c2014-06-05 10:57:05 +010035static constexpr RegStorage core_regs_arr_64[] = {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070036 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
buzbee091cc402014-03-31 10:14:40 -070037 rs_r8, rs_r9, rs_r10, rs_r11, rs_r12, rs_r13, rs_r14, rs_r15
Brian Carlstrom7940e442013-07-12 13:46:57 -070038};
Vladimir Marko089142c2014-06-05 10:57:05 +010039static constexpr RegStorage core_regs_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070040 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 +070041 rs_r8q, rs_r9q, rs_r10q, rs_r11q, rs_r12q, rs_r13q, rs_r14q, rs_r15q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070042};
Vladimir Marko089142c2014-06-05 10:57:05 +010043static constexpr RegStorage sp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070044 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
45};
Vladimir Marko089142c2014-06-05 10:57:05 +010046static constexpr RegStorage sp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070047 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
buzbee091cc402014-03-31 10:14:40 -070048 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070049};
Vladimir Marko089142c2014-06-05 10:57:05 +010050static constexpr RegStorage dp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070051 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
52};
Vladimir Marko089142c2014-06-05 10:57:05 +010053static constexpr RegStorage dp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070054 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
buzbee091cc402014-03-31 10:14:40 -070055 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070056};
Serguei Katkovc3801912014-07-08 17:21:53 +070057static constexpr RegStorage xp_regs_arr_32[] = {
58 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
59};
60static constexpr RegStorage xp_regs_arr_64[] = {
61 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
62 rs_xr8, rs_xr9, rs_xr10, rs_xr11, rs_xr12, rs_xr13, rs_xr14, rs_xr15
63};
Vladimir Marko089142c2014-06-05 10:57:05 +010064static constexpr RegStorage reserved_regs_arr_32[] = {rs_rX86_SP_32};
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070065static constexpr RegStorage reserved_regs_arr_64[] = {rs_rX86_SP_32};
Vladimir Marko089142c2014-06-05 10:57:05 +010066static constexpr RegStorage reserved_regs_arr_64q[] = {rs_rX86_SP_64};
67static constexpr RegStorage core_temps_arr_32[] = {rs_rAX, rs_rCX, rs_rDX, rs_rBX};
68static constexpr RegStorage core_temps_arr_64[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070069 rs_rAX, rs_rCX, rs_rDX, rs_rSI, rs_rDI,
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070070 rs_r8, rs_r9, rs_r10, rs_r11
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070071};
Serguei Katkovc3801912014-07-08 17:21:53 +070072
73// How to add register to be available for promotion:
74// 1) Remove register from array defining temp
75// 2) Update ClobberCallerSave
76// 3) Update JNI compiler ABI:
77// 3.1) add reg in JniCallingConvention method
78// 3.2) update CoreSpillMask/FpSpillMask
79// 4) Update entrypoints
80// 4.1) Update constants in asm_support_x86_64.h for new frame size
81// 4.2) Remove entry in SmashCallerSaves
82// 4.3) Update jni_entrypoints to spill/unspill new callee save reg
83// 4.4) Update quick_entrypoints to spill/unspill new callee save reg
84// 5) Update runtime ABI
85// 5.1) Update quick_method_frame_info with new required spills
86// 5.2) Update QuickArgumentVisitor with new offsets to gprs and xmms
87// Note that you cannot use register corresponding to incoming args
88// according to ABI and QCG needs one additional XMM temp for
89// bulk copy in preparation to call.
Vladimir Marko089142c2014-06-05 10:57:05 +010090static constexpr RegStorage core_temps_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070091 rs_r0q, rs_r1q, rs_r2q, rs_r6q, rs_r7q,
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070092 rs_r8q, rs_r9q, rs_r10q, rs_r11q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070093};
Vladimir Marko089142c2014-06-05 10:57:05 +010094static constexpr RegStorage sp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070095 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
96};
Vladimir Marko089142c2014-06-05 10:57:05 +010097static constexpr RegStorage sp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070098 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
Serguei Katkovc3801912014-07-08 17:21:53 +070099 rs_fr8, rs_fr9, rs_fr10, rs_fr11
buzbee091cc402014-03-31 10:14:40 -0700100};
Vladimir Marko089142c2014-06-05 10:57:05 +0100101static constexpr RegStorage dp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700102 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
103};
Vladimir Marko089142c2014-06-05 10:57:05 +0100104static constexpr RegStorage dp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -0700105 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700106 rs_dr8, rs_dr9, rs_dr10, rs_dr11
buzbee091cc402014-03-31 10:14:40 -0700107};
108
Vladimir Marko089142c2014-06-05 10:57:05 +0100109static constexpr RegStorage xp_temps_arr_32[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400110 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
111};
Vladimir Marko089142c2014-06-05 10:57:05 +0100112static constexpr RegStorage xp_temps_arr_64[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400113 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700114 rs_xr8, rs_xr9, rs_xr10, rs_xr11
Mark Mendellfe945782014-05-22 09:52:36 -0400115};
116
Vladimir Marko089142c2014-06-05 10:57:05 +0100117static constexpr ArrayRef<const RegStorage> empty_pool;
118static constexpr ArrayRef<const RegStorage> core_regs_32(core_regs_arr_32);
119static constexpr ArrayRef<const RegStorage> core_regs_64(core_regs_arr_64);
120static constexpr ArrayRef<const RegStorage> core_regs_64q(core_regs_arr_64q);
121static constexpr ArrayRef<const RegStorage> sp_regs_32(sp_regs_arr_32);
122static constexpr ArrayRef<const RegStorage> sp_regs_64(sp_regs_arr_64);
123static constexpr ArrayRef<const RegStorage> dp_regs_32(dp_regs_arr_32);
124static constexpr ArrayRef<const RegStorage> dp_regs_64(dp_regs_arr_64);
Serguei Katkovc3801912014-07-08 17:21:53 +0700125static constexpr ArrayRef<const RegStorage> xp_regs_32(xp_regs_arr_32);
126static constexpr ArrayRef<const RegStorage> xp_regs_64(xp_regs_arr_64);
Vladimir Marko089142c2014-06-05 10:57:05 +0100127static constexpr ArrayRef<const RegStorage> reserved_regs_32(reserved_regs_arr_32);
128static constexpr ArrayRef<const RegStorage> reserved_regs_64(reserved_regs_arr_64);
129static constexpr ArrayRef<const RegStorage> reserved_regs_64q(reserved_regs_arr_64q);
130static constexpr ArrayRef<const RegStorage> core_temps_32(core_temps_arr_32);
131static constexpr ArrayRef<const RegStorage> core_temps_64(core_temps_arr_64);
132static constexpr ArrayRef<const RegStorage> core_temps_64q(core_temps_arr_64q);
133static constexpr ArrayRef<const RegStorage> sp_temps_32(sp_temps_arr_32);
134static constexpr ArrayRef<const RegStorage> sp_temps_64(sp_temps_arr_64);
135static constexpr ArrayRef<const RegStorage> dp_temps_32(dp_temps_arr_32);
136static constexpr ArrayRef<const RegStorage> dp_temps_64(dp_temps_arr_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700137
Vladimir Marko089142c2014-06-05 10:57:05 +0100138static constexpr ArrayRef<const RegStorage> xp_temps_32(xp_temps_arr_32);
139static constexpr ArrayRef<const RegStorage> xp_temps_64(xp_temps_arr_64);
Mark Mendellfe945782014-05-22 09:52:36 -0400140
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700141RegStorage rs_rX86_SP;
142
143X86NativeRegisterPool rX86_ARG0;
144X86NativeRegisterPool rX86_ARG1;
145X86NativeRegisterPool rX86_ARG2;
146X86NativeRegisterPool rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700147X86NativeRegisterPool rX86_ARG4;
148X86NativeRegisterPool rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700149X86NativeRegisterPool rX86_FARG0;
150X86NativeRegisterPool rX86_FARG1;
151X86NativeRegisterPool rX86_FARG2;
152X86NativeRegisterPool rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700153X86NativeRegisterPool rX86_FARG4;
154X86NativeRegisterPool rX86_FARG5;
155X86NativeRegisterPool rX86_FARG6;
156X86NativeRegisterPool rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700157X86NativeRegisterPool rX86_RET0;
158X86NativeRegisterPool rX86_RET1;
159X86NativeRegisterPool rX86_INVOKE_TGT;
160X86NativeRegisterPool rX86_COUNT;
161
162RegStorage rs_rX86_ARG0;
163RegStorage rs_rX86_ARG1;
164RegStorage rs_rX86_ARG2;
165RegStorage rs_rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700166RegStorage rs_rX86_ARG4;
167RegStorage rs_rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700168RegStorage rs_rX86_FARG0;
169RegStorage rs_rX86_FARG1;
170RegStorage rs_rX86_FARG2;
171RegStorage rs_rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700172RegStorage rs_rX86_FARG4;
173RegStorage rs_rX86_FARG5;
174RegStorage rs_rX86_FARG6;
175RegStorage rs_rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700176RegStorage rs_rX86_RET0;
177RegStorage rs_rX86_RET1;
178RegStorage rs_rX86_INVOKE_TGT;
179RegStorage rs_rX86_COUNT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700181RegLocation X86Mir2Lir::LocCReturn() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000182 return x86_loc_c_return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183}
184
buzbeea0cd2d72014-06-01 09:33:49 -0700185RegLocation X86Mir2Lir::LocCReturnRef() {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700186 return cu_->target64 ? x86_64_loc_c_return_ref : x86_loc_c_return_ref;
buzbeea0cd2d72014-06-01 09:33:49 -0700187}
188
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700189RegLocation X86Mir2Lir::LocCReturnWide() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700190 return cu_->target64 ? x86_64_loc_c_return_wide : x86_loc_c_return_wide;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191}
192
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700193RegLocation X86Mir2Lir::LocCReturnFloat() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000194 return x86_loc_c_return_float;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195}
196
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700197RegLocation X86Mir2Lir::LocCReturnDouble() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000198 return x86_loc_c_return_double;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199}
200
Chao-ying Fua77ee512014-07-01 17:43:41 -0700201// Return a target-dependent special register for 32-bit.
202RegStorage X86Mir2Lir::TargetReg32(SpecialTargetRegister reg) {
buzbee091cc402014-03-31 10:14:40 -0700203 RegStorage res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 switch (reg) {
buzbee091cc402014-03-31 10:14:40 -0700205 case kSelf: res_reg = RegStorage::InvalidReg(); break;
206 case kSuspend: res_reg = RegStorage::InvalidReg(); break;
207 case kLr: res_reg = RegStorage::InvalidReg(); break;
208 case kPc: res_reg = RegStorage::InvalidReg(); break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700209 case kSp: res_reg = rs_rX86_SP_32; break; // This must be the concrete one, as _SP is target-
210 // specific size.
buzbee091cc402014-03-31 10:14:40 -0700211 case kArg0: res_reg = rs_rX86_ARG0; break;
212 case kArg1: res_reg = rs_rX86_ARG1; break;
213 case kArg2: res_reg = rs_rX86_ARG2; break;
214 case kArg3: res_reg = rs_rX86_ARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700215 case kArg4: res_reg = rs_rX86_ARG4; break;
216 case kArg5: res_reg = rs_rX86_ARG5; break;
buzbee091cc402014-03-31 10:14:40 -0700217 case kFArg0: res_reg = rs_rX86_FARG0; break;
218 case kFArg1: res_reg = rs_rX86_FARG1; break;
219 case kFArg2: res_reg = rs_rX86_FARG2; break;
220 case kFArg3: res_reg = rs_rX86_FARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700221 case kFArg4: res_reg = rs_rX86_FARG4; break;
222 case kFArg5: res_reg = rs_rX86_FARG5; break;
223 case kFArg6: res_reg = rs_rX86_FARG6; break;
224 case kFArg7: res_reg = rs_rX86_FARG7; break;
buzbee091cc402014-03-31 10:14:40 -0700225 case kRet0: res_reg = rs_rX86_RET0; break;
226 case kRet1: res_reg = rs_rX86_RET1; break;
227 case kInvokeTgt: res_reg = rs_rX86_INVOKE_TGT; break;
228 case kHiddenArg: res_reg = rs_rAX; break;
Elena Sayapinadd644502014-07-01 18:39:52 +0700229 case kHiddenFpArg: DCHECK(!cu_->target64); res_reg = rs_fr0; break;
buzbee091cc402014-03-31 10:14:40 -0700230 case kCount: res_reg = rs_rX86_COUNT; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700231 default: res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 }
buzbee091cc402014-03-31 10:14:40 -0700233 return res_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234}
235
Chao-ying Fua77ee512014-07-01 17:43:41 -0700236RegStorage X86Mir2Lir::TargetReg(SpecialTargetRegister reg) {
237 LOG(FATAL) << "Do not use this function!!!";
238 return RegStorage::InvalidReg();
239}
240
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241/*
242 * Decode the register id.
243 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100244ResourceMask X86Mir2Lir::GetRegMaskCommon(const RegStorage& reg) const {
245 /* Double registers in x86 are just a single FP register. This is always just a single bit. */
246 return ResourceMask::Bit(
247 /* FP register starts at bit position 16 */
248 ((reg.IsFloat() || reg.StorageSize() > 8) ? kX86FPReg0 : 0) + reg.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249}
250
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100251ResourceMask X86Mir2Lir::GetPCUseDefEncoding() const {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100252 return kEncodeNone;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253}
254
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100255void X86Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags,
256 ResourceMask* use_mask, ResourceMask* def_mask) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700257 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbeeb48819d2013-09-14 16:15:25 -0700258 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259
260 // X86-specific resource map setup here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 if (flags & REG_USE_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100262 use_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 }
264
265 if (flags & REG_DEF_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100266 def_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 }
268
269 if (flags & REG_DEFA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100270 SetupRegMask(def_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 }
272
273 if (flags & REG_DEFD) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100274 SetupRegMask(def_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 }
276 if (flags & REG_USEA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100277 SetupRegMask(use_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 }
279
280 if (flags & REG_USEC) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100281 SetupRegMask(use_mask, rs_rCX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 }
283
284 if (flags & REG_USED) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100285 SetupRegMask(use_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 }
Vladimir Marko70b797d2013-12-03 15:25:24 +0000287
288 if (flags & REG_USEB) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100289 SetupRegMask(use_mask, rs_rBX.GetReg());
Vladimir Marko70b797d2013-12-03 15:25:24 +0000290 }
Mark Mendell4028a6c2014-02-19 20:06:20 -0800291
292 // Fixup hard to describe instruction: Uses rAX, rCX, rDI; sets rDI.
293 if (lir->opcode == kX86RepneScasw) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100294 SetupRegMask(use_mask, rs_rAX.GetReg());
295 SetupRegMask(use_mask, rs_rCX.GetReg());
296 SetupRegMask(use_mask, rs_rDI.GetReg());
297 SetupRegMask(def_mask, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800298 }
Serguei Katkove90501d2014-03-12 15:56:54 +0700299
300 if (flags & USE_FP_STACK) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100301 use_mask->SetBit(kX86FPStack);
302 def_mask->SetBit(kX86FPStack);
Serguei Katkove90501d2014-03-12 15:56:54 +0700303 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304}
305
306/* For dumping instructions */
307static const char* x86RegName[] = {
308 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
309 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
310};
311
312static const char* x86CondName[] = {
313 "O",
314 "NO",
315 "B/NAE/C",
316 "NB/AE/NC",
317 "Z/EQ",
318 "NZ/NE",
319 "BE/NA",
320 "NBE/A",
321 "S",
322 "NS",
323 "P/PE",
324 "NP/PO",
325 "L/NGE",
326 "NL/GE",
327 "LE/NG",
328 "NLE/G"
329};
330
331/*
332 * Interpret a format string and build a string no longer than size
333 * See format key in Assemble.cc.
334 */
335std::string X86Mir2Lir::BuildInsnString(const char *fmt, LIR *lir, unsigned char* base_addr) {
336 std::string buf;
337 size_t i = 0;
338 size_t fmt_len = strlen(fmt);
339 while (i < fmt_len) {
340 if (fmt[i] != '!') {
341 buf += fmt[i];
342 i++;
343 } else {
344 i++;
345 DCHECK_LT(i, fmt_len);
346 char operand_number_ch = fmt[i];
347 i++;
348 if (operand_number_ch == '!') {
349 buf += "!";
350 } else {
351 int operand_number = operand_number_ch - '0';
352 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
353 DCHECK_LT(i, fmt_len);
354 int operand = lir->operands[operand_number];
355 switch (fmt[i]) {
356 case 'c':
357 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
358 buf += x86CondName[operand];
359 break;
360 case 'd':
361 buf += StringPrintf("%d", operand);
362 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400363 case 'q': {
364 int64_t value = static_cast<int64_t>(static_cast<int64_t>(operand) << 32 |
365 static_cast<uint32_t>(lir->operands[operand_number+1]));
366 buf +=StringPrintf("%" PRId64, value);
Haitao Fenge70f1792014-08-09 08:31:02 +0800367 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400368 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 case 'p': {
buzbee0d829482013-10-11 15:24:55 -0700370 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(operand));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 buf += StringPrintf("0x%08x", tab_rec->offset);
372 break;
373 }
374 case 'r':
buzbee091cc402014-03-31 10:14:40 -0700375 if (RegStorage::IsFloat(operand)) {
376 int fp_reg = RegStorage::RegNum(operand);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 buf += StringPrintf("xmm%d", fp_reg);
378 } else {
buzbee091cc402014-03-31 10:14:40 -0700379 int reg_num = RegStorage::RegNum(operand);
380 DCHECK_LT(static_cast<size_t>(reg_num), sizeof(x86RegName));
381 buf += x86RegName[reg_num];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 }
383 break;
384 case 't':
Ian Rogers107c31e2014-01-23 20:55:29 -0800385 buf += StringPrintf("0x%08" PRIxPTR " (L%p)",
386 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + operand,
387 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 break;
389 default:
390 buf += StringPrintf("DecodeError '%c'", fmt[i]);
391 break;
392 }
393 i++;
394 }
395 }
396 }
397 return buf;
398}
399
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100400void X86Mir2Lir::DumpResourceMask(LIR *x86LIR, const ResourceMask& mask, const char *prefix) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 char buf[256];
402 buf[0] = 0;
403
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100404 if (mask.Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 strcpy(buf, "all");
406 } else {
407 char num[8];
408 int i;
409
410 for (i = 0; i < kX86RegEnd; i++) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100411 if (mask.HasBit(i)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800412 snprintf(num, arraysize(num), "%d ", i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 strcat(buf, num);
414 }
415 }
416
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100417 if (mask.HasBit(ResourceMask::kCCode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 strcat(buf, "cc ");
419 }
420 /* Memory bits */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100421 if (x86LIR && (mask.HasBit(ResourceMask::kDalvikReg))) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800422 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
423 DECODE_ALIAS_INFO_REG(x86LIR->flags.alias_info),
424 (DECODE_ALIAS_INFO_WIDE(x86LIR->flags.alias_info)) ? "(+1)" : "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100426 if (mask.HasBit(ResourceMask::kLiteral)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 strcat(buf, "lit ");
428 }
429
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100430 if (mask.HasBit(ResourceMask::kHeapRef)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 strcat(buf, "heap ");
432 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100433 if (mask.HasBit(ResourceMask::kMustNotAlias)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 strcat(buf, "noalias ");
435 }
436 }
437 if (buf[0]) {
438 LOG(INFO) << prefix << ": " << buf;
439 }
440}
441
442void X86Mir2Lir::AdjustSpillMask() {
443 // Adjustment for LR spilling, x86 has no LR so nothing to do here
buzbee091cc402014-03-31 10:14:40 -0700444 core_spill_mask_ |= (1 << rs_rRET.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 num_core_spills_++;
446}
447
Mark Mendelle87f9b52014-04-30 14:13:18 -0400448RegStorage X86Mir2Lir::AllocateByteRegister() {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700449 RegStorage reg = AllocTypedTemp(false, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +0700450 if (!cu_->target64) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700451 DCHECK_LT(reg.GetRegNum(), rs_rX86_SP.GetRegNum());
452 }
453 return reg;
454}
455
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700456RegStorage X86Mir2Lir::Get128BitRegister(RegStorage reg) {
457 return GetRegInfo(reg)->FindMatchingView(RegisterInfo::k128SoloStorageMask)->GetReg();
458}
459
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700460bool X86Mir2Lir::IsByteRegister(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700461 return cu_->target64 || reg.GetRegNum() < rs_rX86_SP.GetRegNum();
Mark Mendelle87f9b52014-04-30 14:13:18 -0400462}
463
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464/* Clobber all regs that might be used by an external C call */
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000465void X86Mir2Lir::ClobberCallerSave() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700466 if (cu_->target64) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700467 Clobber(rs_rAX);
468 Clobber(rs_rCX);
469 Clobber(rs_rDX);
470 Clobber(rs_rSI);
471 Clobber(rs_rDI);
472
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700473 Clobber(rs_r8);
474 Clobber(rs_r9);
475 Clobber(rs_r10);
476 Clobber(rs_r11);
477
478 Clobber(rs_fr8);
479 Clobber(rs_fr9);
480 Clobber(rs_fr10);
481 Clobber(rs_fr11);
Serguei Katkovc3801912014-07-08 17:21:53 +0700482 } else {
483 Clobber(rs_rAX);
484 Clobber(rs_rCX);
485 Clobber(rs_rDX);
486 Clobber(rs_rBX);
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700487 }
Serguei Katkovc3801912014-07-08 17:21:53 +0700488
489 Clobber(rs_fr0);
490 Clobber(rs_fr1);
491 Clobber(rs_fr2);
492 Clobber(rs_fr3);
493 Clobber(rs_fr4);
494 Clobber(rs_fr5);
495 Clobber(rs_fr6);
496 Clobber(rs_fr7);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497}
498
499RegLocation X86Mir2Lir::GetReturnWideAlt() {
500 RegLocation res = LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -0700501 DCHECK(res.reg.GetLowReg() == rs_rAX.GetReg());
502 DCHECK(res.reg.GetHighReg() == rs_rDX.GetReg());
503 Clobber(rs_rAX);
504 Clobber(rs_rDX);
505 MarkInUse(rs_rAX);
506 MarkInUse(rs_rDX);
507 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 return res;
509}
510
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700511RegLocation X86Mir2Lir::GetReturnAlt() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 RegLocation res = LocCReturn();
buzbee091cc402014-03-31 10:14:40 -0700513 res.reg.SetReg(rs_rDX.GetReg());
514 Clobber(rs_rDX);
515 MarkInUse(rs_rDX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 return res;
517}
518
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700520void X86Mir2Lir::LockCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700521 LockTemp(rs_rX86_ARG0);
522 LockTemp(rs_rX86_ARG1);
523 LockTemp(rs_rX86_ARG2);
524 LockTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700525 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700526 LockTemp(rs_rX86_ARG4);
527 LockTemp(rs_rX86_ARG5);
528 LockTemp(rs_rX86_FARG0);
529 LockTemp(rs_rX86_FARG1);
530 LockTemp(rs_rX86_FARG2);
531 LockTemp(rs_rX86_FARG3);
532 LockTemp(rs_rX86_FARG4);
533 LockTemp(rs_rX86_FARG5);
534 LockTemp(rs_rX86_FARG6);
535 LockTemp(rs_rX86_FARG7);
536 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537}
538
539/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700540void X86Mir2Lir::FreeCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700541 FreeTemp(rs_rX86_ARG0);
542 FreeTemp(rs_rX86_ARG1);
543 FreeTemp(rs_rX86_ARG2);
544 FreeTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700545 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700546 FreeTemp(rs_rX86_ARG4);
547 FreeTemp(rs_rX86_ARG5);
548 FreeTemp(rs_rX86_FARG0);
549 FreeTemp(rs_rX86_FARG1);
550 FreeTemp(rs_rX86_FARG2);
551 FreeTemp(rs_rX86_FARG3);
552 FreeTemp(rs_rX86_FARG4);
553 FreeTemp(rs_rX86_FARG5);
554 FreeTemp(rs_rX86_FARG6);
555 FreeTemp(rs_rX86_FARG7);
556 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557}
558
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800559bool X86Mir2Lir::ProvidesFullMemoryBarrier(X86OpCode opcode) {
560 switch (opcode) {
561 case kX86LockCmpxchgMR:
562 case kX86LockCmpxchgAR:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700563 case kX86LockCmpxchg64M:
564 case kX86LockCmpxchg64A:
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800565 case kX86XchgMR:
566 case kX86Mfence:
567 // Atomic memory instructions provide full barrier.
568 return true;
569 default:
570 break;
571 }
572
573 // Conservative if cannot prove it provides full barrier.
574 return false;
575}
576
Andreas Gampeb14329f2014-05-15 11:16:06 -0700577bool X86Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800579 // Start off with using the last LIR as the barrier. If it is not enough, then we will update it.
580 LIR* mem_barrier = last_lir_insn_;
581
Andreas Gampeb14329f2014-05-15 11:16:06 -0700582 bool ret = false;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800583 /*
Hans Boehm48f5c472014-06-27 14:50:10 -0700584 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
585 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
586 * 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 -0800587 */
Hans Boehm48f5c472014-06-27 14:50:10 -0700588 if (barrier_kind == kAnyAny) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800589 // If no LIR exists already that can be used a barrier, then generate an mfence.
590 if (mem_barrier == nullptr) {
591 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700592 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800593 }
594
595 // If last instruction does not provide full barrier, then insert an mfence.
596 if (ProvidesFullMemoryBarrier(static_cast<X86OpCode>(mem_barrier->opcode)) == false) {
597 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700598 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800599 }
600 }
601
602 // Now ensure that a scheduling barrier is in place.
603 if (mem_barrier == nullptr) {
604 GenBarrier();
605 } else {
606 // Mark as a scheduling barrier.
607 DCHECK(!mem_barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100608 mem_barrier->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800609 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700610 return ret;
611#else
612 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613#endif
614}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000615
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616void X86Mir2Lir::CompilerInitializeRegAlloc() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700617 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700618 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_64, core_regs_64q, sp_regs_64,
619 dp_regs_64, reserved_regs_64, reserved_regs_64q,
620 core_temps_64, core_temps_64q, sp_temps_64, dp_temps_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700621 } else {
buzbeeb01bf152014-05-13 15:59:07 -0700622 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
623 dp_regs_32, reserved_regs_32, empty_pool,
624 core_temps_32, empty_pool, sp_temps_32, dp_temps_32);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700625 }
buzbee091cc402014-03-31 10:14:40 -0700626
627 // Target-specific adjustments.
628
Mark Mendellfe945782014-05-22 09:52:36 -0400629 // Add in XMM registers.
Serguei Katkovc3801912014-07-08 17:21:53 +0700630 const ArrayRef<const RegStorage> *xp_regs = cu_->target64 ? &xp_regs_64 : &xp_regs_32;
631 for (RegStorage reg : *xp_regs) {
Mark Mendellfe945782014-05-22 09:52:36 -0400632 RegisterInfo* info = new (arena_) RegisterInfo(reg, GetRegMaskCommon(reg));
633 reginfo_map_.Put(reg.GetReg(), info);
Serguei Katkovc3801912014-07-08 17:21:53 +0700634 }
635 const ArrayRef<const RegStorage> *xp_temps = cu_->target64 ? &xp_temps_64 : &xp_temps_32;
636 for (RegStorage reg : *xp_temps) {
637 RegisterInfo* xp_reg_info = GetRegInfo(reg);
638 xp_reg_info->SetIsTemp(true);
Mark Mendellfe945782014-05-22 09:52:36 -0400639 }
640
buzbee091cc402014-03-31 10:14:40 -0700641 // Alias single precision xmm to double xmms.
642 // TODO: as needed, add larger vector sizes - alias all to the largest.
643 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
644 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
645 int sp_reg_num = info->GetReg().GetRegNum();
Mark Mendellfe945782014-05-22 09:52:36 -0400646 RegStorage xp_reg = RegStorage::Solo128(sp_reg_num);
647 RegisterInfo* xp_reg_info = GetRegInfo(xp_reg);
648 // 128-bit xmm vector register's master storage should refer to itself.
649 DCHECK_EQ(xp_reg_info, xp_reg_info->Master());
650
651 // Redirect 32-bit vector's master storage to 128-bit vector.
652 info->SetMaster(xp_reg_info);
653
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700654 RegStorage dp_reg = RegStorage::FloatSolo64(sp_reg_num);
buzbee091cc402014-03-31 10:14:40 -0700655 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
Mark Mendellfe945782014-05-22 09:52:36 -0400656 // Redirect 64-bit vector's master storage to 128-bit vector.
657 dp_reg_info->SetMaster(xp_reg_info);
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700658 // Singles should show a single 32-bit mask bit, at first referring to the low half.
659 DCHECK_EQ(info->StorageMask(), 0x1U);
660 }
661
Elena Sayapinadd644502014-07-01 18:39:52 +0700662 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700663 // Alias 32bit W registers to corresponding 64bit X registers.
664 GrowableArray<RegisterInfo*>::Iterator w_it(&reg_pool_->core_regs_);
665 for (RegisterInfo* info = w_it.Next(); info != nullptr; info = w_it.Next()) {
666 int x_reg_num = info->GetReg().GetRegNum();
667 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
668 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
669 // 64bit X register's master storage should refer to itself.
670 DCHECK_EQ(x_reg_info, x_reg_info->Master());
671 // Redirect 32bit W master storage to 64bit X.
672 info->SetMaster(x_reg_info);
673 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
674 DCHECK_EQ(info->StorageMask(), 0x1U);
675 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 }
buzbee091cc402014-03-31 10:14:40 -0700677
678 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
679 // TODO: adjust for x86/hard float calling convention.
680 reg_pool_->next_core_reg_ = 2;
681 reg_pool_->next_sp_reg_ = 2;
682 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683}
684
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700685int X86Mir2Lir::VectorRegisterSize() {
686 return 128;
687}
688
689int X86Mir2Lir::NumReservableVectorRegisters(bool fp_used) {
690 return fp_used ? 5 : 7;
691}
692
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693void X86Mir2Lir::SpillCoreRegs() {
694 if (num_core_spills_ == 0) {
695 return;
696 }
697 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700698 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700699 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700700 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 for (int reg = 0; mask; mask >>= 1, reg++) {
702 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700703 StoreBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
704 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700705 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 }
707 }
708}
709
710void X86Mir2Lir::UnSpillCoreRegs() {
711 if (num_core_spills_ == 0) {
712 return;
713 }
714 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700715 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700716 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700717 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 for (int reg = 0; mask; mask >>= 1, reg++) {
719 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700720 LoadBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
721 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700722 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 }
724 }
725}
726
Serguei Katkovc3801912014-07-08 17:21:53 +0700727void X86Mir2Lir::SpillFPRegs() {
728 if (num_fp_spills_ == 0) {
729 return;
730 }
731 uint32_t mask = fp_spill_mask_;
732 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
733 for (int reg = 0; mask; mask >>= 1, reg++) {
734 if (mask & 0x1) {
735 StoreBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
736 k64, kNotVolatile);
737 offset += sizeof(double);
738 }
739 }
740}
741void X86Mir2Lir::UnSpillFPRegs() {
742 if (num_fp_spills_ == 0) {
743 return;
744 }
745 uint32_t mask = fp_spill_mask_;
746 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
747 for (int reg = 0; mask; mask >>= 1, reg++) {
748 if (mask & 0x1) {
749 LoadBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
750 k64, kNotVolatile);
751 offset += sizeof(double);
752 }
753 }
754}
755
756
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700757bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
759}
760
Vladimir Marko674744e2014-04-24 15:18:26 +0100761RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700762 // X86_64 can handle any size.
Elena Sayapinadd644502014-07-01 18:39:52 +0700763 if (cu_->target64) {
Chao-ying Fu06839f82014-08-14 15:59:17 -0700764 return RegClassBySize(size);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700765 }
766
Vladimir Marko674744e2014-04-24 15:18:26 +0100767 if (UNLIKELY(is_volatile)) {
768 // On x86, atomic 64-bit load/store requires an fp register.
769 // Smaller aligned load/store is atomic for both core and fp registers.
770 if (size == k64 || size == kDouble) {
771 return kFPReg;
772 }
773 }
774 return RegClassBySize(size);
775}
776
Elena Sayapinadd644502014-07-01 18:39:52 +0700777X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800778 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700779 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800780 method_address_insns_(arena, 100, kGrowableArrayMisc),
781 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800782 call_method_insns_(arena, 100, kGrowableArrayMisc),
Elena Sayapinadd644502014-07-01 18:39:52 +0700783 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400784 const_vectors_(nullptr) {
785 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700786 if (kIsDebugBuild) {
787 for (int i = 0; i < kX86Last; i++) {
788 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
789 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400790 << " is wrong: expecting " << i << ", seeing "
791 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700792 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 }
794 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700795 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700796 rs_rX86_SP = rs_rX86_SP_64;
797
798 rs_rX86_ARG0 = rs_rDI;
799 rs_rX86_ARG1 = rs_rSI;
800 rs_rX86_ARG2 = rs_rDX;
801 rs_rX86_ARG3 = rs_rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700802 rs_rX86_ARG4 = rs_r8;
803 rs_rX86_ARG5 = rs_r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700804 rs_rX86_FARG0 = rs_fr0;
805 rs_rX86_FARG1 = rs_fr1;
806 rs_rX86_FARG2 = rs_fr2;
807 rs_rX86_FARG3 = rs_fr3;
808 rs_rX86_FARG4 = rs_fr4;
809 rs_rX86_FARG5 = rs_fr5;
810 rs_rX86_FARG6 = rs_fr6;
811 rs_rX86_FARG7 = rs_fr7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700812 rX86_ARG0 = rDI;
813 rX86_ARG1 = rSI;
814 rX86_ARG2 = rDX;
815 rX86_ARG3 = rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700816 rX86_ARG4 = r8;
817 rX86_ARG5 = r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700818 rX86_FARG0 = fr0;
819 rX86_FARG1 = fr1;
820 rX86_FARG2 = fr2;
821 rX86_FARG3 = fr3;
822 rX86_FARG4 = fr4;
823 rX86_FARG5 = fr5;
824 rX86_FARG6 = fr6;
825 rX86_FARG7 = fr7;
Mark Mendell55884bc2014-06-10 10:21:29 -0400826 rs_rX86_INVOKE_TGT = rs_rDI;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700827 } else {
828 rs_rX86_SP = rs_rX86_SP_32;
829
830 rs_rX86_ARG0 = rs_rAX;
831 rs_rX86_ARG1 = rs_rCX;
832 rs_rX86_ARG2 = rs_rDX;
833 rs_rX86_ARG3 = rs_rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700834 rs_rX86_ARG4 = RegStorage::InvalidReg();
835 rs_rX86_ARG5 = RegStorage::InvalidReg();
836 rs_rX86_FARG0 = rs_rAX;
837 rs_rX86_FARG1 = rs_rCX;
838 rs_rX86_FARG2 = rs_rDX;
839 rs_rX86_FARG3 = rs_rBX;
840 rs_rX86_FARG4 = RegStorage::InvalidReg();
841 rs_rX86_FARG5 = RegStorage::InvalidReg();
842 rs_rX86_FARG6 = RegStorage::InvalidReg();
843 rs_rX86_FARG7 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700844 rX86_ARG0 = rAX;
845 rX86_ARG1 = rCX;
846 rX86_ARG2 = rDX;
847 rX86_ARG3 = rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700848 rX86_FARG0 = rAX;
849 rX86_FARG1 = rCX;
850 rX86_FARG2 = rDX;
851 rX86_FARG3 = rBX;
Mark Mendell55884bc2014-06-10 10:21:29 -0400852 rs_rX86_INVOKE_TGT = rs_rAX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700853 // TODO(64): Initialize with invalid reg
854// rX86_ARG4 = RegStorage::InvalidReg();
855// rX86_ARG5 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700856 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700857 rs_rX86_RET0 = rs_rAX;
858 rs_rX86_RET1 = rs_rDX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700859 rs_rX86_COUNT = rs_rCX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700860 rX86_RET0 = rAX;
861 rX86_RET1 = rDX;
862 rX86_INVOKE_TGT = rAX;
863 rX86_COUNT = rCX;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700864
865 // Initialize the number of reserved vector registers
866 num_reserved_vector_regs_ = -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867}
868
869Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
870 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700871 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872}
873
Andreas Gampe98430592014-07-27 19:44:50 -0700874// Not used in x86(-64)
875RegStorage X86Mir2Lir::LoadHelper(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700876 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
877 return RegStorage::InvalidReg();
878}
879
Dave Allisonb373e092014-02-20 16:06:36 -0800880LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
Dave Allison69dfe512014-07-11 17:11:58 +0000881 // First load the pointer in fs:[suspend-trigger] into eax
882 // Then use a test instruction to indirect via that address.
Dave Allisondfd3b472014-07-16 16:04:32 -0700883 if (cu_->target64) {
884 NewLIR2(kX86Mov64RT, rs_rAX.GetReg(),
885 Thread::ThreadSuspendTriggerOffset<8>().Int32Value());
886 } else {
887 NewLIR2(kX86Mov32RT, rs_rAX.GetReg(),
888 Thread::ThreadSuspendTriggerOffset<4>().Int32Value());
889 }
Dave Allison69dfe512014-07-11 17:11:58 +0000890 return NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rAX.GetReg(), 0);
Dave Allisonb373e092014-02-20 16:06:36 -0800891}
892
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700893uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700894 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 return X86Mir2Lir::EncodingMap[opcode].flags;
896}
897
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700898const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700899 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 return X86Mir2Lir::EncodingMap[opcode].name;
901}
902
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700903const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700904 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 return X86Mir2Lir::EncodingMap[opcode].fmt;
906}
907
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000908void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
909 // Can we do this directly to memory?
910 rl_dest = UpdateLocWide(rl_dest);
911 if ((rl_dest.location == kLocDalvikFrame) ||
912 (rl_dest.location == kLocCompilerTemp)) {
913 int32_t val_lo = Low32Bits(value);
914 int32_t val_hi = High32Bits(value);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700915 int r_base = rs_rX86_SP.GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000916 int displacement = SRegOffset(rl_dest.s_reg_low);
917
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100918 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800919 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000920 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
921 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800922 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000923 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
924 false /* is_load */, true /* is64bit */);
925 return;
926 }
927
928 // Just use the standard code to do the generation.
929 Mir2Lir::GenConstWide(rl_dest, value);
930}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800931
932// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
933void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
934 LOG(INFO) << "location: " << loc.location << ','
935 << (loc.wide ? " w" : " ")
936 << (loc.defined ? " D" : " ")
937 << (loc.is_const ? " c" : " ")
938 << (loc.fp ? " F" : " ")
939 << (loc.core ? " C" : " ")
940 << (loc.ref ? " r" : " ")
941 << (loc.high_word ? " h" : " ")
942 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800943 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000944 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800945 << ", s_reg: " << loc.s_reg_low
946 << ", orig: " << loc.orig_sreg;
947}
948
Mark Mendell67c39c42014-01-31 17:28:00 -0800949void X86Mir2Lir::Materialize() {
950 // A good place to put the analysis before starting.
951 AnalyzeMIR();
952
953 // Now continue with regular code generation.
954 Mir2Lir::Materialize();
955}
956
Jeff Hao49161ce2014-03-12 11:05:25 -0700957void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800958 SpecialTargetRegister symbolic_reg) {
959 /*
960 * For x86, just generate a 32 bit move immediate instruction, that will be filled
961 * in at 'link time'. For now, put a unique value based on target to ensure that
962 * code deduplication works.
963 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700964 int target_method_idx = target_method.dex_method_index;
965 const DexFile* target_dex_file = target_method.dex_file;
966 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
967 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800968
Jeff Hao49161ce2014-03-12 11:05:25 -0700969 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700970 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
971 TargetReg(symbolic_reg, kNotWide).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700972 static_cast<int>(target_method_id_ptr), target_method_idx,
973 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800974 AppendLIR(move);
975 method_address_insns_.Insert(move);
976}
977
Fred Shihe7f82e22014-08-06 10:46:37 -0700978void X86Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
979 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800980 /*
981 * For x86, just generate a 32 bit move immediate instruction, that will be filled
982 * in at 'link time'. For now, put a unique value based on target to ensure that
983 * code deduplication works.
984 */
Fred Shihe7f82e22014-08-06 10:46:37 -0700985 const DexFile::TypeId& id = dex_file.GetTypeId(type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800986 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
987
988 // Generate the move instruction with the unique pointer and save index and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700989 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
990 TargetReg(symbolic_reg, kNotWide).GetReg(),
Fred Shihe7f82e22014-08-06 10:46:37 -0700991 static_cast<int>(ptr), type_idx,
992 WrapPointer(const_cast<DexFile*>(&dex_file)));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800993 AppendLIR(move);
994 class_type_address_insns_.Insert(move);
995}
996
Jeff Hao49161ce2014-03-12 11:05:25 -0700997LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800998 /*
999 * For x86, just generate a 32 bit call relative instruction, that will be filled
1000 * in at 'link time'. For now, put a unique value based on target to ensure that
1001 * code deduplication works.
1002 */
Jeff Hao49161ce2014-03-12 11:05:25 -07001003 int target_method_idx = target_method.dex_method_index;
1004 const DexFile* target_dex_file = target_method.dex_file;
1005 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
1006 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001007
Jeff Hao49161ce2014-03-12 11:05:25 -07001008 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
1009 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
1010 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001011 AppendLIR(call);
1012 call_method_insns_.Insert(call);
1013 return call;
1014}
1015
1016void X86Mir2Lir::InstallLiteralPools() {
1017 // These are handled differently for x86.
1018 DCHECK(code_literal_list_ == nullptr);
1019 DCHECK(method_literal_list_ == nullptr);
1020 DCHECK(class_literal_list_ == nullptr);
1021
Mark Mendelld65c51a2014-04-29 16:55:20 -04001022 // Align to 16 byte boundary. We have implicit knowledge that the start of the method is
1023 // on a 4 byte boundary. How can I check this if it changes (other than aligned loads
1024 // will fail at runtime)?
1025 if (const_vectors_ != nullptr) {
1026 int align_size = (16-4) - (code_buffer_.size() & 0xF);
1027 if (align_size < 0) {
1028 align_size += 16;
1029 }
1030
1031 while (align_size > 0) {
1032 code_buffer_.push_back(0);
1033 align_size--;
1034 }
1035 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001036 PushWord(&code_buffer_, p->operands[0]);
1037 PushWord(&code_buffer_, p->operands[1]);
1038 PushWord(&code_buffer_, p->operands[2]);
1039 PushWord(&code_buffer_, p->operands[3]);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001040 }
1041 }
1042
Mark Mendell55d0eac2014-02-06 11:02:52 -08001043 // Handle the fixups for methods.
1044 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
1045 LIR* p = method_address_insns_.Get(i);
1046 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001047 uint32_t target_method_idx = p->operands[2];
1048 const DexFile* target_dex_file =
1049 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001050
1051 // The offset to patch is the last 4 bytes of the instruction.
1052 int patch_offset = p->offset + p->flags.size - 4;
1053 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
1054 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -07001055 target_method_idx, target_dex_file,
1056 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001057 patch_offset);
1058 }
1059
1060 // Handle the fixups for class types.
1061 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
1062 LIR* p = class_type_address_insns_.Get(i);
1063 DCHECK_EQ(p->opcode, kX86Mov32RI);
Fred Shihe7f82e22014-08-06 10:46:37 -07001064
1065 const DexFile* class_dex_file =
1066 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Jeff Hao49161ce2014-03-12 11:05:25 -07001067 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001068
1069 // The offset to patch is the last 4 bytes of the instruction.
1070 int patch_offset = p->offset + p->flags.size - 4;
1071 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -07001072 cu_->method_idx, target_method_idx, class_dex_file,
1073 patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001074 }
1075
1076 // And now the PC-relative calls to methods.
1077 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
1078 LIR* p = call_method_insns_.Get(i);
1079 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001080 uint32_t target_method_idx = p->operands[1];
1081 const DexFile* target_dex_file =
1082 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001083
1084 // The offset to patch is the last 4 bytes of the instruction.
1085 int patch_offset = p->offset + p->flags.size - 4;
1086 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001087 cu_->method_idx, cu_->invoke_type,
1088 target_method_idx, target_dex_file,
1089 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001090 patch_offset, -4 /* offset */);
1091 }
1092
1093 // And do the normal processing.
1094 Mir2Lir::InstallLiteralPools();
1095}
1096
DaniilSokolov70c4f062014-06-24 17:34:00 -07001097bool X86Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
DaniilSokolov70c4f062014-06-24 17:34:00 -07001098 RegLocation rl_src = info->args[0];
1099 RegLocation rl_srcPos = info->args[1];
1100 RegLocation rl_dst = info->args[2];
1101 RegLocation rl_dstPos = info->args[3];
1102 RegLocation rl_length = info->args[4];
1103 if (rl_srcPos.is_const && (mir_graph_->ConstantValue(rl_srcPos) < 0)) {
1104 return false;
1105 }
1106 if (rl_dstPos.is_const && (mir_graph_->ConstantValue(rl_dstPos) < 0)) {
1107 return false;
1108 }
1109 ClobberCallerSave();
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001110 LockCallTemps(); // Using fixed registers.
1111 RegStorage tmp_reg = cu_->target64 ? rs_r11 : rs_rBX;
1112 LoadValueDirectFixed(rl_src, rs_rAX);
1113 LoadValueDirectFixed(rl_dst, rs_rCX);
1114 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_rAX, rs_rCX, nullptr);
1115 LIR* src_null_branch = OpCmpImmBranch(kCondEq, rs_rAX, 0, nullptr);
1116 LIR* dst_null_branch = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1117 LoadValueDirectFixed(rl_length, rs_rDX);
1118 // If the length of the copy is > 128 characters (256 bytes) or negative then go slow path.
1119 LIR* len_too_big = OpCmpImmBranch(kCondHi, rs_rDX, 128, nullptr);
1120 LoadValueDirectFixed(rl_src, rs_rAX);
1121 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001122 LIR* src_bad_len = nullptr;
1123 LIR* srcPos_negative = nullptr;
1124 if (!rl_srcPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001125 LoadValueDirectFixed(rl_srcPos, tmp_reg);
1126 srcPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1127 OpRegReg(kOpAdd, tmp_reg, rs_rDX);
1128 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001129 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001130 int32_t pos_val = mir_graph_->ConstantValue(rl_srcPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001131 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001132 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001133 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001134 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1135 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001136 }
1137 }
1138 LIR* dstPos_negative = nullptr;
1139 LIR* dst_bad_len = nullptr;
1140 LoadValueDirectFixed(rl_dst, rs_rAX);
1141 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1142 if (!rl_dstPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001143 LoadValueDirectFixed(rl_dstPos, tmp_reg);
1144 dstPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1145 OpRegRegReg(kOpAdd, tmp_reg, tmp_reg, rs_rDX);
1146 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001147 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001148 int32_t pos_val = mir_graph_->ConstantValue(rl_dstPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001149 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001150 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001151 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001152 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1153 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001154 }
1155 }
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001156 // Everything is checked now.
1157 LoadValueDirectFixed(rl_src, rs_rAX);
1158 LoadValueDirectFixed(rl_dst, tmp_reg);
1159 LoadValueDirectFixed(rl_srcPos, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001160 NewLIR5(kX86Lea32RA, rs_rAX.GetReg(), rs_rAX.GetReg(),
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001161 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value());
1162 // RAX now holds the address of the first src element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001163
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001164 LoadValueDirectFixed(rl_dstPos, rs_rCX);
1165 NewLIR5(kX86Lea32RA, tmp_reg.GetReg(), tmp_reg.GetReg(),
1166 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value() );
1167 // RBX now holds the address of the first dst element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001168
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001169 // Check if the number of elements to be copied is odd or even. If odd
DaniilSokolov70c4f062014-06-24 17:34:00 -07001170 // then copy the first element (so that the remaining number of elements
1171 // is even).
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001172 LoadValueDirectFixed(rl_length, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001173 OpRegImm(kOpAnd, rs_rCX, 1);
1174 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1175 OpRegImm(kOpSub, rs_rDX, 1);
1176 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001177 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001178
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001179 // Since the remaining number of elements is even, we will copy by
DaniilSokolov70c4f062014-06-24 17:34:00 -07001180 // two elements at a time.
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001181 LIR* beginLoop = NewLIR0(kPseudoTargetLabel);
1182 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_rDX, 0, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001183 OpRegImm(kOpSub, rs_rDX, 2);
1184 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001185 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001186 OpUnconditionalBranch(beginLoop);
1187 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1188 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1189 LIR *return_point = NewLIR0(kPseudoTargetLabel);
1190 jmp_to_ret->target = return_point;
1191 jmp_to_begin_loop->target = beginLoop;
1192 src_dst_same->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001193 len_too_big->target = check_failed;
1194 src_null_branch->target = check_failed;
1195 if (srcPos_negative != nullptr)
1196 srcPos_negative ->target = check_failed;
1197 if (src_bad_len != nullptr)
1198 src_bad_len->target = check_failed;
1199 dst_null_branch->target = check_failed;
1200 if (dstPos_negative != nullptr)
1201 dstPos_negative->target = check_failed;
1202 if (dst_bad_len != nullptr)
1203 dst_bad_len->target = check_failed;
1204 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
1205 return true;
1206}
1207
1208
Mark Mendell4028a6c2014-02-19 20:06:20 -08001209/*
1210 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1211 * otherwise bails to standard library code.
1212 */
1213bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001214 RegLocation rl_obj = info->args[0];
1215 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001216 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001217 // RBX is promotable in 64-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001218 RegStorage rs_tmp = cu_->target64 ? rs_r11 : rs_rBX;
1219 int start_value = -1;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001220
1221 uint32_t char_value =
1222 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1223
1224 if (char_value > 0xFFFF) {
1225 // We have to punt to the real String.indexOf.
1226 return false;
1227 }
1228
1229 // Okay, we are commited to inlining this.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001230 // EAX: 16 bit character being searched.
1231 // ECX: count: number of words to be searched.
1232 // EDI: String being searched.
1233 // EDX: temporary during execution.
1234 // EBX or R11: temporary during execution (depending on mode).
1235 // REP SCASW: search instruction.
1236
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001237 FlushAllRegs();
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001238
buzbeea0cd2d72014-06-01 09:33:49 -07001239 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001240 RegLocation rl_dest = InlineTarget(info);
1241
1242 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001243 LoadValueDirectFixed(rl_obj, rs_rDX);
1244 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001245 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001246
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001247 LIR *slowpath_branch = nullptr, *length_compare = nullptr;
1248
1249 // We need the value in EAX.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001250 if (rl_char.is_const) {
buzbee2700f7e2014-03-07 09:46:20 -08001251 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001252 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001253 // Does the character fit in 16 bits? Compare it at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001254 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001255 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001256 }
1257
1258 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001259 // Location of reference to data array within the String object.
1260 int value_offset = mirror::String::ValueOffset().Int32Value();
1261 // Location of count within the String object.
1262 int count_offset = mirror::String::CountOffset().Int32Value();
1263 // Starting offset within data array.
1264 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1265 // Start of char data with array_.
1266 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001267
Dave Allison69dfe512014-07-11 17:11:58 +00001268 // Compute the number of words to search in to rCX.
1269 Load32Disp(rs_rDX, count_offset, rs_rCX);
1270
Dave Allisondfd3b472014-07-16 16:04:32 -07001271 // Possible signal here due to null pointer dereference.
1272 // Note that the signal handler will expect the top word of
1273 // the stack to be the ArtMethod*. If the PUSH edi instruction
1274 // below is ahead of the load above then this will not be true
1275 // and the signal handler will not work.
1276 MarkPossibleNullPointerException(0);
Dave Allison69dfe512014-07-11 17:11:58 +00001277
Dave Allisondfd3b472014-07-16 16:04:32 -07001278 if (!cu_->target64) {
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001279 // EDI is promotable in 32-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001280 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1281 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001282
Mark Mendell4028a6c2014-02-19 20:06:20 -08001283 if (zero_based) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001284 // Start index is not present.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001285 // We have to handle an empty string. Use special instruction JECXZ.
1286 length_compare = NewLIR0(kX86Jecxz8);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001287
1288 // Copy the number of words to search in a temporary register.
1289 // We will use the register at the end to calculate result.
1290 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001291 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001292 // Start index is present.
buzbeea44d4f52014-03-05 11:26:39 -08001293 rl_start = info->args[2];
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001294
Mark Mendell4028a6c2014-02-19 20:06:20 -08001295 // We have to offset by the start index.
1296 if (rl_start.is_const) {
1297 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1298 start_value = std::max(start_value, 0);
1299
1300 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001301 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001302 OpRegImm(kOpMov, rs_rDI, start_value);
1303
1304 // Copy the number of words to search in a temporary register.
1305 // We will use the register at the end to calculate result.
1306 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001307
1308 if (start_value != 0) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001309 // Decrease the number of words to search by the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001310 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001311 }
1312 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001313 // Handle "start index < 0" case.
1314 if (!cu_->target64 && rl_start.location != kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001315 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001316 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko74de63b2014-08-19 15:00:34 +01001317 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1318 Load32Disp(rs_rX86_SP, displacement, rs_rDI);
1319 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1320 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1321 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - 1;
1322 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001323 } else {
1324 LoadValueDirectFixed(rl_start, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001325 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001326 OpRegReg(kOpXor, rs_tmp, rs_tmp);
1327 OpRegReg(kOpCmp, rs_rDI, rs_tmp);
1328 OpCondRegReg(kOpCmov, kCondLt, rs_rDI, rs_tmp);
1329
1330 // The length of the string should be greater than the start index.
1331 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rDI, nullptr);
1332
1333 // Copy the number of words to search in a temporary register.
1334 // We will use the register at the end to calculate result.
1335 OpRegReg(kOpMov, rs_tmp, rs_rCX);
1336
1337 // Decrease the number of words to search by the start index.
1338 OpRegReg(kOpSub, rs_rCX, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001339 }
1340 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001341
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001342 // Load the address of the string into EDI.
1343 // In case of start index we have to add the address to existing value in EDI.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001344 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001345 if (zero_based || (!zero_based && rl_start.is_const && start_value == 0)) {
1346 Load32Disp(rs_rDX, offset_offset, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001347 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001348 OpRegMem(kOpAdd, rs_rDI, rs_rDX, offset_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001349 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001350 OpRegImm(kOpLsl, rs_rDI, 1);
1351 OpRegMem(kOpAdd, rs_rDI, rs_rDX, value_offset);
1352 OpRegImm(kOpAdd, rs_rDI, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001353
1354 // EDI now contains the start of the string to be searched.
1355 // We are all prepared to do the search for the character.
1356 NewLIR0(kX86RepneScasw);
1357
1358 // Did we find a match?
1359 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1360
1361 // yes, we matched. Compute the index of the result.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001362 OpRegReg(kOpSub, rs_tmp, rs_rCX);
1363 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_tmp.GetReg(), -1);
1364
Mark Mendell4028a6c2014-02-19 20:06:20 -08001365 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1366
1367 // Failed to match; return -1.
1368 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1369 length_compare->target = not_found;
1370 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001371 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001372
1373 // And join up at the end.
1374 all_done->target = NewLIR0(kPseudoTargetLabel);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001375
1376 if (!cu_->target64)
1377 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001378
1379 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001380 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001381 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001382 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001383 }
1384
1385 StoreValue(rl_dest, rl_return);
1386 return true;
1387}
1388
Tong Shen35e1e6a2014-07-30 09:31:22 -07001389static bool ARTRegIDToDWARFRegID(bool is_x86_64, int art_reg_id, int* dwarf_reg_id) {
1390 if (is_x86_64) {
1391 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001392 case 3 : *dwarf_reg_id = 3; return true; // %rbx
Tong Shen35e1e6a2014-07-30 09:31:22 -07001393 // This is the only discrepancy between ART & DWARF register numbering.
Andreas Gampebda27222014-07-30 23:21:36 -07001394 case 5 : *dwarf_reg_id = 6; return true; // %rbp
1395 case 12: *dwarf_reg_id = 12; return true; // %r12
1396 case 13: *dwarf_reg_id = 13; return true; // %r13
1397 case 14: *dwarf_reg_id = 14; return true; // %r14
1398 case 15: *dwarf_reg_id = 15; return true; // %r15
1399 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001400 }
1401 } else {
1402 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001403 case 5: *dwarf_reg_id = 5; return true; // %ebp
1404 case 6: *dwarf_reg_id = 6; return true; // %esi
1405 case 7: *dwarf_reg_id = 7; return true; // %edi
1406 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001407 }
1408 }
1409}
1410
Tong Shen547cdfd2014-08-05 01:54:19 -07001411std::vector<uint8_t>* X86Mir2Lir::ReturnFrameDescriptionEntry() {
1412 std::vector<uint8_t>* cfi_info = new std::vector<uint8_t>;
Mark Mendellae9fd932014-02-10 16:14:35 -08001413
1414 // Generate the FDE for the method.
1415 DCHECK_NE(data_offset_, 0U);
1416
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001417 WriteFDEHeader(cfi_info, cu_->target64);
1418 WriteFDEAddressRange(cfi_info, data_offset_, cu_->target64);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001419
Mark Mendellae9fd932014-02-10 16:14:35 -08001420 // The instructions in the FDE.
1421 if (stack_decrement_ != nullptr) {
1422 // Advance LOC to just past the stack decrement.
1423 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001424 DW_CFA_advance_loc(cfi_info, pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001425
1426 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
Tong Shen547cdfd2014-08-05 01:54:19 -07001427 DW_CFA_def_cfa_offset(cfi_info, frame_size_);
Mark Mendellae9fd932014-02-10 16:14:35 -08001428
Tong Shen35e1e6a2014-07-30 09:31:22 -07001429 // Handle register spills
1430 const uint32_t kSpillInstLen = (cu_->target64) ? 5 : 4;
1431 const int kDataAlignmentFactor = (cu_->target64) ? -8 : -4;
1432 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
1433 int offset = -(GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
1434 for (int reg = 0; mask; mask >>= 1, reg++) {
1435 if (mask & 0x1) {
1436 pc += kSpillInstLen;
1437
1438 // Advance LOC to pass this instruction
Tong Shen547cdfd2014-08-05 01:54:19 -07001439 DW_CFA_advance_loc(cfi_info, kSpillInstLen);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001440
1441 int dwarf_reg_id;
1442 if (ARTRegIDToDWARFRegID(cu_->target64, reg, &dwarf_reg_id)) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001443 // DW_CFA_offset_extended_sf reg offset
1444 DW_CFA_offset_extended_sf(cfi_info, dwarf_reg_id, offset / kDataAlignmentFactor);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001445 }
1446
1447 offset += GetInstructionSetPointerSize(cu_->instruction_set);
1448 }
1449 }
1450
Mark Mendellae9fd932014-02-10 16:14:35 -08001451 // We continue with that stack until the epilogue.
1452 if (stack_increment_ != nullptr) {
1453 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001454 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001455
1456 // We probably have code snippets after the epilogue, so save the
1457 // current state: DW_CFA_remember_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001458 DW_CFA_remember_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001459
Tong Shen35e1e6a2014-07-30 09:31:22 -07001460 // We have now popped the stack: DW_CFA_def_cfa_offset 4/8.
1461 // There is only the return PC on the stack now.
Tong Shen547cdfd2014-08-05 01:54:19 -07001462 DW_CFA_def_cfa_offset(cfi_info, GetInstructionSetPointerSize(cu_->instruction_set));
Mark Mendellae9fd932014-02-10 16:14:35 -08001463
1464 // Everything after that is the same as before the epilogue.
1465 // Stack bump was followed by RET instruction.
1466 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1467 if (post_ret_insn != nullptr) {
1468 pc = new_pc;
1469 new_pc = post_ret_insn->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001470 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001471 // Restore the state: DW_CFA_restore_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001472 DW_CFA_restore_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001473 }
1474 }
1475 }
1476
Tong Shen547cdfd2014-08-05 01:54:19 -07001477 PadCFI(cfi_info);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001478 WriteCFILength(cfi_info, cu_->target64);
Mark Mendellae9fd932014-02-10 16:14:35 -08001479
Mark Mendellae9fd932014-02-10 16:14:35 -08001480 return cfi_info;
1481}
1482
Mark Mendelld65c51a2014-04-29 16:55:20 -04001483void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1484 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001485 case kMirOpReserveVectorRegisters:
1486 ReserveVectorRegisters(mir);
1487 break;
1488 case kMirOpReturnVectorRegisters:
1489 ReturnVectorRegisters();
1490 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001491 case kMirOpConstVector:
1492 GenConst128(bb, mir);
1493 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001494 case kMirOpMoveVector:
1495 GenMoveVector(bb, mir);
1496 break;
1497 case kMirOpPackedMultiply:
1498 GenMultiplyVector(bb, mir);
1499 break;
1500 case kMirOpPackedAddition:
1501 GenAddVector(bb, mir);
1502 break;
1503 case kMirOpPackedSubtract:
1504 GenSubtractVector(bb, mir);
1505 break;
1506 case kMirOpPackedShiftLeft:
1507 GenShiftLeftVector(bb, mir);
1508 break;
1509 case kMirOpPackedSignedShiftRight:
1510 GenSignedShiftRightVector(bb, mir);
1511 break;
1512 case kMirOpPackedUnsignedShiftRight:
1513 GenUnsignedShiftRightVector(bb, mir);
1514 break;
1515 case kMirOpPackedAnd:
1516 GenAndVector(bb, mir);
1517 break;
1518 case kMirOpPackedOr:
1519 GenOrVector(bb, mir);
1520 break;
1521 case kMirOpPackedXor:
1522 GenXorVector(bb, mir);
1523 break;
1524 case kMirOpPackedAddReduce:
1525 GenAddReduceVector(bb, mir);
1526 break;
1527 case kMirOpPackedReduce:
1528 GenReduceVector(bb, mir);
1529 break;
1530 case kMirOpPackedSet:
1531 GenSetVector(bb, mir);
1532 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001533 default:
1534 break;
1535 }
1536}
1537
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001538void X86Mir2Lir::ReserveVectorRegisters(MIR* mir) {
1539 // We should not try to reserve twice without returning the registers
1540 DCHECK_NE(num_reserved_vector_regs_, -1);
1541
1542 int num_vector_reg = mir->dalvikInsn.vA;
1543 for (int i = 0; i < num_vector_reg; i++) {
1544 RegStorage xp_reg = RegStorage::Solo128(i);
1545 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1546 Clobber(xp_reg);
1547
1548 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1549 info != nullptr;
1550 info = info->GetAliasChain()) {
1551 if (info->GetReg().IsSingle()) {
1552 reg_pool_->sp_regs_.Delete(info);
1553 } else {
1554 reg_pool_->dp_regs_.Delete(info);
1555 }
1556 }
1557 }
1558
1559 num_reserved_vector_regs_ = num_vector_reg;
1560}
1561
1562void X86Mir2Lir::ReturnVectorRegisters() {
1563 // Return all the reserved registers
1564 for (int i = 0; i < num_reserved_vector_regs_; i++) {
1565 RegStorage xp_reg = RegStorage::Solo128(i);
1566 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1567
1568 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1569 info != nullptr;
1570 info = info->GetAliasChain()) {
1571 if (info->GetReg().IsSingle()) {
1572 reg_pool_->sp_regs_.Insert(info);
1573 } else {
1574 reg_pool_->dp_regs_.Insert(info);
1575 }
1576 }
1577 }
1578
1579 // We don't have anymore reserved vector registers
1580 num_reserved_vector_regs_ = -1;
1581}
1582
Mark Mendelld65c51a2014-04-29 16:55:20 -04001583void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001584 store_method_addr_used_ = true;
1585 int type_size = mir->dalvikInsn.vB;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001586 // We support 128 bit vectors.
1587 DCHECK_EQ(type_size & 0xFFFF, 128);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001588 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
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.
1598 AppendOpcodeWithConst(kX86MovupsRM, reg, mir);
1599}
1600
1601void X86Mir2Lir::AppendOpcodeWithConst(X86OpCode opcode, int reg, MIR* mir) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04001602 // Okay, load it from the constant vector area.
1603 LIR *data_target = ScanVectorLiteral(mir);
1604 if (data_target == nullptr) {
1605 data_target = AddVectorLiteral(mir);
1606 }
1607
1608 // Address the start of the method.
1609 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001610 if (rl_method.wide) {
1611 rl_method = LoadValueWide(rl_method, kCoreReg);
1612 } else {
1613 rl_method = LoadValue(rl_method, kCoreReg);
1614 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001615
1616 // Load the proper value from the literal area.
1617 // We don't know the proper offset for the value, so pick one that will force
1618 // 4 byte offset. We will fix this up in the assembler later to have the right
1619 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001620 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001621 LIR *load = NewLIR2(opcode, reg, rl_method.reg.GetReg());
Mark Mendelld65c51a2014-04-29 16:55:20 -04001622 load->flags.fixup = kFixupLoad;
1623 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001624}
1625
Mark Mendellfe945782014-05-22 09:52:36 -04001626void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1627 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001628 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1629 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
1630 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001631 NewLIR2(kX86Mova128RR, rs_dest.GetReg(), rs_src.GetReg());
1632}
1633
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001634void X86Mir2Lir::GenMultiplyVectorSignedByte(BasicBlock *bb, MIR *mir) {
1635 const int BYTE_SIZE = 8;
1636 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1637 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
1638 RegStorage rs_src1_high_tmp = Get128BitRegister(AllocTempWide());
1639
1640 /*
1641 * Emulate the behavior of a kSignedByte by separating out the 16 values in the two XMM
1642 * and multiplying 8 at a time before recombining back into one XMM register.
1643 *
1644 * let xmm1, xmm2 be real srcs (keep low bits of 16bit lanes)
1645 * xmm3 is tmp (operate on high bits of 16bit lanes)
1646 *
1647 * xmm3 = xmm1
1648 * xmm1 = xmm1 .* xmm2
1649 * xmm1 = xmm1 & 0x00ff00ff00ff00ff00ff00ff00ff00ff // xmm1 now has low bits
1650 * xmm3 = xmm3 .>> 8
1651 * xmm2 = xmm2 & 0xff00ff00ff00ff00ff00ff00ff00ff00
1652 * xmm2 = xmm2 .* xmm3 // xmm2 now has high bits
1653 * xmm1 = xmm1 | xmm2 // combine results
1654 */
1655
1656 // Copy xmm1.
1657 NewLIR2(kX86Mova128RR, rs_src1_high_tmp.GetReg(), rs_dest_src1.GetReg());
1658
1659 // Multiply low bits.
1660 NewLIR2(kX86PmullwRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1661
1662 // xmm1 now has low bits.
1663 AndMaskVectorRegister(rs_dest_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
1664
1665 // Prepare high bits for multiplication.
1666 NewLIR2(kX86PsrlwRI, rs_src1_high_tmp.GetReg(), BYTE_SIZE);
1667 AndMaskVectorRegister(rs_src2, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1668
1669 // Multiply high bits and xmm2 now has high bits.
1670 NewLIR2(kX86PmullwRR, rs_src2.GetReg(), rs_src1_high_tmp.GetReg());
1671
1672 // Combine back into dest XMM register.
1673 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1674}
1675
Mark Mendellfe945782014-05-22 09:52:36 -04001676void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001677 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1678 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1679 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1680 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001681 int opcode = 0;
1682 switch (opsize) {
1683 case k32:
1684 opcode = kX86PmulldRR;
1685 break;
1686 case kSignedHalf:
1687 opcode = kX86PmullwRR;
1688 break;
1689 case kSingle:
1690 opcode = kX86MulpsRR;
1691 break;
1692 case kDouble:
1693 opcode = kX86MulpdRR;
1694 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001695 case kSignedByte:
1696 // HW doesn't support 16x16 byte multiplication so emulate it.
1697 GenMultiplyVectorSignedByte(bb, mir);
1698 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001699 default:
1700 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1701 break;
1702 }
1703 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1704}
1705
1706void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001707 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1708 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1709 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1710 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001711 int opcode = 0;
1712 switch (opsize) {
1713 case k32:
1714 opcode = kX86PadddRR;
1715 break;
1716 case kSignedHalf:
1717 case kUnsignedHalf:
1718 opcode = kX86PaddwRR;
1719 break;
1720 case kUnsignedByte:
1721 case kSignedByte:
1722 opcode = kX86PaddbRR;
1723 break;
1724 case kSingle:
1725 opcode = kX86AddpsRR;
1726 break;
1727 case kDouble:
1728 opcode = kX86AddpdRR;
1729 break;
1730 default:
1731 LOG(FATAL) << "Unsupported vector addition " << opsize;
1732 break;
1733 }
1734 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1735}
1736
1737void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001738 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1739 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1740 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1741 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001742 int opcode = 0;
1743 switch (opsize) {
1744 case k32:
1745 opcode = kX86PsubdRR;
1746 break;
1747 case kSignedHalf:
1748 case kUnsignedHalf:
1749 opcode = kX86PsubwRR;
1750 break;
1751 case kUnsignedByte:
1752 case kSignedByte:
1753 opcode = kX86PsubbRR;
1754 break;
1755 case kSingle:
1756 opcode = kX86SubpsRR;
1757 break;
1758 case kDouble:
1759 opcode = kX86SubpdRR;
1760 break;
1761 default:
1762 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1763 break;
1764 }
1765 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1766}
1767
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001768void X86Mir2Lir::GenShiftByteVector(BasicBlock *bb, MIR *mir) {
1769 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1770 RegStorage rs_tmp = Get128BitRegister(AllocTempWide());
1771
1772 int opcode = 0;
1773 int imm = mir->dalvikInsn.vB;
1774
1775 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1776 case kMirOpPackedShiftLeft:
1777 opcode = kX86PsllwRI;
1778 break;
1779 case kMirOpPackedSignedShiftRight:
1780 opcode = kX86PsrawRI;
1781 break;
1782 case kMirOpPackedUnsignedShiftRight:
1783 opcode = kX86PsrlwRI;
1784 break;
1785 default:
1786 LOG(FATAL) << "Unsupported shift operation on byte vector " << opcode;
1787 break;
1788 }
1789
1790 /*
1791 * xmm1 will have low bits
1792 * xmm2 will have high bits
1793 *
1794 * xmm2 = xmm1
1795 * xmm1 = xmm1 .<< N
1796 * xmm2 = xmm2 && 0xFF00FF00FF00FF00FF00FF00FF00FF00
1797 * xmm2 = xmm2 .<< N
1798 * xmm1 = xmm1 | xmm2
1799 */
1800
1801 // Copy xmm1.
1802 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_dest_src1.GetReg());
1803
1804 // Shift lower values.
1805 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1806
1807 // Mask bottom bits.
1808 AndMaskVectorRegister(rs_tmp, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1809
1810 // Shift higher values.
1811 NewLIR2(opcode, rs_tmp.GetReg(), imm);
1812
1813 // Combine back into dest XMM register.
1814 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_tmp.GetReg());
1815}
1816
Mark Mendellfe945782014-05-22 09:52:36 -04001817void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001818 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1819 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1820 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1821 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001822 int opcode = 0;
1823 switch (opsize) {
1824 case k32:
1825 opcode = kX86PslldRI;
1826 break;
1827 case k64:
1828 opcode = kX86PsllqRI;
1829 break;
1830 case kSignedHalf:
1831 case kUnsignedHalf:
1832 opcode = kX86PsllwRI;
1833 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001834 case kSignedByte:
1835 case kUnsignedByte:
1836 GenShiftByteVector(bb, mir);
1837 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001838 default:
1839 LOG(FATAL) << "Unsupported vector shift left " << opsize;
1840 break;
1841 }
1842 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1843}
1844
1845void X86Mir2Lir::GenSignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001846 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1847 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1848 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1849 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001850 int opcode = 0;
1851 switch (opsize) {
1852 case k32:
1853 opcode = kX86PsradRI;
1854 break;
1855 case kSignedHalf:
1856 case kUnsignedHalf:
1857 opcode = kX86PsrawRI;
1858 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001859 case kSignedByte:
1860 case kUnsignedByte:
1861 GenShiftByteVector(bb, mir);
1862 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001863 default:
1864 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
1865 break;
1866 }
1867 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1868}
1869
1870void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001871 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1872 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1873 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1874 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001875 int opcode = 0;
1876 switch (opsize) {
1877 case k32:
1878 opcode = kX86PsrldRI;
1879 break;
1880 case k64:
1881 opcode = kX86PsrlqRI;
1882 break;
1883 case kSignedHalf:
1884 case kUnsignedHalf:
1885 opcode = kX86PsrlwRI;
1886 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001887 case kSignedByte:
1888 case kUnsignedByte:
1889 GenShiftByteVector(bb, mir);
1890 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001891 default:
1892 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1893 break;
1894 }
1895 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1896}
1897
1898void X86Mir2Lir::GenAndVector(BasicBlock *bb, MIR *mir) {
1899 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001900 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1901 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1902 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001903 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1904}
1905
1906void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
1907 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001908 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1909 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1910 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001911 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1912}
1913
1914void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
1915 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001916 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1917 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1918 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001919 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1920}
1921
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001922void X86Mir2Lir::AndMaskVectorRegister(RegStorage rs_src1, uint32_t m1, uint32_t m2, uint32_t m3, uint32_t m4) {
1923 MaskVectorRegister(kX86PandRM, rs_src1, m1, m2, m3, m4);
1924}
1925
1926void X86Mir2Lir::MaskVectorRegister(X86OpCode opcode, RegStorage rs_src1, uint32_t m0, uint32_t m1, uint32_t m2, uint32_t m3) {
1927 // Create temporary MIR as container for 128-bit binary mask.
1928 MIR const_mir;
1929 MIR* const_mirp = &const_mir;
1930 const_mirp->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpConstVector);
1931 const_mirp->dalvikInsn.arg[0] = m0;
1932 const_mirp->dalvikInsn.arg[1] = m1;
1933 const_mirp->dalvikInsn.arg[2] = m2;
1934 const_mirp->dalvikInsn.arg[3] = m3;
1935
1936 // Mask vector with const from literal pool.
1937 AppendOpcodeWithConst(opcode, rs_src1.GetReg(), const_mirp);
1938}
1939
Mark Mendellfe945782014-05-22 09:52:36 -04001940void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001941 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1942 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1943 RegLocation rl_dest = mir_graph_->GetDest(mir);
1944 RegStorage rs_tmp;
1945
1946 int vec_bytes = (mir->dalvikInsn.vC & 0xFFFF) / 8;
1947 int vec_unit_size = 0;
Mark Mendellfe945782014-05-22 09:52:36 -04001948 int opcode = 0;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001949 int extr_opcode = 0;
1950 RegLocation rl_result;
1951
Mark Mendellfe945782014-05-22 09:52:36 -04001952 switch (opsize) {
1953 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001954 extr_opcode = kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001955 opcode = kX86PhadddRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001956 vec_unit_size = 4;
1957 break;
1958 case kSignedByte:
1959 case kUnsignedByte:
1960 extr_opcode = kX86PextrbRRI;
1961 opcode = kX86PhaddwRR;
1962 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001963 break;
1964 case kSignedHalf:
1965 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001966 extr_opcode = kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001967 opcode = kX86PhaddwRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001968 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001969 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001970 case kSingle:
1971 rl_result = EvalLoc(rl_dest, kFPReg, true);
1972 vec_unit_size = 4;
1973 for (int i = 0; i < 3; i++) {
1974 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
1975 NewLIR3(kX86ShufpsRRI, rs_src1.GetReg(), rs_src1.GetReg(), 0x39);
1976 }
1977 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
1978 StoreValue(rl_dest, rl_result);
1979
1980 // For single-precision floats, we are done here
1981 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001982 default:
1983 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
1984 break;
1985 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001986
1987 int elems = vec_bytes / vec_unit_size;
1988
1989 // Emulate horizontal add instruction by reducing 2 vectors with 8 values before adding them again
1990 // TODO is overflow handled correctly?
1991 if (opsize == kSignedByte || opsize == kUnsignedByte) {
1992 rs_tmp = Get128BitRegister(AllocTempWide());
1993
1994 // tmp = xmm1 .>> 8.
1995 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_src1.GetReg());
1996 NewLIR2(kX86PsrlwRI, rs_tmp.GetReg(), 8);
1997
1998 // Zero extend low bits in xmm1.
1999 AndMaskVectorRegister(rs_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
2000 }
2001
2002 while (elems > 1) {
2003 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2004 NewLIR2(opcode, rs_tmp.GetReg(), rs_tmp.GetReg());
2005 }
2006 NewLIR2(opcode, rs_src1.GetReg(), rs_src1.GetReg());
2007 elems >>= 1;
2008 }
2009
2010 // Combine the results if we separated them.
2011 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2012 NewLIR2(kX86PaddbRR, rs_src1.GetReg(), rs_tmp.GetReg());
2013 }
2014
2015 // We need to extract to a GPR.
2016 RegStorage temp = AllocTemp();
2017 NewLIR3(extr_opcode, temp.GetReg(), rs_src1.GetReg(), 0);
2018
2019 // Can we do this directly into memory?
2020 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2021 if (rl_result.location == kLocPhysReg) {
2022 // Ensure res is in a core reg
2023 rl_result = EvalLoc(rl_dest, kCoreReg, true);
2024 OpRegReg(kOpAdd, rl_result.reg, temp);
2025 StoreFinalValue(rl_dest, rl_result);
2026 } else {
2027 OpMemReg(kOpAdd, rl_result, temp.GetReg());
2028 }
2029
2030 FreeTemp(temp);
Mark Mendellfe945782014-05-22 09:52:36 -04002031}
2032
2033void X86Mir2Lir::GenReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002034 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2035 RegLocation rl_dest = mir_graph_->GetDest(mir);
2036 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
2037 int extract_index = mir->dalvikInsn.arg[0];
2038 int extr_opcode = 0;
2039 RegLocation rl_result;
2040 bool is_wide = false;
2041
Mark Mendellfe945782014-05-22 09:52:36 -04002042 switch (opsize) {
2043 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002044 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2045 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrdMRI : kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002046 break;
2047 case kSignedHalf:
2048 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002049 rl_result= UpdateLocTyped(rl_dest, kCoreReg);
2050 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrwMRI : kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002051 break;
2052 default:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002053 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2054 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002055 break;
2056 }
Mark Mendellfe945782014-05-22 09:52:36 -04002057
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002058 if (rl_result.location == kLocPhysReg) {
2059 NewLIR3(extr_opcode, rl_result.reg.GetReg(), rs_src1.GetReg(), extract_index);
2060 if (is_wide == true) {
2061 StoreFinalValue(rl_dest, rl_result);
2062 } else {
2063 StoreFinalValueWide(rl_dest, rl_result);
2064 }
2065 } else {
2066 int displacement = SRegOffset(rl_result.s_reg_low);
2067 LIR *l = NewLIR3(extr_opcode, rs_rX86_SP.GetReg(), displacement, rs_src1.GetReg());
2068 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is_wide /* is_64bit */);
2069 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is_wide /* is_64bit */);
2070 }
Mark Mendellfe945782014-05-22 09:52:36 -04002071}
2072
2073void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002074 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2075 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2076 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
2077 int op_low = 0, op_high = 0, imm = 0, op_mov = kX86MovdxrRR;
2078 RegisterClass reg_type = kCoreReg;
2079
Mark Mendellfe945782014-05-22 09:52:36 -04002080 switch (opsize) {
2081 case k32:
2082 op_low = kX86PshufdRRI;
2083 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002084 case kSingle:
2085 op_low = kX86PshufdRRI;
2086 op_mov = kX86Mova128RR;
2087 reg_type = kFPReg;
2088 break;
2089 case k64:
2090 op_low = kX86PshufdRRI;
2091 imm = 0x44;
2092 break;
2093 case kDouble:
2094 op_low = kX86PshufdRRI;
2095 op_mov = kX86Mova128RR;
2096 reg_type = kFPReg;
2097 imm = 0x44;
2098 break;
2099 case kSignedByte:
2100 case kUnsignedByte:
2101 // Shuffle 8 bit value into 16 bit word.
2102 // We set val = val + (val << 8) below and use 16 bit shuffle.
Mark Mendellfe945782014-05-22 09:52:36 -04002103 case kSignedHalf:
2104 case kUnsignedHalf:
2105 // Handles low quadword.
2106 op_low = kX86PshuflwRRI;
2107 // Handles upper quadword.
2108 op_high = kX86PshufdRRI;
2109 break;
2110 default:
2111 LOG(FATAL) << "Unsupported vector set " << opsize;
2112 break;
2113 }
2114
Mark Mendellfe945782014-05-22 09:52:36 -04002115 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002116
2117 // Load the value from the VR into the reg.
2118 if (rl_src.wide == 0) {
2119 rl_src = LoadValue(rl_src, reg_type);
2120 } else {
2121 rl_src = LoadValueWide(rl_src, reg_type);
2122 }
2123
2124 // If opsize is 8 bits wide then double value and use 16 bit shuffle instead.
2125 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2126 RegStorage temp = AllocTemp();
2127 // val = val + (val << 8).
2128 NewLIR2(kX86Mov32RR, temp.GetReg(), rl_src.reg.GetReg());
2129 NewLIR2(kX86Sal32RI, temp.GetReg(), 8);
2130 NewLIR2(kX86Or32RR, rl_src.reg.GetReg(), temp.GetReg());
2131 FreeTemp(temp);
2132 }
Mark Mendellfe945782014-05-22 09:52:36 -04002133
2134 // Load the value into the XMM register.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002135 NewLIR2(op_mov, rs_dest.GetReg(), rl_src.reg.GetReg());
Mark Mendellfe945782014-05-22 09:52:36 -04002136
2137 // Now shuffle the value across the destination.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002138 NewLIR3(op_low, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002139
2140 // And then repeat as needed.
2141 if (op_high != 0) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002142 NewLIR3(op_high, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002143 }
2144}
2145
Mark Mendelld65c51a2014-04-29 16:55:20 -04002146LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
2147 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2148 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
2149 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
2150 args[2] == p->operands[2] && args[3] == p->operands[3]) {
2151 return p;
2152 }
2153 }
2154 return nullptr;
2155}
2156
2157LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
2158 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
2159 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2160 new_value->operands[0] = args[0];
2161 new_value->operands[1] = args[1];
2162 new_value->operands[2] = args[2];
2163 new_value->operands[3] = args[3];
2164 new_value->next = const_vectors_;
2165 if (const_vectors_ == nullptr) {
2166 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
2167 }
2168 estimated_native_code_size_ += 16; // Space for one vector.
2169 const_vectors_ = new_value;
2170 return new_value;
2171}
2172
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002173// ------------ ABI support: mapping of args to physical registers -------------
Andreas Gampeccc60262014-07-04 18:02:38 -07002174RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide,
2175 bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002176 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
Andreas Gampeccc60262014-07-04 18:02:38 -07002177 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) /
2178 sizeof(SpecialTargetRegister);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002179 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
Andreas Gampeccc60262014-07-04 18:02:38 -07002180 kFArg4, kFArg5, kFArg6, kFArg7};
2181 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) /
2182 sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002183
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002184 if (is_double_or_float) {
2185 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002186 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide ? kWide : kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002187 }
2188 } else {
2189 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002190 return ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++],
2191 is_ref ? kRef : (is_wide ? kWide : kNotWide));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002192 }
2193 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07002194 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002195}
2196
2197RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
2198 DCHECK(IsInitialized());
2199 auto res = mapping_.find(in_position);
2200 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
2201}
2202
Andreas Gampeccc60262014-07-04 18:02:38 -07002203void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count,
2204 InToRegStorageMapper* mapper) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002205 DCHECK(mapper != nullptr);
2206 max_mapped_in_ = -1;
2207 is_there_stack_mapped_ = false;
2208 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07002209 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
2210 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002211 if (reg.Valid()) {
2212 mapping_[in_position] = reg;
2213 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07002214 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002215 // We covered 2 args, so skip the next one
2216 in_position++;
2217 }
2218 } else {
2219 is_there_stack_mapped_ = true;
2220 }
2221 }
2222 initialized_ = true;
2223}
2224
2225RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002226 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002227 return GetCoreArgMappingToPhysicalReg(arg_num);
2228 }
2229
2230 if (!in_to_reg_storage_mapping_.IsInitialized()) {
2231 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2232 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
2233
Chao-ying Fua77ee512014-07-01 17:43:41 -07002234 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002235 in_to_reg_storage_mapping_.Initialize(arg_locs, cu_->num_ins, &mapper);
2236 }
2237 return in_to_reg_storage_mapping_.Get(arg_num);
2238}
2239
2240RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
2241 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
2242 // Not used for 64-bit, TODO: Move X86_32 to the same framework
2243 switch (core_arg_num) {
2244 case 0:
2245 return rs_rX86_ARG1;
2246 case 1:
2247 return rs_rX86_ARG2;
2248 case 2:
2249 return rs_rX86_ARG3;
2250 default:
2251 return RegStorage::InvalidReg();
2252 }
2253}
2254
2255// ---------End of ABI support: mapping of args to physical registers -------------
2256
2257/*
2258 * If there are any ins passed in registers that have not been promoted
2259 * to a callee-save register, flush them to the frame. Perform initial
2260 * assignment of promoted arguments.
2261 *
2262 * ArgLocs is an array of location records describing the incoming arguments
2263 * with one location record per word of argument.
2264 */
2265void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002266 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002267 /*
2268 * Dummy up a RegLocation for the incoming Method*
2269 * It will attempt to keep kArg0 live (or copy it to home location
2270 * if promoted).
2271 */
2272
2273 RegLocation rl_src = rl_method;
2274 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -07002275 rl_src.reg = TargetReg(kArg0, kRef);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002276 rl_src.home = false;
2277 MarkLive(rl_src);
2278 StoreValue(rl_method, rl_src);
2279 // If Method* has been promoted, explicitly flush
2280 if (rl_method.location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002281 StoreRefDisp(rs_rX86_SP, 0, As32BitReg(TargetReg(kArg0, kRef)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002282 }
2283
2284 if (cu_->num_ins == 0) {
2285 return;
2286 }
2287
2288 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2289 /*
2290 * Copy incoming arguments to their proper home locations.
2291 * NOTE: an older version of dx had an issue in which
2292 * it would reuse static method argument registers.
2293 * This could result in the same Dalvik virtual register
2294 * being promoted to both core and fp regs. To account for this,
2295 * we only copy to the corresponding promoted physical register
2296 * if it matches the type of the SSA name for the incoming
2297 * argument. It is also possible that long and double arguments
2298 * end up half-promoted. In those cases, we must flush the promoted
2299 * half to memory as well.
2300 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002301 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002302 for (int i = 0; i < cu_->num_ins; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002303 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002304 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002305
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002306 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002307 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002308 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002309
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002310 // We have already updated the arg location with promoted info
2311 // so we can be based on it.
2312 if (t_loc->location == kLocPhysReg) {
2313 // Just copy it.
2314 OpRegCopy(t_loc->reg, reg);
2315 } else {
2316 // Needs flush.
2317 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002318 StoreRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002319 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002320 StoreBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002321 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002322 }
2323 }
2324 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002325 // If arriving in frame & promoted.
2326 if (t_loc->location == kLocPhysReg) {
2327 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002328 LoadRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002329 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002330 LoadBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002331 t_loc->wide ? k64 : k32, kNotVolatile);
2332 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002333 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002334 }
2335 if (t_loc->wide) {
2336 // Increment i to skip the next one.
2337 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002338 }
2339 }
2340}
2341
2342/*
2343 * Load up to 5 arguments, the first three of which will be in
2344 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2345 * and as part of the load sequence, it must be replaced with
2346 * the target method pointer. Note, this may also be called
2347 * for "range" variants if the number of arguments is 5 or fewer.
2348 */
2349int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2350 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2351 const MethodReference& target_method,
2352 uint32_t vtable_idx, uintptr_t direct_code,
2353 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002354 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002355 return Mir2Lir::GenDalvikArgsNoRange(info,
2356 call_state, pcrLabel, next_call_insn,
2357 target_method,
2358 vtable_idx, direct_code,
2359 direct_method, type, skip_this);
2360 }
2361 return GenDalvikArgsRange(info,
2362 call_state, pcrLabel, next_call_insn,
2363 target_method,
2364 vtable_idx, direct_code,
2365 direct_method, type, skip_this);
2366}
2367
2368/*
2369 * May have 0+ arguments (also used for jumbo). Note that
2370 * source virtual registers may be in physical registers, so may
2371 * need to be flushed to home location before copying. This
2372 * applies to arg3 and above (see below).
2373 *
2374 * Two general strategies:
2375 * If < 20 arguments
2376 * Pass args 3-18 using vldm/vstm block copy
2377 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2378 * If 20+ arguments
2379 * Pass args arg19+ using memcpy block copy
2380 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2381 *
2382 */
2383int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2384 LIR** pcrLabel, NextCallInsn next_call_insn,
2385 const MethodReference& target_method,
2386 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2387 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002388 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002389 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2390 pcrLabel, next_call_insn,
2391 target_method,
2392 vtable_idx, direct_code, direct_method,
2393 type, skip_this);
2394 }
2395
2396 /* If no arguments, just return */
2397 if (info->num_arg_words == 0)
2398 return call_state;
2399
2400 const int start_index = skip_this ? 1 : 0;
2401
Chao-ying Fua77ee512014-07-01 17:43:41 -07002402 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002403 InToRegStorageMapping in_to_reg_storage_mapping;
2404 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2405 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2406 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
Serguei Katkov8e3acdd2014-07-15 12:01:00 +07002407 info->args[last_mapped_in].wide ? 2 : 1;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002408 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2409
2410 // Fisrt of all, check whether it make sense to use bulk copying
2411 // Optimization is aplicable only for range case
2412 // TODO: make a constant instead of 2
2413 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2414 // Scan the rest of the args - if in phys_reg flush to memory
2415 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2416 RegLocation loc = info->args[next_arg];
2417 if (loc.wide) {
2418 loc = UpdateLocWide(loc);
2419 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002420 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002421 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002422 }
2423 next_arg += 2;
2424 } else {
2425 loc = UpdateLoc(loc);
2426 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002427 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002428 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002429 }
2430 next_arg++;
2431 }
2432 }
2433
2434 // Logic below assumes that Method pointer is at offset zero from SP.
2435 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
2436
2437 // The rest can be copied together
2438 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
Andreas Gampeccc60262014-07-04 18:02:38 -07002439 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped,
2440 cu_->instruction_set);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002441
2442 int current_src_offset = start_offset;
2443 int current_dest_offset = outs_offset;
2444
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002445 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2446 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002447 while (regs_left_to_pass_via_stack > 0) {
2448 // This is based on the knowledge that the stack itself is 16-byte aligned.
2449 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2450 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2451 size_t bytes_to_move;
2452
2453 /*
2454 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2455 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2456 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2457 * We do this because we could potentially do a smaller move to align.
2458 */
2459 if (regs_left_to_pass_via_stack == 4 ||
2460 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2461 // Moving 128-bits via xmm register.
2462 bytes_to_move = sizeof(uint32_t) * 4;
2463
2464 // Allocate a free xmm temp. Since we are working through the calling sequence,
2465 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2466 // there are no free registers.
2467 RegStorage temp = AllocTempDouble();
2468
2469 LIR* ld1 = nullptr;
2470 LIR* ld2 = nullptr;
2471 LIR* st1 = nullptr;
2472 LIR* st2 = nullptr;
2473
2474 /*
2475 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2476 * do an aligned move. If we have 8-byte alignment, then do the move in two
2477 * parts. This approach prevents possible cache line splits. Finally, fall back
2478 * to doing an unaligned move. In most cases we likely won't split the cache
2479 * line but we cannot prove it and thus take a conservative approach.
2480 */
2481 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2482 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2483
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002484 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002485 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002486 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002487 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002488 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovLo128FP);
2489 ld2 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002490 kMovHi128FP);
2491 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002492 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002493 }
2494
2495 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002496 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002497 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002498 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovLo128FP);
2499 st2 = OpMovMemReg(rs_rX86_SP, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002500 temp, kMovHi128FP);
2501 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002502 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002503 }
2504
2505 // TODO If we could keep track of aliasing information for memory accesses that are wider
2506 // than 64-bit, we wouldn't need to set up a barrier.
2507 if (ld1 != nullptr) {
2508 if (ld2 != nullptr) {
2509 // For 64-bit load we can actually set up the aliasing information.
2510 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2511 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2512 } else {
2513 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002514 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002515 }
2516 }
2517 if (st1 != nullptr) {
2518 if (st2 != nullptr) {
2519 // For 64-bit store we can actually set up the aliasing information.
2520 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2521 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2522 } else {
2523 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002524 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002525 }
2526 }
2527
2528 // Free the temporary used for the data movement.
2529 FreeTemp(temp);
2530 } else {
2531 // Moving 32-bits via general purpose register.
2532 bytes_to_move = sizeof(uint32_t);
2533
2534 // Instead of allocating a new temp, simply reuse one of the registers being used
2535 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07002536 RegStorage temp = TargetReg(kArg3, kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002537
2538 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002539 Load32Disp(rs_rX86_SP, current_src_offset, temp);
2540 Store32Disp(rs_rX86_SP, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002541 }
2542
2543 current_src_offset += bytes_to_move;
2544 current_dest_offset += bytes_to_move;
2545 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2546 }
2547 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2548 }
2549
2550 // Now handle rest not registers if they are
2551 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002552 RegStorage regSingle = TargetReg(kArg2, kNotWide);
2553 RegStorage regWide = TargetReg(kArg3, kWide);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002554 for (int i = start_index;
2555 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002556 RegLocation rl_arg = info->args[i];
2557 rl_arg = UpdateRawLoc(rl_arg);
2558 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2559 if (!reg.Valid()) {
2560 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2561
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002562 {
2563 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2564 if (rl_arg.wide) {
2565 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002566 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002567 } else {
2568 LoadValueDirectWideFixed(rl_arg, regWide);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002569 StoreBaseDisp(rs_rX86_SP, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002570 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002571 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002572 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002573 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002574 } else {
2575 LoadValueDirectFixed(rl_arg, regSingle);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002576 StoreBaseDisp(rs_rX86_SP, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002577 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002578 }
2579 }
2580 call_state = next_call_insn(cu_, info, call_state, target_method,
2581 vtable_idx, direct_code, direct_method, type);
2582 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002583 if (rl_arg.wide) {
2584 i++;
2585 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002586 }
2587 }
2588
2589 // Finish with mapped registers
2590 for (int i = start_index; i <= last_mapped_in; i++) {
2591 RegLocation rl_arg = info->args[i];
2592 rl_arg = UpdateRawLoc(rl_arg);
2593 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2594 if (reg.Valid()) {
2595 if (rl_arg.wide) {
2596 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002597 } else {
2598 LoadValueDirectFixed(rl_arg, reg);
2599 }
2600 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2601 direct_code, direct_method, type);
2602 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002603 if (rl_arg.wide) {
2604 i++;
2605 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002606 }
2607
2608 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2609 direct_code, direct_method, type);
2610 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00002611 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002612 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002613 } else {
2614 *pcrLabel = nullptr;
2615 // In lieu of generating a check for kArg1 being null, we need to
2616 // perform a load when doing implicit checks.
2617 RegStorage tmp = AllocTemp();
Andreas Gampeccc60262014-07-04 18:02:38 -07002618 Load32Disp(TargetReg(kArg1, kRef), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002619 MarkPossibleNullPointerException(info->opt_flags);
2620 FreeTemp(tmp);
2621 }
2622 }
2623 return call_state;
2624}
2625
Andreas Gampe98430592014-07-27 19:44:50 -07002626bool X86Mir2Lir::GenInlinedCharAt(CallInfo* info) {
2627 // Location of reference to data array
2628 int value_offset = mirror::String::ValueOffset().Int32Value();
2629 // Location of count
2630 int count_offset = mirror::String::CountOffset().Int32Value();
2631 // Starting offset within data array
2632 int offset_offset = mirror::String::OffsetOffset().Int32Value();
2633 // Start of char data with array_
2634 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
2635
2636 RegLocation rl_obj = info->args[0];
2637 RegLocation rl_idx = info->args[1];
2638 rl_obj = LoadValue(rl_obj, kRefReg);
2639 // X86 wants to avoid putting a constant index into a register.
2640 if (!rl_idx.is_const) {
2641 rl_idx = LoadValue(rl_idx, kCoreReg);
2642 }
2643 RegStorage reg_max;
2644 GenNullCheck(rl_obj.reg, info->opt_flags);
2645 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
2646 LIR* range_check_branch = nullptr;
2647 RegStorage reg_off;
2648 RegStorage reg_ptr;
2649 if (range_check) {
2650 // On x86, we can compare to memory directly
2651 // Set up a launch pad to allow retry in case of bounds violation */
2652 if (rl_idx.is_const) {
2653 LIR* comparison;
2654 range_check_branch = OpCmpMemImmBranch(
2655 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
2656 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr, &comparison);
2657 MarkPossibleNullPointerExceptionAfter(0, comparison);
2658 } else {
2659 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
2660 MarkPossibleNullPointerException(0);
2661 range_check_branch = OpCondBranch(kCondUge, nullptr);
2662 }
2663 }
2664 reg_off = AllocTemp();
2665 reg_ptr = AllocTempRef();
2666 Load32Disp(rl_obj.reg, offset_offset, reg_off);
2667 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
2668 if (rl_idx.is_const) {
2669 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
2670 } else {
2671 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
2672 }
2673 FreeTemp(rl_obj.reg);
2674 if (rl_idx.location == kLocPhysReg) {
2675 FreeTemp(rl_idx.reg);
2676 }
2677 RegLocation rl_dest = InlineTarget(info);
2678 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
2679 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
2680 FreeTemp(reg_off);
2681 FreeTemp(reg_ptr);
2682 StoreValue(rl_dest, rl_result);
2683 if (range_check) {
2684 DCHECK(range_check_branch != nullptr);
2685 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
2686 AddIntrinsicSlowPath(info, range_check_branch);
2687 }
2688 return true;
2689}
2690
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002691bool X86Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
2692 RegLocation rl_dest = InlineTarget(info);
2693
2694 // Early exit if the result is unused.
2695 if (rl_dest.orig_sreg < 0) {
2696 return true;
2697 }
2698
2699 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
2700
2701 if (cu_->target64) {
2702 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<8>());
2703 } else {
2704 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<4>());
2705 }
2706
2707 StoreValue(rl_dest, rl_result);
2708 return true;
2709}
2710
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002711} // namespace art