blob: 68c1633c2ab7c601e12fe7f1950ced89980b0010 [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 }
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700600 } else if (barrier_kind == kNTStoreStore) {
601 mem_barrier = NewLIR0(kX86Sfence);
602 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800603 }
604
605 // Now ensure that a scheduling barrier is in place.
606 if (mem_barrier == nullptr) {
607 GenBarrier();
608 } else {
609 // Mark as a scheduling barrier.
610 DCHECK(!mem_barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100611 mem_barrier->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800612 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700613 return ret;
614#else
615 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616#endif
617}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000618
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619void X86Mir2Lir::CompilerInitializeRegAlloc() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700620 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700621 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_64, core_regs_64q, sp_regs_64,
622 dp_regs_64, reserved_regs_64, reserved_regs_64q,
623 core_temps_64, core_temps_64q, sp_temps_64, dp_temps_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700624 } else {
buzbeeb01bf152014-05-13 15:59:07 -0700625 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
626 dp_regs_32, reserved_regs_32, empty_pool,
627 core_temps_32, empty_pool, sp_temps_32, dp_temps_32);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700628 }
buzbee091cc402014-03-31 10:14:40 -0700629
630 // Target-specific adjustments.
631
Mark Mendellfe945782014-05-22 09:52:36 -0400632 // Add in XMM registers.
Serguei Katkovc3801912014-07-08 17:21:53 +0700633 const ArrayRef<const RegStorage> *xp_regs = cu_->target64 ? &xp_regs_64 : &xp_regs_32;
634 for (RegStorage reg : *xp_regs) {
Mark Mendellfe945782014-05-22 09:52:36 -0400635 RegisterInfo* info = new (arena_) RegisterInfo(reg, GetRegMaskCommon(reg));
636 reginfo_map_.Put(reg.GetReg(), info);
Serguei Katkovc3801912014-07-08 17:21:53 +0700637 }
638 const ArrayRef<const RegStorage> *xp_temps = cu_->target64 ? &xp_temps_64 : &xp_temps_32;
639 for (RegStorage reg : *xp_temps) {
640 RegisterInfo* xp_reg_info = GetRegInfo(reg);
641 xp_reg_info->SetIsTemp(true);
Mark Mendellfe945782014-05-22 09:52:36 -0400642 }
643
buzbee091cc402014-03-31 10:14:40 -0700644 // Alias single precision xmm to double xmms.
645 // TODO: as needed, add larger vector sizes - alias all to the largest.
646 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
647 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
648 int sp_reg_num = info->GetReg().GetRegNum();
Mark Mendellfe945782014-05-22 09:52:36 -0400649 RegStorage xp_reg = RegStorage::Solo128(sp_reg_num);
650 RegisterInfo* xp_reg_info = GetRegInfo(xp_reg);
651 // 128-bit xmm vector register's master storage should refer to itself.
652 DCHECK_EQ(xp_reg_info, xp_reg_info->Master());
653
654 // Redirect 32-bit vector's master storage to 128-bit vector.
655 info->SetMaster(xp_reg_info);
656
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700657 RegStorage dp_reg = RegStorage::FloatSolo64(sp_reg_num);
buzbee091cc402014-03-31 10:14:40 -0700658 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
Mark Mendellfe945782014-05-22 09:52:36 -0400659 // Redirect 64-bit vector's master storage to 128-bit vector.
660 dp_reg_info->SetMaster(xp_reg_info);
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700661 // Singles should show a single 32-bit mask bit, at first referring to the low half.
662 DCHECK_EQ(info->StorageMask(), 0x1U);
663 }
664
Elena Sayapinadd644502014-07-01 18:39:52 +0700665 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700666 // Alias 32bit W registers to corresponding 64bit X registers.
667 GrowableArray<RegisterInfo*>::Iterator w_it(&reg_pool_->core_regs_);
668 for (RegisterInfo* info = w_it.Next(); info != nullptr; info = w_it.Next()) {
669 int x_reg_num = info->GetReg().GetRegNum();
670 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
671 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
672 // 64bit X register's master storage should refer to itself.
673 DCHECK_EQ(x_reg_info, x_reg_info->Master());
674 // Redirect 32bit W master storage to 64bit X.
675 info->SetMaster(x_reg_info);
676 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
677 DCHECK_EQ(info->StorageMask(), 0x1U);
678 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 }
buzbee091cc402014-03-31 10:14:40 -0700680
681 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
682 // TODO: adjust for x86/hard float calling convention.
683 reg_pool_->next_core_reg_ = 2;
684 reg_pool_->next_sp_reg_ = 2;
685 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686}
687
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700688int X86Mir2Lir::VectorRegisterSize() {
689 return 128;
690}
691
692int X86Mir2Lir::NumReservableVectorRegisters(bool fp_used) {
693 return fp_used ? 5 : 7;
694}
695
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696void X86Mir2Lir::SpillCoreRegs() {
697 if (num_core_spills_ == 0) {
698 return;
699 }
700 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700701 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700702 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700703 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 for (int reg = 0; mask; mask >>= 1, reg++) {
705 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700706 StoreBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
707 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700708 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 }
710 }
711}
712
713void X86Mir2Lir::UnSpillCoreRegs() {
714 if (num_core_spills_ == 0) {
715 return;
716 }
717 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700718 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700719 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700720 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 for (int reg = 0; mask; mask >>= 1, reg++) {
722 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700723 LoadBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
724 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700725 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 }
727 }
728}
729
Serguei Katkovc3801912014-07-08 17:21:53 +0700730void X86Mir2Lir::SpillFPRegs() {
731 if (num_fp_spills_ == 0) {
732 return;
733 }
734 uint32_t mask = fp_spill_mask_;
735 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
736 for (int reg = 0; mask; mask >>= 1, reg++) {
737 if (mask & 0x1) {
738 StoreBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
739 k64, kNotVolatile);
740 offset += sizeof(double);
741 }
742 }
743}
744void X86Mir2Lir::UnSpillFPRegs() {
745 if (num_fp_spills_ == 0) {
746 return;
747 }
748 uint32_t mask = fp_spill_mask_;
749 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
750 for (int reg = 0; mask; mask >>= 1, reg++) {
751 if (mask & 0x1) {
752 LoadBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
753 k64, kNotVolatile);
754 offset += sizeof(double);
755 }
756 }
757}
758
759
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700760bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
762}
763
Vladimir Marko674744e2014-04-24 15:18:26 +0100764RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700765 // X86_64 can handle any size.
Elena Sayapinadd644502014-07-01 18:39:52 +0700766 if (cu_->target64) {
Chao-ying Fu06839f82014-08-14 15:59:17 -0700767 return RegClassBySize(size);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700768 }
769
Vladimir Marko674744e2014-04-24 15:18:26 +0100770 if (UNLIKELY(is_volatile)) {
771 // On x86, atomic 64-bit load/store requires an fp register.
772 // Smaller aligned load/store is atomic for both core and fp registers.
773 if (size == k64 || size == kDouble) {
774 return kFPReg;
775 }
776 }
777 return RegClassBySize(size);
778}
779
Elena Sayapinadd644502014-07-01 18:39:52 +0700780X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800781 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700782 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800783 method_address_insns_(arena, 100, kGrowableArrayMisc),
784 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800785 call_method_insns_(arena, 100, kGrowableArrayMisc),
Elena Sayapinadd644502014-07-01 18:39:52 +0700786 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400787 const_vectors_(nullptr) {
788 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700789 if (kIsDebugBuild) {
790 for (int i = 0; i < kX86Last; i++) {
791 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
792 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400793 << " is wrong: expecting " << i << ", seeing "
794 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700795 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 }
797 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700798 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700799 rs_rX86_SP = rs_rX86_SP_64;
800
801 rs_rX86_ARG0 = rs_rDI;
802 rs_rX86_ARG1 = rs_rSI;
803 rs_rX86_ARG2 = rs_rDX;
804 rs_rX86_ARG3 = rs_rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700805 rs_rX86_ARG4 = rs_r8;
806 rs_rX86_ARG5 = rs_r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700807 rs_rX86_FARG0 = rs_fr0;
808 rs_rX86_FARG1 = rs_fr1;
809 rs_rX86_FARG2 = rs_fr2;
810 rs_rX86_FARG3 = rs_fr3;
811 rs_rX86_FARG4 = rs_fr4;
812 rs_rX86_FARG5 = rs_fr5;
813 rs_rX86_FARG6 = rs_fr6;
814 rs_rX86_FARG7 = rs_fr7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700815 rX86_ARG0 = rDI;
816 rX86_ARG1 = rSI;
817 rX86_ARG2 = rDX;
818 rX86_ARG3 = rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700819 rX86_ARG4 = r8;
820 rX86_ARG5 = r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700821 rX86_FARG0 = fr0;
822 rX86_FARG1 = fr1;
823 rX86_FARG2 = fr2;
824 rX86_FARG3 = fr3;
825 rX86_FARG4 = fr4;
826 rX86_FARG5 = fr5;
827 rX86_FARG6 = fr6;
828 rX86_FARG7 = fr7;
Mark Mendell55884bc2014-06-10 10:21:29 -0400829 rs_rX86_INVOKE_TGT = rs_rDI;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700830 } else {
831 rs_rX86_SP = rs_rX86_SP_32;
832
833 rs_rX86_ARG0 = rs_rAX;
834 rs_rX86_ARG1 = rs_rCX;
835 rs_rX86_ARG2 = rs_rDX;
836 rs_rX86_ARG3 = rs_rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700837 rs_rX86_ARG4 = RegStorage::InvalidReg();
838 rs_rX86_ARG5 = RegStorage::InvalidReg();
839 rs_rX86_FARG0 = rs_rAX;
840 rs_rX86_FARG1 = rs_rCX;
841 rs_rX86_FARG2 = rs_rDX;
842 rs_rX86_FARG3 = rs_rBX;
843 rs_rX86_FARG4 = RegStorage::InvalidReg();
844 rs_rX86_FARG5 = RegStorage::InvalidReg();
845 rs_rX86_FARG6 = RegStorage::InvalidReg();
846 rs_rX86_FARG7 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700847 rX86_ARG0 = rAX;
848 rX86_ARG1 = rCX;
849 rX86_ARG2 = rDX;
850 rX86_ARG3 = rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700851 rX86_FARG0 = rAX;
852 rX86_FARG1 = rCX;
853 rX86_FARG2 = rDX;
854 rX86_FARG3 = rBX;
Mark Mendell55884bc2014-06-10 10:21:29 -0400855 rs_rX86_INVOKE_TGT = rs_rAX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700856 // TODO(64): Initialize with invalid reg
857// rX86_ARG4 = RegStorage::InvalidReg();
858// rX86_ARG5 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700859 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700860 rs_rX86_RET0 = rs_rAX;
861 rs_rX86_RET1 = rs_rDX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700862 rs_rX86_COUNT = rs_rCX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700863 rX86_RET0 = rAX;
864 rX86_RET1 = rDX;
865 rX86_INVOKE_TGT = rAX;
866 rX86_COUNT = rCX;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700867
868 // Initialize the number of reserved vector registers
869 num_reserved_vector_regs_ = -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870}
871
872Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
873 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700874 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875}
876
Andreas Gampe98430592014-07-27 19:44:50 -0700877// Not used in x86(-64)
878RegStorage X86Mir2Lir::LoadHelper(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700879 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
880 return RegStorage::InvalidReg();
881}
882
Dave Allisonb373e092014-02-20 16:06:36 -0800883LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
Dave Allison69dfe512014-07-11 17:11:58 +0000884 // First load the pointer in fs:[suspend-trigger] into eax
885 // Then use a test instruction to indirect via that address.
Dave Allisondfd3b472014-07-16 16:04:32 -0700886 if (cu_->target64) {
887 NewLIR2(kX86Mov64RT, rs_rAX.GetReg(),
888 Thread::ThreadSuspendTriggerOffset<8>().Int32Value());
889 } else {
890 NewLIR2(kX86Mov32RT, rs_rAX.GetReg(),
891 Thread::ThreadSuspendTriggerOffset<4>().Int32Value());
892 }
Dave Allison69dfe512014-07-11 17:11:58 +0000893 return NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rAX.GetReg(), 0);
Dave Allisonb373e092014-02-20 16:06:36 -0800894}
895
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700896uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700897 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 return X86Mir2Lir::EncodingMap[opcode].flags;
899}
900
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700901const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700902 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 return X86Mir2Lir::EncodingMap[opcode].name;
904}
905
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700906const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700907 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 return X86Mir2Lir::EncodingMap[opcode].fmt;
909}
910
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000911void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
912 // Can we do this directly to memory?
913 rl_dest = UpdateLocWide(rl_dest);
914 if ((rl_dest.location == kLocDalvikFrame) ||
915 (rl_dest.location == kLocCompilerTemp)) {
916 int32_t val_lo = Low32Bits(value);
917 int32_t val_hi = High32Bits(value);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700918 int r_base = rs_rX86_SP.GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000919 int displacement = SRegOffset(rl_dest.s_reg_low);
920
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100921 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800922 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000923 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
924 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800925 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000926 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
927 false /* is_load */, true /* is64bit */);
928 return;
929 }
930
931 // Just use the standard code to do the generation.
932 Mir2Lir::GenConstWide(rl_dest, value);
933}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800934
935// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
936void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
937 LOG(INFO) << "location: " << loc.location << ','
938 << (loc.wide ? " w" : " ")
939 << (loc.defined ? " D" : " ")
940 << (loc.is_const ? " c" : " ")
941 << (loc.fp ? " F" : " ")
942 << (loc.core ? " C" : " ")
943 << (loc.ref ? " r" : " ")
944 << (loc.high_word ? " h" : " ")
945 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800946 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000947 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800948 << ", s_reg: " << loc.s_reg_low
949 << ", orig: " << loc.orig_sreg;
950}
951
Mark Mendell67c39c42014-01-31 17:28:00 -0800952void X86Mir2Lir::Materialize() {
953 // A good place to put the analysis before starting.
954 AnalyzeMIR();
955
956 // Now continue with regular code generation.
957 Mir2Lir::Materialize();
958}
959
Jeff Hao49161ce2014-03-12 11:05:25 -0700960void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800961 SpecialTargetRegister symbolic_reg) {
962 /*
963 * For x86, just generate a 32 bit move immediate instruction, that will be filled
964 * in at 'link time'. For now, put a unique value based on target to ensure that
965 * code deduplication works.
966 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700967 int target_method_idx = target_method.dex_method_index;
968 const DexFile* target_dex_file = target_method.dex_file;
969 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
970 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800971
Jeff Hao49161ce2014-03-12 11:05:25 -0700972 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700973 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
974 TargetReg(symbolic_reg, kNotWide).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700975 static_cast<int>(target_method_id_ptr), target_method_idx,
976 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800977 AppendLIR(move);
978 method_address_insns_.Insert(move);
979}
980
Fred Shihe7f82e22014-08-06 10:46:37 -0700981void X86Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
982 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800983 /*
984 * For x86, just generate a 32 bit move immediate instruction, that will be filled
985 * in at 'link time'. For now, put a unique value based on target to ensure that
986 * code deduplication works.
987 */
Fred Shihe7f82e22014-08-06 10:46:37 -0700988 const DexFile::TypeId& id = dex_file.GetTypeId(type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800989 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
990
991 // Generate the move instruction with the unique pointer and save index and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700992 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
993 TargetReg(symbolic_reg, kNotWide).GetReg(),
Fred Shihe7f82e22014-08-06 10:46:37 -0700994 static_cast<int>(ptr), type_idx,
995 WrapPointer(const_cast<DexFile*>(&dex_file)));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800996 AppendLIR(move);
997 class_type_address_insns_.Insert(move);
998}
999
Jeff Hao49161ce2014-03-12 11:05:25 -07001000LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001001 /*
1002 * For x86, just generate a 32 bit call relative instruction, that will be filled
1003 * in at 'link time'. For now, put a unique value based on target to ensure that
1004 * code deduplication works.
1005 */
Jeff Hao49161ce2014-03-12 11:05:25 -07001006 int target_method_idx = target_method.dex_method_index;
1007 const DexFile* target_dex_file = target_method.dex_file;
1008 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
1009 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001010
Jeff Hao49161ce2014-03-12 11:05:25 -07001011 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
1012 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
1013 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001014 AppendLIR(call);
1015 call_method_insns_.Insert(call);
1016 return call;
1017}
1018
1019void X86Mir2Lir::InstallLiteralPools() {
1020 // These are handled differently for x86.
1021 DCHECK(code_literal_list_ == nullptr);
1022 DCHECK(method_literal_list_ == nullptr);
1023 DCHECK(class_literal_list_ == nullptr);
1024
Mark Mendelld65c51a2014-04-29 16:55:20 -04001025 // Align to 16 byte boundary. We have implicit knowledge that the start of the method is
1026 // on a 4 byte boundary. How can I check this if it changes (other than aligned loads
1027 // will fail at runtime)?
1028 if (const_vectors_ != nullptr) {
1029 int align_size = (16-4) - (code_buffer_.size() & 0xF);
1030 if (align_size < 0) {
1031 align_size += 16;
1032 }
1033
1034 while (align_size > 0) {
1035 code_buffer_.push_back(0);
1036 align_size--;
1037 }
1038 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001039 PushWord(&code_buffer_, p->operands[0]);
1040 PushWord(&code_buffer_, p->operands[1]);
1041 PushWord(&code_buffer_, p->operands[2]);
1042 PushWord(&code_buffer_, p->operands[3]);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001043 }
1044 }
1045
Mark Mendell55d0eac2014-02-06 11:02:52 -08001046 // Handle the fixups for methods.
1047 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
1048 LIR* p = method_address_insns_.Get(i);
1049 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001050 uint32_t target_method_idx = p->operands[2];
1051 const DexFile* target_dex_file =
1052 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001053
1054 // The offset to patch is the last 4 bytes of the instruction.
1055 int patch_offset = p->offset + p->flags.size - 4;
1056 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
1057 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -07001058 target_method_idx, target_dex_file,
1059 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001060 patch_offset);
1061 }
1062
1063 // Handle the fixups for class types.
1064 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
1065 LIR* p = class_type_address_insns_.Get(i);
1066 DCHECK_EQ(p->opcode, kX86Mov32RI);
Fred Shihe7f82e22014-08-06 10:46:37 -07001067
1068 const DexFile* class_dex_file =
1069 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Jeff Hao49161ce2014-03-12 11:05:25 -07001070 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001071
1072 // The offset to patch is the last 4 bytes of the instruction.
1073 int patch_offset = p->offset + p->flags.size - 4;
1074 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -07001075 cu_->method_idx, target_method_idx, class_dex_file,
1076 patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001077 }
1078
1079 // And now the PC-relative calls to methods.
1080 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
1081 LIR* p = call_method_insns_.Get(i);
1082 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001083 uint32_t target_method_idx = p->operands[1];
1084 const DexFile* target_dex_file =
1085 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001086
1087 // The offset to patch is the last 4 bytes of the instruction.
1088 int patch_offset = p->offset + p->flags.size - 4;
1089 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001090 cu_->method_idx, cu_->invoke_type,
1091 target_method_idx, target_dex_file,
1092 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001093 patch_offset, -4 /* offset */);
1094 }
1095
1096 // And do the normal processing.
1097 Mir2Lir::InstallLiteralPools();
1098}
1099
DaniilSokolov70c4f062014-06-24 17:34:00 -07001100bool X86Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
DaniilSokolov70c4f062014-06-24 17:34:00 -07001101 RegLocation rl_src = info->args[0];
1102 RegLocation rl_srcPos = info->args[1];
1103 RegLocation rl_dst = info->args[2];
1104 RegLocation rl_dstPos = info->args[3];
1105 RegLocation rl_length = info->args[4];
1106 if (rl_srcPos.is_const && (mir_graph_->ConstantValue(rl_srcPos) < 0)) {
1107 return false;
1108 }
1109 if (rl_dstPos.is_const && (mir_graph_->ConstantValue(rl_dstPos) < 0)) {
1110 return false;
1111 }
1112 ClobberCallerSave();
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001113 LockCallTemps(); // Using fixed registers.
1114 RegStorage tmp_reg = cu_->target64 ? rs_r11 : rs_rBX;
1115 LoadValueDirectFixed(rl_src, rs_rAX);
1116 LoadValueDirectFixed(rl_dst, rs_rCX);
1117 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_rAX, rs_rCX, nullptr);
1118 LIR* src_null_branch = OpCmpImmBranch(kCondEq, rs_rAX, 0, nullptr);
1119 LIR* dst_null_branch = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1120 LoadValueDirectFixed(rl_length, rs_rDX);
1121 // If the length of the copy is > 128 characters (256 bytes) or negative then go slow path.
1122 LIR* len_too_big = OpCmpImmBranch(kCondHi, rs_rDX, 128, nullptr);
1123 LoadValueDirectFixed(rl_src, rs_rAX);
1124 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001125 LIR* src_bad_len = nullptr;
1126 LIR* srcPos_negative = nullptr;
1127 if (!rl_srcPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001128 LoadValueDirectFixed(rl_srcPos, tmp_reg);
1129 srcPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1130 OpRegReg(kOpAdd, tmp_reg, rs_rDX);
1131 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001132 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001133 int32_t pos_val = mir_graph_->ConstantValue(rl_srcPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001134 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001135 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001136 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001137 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1138 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001139 }
1140 }
1141 LIR* dstPos_negative = nullptr;
1142 LIR* dst_bad_len = nullptr;
1143 LoadValueDirectFixed(rl_dst, rs_rAX);
1144 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1145 if (!rl_dstPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001146 LoadValueDirectFixed(rl_dstPos, tmp_reg);
1147 dstPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1148 OpRegRegReg(kOpAdd, tmp_reg, tmp_reg, rs_rDX);
1149 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001150 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001151 int32_t pos_val = mir_graph_->ConstantValue(rl_dstPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001152 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001153 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001154 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001155 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1156 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001157 }
1158 }
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001159 // Everything is checked now.
1160 LoadValueDirectFixed(rl_src, rs_rAX);
1161 LoadValueDirectFixed(rl_dst, tmp_reg);
1162 LoadValueDirectFixed(rl_srcPos, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001163 NewLIR5(kX86Lea32RA, rs_rAX.GetReg(), rs_rAX.GetReg(),
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001164 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value());
1165 // RAX now holds the address of the first src element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001166
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001167 LoadValueDirectFixed(rl_dstPos, rs_rCX);
1168 NewLIR5(kX86Lea32RA, tmp_reg.GetReg(), tmp_reg.GetReg(),
1169 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value() );
1170 // RBX now holds the address of the first dst element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001171
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001172 // Check if the number of elements to be copied is odd or even. If odd
DaniilSokolov70c4f062014-06-24 17:34:00 -07001173 // then copy the first element (so that the remaining number of elements
1174 // is even).
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001175 LoadValueDirectFixed(rl_length, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001176 OpRegImm(kOpAnd, rs_rCX, 1);
1177 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1178 OpRegImm(kOpSub, rs_rDX, 1);
1179 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001180 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001181
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001182 // Since the remaining number of elements is even, we will copy by
DaniilSokolov70c4f062014-06-24 17:34:00 -07001183 // two elements at a time.
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001184 LIR* beginLoop = NewLIR0(kPseudoTargetLabel);
1185 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_rDX, 0, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001186 OpRegImm(kOpSub, rs_rDX, 2);
1187 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001188 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001189 OpUnconditionalBranch(beginLoop);
1190 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1191 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1192 LIR *return_point = NewLIR0(kPseudoTargetLabel);
1193 jmp_to_ret->target = return_point;
1194 jmp_to_begin_loop->target = beginLoop;
1195 src_dst_same->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001196 len_too_big->target = check_failed;
1197 src_null_branch->target = check_failed;
1198 if (srcPos_negative != nullptr)
1199 srcPos_negative ->target = check_failed;
1200 if (src_bad_len != nullptr)
1201 src_bad_len->target = check_failed;
1202 dst_null_branch->target = check_failed;
1203 if (dstPos_negative != nullptr)
1204 dstPos_negative->target = check_failed;
1205 if (dst_bad_len != nullptr)
1206 dst_bad_len->target = check_failed;
1207 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
1208 return true;
1209}
1210
1211
Mark Mendell4028a6c2014-02-19 20:06:20 -08001212/*
1213 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1214 * otherwise bails to standard library code.
1215 */
1216bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001217 RegLocation rl_obj = info->args[0];
1218 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001219 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001220 // RBX is promotable in 64-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001221 RegStorage rs_tmp = cu_->target64 ? rs_r11 : rs_rBX;
1222 int start_value = -1;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001223
1224 uint32_t char_value =
1225 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1226
1227 if (char_value > 0xFFFF) {
1228 // We have to punt to the real String.indexOf.
1229 return false;
1230 }
1231
1232 // Okay, we are commited to inlining this.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001233 // EAX: 16 bit character being searched.
1234 // ECX: count: number of words to be searched.
1235 // EDI: String being searched.
1236 // EDX: temporary during execution.
1237 // EBX or R11: temporary during execution (depending on mode).
1238 // REP SCASW: search instruction.
1239
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001240 FlushAllRegs();
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001241
buzbeea0cd2d72014-06-01 09:33:49 -07001242 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001243 RegLocation rl_dest = InlineTarget(info);
1244
1245 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001246 LoadValueDirectFixed(rl_obj, rs_rDX);
1247 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001248 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001249
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001250 LIR *slowpath_branch = nullptr, *length_compare = nullptr;
1251
1252 // We need the value in EAX.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001253 if (rl_char.is_const) {
buzbee2700f7e2014-03-07 09:46:20 -08001254 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001255 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001256 // Does the character fit in 16 bits? Compare it at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001257 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001258 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001259 }
1260
1261 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001262 // Location of reference to data array within the String object.
1263 int value_offset = mirror::String::ValueOffset().Int32Value();
1264 // Location of count within the String object.
1265 int count_offset = mirror::String::CountOffset().Int32Value();
1266 // Starting offset within data array.
1267 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1268 // Start of char data with array_.
1269 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001270
Dave Allison69dfe512014-07-11 17:11:58 +00001271 // Compute the number of words to search in to rCX.
1272 Load32Disp(rs_rDX, count_offset, rs_rCX);
1273
Dave Allisondfd3b472014-07-16 16:04:32 -07001274 // Possible signal here due to null pointer dereference.
1275 // Note that the signal handler will expect the top word of
1276 // the stack to be the ArtMethod*. If the PUSH edi instruction
1277 // below is ahead of the load above then this will not be true
1278 // and the signal handler will not work.
1279 MarkPossibleNullPointerException(0);
Dave Allison69dfe512014-07-11 17:11:58 +00001280
Dave Allisondfd3b472014-07-16 16:04:32 -07001281 if (!cu_->target64) {
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001282 // EDI is promotable in 32-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001283 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1284 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001285
Mark Mendell4028a6c2014-02-19 20:06:20 -08001286 if (zero_based) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001287 // Start index is not present.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001288 // We have to handle an empty string. Use special instruction JECXZ.
1289 length_compare = NewLIR0(kX86Jecxz8);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001290
1291 // Copy the number of words to search in a temporary register.
1292 // We will use the register at the end to calculate result.
1293 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001294 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001295 // Start index is present.
buzbeea44d4f52014-03-05 11:26:39 -08001296 rl_start = info->args[2];
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001297
Mark Mendell4028a6c2014-02-19 20:06:20 -08001298 // We have to offset by the start index.
1299 if (rl_start.is_const) {
1300 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1301 start_value = std::max(start_value, 0);
1302
1303 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001304 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001305 OpRegImm(kOpMov, rs_rDI, start_value);
1306
1307 // Copy the number of words to search in a temporary register.
1308 // We will use the register at the end to calculate result.
1309 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001310
1311 if (start_value != 0) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001312 // Decrease the number of words to search by the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001313 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001314 }
1315 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001316 // Handle "start index < 0" case.
1317 if (!cu_->target64 && rl_start.location != kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001318 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001319 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko74de63b2014-08-19 15:00:34 +01001320 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1321 Load32Disp(rs_rX86_SP, displacement, rs_rDI);
1322 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1323 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1324 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - 1;
1325 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001326 } else {
1327 LoadValueDirectFixed(rl_start, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001328 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001329 OpRegReg(kOpXor, rs_tmp, rs_tmp);
1330 OpRegReg(kOpCmp, rs_rDI, rs_tmp);
1331 OpCondRegReg(kOpCmov, kCondLt, rs_rDI, rs_tmp);
1332
1333 // The length of the string should be greater than the start index.
1334 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rDI, nullptr);
1335
1336 // Copy the number of words to search in a temporary register.
1337 // We will use the register at the end to calculate result.
1338 OpRegReg(kOpMov, rs_tmp, rs_rCX);
1339
1340 // Decrease the number of words to search by the start index.
1341 OpRegReg(kOpSub, rs_rCX, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001342 }
1343 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001344
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001345 // Load the address of the string into EDI.
1346 // In case of start index we have to add the address to existing value in EDI.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001347 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001348 if (zero_based || (!zero_based && rl_start.is_const && start_value == 0)) {
1349 Load32Disp(rs_rDX, offset_offset, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001350 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001351 OpRegMem(kOpAdd, rs_rDI, rs_rDX, offset_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001352 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001353 OpRegImm(kOpLsl, rs_rDI, 1);
1354 OpRegMem(kOpAdd, rs_rDI, rs_rDX, value_offset);
1355 OpRegImm(kOpAdd, rs_rDI, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001356
1357 // EDI now contains the start of the string to be searched.
1358 // We are all prepared to do the search for the character.
1359 NewLIR0(kX86RepneScasw);
1360
1361 // Did we find a match?
1362 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1363
1364 // yes, we matched. Compute the index of the result.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001365 OpRegReg(kOpSub, rs_tmp, rs_rCX);
1366 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_tmp.GetReg(), -1);
1367
Mark Mendell4028a6c2014-02-19 20:06:20 -08001368 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1369
1370 // Failed to match; return -1.
1371 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1372 length_compare->target = not_found;
1373 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001374 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001375
1376 // And join up at the end.
1377 all_done->target = NewLIR0(kPseudoTargetLabel);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001378
1379 if (!cu_->target64)
1380 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001381
1382 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001383 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001384 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001385 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001386 }
1387
1388 StoreValue(rl_dest, rl_return);
1389 return true;
1390}
1391
Tong Shen35e1e6a2014-07-30 09:31:22 -07001392static bool ARTRegIDToDWARFRegID(bool is_x86_64, int art_reg_id, int* dwarf_reg_id) {
1393 if (is_x86_64) {
1394 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001395 case 3 : *dwarf_reg_id = 3; return true; // %rbx
Tong Shen35e1e6a2014-07-30 09:31:22 -07001396 // This is the only discrepancy between ART & DWARF register numbering.
Andreas Gampebda27222014-07-30 23:21:36 -07001397 case 5 : *dwarf_reg_id = 6; return true; // %rbp
1398 case 12: *dwarf_reg_id = 12; return true; // %r12
1399 case 13: *dwarf_reg_id = 13; return true; // %r13
1400 case 14: *dwarf_reg_id = 14; return true; // %r14
1401 case 15: *dwarf_reg_id = 15; return true; // %r15
1402 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001403 }
1404 } else {
1405 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001406 case 5: *dwarf_reg_id = 5; return true; // %ebp
1407 case 6: *dwarf_reg_id = 6; return true; // %esi
1408 case 7: *dwarf_reg_id = 7; return true; // %edi
1409 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001410 }
1411 }
1412}
1413
Tong Shen547cdfd2014-08-05 01:54:19 -07001414std::vector<uint8_t>* X86Mir2Lir::ReturnFrameDescriptionEntry() {
1415 std::vector<uint8_t>* cfi_info = new std::vector<uint8_t>;
Mark Mendellae9fd932014-02-10 16:14:35 -08001416
1417 // Generate the FDE for the method.
1418 DCHECK_NE(data_offset_, 0U);
1419
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001420 WriteFDEHeader(cfi_info, cu_->target64);
1421 WriteFDEAddressRange(cfi_info, data_offset_, cu_->target64);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001422
Mark Mendellae9fd932014-02-10 16:14:35 -08001423 // The instructions in the FDE.
1424 if (stack_decrement_ != nullptr) {
1425 // Advance LOC to just past the stack decrement.
1426 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001427 DW_CFA_advance_loc(cfi_info, pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001428
1429 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
Tong Shen547cdfd2014-08-05 01:54:19 -07001430 DW_CFA_def_cfa_offset(cfi_info, frame_size_);
Mark Mendellae9fd932014-02-10 16:14:35 -08001431
Tong Shen35e1e6a2014-07-30 09:31:22 -07001432 // Handle register spills
1433 const uint32_t kSpillInstLen = (cu_->target64) ? 5 : 4;
1434 const int kDataAlignmentFactor = (cu_->target64) ? -8 : -4;
1435 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
1436 int offset = -(GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
1437 for (int reg = 0; mask; mask >>= 1, reg++) {
1438 if (mask & 0x1) {
1439 pc += kSpillInstLen;
1440
1441 // Advance LOC to pass this instruction
Tong Shen547cdfd2014-08-05 01:54:19 -07001442 DW_CFA_advance_loc(cfi_info, kSpillInstLen);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001443
1444 int dwarf_reg_id;
1445 if (ARTRegIDToDWARFRegID(cu_->target64, reg, &dwarf_reg_id)) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001446 // DW_CFA_offset_extended_sf reg offset
1447 DW_CFA_offset_extended_sf(cfi_info, dwarf_reg_id, offset / kDataAlignmentFactor);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001448 }
1449
1450 offset += GetInstructionSetPointerSize(cu_->instruction_set);
1451 }
1452 }
1453
Mark Mendellae9fd932014-02-10 16:14:35 -08001454 // We continue with that stack until the epilogue.
1455 if (stack_increment_ != nullptr) {
1456 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001457 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001458
1459 // We probably have code snippets after the epilogue, so save the
1460 // current state: DW_CFA_remember_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001461 DW_CFA_remember_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001462
Tong Shen35e1e6a2014-07-30 09:31:22 -07001463 // We have now popped the stack: DW_CFA_def_cfa_offset 4/8.
1464 // There is only the return PC on the stack now.
Tong Shen547cdfd2014-08-05 01:54:19 -07001465 DW_CFA_def_cfa_offset(cfi_info, GetInstructionSetPointerSize(cu_->instruction_set));
Mark Mendellae9fd932014-02-10 16:14:35 -08001466
1467 // Everything after that is the same as before the epilogue.
1468 // Stack bump was followed by RET instruction.
1469 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1470 if (post_ret_insn != nullptr) {
1471 pc = new_pc;
1472 new_pc = post_ret_insn->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001473 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001474 // Restore the state: DW_CFA_restore_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001475 DW_CFA_restore_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001476 }
1477 }
1478 }
1479
Tong Shen547cdfd2014-08-05 01:54:19 -07001480 PadCFI(cfi_info);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001481 WriteCFILength(cfi_info, cu_->target64);
Mark Mendellae9fd932014-02-10 16:14:35 -08001482
Mark Mendellae9fd932014-02-10 16:14:35 -08001483 return cfi_info;
1484}
1485
Mark Mendelld65c51a2014-04-29 16:55:20 -04001486void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1487 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001488 case kMirOpReserveVectorRegisters:
1489 ReserveVectorRegisters(mir);
1490 break;
1491 case kMirOpReturnVectorRegisters:
1492 ReturnVectorRegisters();
1493 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001494 case kMirOpConstVector:
1495 GenConst128(bb, mir);
1496 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001497 case kMirOpMoveVector:
1498 GenMoveVector(bb, mir);
1499 break;
1500 case kMirOpPackedMultiply:
1501 GenMultiplyVector(bb, mir);
1502 break;
1503 case kMirOpPackedAddition:
1504 GenAddVector(bb, mir);
1505 break;
1506 case kMirOpPackedSubtract:
1507 GenSubtractVector(bb, mir);
1508 break;
1509 case kMirOpPackedShiftLeft:
1510 GenShiftLeftVector(bb, mir);
1511 break;
1512 case kMirOpPackedSignedShiftRight:
1513 GenSignedShiftRightVector(bb, mir);
1514 break;
1515 case kMirOpPackedUnsignedShiftRight:
1516 GenUnsignedShiftRightVector(bb, mir);
1517 break;
1518 case kMirOpPackedAnd:
1519 GenAndVector(bb, mir);
1520 break;
1521 case kMirOpPackedOr:
1522 GenOrVector(bb, mir);
1523 break;
1524 case kMirOpPackedXor:
1525 GenXorVector(bb, mir);
1526 break;
1527 case kMirOpPackedAddReduce:
1528 GenAddReduceVector(bb, mir);
1529 break;
1530 case kMirOpPackedReduce:
1531 GenReduceVector(bb, mir);
1532 break;
1533 case kMirOpPackedSet:
1534 GenSetVector(bb, mir);
1535 break;
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07001536 case kMirOpMemBarrier:
1537 GenMemBarrier(static_cast<MemBarrierKind>(mir->dalvikInsn.vA));
1538 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001539 default:
1540 break;
1541 }
1542}
1543
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001544void X86Mir2Lir::ReserveVectorRegisters(MIR* mir) {
1545 // We should not try to reserve twice without returning the registers
1546 DCHECK_NE(num_reserved_vector_regs_, -1);
1547
1548 int num_vector_reg = mir->dalvikInsn.vA;
1549 for (int i = 0; i < num_vector_reg; i++) {
1550 RegStorage xp_reg = RegStorage::Solo128(i);
1551 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1552 Clobber(xp_reg);
1553
1554 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1555 info != nullptr;
1556 info = info->GetAliasChain()) {
1557 if (info->GetReg().IsSingle()) {
1558 reg_pool_->sp_regs_.Delete(info);
1559 } else {
1560 reg_pool_->dp_regs_.Delete(info);
1561 }
1562 }
1563 }
1564
1565 num_reserved_vector_regs_ = num_vector_reg;
1566}
1567
1568void X86Mir2Lir::ReturnVectorRegisters() {
1569 // Return all the reserved registers
1570 for (int i = 0; i < num_reserved_vector_regs_; i++) {
1571 RegStorage xp_reg = RegStorage::Solo128(i);
1572 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1573
1574 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1575 info != nullptr;
1576 info = info->GetAliasChain()) {
1577 if (info->GetReg().IsSingle()) {
1578 reg_pool_->sp_regs_.Insert(info);
1579 } else {
1580 reg_pool_->dp_regs_.Insert(info);
1581 }
1582 }
1583 }
1584
1585 // We don't have anymore reserved vector registers
1586 num_reserved_vector_regs_ = -1;
1587}
1588
Mark Mendelld65c51a2014-04-29 16:55:20 -04001589void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001590 store_method_addr_used_ = true;
1591 int type_size = mir->dalvikInsn.vB;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001592 // We support 128 bit vectors.
1593 DCHECK_EQ(type_size & 0xFFFF, 128);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001594 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001595 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001596 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001597 // Check for all 0 case.
1598 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1599 NewLIR2(kX86XorpsRR, reg, reg);
1600 return;
1601 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001602
1603 // Append the mov const vector to reg opcode.
1604 AppendOpcodeWithConst(kX86MovupsRM, reg, mir);
1605}
1606
1607void X86Mir2Lir::AppendOpcodeWithConst(X86OpCode opcode, int reg, MIR* mir) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04001608 // Okay, load it from the constant vector area.
1609 LIR *data_target = ScanVectorLiteral(mir);
1610 if (data_target == nullptr) {
1611 data_target = AddVectorLiteral(mir);
1612 }
1613
1614 // Address the start of the method.
1615 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001616 if (rl_method.wide) {
1617 rl_method = LoadValueWide(rl_method, kCoreReg);
1618 } else {
1619 rl_method = LoadValue(rl_method, kCoreReg);
1620 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001621
1622 // Load the proper value from the literal area.
1623 // We don't know the proper offset for the value, so pick one that will force
1624 // 4 byte offset. We will fix this up in the assembler later to have the right
1625 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001626 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001627 LIR *load = NewLIR2(opcode, reg, rl_method.reg.GetReg());
Mark Mendelld65c51a2014-04-29 16:55:20 -04001628 load->flags.fixup = kFixupLoad;
1629 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001630}
1631
Mark Mendellfe945782014-05-22 09:52:36 -04001632void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1633 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001634 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1635 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
1636 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001637 NewLIR2(kX86Mova128RR, rs_dest.GetReg(), rs_src.GetReg());
1638}
1639
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001640void X86Mir2Lir::GenMultiplyVectorSignedByte(BasicBlock *bb, MIR *mir) {
1641 const int BYTE_SIZE = 8;
1642 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1643 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
1644 RegStorage rs_src1_high_tmp = Get128BitRegister(AllocTempWide());
1645
1646 /*
1647 * Emulate the behavior of a kSignedByte by separating out the 16 values in the two XMM
1648 * and multiplying 8 at a time before recombining back into one XMM register.
1649 *
1650 * let xmm1, xmm2 be real srcs (keep low bits of 16bit lanes)
1651 * xmm3 is tmp (operate on high bits of 16bit lanes)
1652 *
1653 * xmm3 = xmm1
1654 * xmm1 = xmm1 .* xmm2
1655 * xmm1 = xmm1 & 0x00ff00ff00ff00ff00ff00ff00ff00ff // xmm1 now has low bits
1656 * xmm3 = xmm3 .>> 8
1657 * xmm2 = xmm2 & 0xff00ff00ff00ff00ff00ff00ff00ff00
1658 * xmm2 = xmm2 .* xmm3 // xmm2 now has high bits
1659 * xmm1 = xmm1 | xmm2 // combine results
1660 */
1661
1662 // Copy xmm1.
1663 NewLIR2(kX86Mova128RR, rs_src1_high_tmp.GetReg(), rs_dest_src1.GetReg());
1664
1665 // Multiply low bits.
1666 NewLIR2(kX86PmullwRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1667
1668 // xmm1 now has low bits.
1669 AndMaskVectorRegister(rs_dest_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
1670
1671 // Prepare high bits for multiplication.
1672 NewLIR2(kX86PsrlwRI, rs_src1_high_tmp.GetReg(), BYTE_SIZE);
1673 AndMaskVectorRegister(rs_src2, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1674
1675 // Multiply high bits and xmm2 now has high bits.
1676 NewLIR2(kX86PmullwRR, rs_src2.GetReg(), rs_src1_high_tmp.GetReg());
1677
1678 // Combine back into dest XMM register.
1679 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1680}
1681
Mark Mendellfe945782014-05-22 09:52:36 -04001682void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001683 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1684 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1685 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1686 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001687 int opcode = 0;
1688 switch (opsize) {
1689 case k32:
1690 opcode = kX86PmulldRR;
1691 break;
1692 case kSignedHalf:
1693 opcode = kX86PmullwRR;
1694 break;
1695 case kSingle:
1696 opcode = kX86MulpsRR;
1697 break;
1698 case kDouble:
1699 opcode = kX86MulpdRR;
1700 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001701 case kSignedByte:
1702 // HW doesn't support 16x16 byte multiplication so emulate it.
1703 GenMultiplyVectorSignedByte(bb, mir);
1704 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001705 default:
1706 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1707 break;
1708 }
1709 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1710}
1711
1712void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001713 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1714 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1715 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1716 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001717 int opcode = 0;
1718 switch (opsize) {
1719 case k32:
1720 opcode = kX86PadddRR;
1721 break;
1722 case kSignedHalf:
1723 case kUnsignedHalf:
1724 opcode = kX86PaddwRR;
1725 break;
1726 case kUnsignedByte:
1727 case kSignedByte:
1728 opcode = kX86PaddbRR;
1729 break;
1730 case kSingle:
1731 opcode = kX86AddpsRR;
1732 break;
1733 case kDouble:
1734 opcode = kX86AddpdRR;
1735 break;
1736 default:
1737 LOG(FATAL) << "Unsupported vector addition " << opsize;
1738 break;
1739 }
1740 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1741}
1742
1743void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001744 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1745 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1746 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1747 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001748 int opcode = 0;
1749 switch (opsize) {
1750 case k32:
1751 opcode = kX86PsubdRR;
1752 break;
1753 case kSignedHalf:
1754 case kUnsignedHalf:
1755 opcode = kX86PsubwRR;
1756 break;
1757 case kUnsignedByte:
1758 case kSignedByte:
1759 opcode = kX86PsubbRR;
1760 break;
1761 case kSingle:
1762 opcode = kX86SubpsRR;
1763 break;
1764 case kDouble:
1765 opcode = kX86SubpdRR;
1766 break;
1767 default:
1768 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1769 break;
1770 }
1771 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1772}
1773
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001774void X86Mir2Lir::GenShiftByteVector(BasicBlock *bb, MIR *mir) {
1775 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1776 RegStorage rs_tmp = Get128BitRegister(AllocTempWide());
1777
1778 int opcode = 0;
1779 int imm = mir->dalvikInsn.vB;
1780
1781 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1782 case kMirOpPackedShiftLeft:
1783 opcode = kX86PsllwRI;
1784 break;
1785 case kMirOpPackedSignedShiftRight:
1786 opcode = kX86PsrawRI;
1787 break;
1788 case kMirOpPackedUnsignedShiftRight:
1789 opcode = kX86PsrlwRI;
1790 break;
1791 default:
1792 LOG(FATAL) << "Unsupported shift operation on byte vector " << opcode;
1793 break;
1794 }
1795
1796 /*
1797 * xmm1 will have low bits
1798 * xmm2 will have high bits
1799 *
1800 * xmm2 = xmm1
1801 * xmm1 = xmm1 .<< N
1802 * xmm2 = xmm2 && 0xFF00FF00FF00FF00FF00FF00FF00FF00
1803 * xmm2 = xmm2 .<< N
1804 * xmm1 = xmm1 | xmm2
1805 */
1806
1807 // Copy xmm1.
1808 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_dest_src1.GetReg());
1809
1810 // Shift lower values.
1811 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1812
1813 // Mask bottom bits.
1814 AndMaskVectorRegister(rs_tmp, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1815
1816 // Shift higher values.
1817 NewLIR2(opcode, rs_tmp.GetReg(), imm);
1818
1819 // Combine back into dest XMM register.
1820 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_tmp.GetReg());
1821}
1822
Mark Mendellfe945782014-05-22 09:52:36 -04001823void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001824 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1825 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1826 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1827 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001828 int opcode = 0;
1829 switch (opsize) {
1830 case k32:
1831 opcode = kX86PslldRI;
1832 break;
1833 case k64:
1834 opcode = kX86PsllqRI;
1835 break;
1836 case kSignedHalf:
1837 case kUnsignedHalf:
1838 opcode = kX86PsllwRI;
1839 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001840 case kSignedByte:
1841 case kUnsignedByte:
1842 GenShiftByteVector(bb, mir);
1843 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001844 default:
1845 LOG(FATAL) << "Unsupported vector shift left " << opsize;
1846 break;
1847 }
1848 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1849}
1850
1851void X86Mir2Lir::GenSignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001852 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1853 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1854 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1855 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001856 int opcode = 0;
1857 switch (opsize) {
1858 case k32:
1859 opcode = kX86PsradRI;
1860 break;
1861 case kSignedHalf:
1862 case kUnsignedHalf:
1863 opcode = kX86PsrawRI;
1864 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001865 case kSignedByte:
1866 case kUnsignedByte:
1867 GenShiftByteVector(bb, mir);
1868 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001869 default:
1870 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
1871 break;
1872 }
1873 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1874}
1875
1876void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001877 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1878 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1879 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1880 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001881 int opcode = 0;
1882 switch (opsize) {
1883 case k32:
1884 opcode = kX86PsrldRI;
1885 break;
1886 case k64:
1887 opcode = kX86PsrlqRI;
1888 break;
1889 case kSignedHalf:
1890 case kUnsignedHalf:
1891 opcode = kX86PsrlwRI;
1892 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001893 case kSignedByte:
1894 case kUnsignedByte:
1895 GenShiftByteVector(bb, mir);
1896 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001897 default:
1898 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1899 break;
1900 }
1901 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1902}
1903
1904void X86Mir2Lir::GenAndVector(BasicBlock *bb, MIR *mir) {
1905 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001906 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1907 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1908 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001909 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1910}
1911
1912void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
1913 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001914 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1915 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1916 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001917 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1918}
1919
1920void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
1921 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001922 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1923 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1924 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001925 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1926}
1927
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001928void X86Mir2Lir::AndMaskVectorRegister(RegStorage rs_src1, uint32_t m1, uint32_t m2, uint32_t m3, uint32_t m4) {
1929 MaskVectorRegister(kX86PandRM, rs_src1, m1, m2, m3, m4);
1930}
1931
1932void X86Mir2Lir::MaskVectorRegister(X86OpCode opcode, RegStorage rs_src1, uint32_t m0, uint32_t m1, uint32_t m2, uint32_t m3) {
1933 // Create temporary MIR as container for 128-bit binary mask.
1934 MIR const_mir;
1935 MIR* const_mirp = &const_mir;
1936 const_mirp->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpConstVector);
1937 const_mirp->dalvikInsn.arg[0] = m0;
1938 const_mirp->dalvikInsn.arg[1] = m1;
1939 const_mirp->dalvikInsn.arg[2] = m2;
1940 const_mirp->dalvikInsn.arg[3] = m3;
1941
1942 // Mask vector with const from literal pool.
1943 AppendOpcodeWithConst(opcode, rs_src1.GetReg(), const_mirp);
1944}
1945
Mark Mendellfe945782014-05-22 09:52:36 -04001946void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001947 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1948 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1949 RegLocation rl_dest = mir_graph_->GetDest(mir);
1950 RegStorage rs_tmp;
1951
1952 int vec_bytes = (mir->dalvikInsn.vC & 0xFFFF) / 8;
1953 int vec_unit_size = 0;
Mark Mendellfe945782014-05-22 09:52:36 -04001954 int opcode = 0;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001955 int extr_opcode = 0;
1956 RegLocation rl_result;
1957
Mark Mendellfe945782014-05-22 09:52:36 -04001958 switch (opsize) {
1959 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001960 extr_opcode = kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001961 opcode = kX86PhadddRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001962 vec_unit_size = 4;
1963 break;
1964 case kSignedByte:
1965 case kUnsignedByte:
1966 extr_opcode = kX86PextrbRRI;
1967 opcode = kX86PhaddwRR;
1968 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001969 break;
1970 case kSignedHalf:
1971 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001972 extr_opcode = kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001973 opcode = kX86PhaddwRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001974 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001975 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001976 case kSingle:
1977 rl_result = EvalLoc(rl_dest, kFPReg, true);
1978 vec_unit_size = 4;
1979 for (int i = 0; i < 3; i++) {
1980 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
1981 NewLIR3(kX86ShufpsRRI, rs_src1.GetReg(), rs_src1.GetReg(), 0x39);
1982 }
1983 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
1984 StoreValue(rl_dest, rl_result);
1985
1986 // For single-precision floats, we are done here
1987 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001988 default:
1989 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
1990 break;
1991 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001992
1993 int elems = vec_bytes / vec_unit_size;
1994
1995 // Emulate horizontal add instruction by reducing 2 vectors with 8 values before adding them again
1996 // TODO is overflow handled correctly?
1997 if (opsize == kSignedByte || opsize == kUnsignedByte) {
1998 rs_tmp = Get128BitRegister(AllocTempWide());
1999
2000 // tmp = xmm1 .>> 8.
2001 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_src1.GetReg());
2002 NewLIR2(kX86PsrlwRI, rs_tmp.GetReg(), 8);
2003
2004 // Zero extend low bits in xmm1.
2005 AndMaskVectorRegister(rs_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
2006 }
2007
2008 while (elems > 1) {
2009 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2010 NewLIR2(opcode, rs_tmp.GetReg(), rs_tmp.GetReg());
2011 }
2012 NewLIR2(opcode, rs_src1.GetReg(), rs_src1.GetReg());
2013 elems >>= 1;
2014 }
2015
2016 // Combine the results if we separated them.
2017 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2018 NewLIR2(kX86PaddbRR, rs_src1.GetReg(), rs_tmp.GetReg());
2019 }
2020
2021 // We need to extract to a GPR.
2022 RegStorage temp = AllocTemp();
2023 NewLIR3(extr_opcode, temp.GetReg(), rs_src1.GetReg(), 0);
2024
2025 // Can we do this directly into memory?
2026 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2027 if (rl_result.location == kLocPhysReg) {
2028 // Ensure res is in a core reg
2029 rl_result = EvalLoc(rl_dest, kCoreReg, true);
2030 OpRegReg(kOpAdd, rl_result.reg, temp);
2031 StoreFinalValue(rl_dest, rl_result);
2032 } else {
2033 OpMemReg(kOpAdd, rl_result, temp.GetReg());
2034 }
2035
2036 FreeTemp(temp);
Mark Mendellfe945782014-05-22 09:52:36 -04002037}
2038
2039void X86Mir2Lir::GenReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002040 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2041 RegLocation rl_dest = mir_graph_->GetDest(mir);
2042 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
2043 int extract_index = mir->dalvikInsn.arg[0];
2044 int extr_opcode = 0;
2045 RegLocation rl_result;
2046 bool is_wide = false;
2047
Mark Mendellfe945782014-05-22 09:52:36 -04002048 switch (opsize) {
2049 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002050 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2051 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrdMRI : kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002052 break;
2053 case kSignedHalf:
2054 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002055 rl_result= UpdateLocTyped(rl_dest, kCoreReg);
2056 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrwMRI : kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002057 break;
2058 default:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002059 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2060 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002061 break;
2062 }
Mark Mendellfe945782014-05-22 09:52:36 -04002063
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002064 if (rl_result.location == kLocPhysReg) {
2065 NewLIR3(extr_opcode, rl_result.reg.GetReg(), rs_src1.GetReg(), extract_index);
2066 if (is_wide == true) {
2067 StoreFinalValue(rl_dest, rl_result);
2068 } else {
2069 StoreFinalValueWide(rl_dest, rl_result);
2070 }
2071 } else {
2072 int displacement = SRegOffset(rl_result.s_reg_low);
2073 LIR *l = NewLIR3(extr_opcode, rs_rX86_SP.GetReg(), displacement, rs_src1.GetReg());
2074 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is_wide /* is_64bit */);
2075 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is_wide /* is_64bit */);
2076 }
Mark Mendellfe945782014-05-22 09:52:36 -04002077}
2078
2079void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002080 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2081 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2082 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
2083 int op_low = 0, op_high = 0, imm = 0, op_mov = kX86MovdxrRR;
2084 RegisterClass reg_type = kCoreReg;
2085
Mark Mendellfe945782014-05-22 09:52:36 -04002086 switch (opsize) {
2087 case k32:
2088 op_low = kX86PshufdRRI;
2089 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002090 case kSingle:
2091 op_low = kX86PshufdRRI;
2092 op_mov = kX86Mova128RR;
2093 reg_type = kFPReg;
2094 break;
2095 case k64:
2096 op_low = kX86PshufdRRI;
2097 imm = 0x44;
2098 break;
2099 case kDouble:
2100 op_low = kX86PshufdRRI;
2101 op_mov = kX86Mova128RR;
2102 reg_type = kFPReg;
2103 imm = 0x44;
2104 break;
2105 case kSignedByte:
2106 case kUnsignedByte:
2107 // Shuffle 8 bit value into 16 bit word.
2108 // We set val = val + (val << 8) below and use 16 bit shuffle.
Mark Mendellfe945782014-05-22 09:52:36 -04002109 case kSignedHalf:
2110 case kUnsignedHalf:
2111 // Handles low quadword.
2112 op_low = kX86PshuflwRRI;
2113 // Handles upper quadword.
2114 op_high = kX86PshufdRRI;
2115 break;
2116 default:
2117 LOG(FATAL) << "Unsupported vector set " << opsize;
2118 break;
2119 }
2120
Mark Mendellfe945782014-05-22 09:52:36 -04002121 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002122
2123 // Load the value from the VR into the reg.
2124 if (rl_src.wide == 0) {
2125 rl_src = LoadValue(rl_src, reg_type);
2126 } else {
2127 rl_src = LoadValueWide(rl_src, reg_type);
2128 }
2129
2130 // If opsize is 8 bits wide then double value and use 16 bit shuffle instead.
2131 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2132 RegStorage temp = AllocTemp();
2133 // val = val + (val << 8).
2134 NewLIR2(kX86Mov32RR, temp.GetReg(), rl_src.reg.GetReg());
2135 NewLIR2(kX86Sal32RI, temp.GetReg(), 8);
2136 NewLIR2(kX86Or32RR, rl_src.reg.GetReg(), temp.GetReg());
2137 FreeTemp(temp);
2138 }
Mark Mendellfe945782014-05-22 09:52:36 -04002139
2140 // Load the value into the XMM register.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002141 NewLIR2(op_mov, rs_dest.GetReg(), rl_src.reg.GetReg());
Mark Mendellfe945782014-05-22 09:52:36 -04002142
2143 // Now shuffle the value across the destination.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002144 NewLIR3(op_low, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002145
2146 // And then repeat as needed.
2147 if (op_high != 0) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002148 NewLIR3(op_high, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002149 }
2150}
2151
Mark Mendelld65c51a2014-04-29 16:55:20 -04002152LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
2153 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2154 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
2155 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
2156 args[2] == p->operands[2] && args[3] == p->operands[3]) {
2157 return p;
2158 }
2159 }
2160 return nullptr;
2161}
2162
2163LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
2164 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
2165 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2166 new_value->operands[0] = args[0];
2167 new_value->operands[1] = args[1];
2168 new_value->operands[2] = args[2];
2169 new_value->operands[3] = args[3];
2170 new_value->next = const_vectors_;
2171 if (const_vectors_ == nullptr) {
2172 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
2173 }
2174 estimated_native_code_size_ += 16; // Space for one vector.
2175 const_vectors_ = new_value;
2176 return new_value;
2177}
2178
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002179// ------------ ABI support: mapping of args to physical registers -------------
Andreas Gampeccc60262014-07-04 18:02:38 -07002180RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide,
2181 bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002182 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
Andreas Gampeccc60262014-07-04 18:02:38 -07002183 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) /
2184 sizeof(SpecialTargetRegister);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002185 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
Andreas Gampeccc60262014-07-04 18:02:38 -07002186 kFArg4, kFArg5, kFArg6, kFArg7};
2187 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) /
2188 sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002189
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002190 if (is_double_or_float) {
2191 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002192 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide ? kWide : kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002193 }
2194 } else {
2195 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002196 return ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++],
2197 is_ref ? kRef : (is_wide ? kWide : kNotWide));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002198 }
2199 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07002200 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002201}
2202
2203RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
2204 DCHECK(IsInitialized());
2205 auto res = mapping_.find(in_position);
2206 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
2207}
2208
Andreas Gampeccc60262014-07-04 18:02:38 -07002209void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count,
2210 InToRegStorageMapper* mapper) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002211 DCHECK(mapper != nullptr);
2212 max_mapped_in_ = -1;
2213 is_there_stack_mapped_ = false;
2214 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07002215 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
2216 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002217 if (reg.Valid()) {
2218 mapping_[in_position] = reg;
2219 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07002220 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002221 // We covered 2 args, so skip the next one
2222 in_position++;
2223 }
2224 } else {
2225 is_there_stack_mapped_ = true;
2226 }
2227 }
2228 initialized_ = true;
2229}
2230
2231RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002232 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002233 return GetCoreArgMappingToPhysicalReg(arg_num);
2234 }
2235
2236 if (!in_to_reg_storage_mapping_.IsInitialized()) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002237 int start_vreg = cu_->mir_graph->GetFirstInVR();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002238 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
2239
Chao-ying Fua77ee512014-07-01 17:43:41 -07002240 InToRegStorageX86_64Mapper mapper(this);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002241 in_to_reg_storage_mapping_.Initialize(arg_locs, mir_graph_->GetNumOfInVRs(), &mapper);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002242 }
2243 return in_to_reg_storage_mapping_.Get(arg_num);
2244}
2245
2246RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
2247 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
2248 // Not used for 64-bit, TODO: Move X86_32 to the same framework
2249 switch (core_arg_num) {
2250 case 0:
2251 return rs_rX86_ARG1;
2252 case 1:
2253 return rs_rX86_ARG2;
2254 case 2:
2255 return rs_rX86_ARG3;
2256 default:
2257 return RegStorage::InvalidReg();
2258 }
2259}
2260
2261// ---------End of ABI support: mapping of args to physical registers -------------
2262
2263/*
2264 * If there are any ins passed in registers that have not been promoted
2265 * to a callee-save register, flush them to the frame. Perform initial
2266 * assignment of promoted arguments.
2267 *
2268 * ArgLocs is an array of location records describing the incoming arguments
2269 * with one location record per word of argument.
2270 */
2271void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002272 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002273 /*
2274 * Dummy up a RegLocation for the incoming Method*
2275 * It will attempt to keep kArg0 live (or copy it to home location
2276 * if promoted).
2277 */
2278
2279 RegLocation rl_src = rl_method;
2280 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -07002281 rl_src.reg = TargetReg(kArg0, kRef);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002282 rl_src.home = false;
2283 MarkLive(rl_src);
2284 StoreValue(rl_method, rl_src);
2285 // If Method* has been promoted, explicitly flush
2286 if (rl_method.location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002287 StoreRefDisp(rs_rX86_SP, 0, As32BitReg(TargetReg(kArg0, kRef)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002288 }
2289
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002290 if (mir_graph_->GetNumOfInVRs() == 0) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002291 return;
2292 }
2293
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002294 int start_vreg = cu_->mir_graph->GetFirstInVR();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002295 /*
2296 * Copy incoming arguments to their proper home locations.
2297 * NOTE: an older version of dx had an issue in which
2298 * it would reuse static method argument registers.
2299 * This could result in the same Dalvik virtual register
2300 * being promoted to both core and fp regs. To account for this,
2301 * we only copy to the corresponding promoted physical register
2302 * if it matches the type of the SSA name for the incoming
2303 * argument. It is also possible that long and double arguments
2304 * end up half-promoted. In those cases, we must flush the promoted
2305 * half to memory as well.
2306 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002307 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002308 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002309 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002310 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002311
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002312 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002313 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002314 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002315
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002316 // We have already updated the arg location with promoted info
2317 // so we can be based on it.
2318 if (t_loc->location == kLocPhysReg) {
2319 // Just copy it.
2320 OpRegCopy(t_loc->reg, reg);
2321 } else {
2322 // Needs flush.
2323 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002324 StoreRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002325 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002326 StoreBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002327 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002328 }
2329 }
2330 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002331 // If arriving in frame & promoted.
2332 if (t_loc->location == kLocPhysReg) {
2333 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002334 LoadRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002335 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002336 LoadBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002337 t_loc->wide ? k64 : k32, kNotVolatile);
2338 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002339 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002340 }
2341 if (t_loc->wide) {
2342 // Increment i to skip the next one.
2343 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002344 }
2345 }
2346}
2347
2348/*
2349 * Load up to 5 arguments, the first three of which will be in
2350 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2351 * and as part of the load sequence, it must be replaced with
2352 * the target method pointer. Note, this may also be called
2353 * for "range" variants if the number of arguments is 5 or fewer.
2354 */
2355int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2356 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2357 const MethodReference& target_method,
2358 uint32_t vtable_idx, uintptr_t direct_code,
2359 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002360 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002361 return Mir2Lir::GenDalvikArgsNoRange(info,
2362 call_state, pcrLabel, next_call_insn,
2363 target_method,
2364 vtable_idx, direct_code,
2365 direct_method, type, skip_this);
2366 }
2367 return GenDalvikArgsRange(info,
2368 call_state, pcrLabel, next_call_insn,
2369 target_method,
2370 vtable_idx, direct_code,
2371 direct_method, type, skip_this);
2372}
2373
2374/*
2375 * May have 0+ arguments (also used for jumbo). Note that
2376 * source virtual registers may be in physical registers, so may
2377 * need to be flushed to home location before copying. This
2378 * applies to arg3 and above (see below).
2379 *
2380 * Two general strategies:
2381 * If < 20 arguments
2382 * Pass args 3-18 using vldm/vstm block copy
2383 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2384 * If 20+ arguments
2385 * Pass args arg19+ using memcpy block copy
2386 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2387 *
2388 */
2389int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2390 LIR** pcrLabel, NextCallInsn next_call_insn,
2391 const MethodReference& target_method,
2392 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2393 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002394 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002395 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2396 pcrLabel, next_call_insn,
2397 target_method,
2398 vtable_idx, direct_code, direct_method,
2399 type, skip_this);
2400 }
2401
2402 /* If no arguments, just return */
2403 if (info->num_arg_words == 0)
2404 return call_state;
2405
2406 const int start_index = skip_this ? 1 : 0;
2407
Chao-ying Fua77ee512014-07-01 17:43:41 -07002408 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002409 InToRegStorageMapping in_to_reg_storage_mapping;
2410 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2411 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2412 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
Serguei Katkov8e3acdd2014-07-15 12:01:00 +07002413 info->args[last_mapped_in].wide ? 2 : 1;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002414 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2415
2416 // Fisrt of all, check whether it make sense to use bulk copying
2417 // Optimization is aplicable only for range case
2418 // TODO: make a constant instead of 2
2419 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2420 // Scan the rest of the args - if in phys_reg flush to memory
2421 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2422 RegLocation loc = info->args[next_arg];
2423 if (loc.wide) {
2424 loc = UpdateLocWide(loc);
2425 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002426 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002427 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002428 }
2429 next_arg += 2;
2430 } else {
2431 loc = UpdateLoc(loc);
2432 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002433 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002434 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002435 }
2436 next_arg++;
2437 }
2438 }
2439
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002440 // The rest can be copied together
2441 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
Andreas Gampeccc60262014-07-04 18:02:38 -07002442 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped,
2443 cu_->instruction_set);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002444
2445 int current_src_offset = start_offset;
2446 int current_dest_offset = outs_offset;
2447
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002448 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2449 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002450 while (regs_left_to_pass_via_stack > 0) {
2451 // This is based on the knowledge that the stack itself is 16-byte aligned.
2452 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2453 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2454 size_t bytes_to_move;
2455
2456 /*
2457 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2458 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2459 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2460 * We do this because we could potentially do a smaller move to align.
2461 */
2462 if (regs_left_to_pass_via_stack == 4 ||
2463 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2464 // Moving 128-bits via xmm register.
2465 bytes_to_move = sizeof(uint32_t) * 4;
2466
2467 // Allocate a free xmm temp. Since we are working through the calling sequence,
2468 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2469 // there are no free registers.
2470 RegStorage temp = AllocTempDouble();
2471
2472 LIR* ld1 = nullptr;
2473 LIR* ld2 = nullptr;
2474 LIR* st1 = nullptr;
2475 LIR* st2 = nullptr;
2476
2477 /*
2478 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2479 * do an aligned move. If we have 8-byte alignment, then do the move in two
2480 * parts. This approach prevents possible cache line splits. Finally, fall back
2481 * to doing an unaligned move. In most cases we likely won't split the cache
2482 * line but we cannot prove it and thus take a conservative approach.
2483 */
2484 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2485 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2486
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002487 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002488 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002489 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002490 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002491 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovLo128FP);
2492 ld2 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002493 kMovHi128FP);
2494 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002495 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002496 }
2497
2498 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002499 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002500 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002501 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovLo128FP);
2502 st2 = OpMovMemReg(rs_rX86_SP, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002503 temp, kMovHi128FP);
2504 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002505 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002506 }
2507
2508 // TODO If we could keep track of aliasing information for memory accesses that are wider
2509 // than 64-bit, we wouldn't need to set up a barrier.
2510 if (ld1 != nullptr) {
2511 if (ld2 != nullptr) {
2512 // For 64-bit load we can actually set up the aliasing information.
2513 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2514 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2515 } else {
2516 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002517 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002518 }
2519 }
2520 if (st1 != nullptr) {
2521 if (st2 != nullptr) {
2522 // For 64-bit store we can actually set up the aliasing information.
2523 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2524 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2525 } else {
2526 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002527 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002528 }
2529 }
2530
2531 // Free the temporary used for the data movement.
2532 FreeTemp(temp);
2533 } else {
2534 // Moving 32-bits via general purpose register.
2535 bytes_to_move = sizeof(uint32_t);
2536
2537 // Instead of allocating a new temp, simply reuse one of the registers being used
2538 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07002539 RegStorage temp = TargetReg(kArg3, kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002540
2541 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002542 Load32Disp(rs_rX86_SP, current_src_offset, temp);
2543 Store32Disp(rs_rX86_SP, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002544 }
2545
2546 current_src_offset += bytes_to_move;
2547 current_dest_offset += bytes_to_move;
2548 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2549 }
2550 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2551 }
2552
2553 // Now handle rest not registers if they are
2554 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002555 RegStorage regSingle = TargetReg(kArg2, kNotWide);
2556 RegStorage regWide = TargetReg(kArg3, kWide);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002557 for (int i = start_index;
2558 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002559 RegLocation rl_arg = info->args[i];
2560 rl_arg = UpdateRawLoc(rl_arg);
2561 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2562 if (!reg.Valid()) {
2563 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2564
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002565 {
2566 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2567 if (rl_arg.wide) {
2568 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002569 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002570 } else {
2571 LoadValueDirectWideFixed(rl_arg, regWide);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002572 StoreBaseDisp(rs_rX86_SP, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002573 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002574 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002575 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002576 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002577 } else {
2578 LoadValueDirectFixed(rl_arg, regSingle);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002579 StoreBaseDisp(rs_rX86_SP, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002580 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002581 }
2582 }
2583 call_state = next_call_insn(cu_, info, call_state, target_method,
2584 vtable_idx, direct_code, direct_method, type);
2585 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002586 if (rl_arg.wide) {
2587 i++;
2588 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002589 }
2590 }
2591
2592 // Finish with mapped registers
2593 for (int i = start_index; i <= last_mapped_in; i++) {
2594 RegLocation rl_arg = info->args[i];
2595 rl_arg = UpdateRawLoc(rl_arg);
2596 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2597 if (reg.Valid()) {
2598 if (rl_arg.wide) {
2599 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002600 } else {
2601 LoadValueDirectFixed(rl_arg, reg);
2602 }
2603 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2604 direct_code, direct_method, type);
2605 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002606 if (rl_arg.wide) {
2607 i++;
2608 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002609 }
2610
2611 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2612 direct_code, direct_method, type);
2613 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00002614 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002615 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002616 } else {
2617 *pcrLabel = nullptr;
2618 // In lieu of generating a check for kArg1 being null, we need to
2619 // perform a load when doing implicit checks.
2620 RegStorage tmp = AllocTemp();
Andreas Gampeccc60262014-07-04 18:02:38 -07002621 Load32Disp(TargetReg(kArg1, kRef), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002622 MarkPossibleNullPointerException(info->opt_flags);
2623 FreeTemp(tmp);
2624 }
2625 }
2626 return call_state;
2627}
2628
Andreas Gampe98430592014-07-27 19:44:50 -07002629bool X86Mir2Lir::GenInlinedCharAt(CallInfo* info) {
2630 // Location of reference to data array
2631 int value_offset = mirror::String::ValueOffset().Int32Value();
2632 // Location of count
2633 int count_offset = mirror::String::CountOffset().Int32Value();
2634 // Starting offset within data array
2635 int offset_offset = mirror::String::OffsetOffset().Int32Value();
2636 // Start of char data with array_
2637 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
2638
2639 RegLocation rl_obj = info->args[0];
2640 RegLocation rl_idx = info->args[1];
2641 rl_obj = LoadValue(rl_obj, kRefReg);
2642 // X86 wants to avoid putting a constant index into a register.
2643 if (!rl_idx.is_const) {
2644 rl_idx = LoadValue(rl_idx, kCoreReg);
2645 }
2646 RegStorage reg_max;
2647 GenNullCheck(rl_obj.reg, info->opt_flags);
2648 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
2649 LIR* range_check_branch = nullptr;
2650 RegStorage reg_off;
2651 RegStorage reg_ptr;
2652 if (range_check) {
2653 // On x86, we can compare to memory directly
2654 // Set up a launch pad to allow retry in case of bounds violation */
2655 if (rl_idx.is_const) {
2656 LIR* comparison;
2657 range_check_branch = OpCmpMemImmBranch(
2658 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
2659 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr, &comparison);
2660 MarkPossibleNullPointerExceptionAfter(0, comparison);
2661 } else {
2662 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
2663 MarkPossibleNullPointerException(0);
2664 range_check_branch = OpCondBranch(kCondUge, nullptr);
2665 }
2666 }
2667 reg_off = AllocTemp();
2668 reg_ptr = AllocTempRef();
2669 Load32Disp(rl_obj.reg, offset_offset, reg_off);
2670 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
2671 if (rl_idx.is_const) {
2672 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
2673 } else {
2674 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
2675 }
2676 FreeTemp(rl_obj.reg);
2677 if (rl_idx.location == kLocPhysReg) {
2678 FreeTemp(rl_idx.reg);
2679 }
2680 RegLocation rl_dest = InlineTarget(info);
2681 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
2682 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
2683 FreeTemp(reg_off);
2684 FreeTemp(reg_ptr);
2685 StoreValue(rl_dest, rl_result);
2686 if (range_check) {
2687 DCHECK(range_check_branch != nullptr);
2688 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
2689 AddIntrinsicSlowPath(info, range_check_branch);
2690 }
2691 return true;
2692}
2693
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002694bool X86Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
2695 RegLocation rl_dest = InlineTarget(info);
2696
2697 // Early exit if the result is unused.
2698 if (rl_dest.orig_sreg < 0) {
2699 return true;
2700 }
2701
2702 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
2703
2704 if (cu_->target64) {
2705 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<8>());
2706 } else {
2707 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<4>());
2708 }
2709
2710 StoreValue(rl_dest, rl_result);
2711 return true;
2712}
2713
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002714} // namespace art