blob: 089495ee37290891ac5f2a9524233c49b0fec6a5 [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
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "codegen_x86.h"
21#include "dex/compiler_internals.h"
22#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070023#include "dex/reg_storage_eq.h"
Mark Mendelle19c91f2014-02-25 08:19:08 -080024#include "mirror/array.h"
25#include "mirror/string.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "x86_lir.h"
Tong Shen547cdfd2014-08-05 01:54:19 -070027#include "utils/dwarf_cfi.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
Brian Carlstrom7940e442013-07-12 13:46:57 -070029namespace art {
30
Vladimir Marko089142c2014-06-05 10:57:05 +010031static constexpr RegStorage core_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070032 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
33};
Vladimir Marko089142c2014-06-05 10:57:05 +010034static constexpr RegStorage core_regs_arr_64[] = {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070035 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
buzbee091cc402014-03-31 10:14:40 -070036 rs_r8, rs_r9, rs_r10, rs_r11, rs_r12, rs_r13, rs_r14, rs_r15
Brian Carlstrom7940e442013-07-12 13:46:57 -070037};
Vladimir Marko089142c2014-06-05 10:57:05 +010038static constexpr RegStorage core_regs_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070039 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 +070040 rs_r8q, rs_r9q, rs_r10q, rs_r11q, rs_r12q, rs_r13q, rs_r14q, rs_r15q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070041};
Vladimir Marko089142c2014-06-05 10:57:05 +010042static constexpr RegStorage sp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070043 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
44};
Vladimir Marko089142c2014-06-05 10:57:05 +010045static constexpr RegStorage sp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070046 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
buzbee091cc402014-03-31 10:14:40 -070047 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070048};
Vladimir Marko089142c2014-06-05 10:57:05 +010049static constexpr RegStorage dp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070050 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
51};
Vladimir Marko089142c2014-06-05 10:57:05 +010052static constexpr RegStorage dp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070053 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
buzbee091cc402014-03-31 10:14:40 -070054 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070055};
Serguei Katkovc3801912014-07-08 17:21:53 +070056static constexpr RegStorage xp_regs_arr_32[] = {
57 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
58};
59static constexpr RegStorage xp_regs_arr_64[] = {
60 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
61 rs_xr8, rs_xr9, rs_xr10, rs_xr11, rs_xr12, rs_xr13, rs_xr14, rs_xr15
62};
Vladimir Marko089142c2014-06-05 10:57:05 +010063static constexpr RegStorage reserved_regs_arr_32[] = {rs_rX86_SP_32};
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070064static constexpr RegStorage reserved_regs_arr_64[] = {rs_rX86_SP_32};
Vladimir Marko089142c2014-06-05 10:57:05 +010065static constexpr RegStorage reserved_regs_arr_64q[] = {rs_rX86_SP_64};
66static constexpr RegStorage core_temps_arr_32[] = {rs_rAX, rs_rCX, rs_rDX, rs_rBX};
67static constexpr RegStorage core_temps_arr_64[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070068 rs_rAX, rs_rCX, rs_rDX, rs_rSI, rs_rDI,
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070069 rs_r8, rs_r9, rs_r10, rs_r11
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070070};
Serguei Katkovc3801912014-07-08 17:21:53 +070071
72// How to add register to be available for promotion:
73// 1) Remove register from array defining temp
74// 2) Update ClobberCallerSave
75// 3) Update JNI compiler ABI:
76// 3.1) add reg in JniCallingConvention method
77// 3.2) update CoreSpillMask/FpSpillMask
78// 4) Update entrypoints
79// 4.1) Update constants in asm_support_x86_64.h for new frame size
80// 4.2) Remove entry in SmashCallerSaves
81// 4.3) Update jni_entrypoints to spill/unspill new callee save reg
82// 4.4) Update quick_entrypoints to spill/unspill new callee save reg
83// 5) Update runtime ABI
84// 5.1) Update quick_method_frame_info with new required spills
85// 5.2) Update QuickArgumentVisitor with new offsets to gprs and xmms
86// Note that you cannot use register corresponding to incoming args
87// according to ABI and QCG needs one additional XMM temp for
88// bulk copy in preparation to call.
Vladimir Marko089142c2014-06-05 10:57:05 +010089static constexpr RegStorage core_temps_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070090 rs_r0q, rs_r1q, rs_r2q, rs_r6q, rs_r7q,
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070091 rs_r8q, rs_r9q, rs_r10q, rs_r11q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070092};
Vladimir Marko089142c2014-06-05 10:57:05 +010093static constexpr RegStorage sp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070094 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
95};
Vladimir Marko089142c2014-06-05 10:57:05 +010096static constexpr RegStorage sp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070097 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
Serguei Katkovc3801912014-07-08 17:21:53 +070098 rs_fr8, rs_fr9, rs_fr10, rs_fr11
buzbee091cc402014-03-31 10:14:40 -070099};
Vladimir Marko089142c2014-06-05 10:57:05 +0100100static constexpr RegStorage dp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700101 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
102};
Vladimir Marko089142c2014-06-05 10:57:05 +0100103static constexpr RegStorage dp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -0700104 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700105 rs_dr8, rs_dr9, rs_dr10, rs_dr11
buzbee091cc402014-03-31 10:14:40 -0700106};
107
Vladimir Marko089142c2014-06-05 10:57:05 +0100108static constexpr RegStorage xp_temps_arr_32[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400109 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
110};
Vladimir Marko089142c2014-06-05 10:57:05 +0100111static constexpr RegStorage xp_temps_arr_64[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400112 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700113 rs_xr8, rs_xr9, rs_xr10, rs_xr11
Mark Mendellfe945782014-05-22 09:52:36 -0400114};
115
Vladimir Marko089142c2014-06-05 10:57:05 +0100116static constexpr ArrayRef<const RegStorage> empty_pool;
117static constexpr ArrayRef<const RegStorage> core_regs_32(core_regs_arr_32);
118static constexpr ArrayRef<const RegStorage> core_regs_64(core_regs_arr_64);
119static constexpr ArrayRef<const RegStorage> core_regs_64q(core_regs_arr_64q);
120static constexpr ArrayRef<const RegStorage> sp_regs_32(sp_regs_arr_32);
121static constexpr ArrayRef<const RegStorage> sp_regs_64(sp_regs_arr_64);
122static constexpr ArrayRef<const RegStorage> dp_regs_32(dp_regs_arr_32);
123static constexpr ArrayRef<const RegStorage> dp_regs_64(dp_regs_arr_64);
Serguei Katkovc3801912014-07-08 17:21:53 +0700124static constexpr ArrayRef<const RegStorage> xp_regs_32(xp_regs_arr_32);
125static constexpr ArrayRef<const RegStorage> xp_regs_64(xp_regs_arr_64);
Vladimir Marko089142c2014-06-05 10:57:05 +0100126static constexpr ArrayRef<const RegStorage> reserved_regs_32(reserved_regs_arr_32);
127static constexpr ArrayRef<const RegStorage> reserved_regs_64(reserved_regs_arr_64);
128static constexpr ArrayRef<const RegStorage> reserved_regs_64q(reserved_regs_arr_64q);
129static constexpr ArrayRef<const RegStorage> core_temps_32(core_temps_arr_32);
130static constexpr ArrayRef<const RegStorage> core_temps_64(core_temps_arr_64);
131static constexpr ArrayRef<const RegStorage> core_temps_64q(core_temps_arr_64q);
132static constexpr ArrayRef<const RegStorage> sp_temps_32(sp_temps_arr_32);
133static constexpr ArrayRef<const RegStorage> sp_temps_64(sp_temps_arr_64);
134static constexpr ArrayRef<const RegStorage> dp_temps_32(dp_temps_arr_32);
135static constexpr ArrayRef<const RegStorage> dp_temps_64(dp_temps_arr_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700136
Vladimir Marko089142c2014-06-05 10:57:05 +0100137static constexpr ArrayRef<const RegStorage> xp_temps_32(xp_temps_arr_32);
138static constexpr ArrayRef<const RegStorage> xp_temps_64(xp_temps_arr_64);
Mark Mendellfe945782014-05-22 09:52:36 -0400139
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700140RegStorage rs_rX86_SP;
141
142X86NativeRegisterPool rX86_ARG0;
143X86NativeRegisterPool rX86_ARG1;
144X86NativeRegisterPool rX86_ARG2;
145X86NativeRegisterPool rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700146X86NativeRegisterPool rX86_ARG4;
147X86NativeRegisterPool rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700148X86NativeRegisterPool rX86_FARG0;
149X86NativeRegisterPool rX86_FARG1;
150X86NativeRegisterPool rX86_FARG2;
151X86NativeRegisterPool rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700152X86NativeRegisterPool rX86_FARG4;
153X86NativeRegisterPool rX86_FARG5;
154X86NativeRegisterPool rX86_FARG6;
155X86NativeRegisterPool rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700156X86NativeRegisterPool rX86_RET0;
157X86NativeRegisterPool rX86_RET1;
158X86NativeRegisterPool rX86_INVOKE_TGT;
159X86NativeRegisterPool rX86_COUNT;
160
161RegStorage rs_rX86_ARG0;
162RegStorage rs_rX86_ARG1;
163RegStorage rs_rX86_ARG2;
164RegStorage rs_rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700165RegStorage rs_rX86_ARG4;
166RegStorage rs_rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700167RegStorage rs_rX86_FARG0;
168RegStorage rs_rX86_FARG1;
169RegStorage rs_rX86_FARG2;
170RegStorage rs_rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700171RegStorage rs_rX86_FARG4;
172RegStorage rs_rX86_FARG5;
173RegStorage rs_rX86_FARG6;
174RegStorage rs_rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700175RegStorage rs_rX86_RET0;
176RegStorage rs_rX86_RET1;
177RegStorage rs_rX86_INVOKE_TGT;
178RegStorage rs_rX86_COUNT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700180RegLocation X86Mir2Lir::LocCReturn() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000181 return x86_loc_c_return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182}
183
buzbeea0cd2d72014-06-01 09:33:49 -0700184RegLocation X86Mir2Lir::LocCReturnRef() {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700185 return cu_->target64 ? x86_64_loc_c_return_ref : x86_loc_c_return_ref;
buzbeea0cd2d72014-06-01 09:33:49 -0700186}
187
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700188RegLocation X86Mir2Lir::LocCReturnWide() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700189 return cu_->target64 ? x86_64_loc_c_return_wide : x86_loc_c_return_wide;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190}
191
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700192RegLocation X86Mir2Lir::LocCReturnFloat() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000193 return x86_loc_c_return_float;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194}
195
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700196RegLocation X86Mir2Lir::LocCReturnDouble() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000197 return x86_loc_c_return_double;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198}
199
Chao-ying Fua77ee512014-07-01 17:43:41 -0700200// Return a target-dependent special register for 32-bit.
201RegStorage X86Mir2Lir::TargetReg32(SpecialTargetRegister reg) {
buzbee091cc402014-03-31 10:14:40 -0700202 RegStorage res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 switch (reg) {
buzbee091cc402014-03-31 10:14:40 -0700204 case kSelf: res_reg = RegStorage::InvalidReg(); break;
205 case kSuspend: res_reg = RegStorage::InvalidReg(); break;
206 case kLr: res_reg = RegStorage::InvalidReg(); break;
207 case kPc: res_reg = RegStorage::InvalidReg(); break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700208 case kSp: res_reg = rs_rX86_SP_32; break; // This must be the concrete one, as _SP is target-
209 // specific size.
buzbee091cc402014-03-31 10:14:40 -0700210 case kArg0: res_reg = rs_rX86_ARG0; break;
211 case kArg1: res_reg = rs_rX86_ARG1; break;
212 case kArg2: res_reg = rs_rX86_ARG2; break;
213 case kArg3: res_reg = rs_rX86_ARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700214 case kArg4: res_reg = rs_rX86_ARG4; break;
215 case kArg5: res_reg = rs_rX86_ARG5; break;
buzbee091cc402014-03-31 10:14:40 -0700216 case kFArg0: res_reg = rs_rX86_FARG0; break;
217 case kFArg1: res_reg = rs_rX86_FARG1; break;
218 case kFArg2: res_reg = rs_rX86_FARG2; break;
219 case kFArg3: res_reg = rs_rX86_FARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700220 case kFArg4: res_reg = rs_rX86_FARG4; break;
221 case kFArg5: res_reg = rs_rX86_FARG5; break;
222 case kFArg6: res_reg = rs_rX86_FARG6; break;
223 case kFArg7: res_reg = rs_rX86_FARG7; break;
buzbee091cc402014-03-31 10:14:40 -0700224 case kRet0: res_reg = rs_rX86_RET0; break;
225 case kRet1: res_reg = rs_rX86_RET1; break;
226 case kInvokeTgt: res_reg = rs_rX86_INVOKE_TGT; break;
227 case kHiddenArg: res_reg = rs_rAX; break;
Elena Sayapinadd644502014-07-01 18:39:52 +0700228 case kHiddenFpArg: DCHECK(!cu_->target64); res_reg = rs_fr0; break;
buzbee091cc402014-03-31 10:14:40 -0700229 case kCount: res_reg = rs_rX86_COUNT; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700230 default: res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 }
buzbee091cc402014-03-31 10:14:40 -0700232 return res_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233}
234
Chao-ying Fua77ee512014-07-01 17:43:41 -0700235RegStorage X86Mir2Lir::TargetReg(SpecialTargetRegister reg) {
236 LOG(FATAL) << "Do not use this function!!!";
237 return RegStorage::InvalidReg();
238}
239
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240/*
241 * Decode the register id.
242 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100243ResourceMask X86Mir2Lir::GetRegMaskCommon(const RegStorage& reg) const {
244 /* Double registers in x86 are just a single FP register. This is always just a single bit. */
245 return ResourceMask::Bit(
246 /* FP register starts at bit position 16 */
247 ((reg.IsFloat() || reg.StorageSize() > 8) ? kX86FPReg0 : 0) + reg.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248}
249
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100250ResourceMask X86Mir2Lir::GetPCUseDefEncoding() const {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100251 return kEncodeNone;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252}
253
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100254void X86Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags,
255 ResourceMask* use_mask, ResourceMask* def_mask) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700256 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbeeb48819d2013-09-14 16:15:25 -0700257 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258
259 // X86-specific resource map setup here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 if (flags & REG_USE_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100261 use_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 }
263
264 if (flags & REG_DEF_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100265 def_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 }
267
268 if (flags & REG_DEFA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100269 SetupRegMask(def_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 }
271
272 if (flags & REG_DEFD) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100273 SetupRegMask(def_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 }
275 if (flags & REG_USEA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100276 SetupRegMask(use_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 }
278
279 if (flags & REG_USEC) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100280 SetupRegMask(use_mask, rs_rCX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 }
282
283 if (flags & REG_USED) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100284 SetupRegMask(use_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 }
Vladimir Marko70b797d2013-12-03 15:25:24 +0000286
287 if (flags & REG_USEB) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100288 SetupRegMask(use_mask, rs_rBX.GetReg());
Vladimir Marko70b797d2013-12-03 15:25:24 +0000289 }
Mark Mendell4028a6c2014-02-19 20:06:20 -0800290
291 // Fixup hard to describe instruction: Uses rAX, rCX, rDI; sets rDI.
292 if (lir->opcode == kX86RepneScasw) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100293 SetupRegMask(use_mask, rs_rAX.GetReg());
294 SetupRegMask(use_mask, rs_rCX.GetReg());
295 SetupRegMask(use_mask, rs_rDI.GetReg());
296 SetupRegMask(def_mask, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800297 }
Serguei Katkove90501d2014-03-12 15:56:54 +0700298
299 if (flags & USE_FP_STACK) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100300 use_mask->SetBit(kX86FPStack);
301 def_mask->SetBit(kX86FPStack);
Serguei Katkove90501d2014-03-12 15:56:54 +0700302 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303}
304
305/* For dumping instructions */
306static const char* x86RegName[] = {
307 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
308 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
309};
310
311static const char* x86CondName[] = {
312 "O",
313 "NO",
314 "B/NAE/C",
315 "NB/AE/NC",
316 "Z/EQ",
317 "NZ/NE",
318 "BE/NA",
319 "NBE/A",
320 "S",
321 "NS",
322 "P/PE",
323 "NP/PO",
324 "L/NGE",
325 "NL/GE",
326 "LE/NG",
327 "NLE/G"
328};
329
330/*
331 * Interpret a format string and build a string no longer than size
332 * See format key in Assemble.cc.
333 */
334std::string X86Mir2Lir::BuildInsnString(const char *fmt, LIR *lir, unsigned char* base_addr) {
335 std::string buf;
336 size_t i = 0;
337 size_t fmt_len = strlen(fmt);
338 while (i < fmt_len) {
339 if (fmt[i] != '!') {
340 buf += fmt[i];
341 i++;
342 } else {
343 i++;
344 DCHECK_LT(i, fmt_len);
345 char operand_number_ch = fmt[i];
346 i++;
347 if (operand_number_ch == '!') {
348 buf += "!";
349 } else {
350 int operand_number = operand_number_ch - '0';
351 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
352 DCHECK_LT(i, fmt_len);
353 int operand = lir->operands[operand_number];
354 switch (fmt[i]) {
355 case 'c':
356 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
357 buf += x86CondName[operand];
358 break;
359 case 'd':
360 buf += StringPrintf("%d", operand);
361 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400362 case 'q': {
363 int64_t value = static_cast<int64_t>(static_cast<int64_t>(operand) << 32 |
364 static_cast<uint32_t>(lir->operands[operand_number+1]));
365 buf +=StringPrintf("%" PRId64, value);
Haitao Fenge70f1792014-08-09 08:31:02 +0800366 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400367 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 case 'p': {
buzbee0d829482013-10-11 15:24:55 -0700369 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(operand));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 buf += StringPrintf("0x%08x", tab_rec->offset);
371 break;
372 }
373 case 'r':
buzbee091cc402014-03-31 10:14:40 -0700374 if (RegStorage::IsFloat(operand)) {
375 int fp_reg = RegStorage::RegNum(operand);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 buf += StringPrintf("xmm%d", fp_reg);
377 } else {
buzbee091cc402014-03-31 10:14:40 -0700378 int reg_num = RegStorage::RegNum(operand);
379 DCHECK_LT(static_cast<size_t>(reg_num), sizeof(x86RegName));
380 buf += x86RegName[reg_num];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 }
382 break;
383 case 't':
Ian Rogers107c31e2014-01-23 20:55:29 -0800384 buf += StringPrintf("0x%08" PRIxPTR " (L%p)",
385 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + operand,
386 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 break;
388 default:
389 buf += StringPrintf("DecodeError '%c'", fmt[i]);
390 break;
391 }
392 i++;
393 }
394 }
395 }
396 return buf;
397}
398
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100399void X86Mir2Lir::DumpResourceMask(LIR *x86LIR, const ResourceMask& mask, const char *prefix) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 char buf[256];
401 buf[0] = 0;
402
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100403 if (mask.Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 strcpy(buf, "all");
405 } else {
406 char num[8];
407 int i;
408
409 for (i = 0; i < kX86RegEnd; i++) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100410 if (mask.HasBit(i)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800411 snprintf(num, arraysize(num), "%d ", i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 strcat(buf, num);
413 }
414 }
415
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100416 if (mask.HasBit(ResourceMask::kCCode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 strcat(buf, "cc ");
418 }
419 /* Memory bits */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100420 if (x86LIR && (mask.HasBit(ResourceMask::kDalvikReg))) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800421 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
422 DECODE_ALIAS_INFO_REG(x86LIR->flags.alias_info),
423 (DECODE_ALIAS_INFO_WIDE(x86LIR->flags.alias_info)) ? "(+1)" : "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100425 if (mask.HasBit(ResourceMask::kLiteral)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 strcat(buf, "lit ");
427 }
428
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100429 if (mask.HasBit(ResourceMask::kHeapRef)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 strcat(buf, "heap ");
431 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100432 if (mask.HasBit(ResourceMask::kMustNotAlias)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 strcat(buf, "noalias ");
434 }
435 }
436 if (buf[0]) {
437 LOG(INFO) << prefix << ": " << buf;
438 }
439}
440
441void X86Mir2Lir::AdjustSpillMask() {
442 // Adjustment for LR spilling, x86 has no LR so nothing to do here
buzbee091cc402014-03-31 10:14:40 -0700443 core_spill_mask_ |= (1 << rs_rRET.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 num_core_spills_++;
445}
446
Mark Mendelle87f9b52014-04-30 14:13:18 -0400447RegStorage X86Mir2Lir::AllocateByteRegister() {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700448 RegStorage reg = AllocTypedTemp(false, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +0700449 if (!cu_->target64) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700450 DCHECK_LT(reg.GetRegNum(), rs_rX86_SP.GetRegNum());
451 }
452 return reg;
453}
454
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700455RegStorage X86Mir2Lir::Get128BitRegister(RegStorage reg) {
456 return GetRegInfo(reg)->FindMatchingView(RegisterInfo::k128SoloStorageMask)->GetReg();
457}
458
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700459bool X86Mir2Lir::IsByteRegister(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700460 return cu_->target64 || reg.GetRegNum() < rs_rX86_SP.GetRegNum();
Mark Mendelle87f9b52014-04-30 14:13:18 -0400461}
462
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463/* Clobber all regs that might be used by an external C call */
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000464void X86Mir2Lir::ClobberCallerSave() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700465 if (cu_->target64) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700466 Clobber(rs_rAX);
467 Clobber(rs_rCX);
468 Clobber(rs_rDX);
469 Clobber(rs_rSI);
470 Clobber(rs_rDI);
471
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700472 Clobber(rs_r8);
473 Clobber(rs_r9);
474 Clobber(rs_r10);
475 Clobber(rs_r11);
476
477 Clobber(rs_fr8);
478 Clobber(rs_fr9);
479 Clobber(rs_fr10);
480 Clobber(rs_fr11);
Serguei Katkovc3801912014-07-08 17:21:53 +0700481 } else {
482 Clobber(rs_rAX);
483 Clobber(rs_rCX);
484 Clobber(rs_rDX);
485 Clobber(rs_rBX);
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700486 }
Serguei Katkovc3801912014-07-08 17:21:53 +0700487
488 Clobber(rs_fr0);
489 Clobber(rs_fr1);
490 Clobber(rs_fr2);
491 Clobber(rs_fr3);
492 Clobber(rs_fr4);
493 Clobber(rs_fr5);
494 Clobber(rs_fr6);
495 Clobber(rs_fr7);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496}
497
498RegLocation X86Mir2Lir::GetReturnWideAlt() {
499 RegLocation res = LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -0700500 DCHECK(res.reg.GetLowReg() == rs_rAX.GetReg());
501 DCHECK(res.reg.GetHighReg() == rs_rDX.GetReg());
502 Clobber(rs_rAX);
503 Clobber(rs_rDX);
504 MarkInUse(rs_rAX);
505 MarkInUse(rs_rDX);
506 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 return res;
508}
509
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700510RegLocation X86Mir2Lir::GetReturnAlt() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 RegLocation res = LocCReturn();
buzbee091cc402014-03-31 10:14:40 -0700512 res.reg.SetReg(rs_rDX.GetReg());
513 Clobber(rs_rDX);
514 MarkInUse(rs_rDX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 return res;
516}
517
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700519void X86Mir2Lir::LockCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700520 LockTemp(rs_rX86_ARG0);
521 LockTemp(rs_rX86_ARG1);
522 LockTemp(rs_rX86_ARG2);
523 LockTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700524 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700525 LockTemp(rs_rX86_ARG4);
526 LockTemp(rs_rX86_ARG5);
527 LockTemp(rs_rX86_FARG0);
528 LockTemp(rs_rX86_FARG1);
529 LockTemp(rs_rX86_FARG2);
530 LockTemp(rs_rX86_FARG3);
531 LockTemp(rs_rX86_FARG4);
532 LockTemp(rs_rX86_FARG5);
533 LockTemp(rs_rX86_FARG6);
534 LockTemp(rs_rX86_FARG7);
535 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536}
537
538/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700539void X86Mir2Lir::FreeCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700540 FreeTemp(rs_rX86_ARG0);
541 FreeTemp(rs_rX86_ARG1);
542 FreeTemp(rs_rX86_ARG2);
543 FreeTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700544 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700545 FreeTemp(rs_rX86_ARG4);
546 FreeTemp(rs_rX86_ARG5);
547 FreeTemp(rs_rX86_FARG0);
548 FreeTemp(rs_rX86_FARG1);
549 FreeTemp(rs_rX86_FARG2);
550 FreeTemp(rs_rX86_FARG3);
551 FreeTemp(rs_rX86_FARG4);
552 FreeTemp(rs_rX86_FARG5);
553 FreeTemp(rs_rX86_FARG6);
554 FreeTemp(rs_rX86_FARG7);
555 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556}
557
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800558bool X86Mir2Lir::ProvidesFullMemoryBarrier(X86OpCode opcode) {
559 switch (opcode) {
560 case kX86LockCmpxchgMR:
561 case kX86LockCmpxchgAR:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700562 case kX86LockCmpxchg64M:
563 case kX86LockCmpxchg64A:
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800564 case kX86XchgMR:
565 case kX86Mfence:
566 // Atomic memory instructions provide full barrier.
567 return true;
568 default:
569 break;
570 }
571
572 // Conservative if cannot prove it provides full barrier.
573 return false;
574}
575
Andreas Gampeb14329f2014-05-15 11:16:06 -0700576bool X86Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800578 // Start off with using the last LIR as the barrier. If it is not enough, then we will update it.
579 LIR* mem_barrier = last_lir_insn_;
580
Andreas Gampeb14329f2014-05-15 11:16:06 -0700581 bool ret = false;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800582 /*
Hans Boehm48f5c472014-06-27 14:50:10 -0700583 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
584 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
585 * 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 -0800586 */
Hans Boehm48f5c472014-06-27 14:50:10 -0700587 if (barrier_kind == kAnyAny) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800588 // If no LIR exists already that can be used a barrier, then generate an mfence.
589 if (mem_barrier == nullptr) {
590 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700591 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800592 }
593
594 // If last instruction does not provide full barrier, then insert an mfence.
595 if (ProvidesFullMemoryBarrier(static_cast<X86OpCode>(mem_barrier->opcode)) == false) {
596 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700597 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800598 }
599 }
600
601 // Now ensure that a scheduling barrier is in place.
602 if (mem_barrier == nullptr) {
603 GenBarrier();
604 } else {
605 // Mark as a scheduling barrier.
606 DCHECK(!mem_barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100607 mem_barrier->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800608 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700609 return ret;
610#else
611 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612#endif
613}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000614
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615void X86Mir2Lir::CompilerInitializeRegAlloc() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700616 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700617 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_64, core_regs_64q, sp_regs_64,
618 dp_regs_64, reserved_regs_64, reserved_regs_64q,
619 core_temps_64, core_temps_64q, sp_temps_64, dp_temps_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700620 } else {
buzbeeb01bf152014-05-13 15:59:07 -0700621 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
622 dp_regs_32, reserved_regs_32, empty_pool,
623 core_temps_32, empty_pool, sp_temps_32, dp_temps_32);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700624 }
buzbee091cc402014-03-31 10:14:40 -0700625
626 // Target-specific adjustments.
627
Mark Mendellfe945782014-05-22 09:52:36 -0400628 // Add in XMM registers.
Serguei Katkovc3801912014-07-08 17:21:53 +0700629 const ArrayRef<const RegStorage> *xp_regs = cu_->target64 ? &xp_regs_64 : &xp_regs_32;
630 for (RegStorage reg : *xp_regs) {
Mark Mendellfe945782014-05-22 09:52:36 -0400631 RegisterInfo* info = new (arena_) RegisterInfo(reg, GetRegMaskCommon(reg));
632 reginfo_map_.Put(reg.GetReg(), info);
Serguei Katkovc3801912014-07-08 17:21:53 +0700633 }
634 const ArrayRef<const RegStorage> *xp_temps = cu_->target64 ? &xp_temps_64 : &xp_temps_32;
635 for (RegStorage reg : *xp_temps) {
636 RegisterInfo* xp_reg_info = GetRegInfo(reg);
637 xp_reg_info->SetIsTemp(true);
Mark Mendellfe945782014-05-22 09:52:36 -0400638 }
639
buzbee091cc402014-03-31 10:14:40 -0700640 // Alias single precision xmm to double xmms.
641 // TODO: as needed, add larger vector sizes - alias all to the largest.
642 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
643 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
644 int sp_reg_num = info->GetReg().GetRegNum();
Mark Mendellfe945782014-05-22 09:52:36 -0400645 RegStorage xp_reg = RegStorage::Solo128(sp_reg_num);
646 RegisterInfo* xp_reg_info = GetRegInfo(xp_reg);
647 // 128-bit xmm vector register's master storage should refer to itself.
648 DCHECK_EQ(xp_reg_info, xp_reg_info->Master());
649
650 // Redirect 32-bit vector's master storage to 128-bit vector.
651 info->SetMaster(xp_reg_info);
652
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700653 RegStorage dp_reg = RegStorage::FloatSolo64(sp_reg_num);
buzbee091cc402014-03-31 10:14:40 -0700654 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
Mark Mendellfe945782014-05-22 09:52:36 -0400655 // Redirect 64-bit vector's master storage to 128-bit vector.
656 dp_reg_info->SetMaster(xp_reg_info);
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700657 // Singles should show a single 32-bit mask bit, at first referring to the low half.
658 DCHECK_EQ(info->StorageMask(), 0x1U);
659 }
660
Elena Sayapinadd644502014-07-01 18:39:52 +0700661 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700662 // Alias 32bit W registers to corresponding 64bit X registers.
663 GrowableArray<RegisterInfo*>::Iterator w_it(&reg_pool_->core_regs_);
664 for (RegisterInfo* info = w_it.Next(); info != nullptr; info = w_it.Next()) {
665 int x_reg_num = info->GetReg().GetRegNum();
666 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
667 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
668 // 64bit X register's master storage should refer to itself.
669 DCHECK_EQ(x_reg_info, x_reg_info->Master());
670 // Redirect 32bit W master storage to 64bit X.
671 info->SetMaster(x_reg_info);
672 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
673 DCHECK_EQ(info->StorageMask(), 0x1U);
674 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 }
buzbee091cc402014-03-31 10:14:40 -0700676
677 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
678 // TODO: adjust for x86/hard float calling convention.
679 reg_pool_->next_core_reg_ = 2;
680 reg_pool_->next_sp_reg_ = 2;
681 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682}
683
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700684int X86Mir2Lir::VectorRegisterSize() {
685 return 128;
686}
687
688int X86Mir2Lir::NumReservableVectorRegisters(bool fp_used) {
689 return fp_used ? 5 : 7;
690}
691
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692void X86Mir2Lir::SpillCoreRegs() {
693 if (num_core_spills_ == 0) {
694 return;
695 }
696 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700697 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700698 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700699 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 for (int reg = 0; mask; mask >>= 1, reg++) {
701 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700702 StoreBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
703 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700704 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 }
706 }
707}
708
709void X86Mir2Lir::UnSpillCoreRegs() {
710 if (num_core_spills_ == 0) {
711 return;
712 }
713 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700714 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700715 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700716 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 for (int reg = 0; mask; mask >>= 1, reg++) {
718 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700719 LoadBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
720 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700721 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 }
723 }
724}
725
Serguei Katkovc3801912014-07-08 17:21:53 +0700726void X86Mir2Lir::SpillFPRegs() {
727 if (num_fp_spills_ == 0) {
728 return;
729 }
730 uint32_t mask = fp_spill_mask_;
731 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
732 for (int reg = 0; mask; mask >>= 1, reg++) {
733 if (mask & 0x1) {
734 StoreBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
735 k64, kNotVolatile);
736 offset += sizeof(double);
737 }
738 }
739}
740void X86Mir2Lir::UnSpillFPRegs() {
741 if (num_fp_spills_ == 0) {
742 return;
743 }
744 uint32_t mask = fp_spill_mask_;
745 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
746 for (int reg = 0; mask; mask >>= 1, reg++) {
747 if (mask & 0x1) {
748 LoadBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
749 k64, kNotVolatile);
750 offset += sizeof(double);
751 }
752 }
753}
754
755
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700756bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
758}
759
Vladimir Marko674744e2014-04-24 15:18:26 +0100760RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700761 // X86_64 can handle any size.
Elena Sayapinadd644502014-07-01 18:39:52 +0700762 if (cu_->target64) {
Chao-ying Fu06839f82014-08-14 15:59:17 -0700763 return RegClassBySize(size);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700764 }
765
Vladimir Marko674744e2014-04-24 15:18:26 +0100766 if (UNLIKELY(is_volatile)) {
767 // On x86, atomic 64-bit load/store requires an fp register.
768 // Smaller aligned load/store is atomic for both core and fp registers.
769 if (size == k64 || size == kDouble) {
770 return kFPReg;
771 }
772 }
773 return RegClassBySize(size);
774}
775
Elena Sayapinadd644502014-07-01 18:39:52 +0700776X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800777 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700778 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800779 method_address_insns_(arena, 100, kGrowableArrayMisc),
780 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800781 call_method_insns_(arena, 100, kGrowableArrayMisc),
Elena Sayapinadd644502014-07-01 18:39:52 +0700782 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400783 const_vectors_(nullptr) {
784 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700785 if (kIsDebugBuild) {
786 for (int i = 0; i < kX86Last; i++) {
787 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
788 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400789 << " is wrong: expecting " << i << ", seeing "
790 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700791 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 }
793 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700794 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700795 rs_rX86_SP = rs_rX86_SP_64;
796
797 rs_rX86_ARG0 = rs_rDI;
798 rs_rX86_ARG1 = rs_rSI;
799 rs_rX86_ARG2 = rs_rDX;
800 rs_rX86_ARG3 = rs_rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700801 rs_rX86_ARG4 = rs_r8;
802 rs_rX86_ARG5 = rs_r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700803 rs_rX86_FARG0 = rs_fr0;
804 rs_rX86_FARG1 = rs_fr1;
805 rs_rX86_FARG2 = rs_fr2;
806 rs_rX86_FARG3 = rs_fr3;
807 rs_rX86_FARG4 = rs_fr4;
808 rs_rX86_FARG5 = rs_fr5;
809 rs_rX86_FARG6 = rs_fr6;
810 rs_rX86_FARG7 = rs_fr7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700811 rX86_ARG0 = rDI;
812 rX86_ARG1 = rSI;
813 rX86_ARG2 = rDX;
814 rX86_ARG3 = rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700815 rX86_ARG4 = r8;
816 rX86_ARG5 = r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700817 rX86_FARG0 = fr0;
818 rX86_FARG1 = fr1;
819 rX86_FARG2 = fr2;
820 rX86_FARG3 = fr3;
821 rX86_FARG4 = fr4;
822 rX86_FARG5 = fr5;
823 rX86_FARG6 = fr6;
824 rX86_FARG7 = fr7;
Mark Mendell55884bc2014-06-10 10:21:29 -0400825 rs_rX86_INVOKE_TGT = rs_rDI;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700826 } else {
827 rs_rX86_SP = rs_rX86_SP_32;
828
829 rs_rX86_ARG0 = rs_rAX;
830 rs_rX86_ARG1 = rs_rCX;
831 rs_rX86_ARG2 = rs_rDX;
832 rs_rX86_ARG3 = rs_rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700833 rs_rX86_ARG4 = RegStorage::InvalidReg();
834 rs_rX86_ARG5 = RegStorage::InvalidReg();
835 rs_rX86_FARG0 = rs_rAX;
836 rs_rX86_FARG1 = rs_rCX;
837 rs_rX86_FARG2 = rs_rDX;
838 rs_rX86_FARG3 = rs_rBX;
839 rs_rX86_FARG4 = RegStorage::InvalidReg();
840 rs_rX86_FARG5 = RegStorage::InvalidReg();
841 rs_rX86_FARG6 = RegStorage::InvalidReg();
842 rs_rX86_FARG7 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700843 rX86_ARG0 = rAX;
844 rX86_ARG1 = rCX;
845 rX86_ARG2 = rDX;
846 rX86_ARG3 = rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700847 rX86_FARG0 = rAX;
848 rX86_FARG1 = rCX;
849 rX86_FARG2 = rDX;
850 rX86_FARG3 = rBX;
Mark Mendell55884bc2014-06-10 10:21:29 -0400851 rs_rX86_INVOKE_TGT = rs_rAX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700852 // TODO(64): Initialize with invalid reg
853// rX86_ARG4 = RegStorage::InvalidReg();
854// rX86_ARG5 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700855 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700856 rs_rX86_RET0 = rs_rAX;
857 rs_rX86_RET1 = rs_rDX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700858 rs_rX86_COUNT = rs_rCX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700859 rX86_RET0 = rAX;
860 rX86_RET1 = rDX;
861 rX86_INVOKE_TGT = rAX;
862 rX86_COUNT = rCX;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700863
864 // Initialize the number of reserved vector registers
865 num_reserved_vector_regs_ = -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866}
867
868Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
869 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700870 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871}
872
Andreas Gampe98430592014-07-27 19:44:50 -0700873// Not used in x86(-64)
874RegStorage X86Mir2Lir::LoadHelper(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700875 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
876 return RegStorage::InvalidReg();
877}
878
Dave Allisonb373e092014-02-20 16:06:36 -0800879LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
Dave Allison69dfe512014-07-11 17:11:58 +0000880 // First load the pointer in fs:[suspend-trigger] into eax
881 // Then use a test instruction to indirect via that address.
Dave Allisondfd3b472014-07-16 16:04:32 -0700882 if (cu_->target64) {
883 NewLIR2(kX86Mov64RT, rs_rAX.GetReg(),
884 Thread::ThreadSuspendTriggerOffset<8>().Int32Value());
885 } else {
886 NewLIR2(kX86Mov32RT, rs_rAX.GetReg(),
887 Thread::ThreadSuspendTriggerOffset<4>().Int32Value());
888 }
Dave Allison69dfe512014-07-11 17:11:58 +0000889 return NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rAX.GetReg(), 0);
Dave Allisonb373e092014-02-20 16:06:36 -0800890}
891
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700892uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700893 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700894 return X86Mir2Lir::EncodingMap[opcode].flags;
895}
896
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700897const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700898 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 return X86Mir2Lir::EncodingMap[opcode].name;
900}
901
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700902const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700903 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 return X86Mir2Lir::EncodingMap[opcode].fmt;
905}
906
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000907void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
908 // Can we do this directly to memory?
909 rl_dest = UpdateLocWide(rl_dest);
910 if ((rl_dest.location == kLocDalvikFrame) ||
911 (rl_dest.location == kLocCompilerTemp)) {
912 int32_t val_lo = Low32Bits(value);
913 int32_t val_hi = High32Bits(value);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700914 int r_base = rs_rX86_SP.GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000915 int displacement = SRegOffset(rl_dest.s_reg_low);
916
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100917 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800918 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000919 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
920 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800921 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000922 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
923 false /* is_load */, true /* is64bit */);
924 return;
925 }
926
927 // Just use the standard code to do the generation.
928 Mir2Lir::GenConstWide(rl_dest, value);
929}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800930
931// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
932void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
933 LOG(INFO) << "location: " << loc.location << ','
934 << (loc.wide ? " w" : " ")
935 << (loc.defined ? " D" : " ")
936 << (loc.is_const ? " c" : " ")
937 << (loc.fp ? " F" : " ")
938 << (loc.core ? " C" : " ")
939 << (loc.ref ? " r" : " ")
940 << (loc.high_word ? " h" : " ")
941 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800942 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000943 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800944 << ", s_reg: " << loc.s_reg_low
945 << ", orig: " << loc.orig_sreg;
946}
947
Mark Mendell67c39c42014-01-31 17:28:00 -0800948void X86Mir2Lir::Materialize() {
949 // A good place to put the analysis before starting.
950 AnalyzeMIR();
951
952 // Now continue with regular code generation.
953 Mir2Lir::Materialize();
954}
955
Jeff Hao49161ce2014-03-12 11:05:25 -0700956void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800957 SpecialTargetRegister symbolic_reg) {
958 /*
959 * For x86, just generate a 32 bit move immediate instruction, that will be filled
960 * in at 'link time'. For now, put a unique value based on target to ensure that
961 * code deduplication works.
962 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700963 int target_method_idx = target_method.dex_method_index;
964 const DexFile* target_dex_file = target_method.dex_file;
965 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
966 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800967
Jeff Hao49161ce2014-03-12 11:05:25 -0700968 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700969 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
970 TargetReg(symbolic_reg, kNotWide).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700971 static_cast<int>(target_method_id_ptr), target_method_idx,
972 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800973 AppendLIR(move);
974 method_address_insns_.Insert(move);
975}
976
Fred Shihe7f82e22014-08-06 10:46:37 -0700977void X86Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
978 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800979 /*
980 * For x86, just generate a 32 bit move immediate instruction, that will be filled
981 * in at 'link time'. For now, put a unique value based on target to ensure that
982 * code deduplication works.
983 */
Fred Shihe7f82e22014-08-06 10:46:37 -0700984 const DexFile::TypeId& id = dex_file.GetTypeId(type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800985 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
986
987 // Generate the move instruction with the unique pointer and save index and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700988 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
989 TargetReg(symbolic_reg, kNotWide).GetReg(),
Fred Shihe7f82e22014-08-06 10:46:37 -0700990 static_cast<int>(ptr), type_idx,
991 WrapPointer(const_cast<DexFile*>(&dex_file)));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800992 AppendLIR(move);
993 class_type_address_insns_.Insert(move);
994}
995
Jeff Hao49161ce2014-03-12 11:05:25 -0700996LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800997 /*
998 * For x86, just generate a 32 bit call relative instruction, that will be filled
999 * in at 'link time'. For now, put a unique value based on target to ensure that
1000 * code deduplication works.
1001 */
Jeff Hao49161ce2014-03-12 11:05:25 -07001002 int target_method_idx = target_method.dex_method_index;
1003 const DexFile* target_dex_file = target_method.dex_file;
1004 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
1005 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001006
Jeff Hao49161ce2014-03-12 11:05:25 -07001007 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
1008 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
1009 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001010 AppendLIR(call);
1011 call_method_insns_.Insert(call);
1012 return call;
1013}
1014
1015void X86Mir2Lir::InstallLiteralPools() {
1016 // These are handled differently for x86.
1017 DCHECK(code_literal_list_ == nullptr);
1018 DCHECK(method_literal_list_ == nullptr);
1019 DCHECK(class_literal_list_ == nullptr);
1020
Mark Mendelld65c51a2014-04-29 16:55:20 -04001021 // Align to 16 byte boundary. We have implicit knowledge that the start of the method is
1022 // on a 4 byte boundary. How can I check this if it changes (other than aligned loads
1023 // will fail at runtime)?
1024 if (const_vectors_ != nullptr) {
1025 int align_size = (16-4) - (code_buffer_.size() & 0xF);
1026 if (align_size < 0) {
1027 align_size += 16;
1028 }
1029
1030 while (align_size > 0) {
1031 code_buffer_.push_back(0);
1032 align_size--;
1033 }
1034 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001035 PushWord(&code_buffer_, p->operands[0]);
1036 PushWord(&code_buffer_, p->operands[1]);
1037 PushWord(&code_buffer_, p->operands[2]);
1038 PushWord(&code_buffer_, p->operands[3]);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001039 }
1040 }
1041
Mark Mendell55d0eac2014-02-06 11:02:52 -08001042 // Handle the fixups for methods.
1043 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
1044 LIR* p = method_address_insns_.Get(i);
1045 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001046 uint32_t target_method_idx = p->operands[2];
1047 const DexFile* target_dex_file =
1048 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001049
1050 // The offset to patch is the last 4 bytes of the instruction.
1051 int patch_offset = p->offset + p->flags.size - 4;
1052 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
1053 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -07001054 target_method_idx, target_dex_file,
1055 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001056 patch_offset);
1057 }
1058
1059 // Handle the fixups for class types.
1060 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
1061 LIR* p = class_type_address_insns_.Get(i);
1062 DCHECK_EQ(p->opcode, kX86Mov32RI);
Fred Shihe7f82e22014-08-06 10:46:37 -07001063
1064 const DexFile* class_dex_file =
1065 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Jeff Hao49161ce2014-03-12 11:05:25 -07001066 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001067
1068 // The offset to patch is the last 4 bytes of the instruction.
1069 int patch_offset = p->offset + p->flags.size - 4;
1070 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -07001071 cu_->method_idx, target_method_idx, class_dex_file,
1072 patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001073 }
1074
1075 // And now the PC-relative calls to methods.
1076 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
1077 LIR* p = call_method_insns_.Get(i);
1078 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001079 uint32_t target_method_idx = p->operands[1];
1080 const DexFile* target_dex_file =
1081 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001082
1083 // The offset to patch is the last 4 bytes of the instruction.
1084 int patch_offset = p->offset + p->flags.size - 4;
1085 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001086 cu_->method_idx, cu_->invoke_type,
1087 target_method_idx, target_dex_file,
1088 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001089 patch_offset, -4 /* offset */);
1090 }
1091
1092 // And do the normal processing.
1093 Mir2Lir::InstallLiteralPools();
1094}
1095
DaniilSokolov70c4f062014-06-24 17:34:00 -07001096bool X86Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
DaniilSokolov70c4f062014-06-24 17:34:00 -07001097 RegLocation rl_src = info->args[0];
1098 RegLocation rl_srcPos = info->args[1];
1099 RegLocation rl_dst = info->args[2];
1100 RegLocation rl_dstPos = info->args[3];
1101 RegLocation rl_length = info->args[4];
1102 if (rl_srcPos.is_const && (mir_graph_->ConstantValue(rl_srcPos) < 0)) {
1103 return false;
1104 }
1105 if (rl_dstPos.is_const && (mir_graph_->ConstantValue(rl_dstPos) < 0)) {
1106 return false;
1107 }
1108 ClobberCallerSave();
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001109 LockCallTemps(); // Using fixed registers.
1110 RegStorage tmp_reg = cu_->target64 ? rs_r11 : rs_rBX;
1111 LoadValueDirectFixed(rl_src, rs_rAX);
1112 LoadValueDirectFixed(rl_dst, rs_rCX);
1113 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_rAX, rs_rCX, nullptr);
1114 LIR* src_null_branch = OpCmpImmBranch(kCondEq, rs_rAX, 0, nullptr);
1115 LIR* dst_null_branch = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1116 LoadValueDirectFixed(rl_length, rs_rDX);
1117 // If the length of the copy is > 128 characters (256 bytes) or negative then go slow path.
1118 LIR* len_too_big = OpCmpImmBranch(kCondHi, rs_rDX, 128, nullptr);
1119 LoadValueDirectFixed(rl_src, rs_rAX);
1120 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001121 LIR* src_bad_len = nullptr;
1122 LIR* srcPos_negative = nullptr;
1123 if (!rl_srcPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001124 LoadValueDirectFixed(rl_srcPos, tmp_reg);
1125 srcPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1126 OpRegReg(kOpAdd, tmp_reg, rs_rDX);
1127 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001128 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001129 int32_t pos_val = mir_graph_->ConstantValue(rl_srcPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001130 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001131 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001132 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001133 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1134 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001135 }
1136 }
1137 LIR* dstPos_negative = nullptr;
1138 LIR* dst_bad_len = nullptr;
1139 LoadValueDirectFixed(rl_dst, rs_rAX);
1140 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1141 if (!rl_dstPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001142 LoadValueDirectFixed(rl_dstPos, tmp_reg);
1143 dstPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1144 OpRegRegReg(kOpAdd, tmp_reg, tmp_reg, rs_rDX);
1145 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001146 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001147 int32_t pos_val = mir_graph_->ConstantValue(rl_dstPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001148 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001149 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001150 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001151 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1152 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001153 }
1154 }
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001155 // Everything is checked now.
1156 LoadValueDirectFixed(rl_src, rs_rAX);
1157 LoadValueDirectFixed(rl_dst, tmp_reg);
1158 LoadValueDirectFixed(rl_srcPos, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001159 NewLIR5(kX86Lea32RA, rs_rAX.GetReg(), rs_rAX.GetReg(),
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001160 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value());
1161 // RAX now holds the address of the first src element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001162
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001163 LoadValueDirectFixed(rl_dstPos, rs_rCX);
1164 NewLIR5(kX86Lea32RA, tmp_reg.GetReg(), tmp_reg.GetReg(),
1165 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value() );
1166 // RBX now holds the address of the first dst element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001167
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001168 // Check if the number of elements to be copied is odd or even. If odd
DaniilSokolov70c4f062014-06-24 17:34:00 -07001169 // then copy the first element (so that the remaining number of elements
1170 // is even).
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001171 LoadValueDirectFixed(rl_length, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001172 OpRegImm(kOpAnd, rs_rCX, 1);
1173 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1174 OpRegImm(kOpSub, rs_rDX, 1);
1175 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001176 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001177
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001178 // Since the remaining number of elements is even, we will copy by
DaniilSokolov70c4f062014-06-24 17:34:00 -07001179 // two elements at a time.
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001180 LIR* beginLoop = NewLIR0(kPseudoTargetLabel);
1181 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_rDX, 0, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001182 OpRegImm(kOpSub, rs_rDX, 2);
1183 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001184 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001185 OpUnconditionalBranch(beginLoop);
1186 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1187 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1188 LIR *return_point = NewLIR0(kPseudoTargetLabel);
1189 jmp_to_ret->target = return_point;
1190 jmp_to_begin_loop->target = beginLoop;
1191 src_dst_same->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001192 len_too_big->target = check_failed;
1193 src_null_branch->target = check_failed;
1194 if (srcPos_negative != nullptr)
1195 srcPos_negative ->target = check_failed;
1196 if (src_bad_len != nullptr)
1197 src_bad_len->target = check_failed;
1198 dst_null_branch->target = check_failed;
1199 if (dstPos_negative != nullptr)
1200 dstPos_negative->target = check_failed;
1201 if (dst_bad_len != nullptr)
1202 dst_bad_len->target = check_failed;
1203 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
1204 return true;
1205}
1206
1207
Mark Mendell4028a6c2014-02-19 20:06:20 -08001208/*
1209 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1210 * otherwise bails to standard library code.
1211 */
1212bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001213 RegLocation rl_obj = info->args[0];
1214 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001215 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001216 // RBX is callee-save register in 64-bit mode.
1217 RegStorage rs_tmp = cu_->target64 ? rs_r11 : rs_rBX;
1218 int start_value = -1;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001219
1220 uint32_t char_value =
1221 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1222
1223 if (char_value > 0xFFFF) {
1224 // We have to punt to the real String.indexOf.
1225 return false;
1226 }
1227
1228 // Okay, we are commited to inlining this.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001229 // EAX: 16 bit character being searched.
1230 // ECX: count: number of words to be searched.
1231 // EDI: String being searched.
1232 // EDX: temporary during execution.
1233 // EBX or R11: temporary during execution (depending on mode).
1234 // REP SCASW: search instruction.
1235
1236 FlushReg(rs_rAX);
1237 Clobber(rs_rAX);
1238 LockTemp(rs_rAX);
1239 FlushReg(rs_rCX);
1240 Clobber(rs_rCX);
1241 LockTemp(rs_rCX);
1242 FlushReg(rs_rDX);
1243 Clobber(rs_rDX);
1244 LockTemp(rs_rDX);
1245 FlushReg(rs_tmp);
1246 Clobber(rs_tmp);
1247 LockTemp(rs_tmp);
1248 if (cu_->target64) {
1249 FlushReg(rs_rDI);
1250 Clobber(rs_rDI);
1251 LockTemp(rs_rDI);
1252 }
1253
buzbeea0cd2d72014-06-01 09:33:49 -07001254 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001255 RegLocation rl_dest = InlineTarget(info);
1256
1257 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001258 LoadValueDirectFixed(rl_obj, rs_rDX);
1259 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001260 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001261
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001262 LIR *slowpath_branch = nullptr, *length_compare = nullptr;
1263
1264 // We need the value in EAX.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001265 if (rl_char.is_const) {
buzbee2700f7e2014-03-07 09:46:20 -08001266 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001267 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001268 // Does the character fit in 16 bits? Compare it at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001269 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001270 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001271 }
1272
1273 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001274 // Location of reference to data array within the String object.
1275 int value_offset = mirror::String::ValueOffset().Int32Value();
1276 // Location of count within the String object.
1277 int count_offset = mirror::String::CountOffset().Int32Value();
1278 // Starting offset within data array.
1279 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1280 // Start of char data with array_.
1281 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001282
Dave Allison69dfe512014-07-11 17:11:58 +00001283 // Compute the number of words to search in to rCX.
1284 Load32Disp(rs_rDX, count_offset, rs_rCX);
1285
Dave Allisondfd3b472014-07-16 16:04:32 -07001286 // Possible signal here due to null pointer dereference.
1287 // Note that the signal handler will expect the top word of
1288 // the stack to be the ArtMethod*. If the PUSH edi instruction
1289 // below is ahead of the load above then this will not be true
1290 // and the signal handler will not work.
1291 MarkPossibleNullPointerException(0);
Dave Allison69dfe512014-07-11 17:11:58 +00001292
Dave Allisondfd3b472014-07-16 16:04:32 -07001293 if (!cu_->target64) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001294 // EDI is callee-save register in 32-bit mode.
1295 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1296 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001297
Mark Mendell4028a6c2014-02-19 20:06:20 -08001298 if (zero_based) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001299 // Start index is not present.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001300 // We have to handle an empty string. Use special instruction JECXZ.
1301 length_compare = NewLIR0(kX86Jecxz8);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001302
1303 // Copy the number of words to search in a temporary register.
1304 // We will use the register at the end to calculate result.
1305 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001306 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001307 // Start index is present.
buzbeea44d4f52014-03-05 11:26:39 -08001308 rl_start = info->args[2];
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001309
Mark Mendell4028a6c2014-02-19 20:06:20 -08001310 // We have to offset by the start index.
1311 if (rl_start.is_const) {
1312 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1313 start_value = std::max(start_value, 0);
1314
1315 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001316 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001317 OpRegImm(kOpMov, rs_rDI, start_value);
1318
1319 // Copy the number of words to search in a temporary register.
1320 // We will use the register at the end to calculate result.
1321 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001322
1323 if (start_value != 0) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001324 // Decrease the number of words to search by the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001325 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001326 }
1327 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001328 // Handle "start index < 0" case.
1329 if (!cu_->target64 && rl_start.location != kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001330 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001331 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001332 {
1333 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001334 Load32Disp(rs_rX86_SP, displacement, rs_rDI);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001335 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001336 } else {
1337 LoadValueDirectFixed(rl_start, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001338 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001339 OpRegReg(kOpXor, rs_tmp, rs_tmp);
1340 OpRegReg(kOpCmp, rs_rDI, rs_tmp);
1341 OpCondRegReg(kOpCmov, kCondLt, rs_rDI, rs_tmp);
1342
1343 // The length of the string should be greater than the start index.
1344 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rDI, nullptr);
1345
1346 // Copy the number of words to search in a temporary register.
1347 // We will use the register at the end to calculate result.
1348 OpRegReg(kOpMov, rs_tmp, rs_rCX);
1349
1350 // Decrease the number of words to search by the start index.
1351 OpRegReg(kOpSub, rs_rCX, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001352 }
1353 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001354
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001355 // Load the address of the string into EDI.
1356 // In case of start index we have to add the address to existing value in EDI.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001357 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001358 if (zero_based || (!zero_based && rl_start.is_const && start_value == 0)) {
1359 Load32Disp(rs_rDX, offset_offset, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001360 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001361 OpRegMem(kOpAdd, rs_rDI, rs_rDX, offset_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001362 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001363 OpRegImm(kOpLsl, rs_rDI, 1);
1364 OpRegMem(kOpAdd, rs_rDI, rs_rDX, value_offset);
1365 OpRegImm(kOpAdd, rs_rDI, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001366
1367 // EDI now contains the start of the string to be searched.
1368 // We are all prepared to do the search for the character.
1369 NewLIR0(kX86RepneScasw);
1370
1371 // Did we find a match?
1372 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1373
1374 // yes, we matched. Compute the index of the result.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001375 OpRegReg(kOpSub, rs_tmp, rs_rCX);
1376 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_tmp.GetReg(), -1);
1377
Mark Mendell4028a6c2014-02-19 20:06:20 -08001378 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1379
1380 // Failed to match; return -1.
1381 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1382 length_compare->target = not_found;
1383 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001384 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001385
1386 // And join up at the end.
1387 all_done->target = NewLIR0(kPseudoTargetLabel);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001388
1389 if (!cu_->target64)
1390 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001391
1392 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001393 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001394 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001395 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001396 }
1397
1398 StoreValue(rl_dest, rl_return);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001399
1400 FreeTemp(rs_rAX);
1401 FreeTemp(rs_rCX);
1402 FreeTemp(rs_rDX);
1403 FreeTemp(rs_tmp);
1404 if (cu_->target64) {
1405 FreeTemp(rs_rDI);
1406 }
1407
Mark Mendell4028a6c2014-02-19 20:06:20 -08001408 return true;
1409}
1410
Tong Shen35e1e6a2014-07-30 09:31:22 -07001411static bool ARTRegIDToDWARFRegID(bool is_x86_64, int art_reg_id, int* dwarf_reg_id) {
1412 if (is_x86_64) {
1413 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001414 case 3 : *dwarf_reg_id = 3; return true; // %rbx
Tong Shen35e1e6a2014-07-30 09:31:22 -07001415 // This is the only discrepancy between ART & DWARF register numbering.
Andreas Gampebda27222014-07-30 23:21:36 -07001416 case 5 : *dwarf_reg_id = 6; return true; // %rbp
1417 case 12: *dwarf_reg_id = 12; return true; // %r12
1418 case 13: *dwarf_reg_id = 13; return true; // %r13
1419 case 14: *dwarf_reg_id = 14; return true; // %r14
1420 case 15: *dwarf_reg_id = 15; return true; // %r15
1421 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001422 }
1423 } else {
1424 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001425 case 5: *dwarf_reg_id = 5; return true; // %ebp
1426 case 6: *dwarf_reg_id = 6; return true; // %esi
1427 case 7: *dwarf_reg_id = 7; return true; // %edi
1428 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001429 }
1430 }
1431}
1432
Tong Shen547cdfd2014-08-05 01:54:19 -07001433std::vector<uint8_t>* X86Mir2Lir::ReturnFrameDescriptionEntry() {
1434 std::vector<uint8_t>* cfi_info = new std::vector<uint8_t>;
Mark Mendellae9fd932014-02-10 16:14:35 -08001435
1436 // Generate the FDE for the method.
1437 DCHECK_NE(data_offset_, 0U);
1438
Tong Shen547cdfd2014-08-05 01:54:19 -07001439 WriteFDEHeader(cfi_info);
1440 WriteFDEAddressRange(cfi_info, data_offset_);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001441
Mark Mendellae9fd932014-02-10 16:14:35 -08001442 // The instructions in the FDE.
1443 if (stack_decrement_ != nullptr) {
1444 // Advance LOC to just past the stack decrement.
1445 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001446 DW_CFA_advance_loc(cfi_info, pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001447
1448 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
Tong Shen547cdfd2014-08-05 01:54:19 -07001449 DW_CFA_def_cfa_offset(cfi_info, frame_size_);
Mark Mendellae9fd932014-02-10 16:14:35 -08001450
Tong Shen35e1e6a2014-07-30 09:31:22 -07001451 // Handle register spills
1452 const uint32_t kSpillInstLen = (cu_->target64) ? 5 : 4;
1453 const int kDataAlignmentFactor = (cu_->target64) ? -8 : -4;
1454 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
1455 int offset = -(GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
1456 for (int reg = 0; mask; mask >>= 1, reg++) {
1457 if (mask & 0x1) {
1458 pc += kSpillInstLen;
1459
1460 // Advance LOC to pass this instruction
Tong Shen547cdfd2014-08-05 01:54:19 -07001461 DW_CFA_advance_loc(cfi_info, kSpillInstLen);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001462
1463 int dwarf_reg_id;
1464 if (ARTRegIDToDWARFRegID(cu_->target64, reg, &dwarf_reg_id)) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001465 // DW_CFA_offset_extended_sf reg offset
1466 DW_CFA_offset_extended_sf(cfi_info, dwarf_reg_id, offset / kDataAlignmentFactor);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001467 }
1468
1469 offset += GetInstructionSetPointerSize(cu_->instruction_set);
1470 }
1471 }
1472
Mark Mendellae9fd932014-02-10 16:14:35 -08001473 // We continue with that stack until the epilogue.
1474 if (stack_increment_ != nullptr) {
1475 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001476 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001477
1478 // We probably have code snippets after the epilogue, so save the
1479 // current state: DW_CFA_remember_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001480 DW_CFA_remember_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001481
Tong Shen35e1e6a2014-07-30 09:31:22 -07001482 // We have now popped the stack: DW_CFA_def_cfa_offset 4/8.
1483 // There is only the return PC on the stack now.
Tong Shen547cdfd2014-08-05 01:54:19 -07001484 DW_CFA_def_cfa_offset(cfi_info, GetInstructionSetPointerSize(cu_->instruction_set));
Mark Mendellae9fd932014-02-10 16:14:35 -08001485
1486 // Everything after that is the same as before the epilogue.
1487 // Stack bump was followed by RET instruction.
1488 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1489 if (post_ret_insn != nullptr) {
1490 pc = new_pc;
1491 new_pc = post_ret_insn->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001492 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001493 // Restore the state: DW_CFA_restore_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001494 DW_CFA_restore_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001495 }
1496 }
1497 }
1498
Tong Shen547cdfd2014-08-05 01:54:19 -07001499 PadCFI(cfi_info);
1500 WriteCFILength(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001501
Mark Mendellae9fd932014-02-10 16:14:35 -08001502 return cfi_info;
1503}
1504
Mark Mendelld65c51a2014-04-29 16:55:20 -04001505void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1506 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001507 case kMirOpReserveVectorRegisters:
1508 ReserveVectorRegisters(mir);
1509 break;
1510 case kMirOpReturnVectorRegisters:
1511 ReturnVectorRegisters();
1512 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001513 case kMirOpConstVector:
1514 GenConst128(bb, mir);
1515 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001516 case kMirOpMoveVector:
1517 GenMoveVector(bb, mir);
1518 break;
1519 case kMirOpPackedMultiply:
1520 GenMultiplyVector(bb, mir);
1521 break;
1522 case kMirOpPackedAddition:
1523 GenAddVector(bb, mir);
1524 break;
1525 case kMirOpPackedSubtract:
1526 GenSubtractVector(bb, mir);
1527 break;
1528 case kMirOpPackedShiftLeft:
1529 GenShiftLeftVector(bb, mir);
1530 break;
1531 case kMirOpPackedSignedShiftRight:
1532 GenSignedShiftRightVector(bb, mir);
1533 break;
1534 case kMirOpPackedUnsignedShiftRight:
1535 GenUnsignedShiftRightVector(bb, mir);
1536 break;
1537 case kMirOpPackedAnd:
1538 GenAndVector(bb, mir);
1539 break;
1540 case kMirOpPackedOr:
1541 GenOrVector(bb, mir);
1542 break;
1543 case kMirOpPackedXor:
1544 GenXorVector(bb, mir);
1545 break;
1546 case kMirOpPackedAddReduce:
1547 GenAddReduceVector(bb, mir);
1548 break;
1549 case kMirOpPackedReduce:
1550 GenReduceVector(bb, mir);
1551 break;
1552 case kMirOpPackedSet:
1553 GenSetVector(bb, mir);
1554 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001555 default:
1556 break;
1557 }
1558}
1559
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001560void X86Mir2Lir::ReserveVectorRegisters(MIR* mir) {
1561 // We should not try to reserve twice without returning the registers
1562 DCHECK_NE(num_reserved_vector_regs_, -1);
1563
1564 int num_vector_reg = mir->dalvikInsn.vA;
1565 for (int i = 0; i < num_vector_reg; i++) {
1566 RegStorage xp_reg = RegStorage::Solo128(i);
1567 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1568 Clobber(xp_reg);
1569
1570 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1571 info != nullptr;
1572 info = info->GetAliasChain()) {
1573 if (info->GetReg().IsSingle()) {
1574 reg_pool_->sp_regs_.Delete(info);
1575 } else {
1576 reg_pool_->dp_regs_.Delete(info);
1577 }
1578 }
1579 }
1580
1581 num_reserved_vector_regs_ = num_vector_reg;
1582}
1583
1584void X86Mir2Lir::ReturnVectorRegisters() {
1585 // Return all the reserved registers
1586 for (int i = 0; i < num_reserved_vector_regs_; i++) {
1587 RegStorage xp_reg = RegStorage::Solo128(i);
1588 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1589
1590 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1591 info != nullptr;
1592 info = info->GetAliasChain()) {
1593 if (info->GetReg().IsSingle()) {
1594 reg_pool_->sp_regs_.Insert(info);
1595 } else {
1596 reg_pool_->dp_regs_.Insert(info);
1597 }
1598 }
1599 }
1600
1601 // We don't have anymore reserved vector registers
1602 num_reserved_vector_regs_ = -1;
1603}
1604
Mark Mendelld65c51a2014-04-29 16:55:20 -04001605void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001606 store_method_addr_used_ = true;
1607 int type_size = mir->dalvikInsn.vB;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001608 // We support 128 bit vectors.
1609 DCHECK_EQ(type_size & 0xFFFF, 128);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001610 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001611 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001612 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001613 // Check for all 0 case.
1614 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1615 NewLIR2(kX86XorpsRR, reg, reg);
1616 return;
1617 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001618
1619 // Append the mov const vector to reg opcode.
1620 AppendOpcodeWithConst(kX86MovupsRM, reg, mir);
1621}
1622
1623void X86Mir2Lir::AppendOpcodeWithConst(X86OpCode opcode, int reg, MIR* mir) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04001624 // Okay, load it from the constant vector area.
1625 LIR *data_target = ScanVectorLiteral(mir);
1626 if (data_target == nullptr) {
1627 data_target = AddVectorLiteral(mir);
1628 }
1629
1630 // Address the start of the method.
1631 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001632 if (rl_method.wide) {
1633 rl_method = LoadValueWide(rl_method, kCoreReg);
1634 } else {
1635 rl_method = LoadValue(rl_method, kCoreReg);
1636 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001637
1638 // Load the proper value from the literal area.
1639 // We don't know the proper offset for the value, so pick one that will force
1640 // 4 byte offset. We will fix this up in the assembler later to have the right
1641 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001642 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001643 LIR *load = NewLIR2(opcode, reg, rl_method.reg.GetReg());
Mark Mendelld65c51a2014-04-29 16:55:20 -04001644 load->flags.fixup = kFixupLoad;
1645 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001646}
1647
Mark Mendellfe945782014-05-22 09:52:36 -04001648void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1649 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001650 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1651 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
1652 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001653 NewLIR2(kX86Mova128RR, rs_dest.GetReg(), rs_src.GetReg());
1654}
1655
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001656void X86Mir2Lir::GenMultiplyVectorSignedByte(BasicBlock *bb, MIR *mir) {
1657 const int BYTE_SIZE = 8;
1658 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1659 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
1660 RegStorage rs_src1_high_tmp = Get128BitRegister(AllocTempWide());
1661
1662 /*
1663 * Emulate the behavior of a kSignedByte by separating out the 16 values in the two XMM
1664 * and multiplying 8 at a time before recombining back into one XMM register.
1665 *
1666 * let xmm1, xmm2 be real srcs (keep low bits of 16bit lanes)
1667 * xmm3 is tmp (operate on high bits of 16bit lanes)
1668 *
1669 * xmm3 = xmm1
1670 * xmm1 = xmm1 .* xmm2
1671 * xmm1 = xmm1 & 0x00ff00ff00ff00ff00ff00ff00ff00ff // xmm1 now has low bits
1672 * xmm3 = xmm3 .>> 8
1673 * xmm2 = xmm2 & 0xff00ff00ff00ff00ff00ff00ff00ff00
1674 * xmm2 = xmm2 .* xmm3 // xmm2 now has high bits
1675 * xmm1 = xmm1 | xmm2 // combine results
1676 */
1677
1678 // Copy xmm1.
1679 NewLIR2(kX86Mova128RR, rs_src1_high_tmp.GetReg(), rs_dest_src1.GetReg());
1680
1681 // Multiply low bits.
1682 NewLIR2(kX86PmullwRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1683
1684 // xmm1 now has low bits.
1685 AndMaskVectorRegister(rs_dest_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
1686
1687 // Prepare high bits for multiplication.
1688 NewLIR2(kX86PsrlwRI, rs_src1_high_tmp.GetReg(), BYTE_SIZE);
1689 AndMaskVectorRegister(rs_src2, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1690
1691 // Multiply high bits and xmm2 now has high bits.
1692 NewLIR2(kX86PmullwRR, rs_src2.GetReg(), rs_src1_high_tmp.GetReg());
1693
1694 // Combine back into dest XMM register.
1695 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1696}
1697
Mark Mendellfe945782014-05-22 09:52:36 -04001698void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001699 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1700 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1701 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1702 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001703 int opcode = 0;
1704 switch (opsize) {
1705 case k32:
1706 opcode = kX86PmulldRR;
1707 break;
1708 case kSignedHalf:
1709 opcode = kX86PmullwRR;
1710 break;
1711 case kSingle:
1712 opcode = kX86MulpsRR;
1713 break;
1714 case kDouble:
1715 opcode = kX86MulpdRR;
1716 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001717 case kSignedByte:
1718 // HW doesn't support 16x16 byte multiplication so emulate it.
1719 GenMultiplyVectorSignedByte(bb, mir);
1720 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001721 default:
1722 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1723 break;
1724 }
1725 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1726}
1727
1728void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001729 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1730 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1731 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1732 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001733 int opcode = 0;
1734 switch (opsize) {
1735 case k32:
1736 opcode = kX86PadddRR;
1737 break;
1738 case kSignedHalf:
1739 case kUnsignedHalf:
1740 opcode = kX86PaddwRR;
1741 break;
1742 case kUnsignedByte:
1743 case kSignedByte:
1744 opcode = kX86PaddbRR;
1745 break;
1746 case kSingle:
1747 opcode = kX86AddpsRR;
1748 break;
1749 case kDouble:
1750 opcode = kX86AddpdRR;
1751 break;
1752 default:
1753 LOG(FATAL) << "Unsupported vector addition " << opsize;
1754 break;
1755 }
1756 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1757}
1758
1759void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001760 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1761 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1762 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1763 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001764 int opcode = 0;
1765 switch (opsize) {
1766 case k32:
1767 opcode = kX86PsubdRR;
1768 break;
1769 case kSignedHalf:
1770 case kUnsignedHalf:
1771 opcode = kX86PsubwRR;
1772 break;
1773 case kUnsignedByte:
1774 case kSignedByte:
1775 opcode = kX86PsubbRR;
1776 break;
1777 case kSingle:
1778 opcode = kX86SubpsRR;
1779 break;
1780 case kDouble:
1781 opcode = kX86SubpdRR;
1782 break;
1783 default:
1784 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1785 break;
1786 }
1787 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1788}
1789
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001790void X86Mir2Lir::GenShiftByteVector(BasicBlock *bb, MIR *mir) {
1791 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1792 RegStorage rs_tmp = Get128BitRegister(AllocTempWide());
1793
1794 int opcode = 0;
1795 int imm = mir->dalvikInsn.vB;
1796
1797 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1798 case kMirOpPackedShiftLeft:
1799 opcode = kX86PsllwRI;
1800 break;
1801 case kMirOpPackedSignedShiftRight:
1802 opcode = kX86PsrawRI;
1803 break;
1804 case kMirOpPackedUnsignedShiftRight:
1805 opcode = kX86PsrlwRI;
1806 break;
1807 default:
1808 LOG(FATAL) << "Unsupported shift operation on byte vector " << opcode;
1809 break;
1810 }
1811
1812 /*
1813 * xmm1 will have low bits
1814 * xmm2 will have high bits
1815 *
1816 * xmm2 = xmm1
1817 * xmm1 = xmm1 .<< N
1818 * xmm2 = xmm2 && 0xFF00FF00FF00FF00FF00FF00FF00FF00
1819 * xmm2 = xmm2 .<< N
1820 * xmm1 = xmm1 | xmm2
1821 */
1822
1823 // Copy xmm1.
1824 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_dest_src1.GetReg());
1825
1826 // Shift lower values.
1827 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1828
1829 // Mask bottom bits.
1830 AndMaskVectorRegister(rs_tmp, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1831
1832 // Shift higher values.
1833 NewLIR2(opcode, rs_tmp.GetReg(), imm);
1834
1835 // Combine back into dest XMM register.
1836 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_tmp.GetReg());
1837}
1838
Mark Mendellfe945782014-05-22 09:52:36 -04001839void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001840 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1841 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1842 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1843 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001844 int opcode = 0;
1845 switch (opsize) {
1846 case k32:
1847 opcode = kX86PslldRI;
1848 break;
1849 case k64:
1850 opcode = kX86PsllqRI;
1851 break;
1852 case kSignedHalf:
1853 case kUnsignedHalf:
1854 opcode = kX86PsllwRI;
1855 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001856 case kSignedByte:
1857 case kUnsignedByte:
1858 GenShiftByteVector(bb, mir);
1859 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001860 default:
1861 LOG(FATAL) << "Unsupported vector shift left " << opsize;
1862 break;
1863 }
1864 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1865}
1866
1867void X86Mir2Lir::GenSignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001868 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1869 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1870 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1871 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001872 int opcode = 0;
1873 switch (opsize) {
1874 case k32:
1875 opcode = kX86PsradRI;
1876 break;
1877 case kSignedHalf:
1878 case kUnsignedHalf:
1879 opcode = kX86PsrawRI;
1880 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001881 case kSignedByte:
1882 case kUnsignedByte:
1883 GenShiftByteVector(bb, mir);
1884 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001885 default:
1886 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
1887 break;
1888 }
1889 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1890}
1891
1892void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001893 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1894 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1895 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1896 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001897 int opcode = 0;
1898 switch (opsize) {
1899 case k32:
1900 opcode = kX86PsrldRI;
1901 break;
1902 case k64:
1903 opcode = kX86PsrlqRI;
1904 break;
1905 case kSignedHalf:
1906 case kUnsignedHalf:
1907 opcode = kX86PsrlwRI;
1908 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001909 case kSignedByte:
1910 case kUnsignedByte:
1911 GenShiftByteVector(bb, mir);
1912 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001913 default:
1914 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1915 break;
1916 }
1917 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1918}
1919
1920void X86Mir2Lir::GenAndVector(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(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1926}
1927
1928void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
1929 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001930 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1931 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1932 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001933 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1934}
1935
1936void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
1937 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001938 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1939 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1940 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001941 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1942}
1943
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001944void X86Mir2Lir::AndMaskVectorRegister(RegStorage rs_src1, uint32_t m1, uint32_t m2, uint32_t m3, uint32_t m4) {
1945 MaskVectorRegister(kX86PandRM, rs_src1, m1, m2, m3, m4);
1946}
1947
1948void X86Mir2Lir::MaskVectorRegister(X86OpCode opcode, RegStorage rs_src1, uint32_t m0, uint32_t m1, uint32_t m2, uint32_t m3) {
1949 // Create temporary MIR as container for 128-bit binary mask.
1950 MIR const_mir;
1951 MIR* const_mirp = &const_mir;
1952 const_mirp->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpConstVector);
1953 const_mirp->dalvikInsn.arg[0] = m0;
1954 const_mirp->dalvikInsn.arg[1] = m1;
1955 const_mirp->dalvikInsn.arg[2] = m2;
1956 const_mirp->dalvikInsn.arg[3] = m3;
1957
1958 // Mask vector with const from literal pool.
1959 AppendOpcodeWithConst(opcode, rs_src1.GetReg(), const_mirp);
1960}
1961
Mark Mendellfe945782014-05-22 09:52:36 -04001962void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001963 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1964 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1965 RegLocation rl_dest = mir_graph_->GetDest(mir);
1966 RegStorage rs_tmp;
1967
1968 int vec_bytes = (mir->dalvikInsn.vC & 0xFFFF) / 8;
1969 int vec_unit_size = 0;
Mark Mendellfe945782014-05-22 09:52:36 -04001970 int opcode = 0;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001971 int extr_opcode = 0;
1972 RegLocation rl_result;
1973
Mark Mendellfe945782014-05-22 09:52:36 -04001974 switch (opsize) {
1975 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001976 extr_opcode = kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001977 opcode = kX86PhadddRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001978 vec_unit_size = 4;
1979 break;
1980 case kSignedByte:
1981 case kUnsignedByte:
1982 extr_opcode = kX86PextrbRRI;
1983 opcode = kX86PhaddwRR;
1984 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001985 break;
1986 case kSignedHalf:
1987 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001988 extr_opcode = kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001989 opcode = kX86PhaddwRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001990 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001991 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001992 case kSingle:
1993 rl_result = EvalLoc(rl_dest, kFPReg, true);
1994 vec_unit_size = 4;
1995 for (int i = 0; i < 3; i++) {
1996 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
1997 NewLIR3(kX86ShufpsRRI, rs_src1.GetReg(), rs_src1.GetReg(), 0x39);
1998 }
1999 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
2000 StoreValue(rl_dest, rl_result);
2001
2002 // For single-precision floats, we are done here
2003 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002004 default:
2005 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2006 break;
2007 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002008
2009 int elems = vec_bytes / vec_unit_size;
2010
2011 // Emulate horizontal add instruction by reducing 2 vectors with 8 values before adding them again
2012 // TODO is overflow handled correctly?
2013 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2014 rs_tmp = Get128BitRegister(AllocTempWide());
2015
2016 // tmp = xmm1 .>> 8.
2017 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_src1.GetReg());
2018 NewLIR2(kX86PsrlwRI, rs_tmp.GetReg(), 8);
2019
2020 // Zero extend low bits in xmm1.
2021 AndMaskVectorRegister(rs_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
2022 }
2023
2024 while (elems > 1) {
2025 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2026 NewLIR2(opcode, rs_tmp.GetReg(), rs_tmp.GetReg());
2027 }
2028 NewLIR2(opcode, rs_src1.GetReg(), rs_src1.GetReg());
2029 elems >>= 1;
2030 }
2031
2032 // Combine the results if we separated them.
2033 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2034 NewLIR2(kX86PaddbRR, rs_src1.GetReg(), rs_tmp.GetReg());
2035 }
2036
2037 // We need to extract to a GPR.
2038 RegStorage temp = AllocTemp();
2039 NewLIR3(extr_opcode, temp.GetReg(), rs_src1.GetReg(), 0);
2040
2041 // Can we do this directly into memory?
2042 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2043 if (rl_result.location == kLocPhysReg) {
2044 // Ensure res is in a core reg
2045 rl_result = EvalLoc(rl_dest, kCoreReg, true);
2046 OpRegReg(kOpAdd, rl_result.reg, temp);
2047 StoreFinalValue(rl_dest, rl_result);
2048 } else {
2049 OpMemReg(kOpAdd, rl_result, temp.GetReg());
2050 }
2051
2052 FreeTemp(temp);
Mark Mendellfe945782014-05-22 09:52:36 -04002053}
2054
2055void X86Mir2Lir::GenReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002056 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2057 RegLocation rl_dest = mir_graph_->GetDest(mir);
2058 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
2059 int extract_index = mir->dalvikInsn.arg[0];
2060 int extr_opcode = 0;
2061 RegLocation rl_result;
2062 bool is_wide = false;
2063
Mark Mendellfe945782014-05-22 09:52:36 -04002064 switch (opsize) {
2065 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002066 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2067 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrdMRI : kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002068 break;
2069 case kSignedHalf:
2070 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002071 rl_result= UpdateLocTyped(rl_dest, kCoreReg);
2072 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrwMRI : kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002073 break;
2074 default:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002075 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2076 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002077 break;
2078 }
Mark Mendellfe945782014-05-22 09:52:36 -04002079
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002080 if (rl_result.location == kLocPhysReg) {
2081 NewLIR3(extr_opcode, rl_result.reg.GetReg(), rs_src1.GetReg(), extract_index);
2082 if (is_wide == true) {
2083 StoreFinalValue(rl_dest, rl_result);
2084 } else {
2085 StoreFinalValueWide(rl_dest, rl_result);
2086 }
2087 } else {
2088 int displacement = SRegOffset(rl_result.s_reg_low);
2089 LIR *l = NewLIR3(extr_opcode, rs_rX86_SP.GetReg(), displacement, rs_src1.GetReg());
2090 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is_wide /* is_64bit */);
2091 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is_wide /* is_64bit */);
2092 }
Mark Mendellfe945782014-05-22 09:52:36 -04002093}
2094
2095void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002096 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2097 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2098 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
2099 int op_low = 0, op_high = 0, imm = 0, op_mov = kX86MovdxrRR;
2100 RegisterClass reg_type = kCoreReg;
2101
Mark Mendellfe945782014-05-22 09:52:36 -04002102 switch (opsize) {
2103 case k32:
2104 op_low = kX86PshufdRRI;
2105 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002106 case kSingle:
2107 op_low = kX86PshufdRRI;
2108 op_mov = kX86Mova128RR;
2109 reg_type = kFPReg;
2110 break;
2111 case k64:
2112 op_low = kX86PshufdRRI;
2113 imm = 0x44;
2114 break;
2115 case kDouble:
2116 op_low = kX86PshufdRRI;
2117 op_mov = kX86Mova128RR;
2118 reg_type = kFPReg;
2119 imm = 0x44;
2120 break;
2121 case kSignedByte:
2122 case kUnsignedByte:
2123 // Shuffle 8 bit value into 16 bit word.
2124 // We set val = val + (val << 8) below and use 16 bit shuffle.
Mark Mendellfe945782014-05-22 09:52:36 -04002125 case kSignedHalf:
2126 case kUnsignedHalf:
2127 // Handles low quadword.
2128 op_low = kX86PshuflwRRI;
2129 // Handles upper quadword.
2130 op_high = kX86PshufdRRI;
2131 break;
2132 default:
2133 LOG(FATAL) << "Unsupported vector set " << opsize;
2134 break;
2135 }
2136
Mark Mendellfe945782014-05-22 09:52:36 -04002137 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002138
2139 // Load the value from the VR into the reg.
2140 if (rl_src.wide == 0) {
2141 rl_src = LoadValue(rl_src, reg_type);
2142 } else {
2143 rl_src = LoadValueWide(rl_src, reg_type);
2144 }
2145
2146 // If opsize is 8 bits wide then double value and use 16 bit shuffle instead.
2147 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2148 RegStorage temp = AllocTemp();
2149 // val = val + (val << 8).
2150 NewLIR2(kX86Mov32RR, temp.GetReg(), rl_src.reg.GetReg());
2151 NewLIR2(kX86Sal32RI, temp.GetReg(), 8);
2152 NewLIR2(kX86Or32RR, rl_src.reg.GetReg(), temp.GetReg());
2153 FreeTemp(temp);
2154 }
Mark Mendellfe945782014-05-22 09:52:36 -04002155
2156 // Load the value into the XMM register.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002157 NewLIR2(op_mov, rs_dest.GetReg(), rl_src.reg.GetReg());
Mark Mendellfe945782014-05-22 09:52:36 -04002158
2159 // Now shuffle the value across the destination.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002160 NewLIR3(op_low, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002161
2162 // And then repeat as needed.
2163 if (op_high != 0) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002164 NewLIR3(op_high, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002165 }
2166}
2167
Mark Mendelld65c51a2014-04-29 16:55:20 -04002168LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
2169 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2170 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
2171 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
2172 args[2] == p->operands[2] && args[3] == p->operands[3]) {
2173 return p;
2174 }
2175 }
2176 return nullptr;
2177}
2178
2179LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
2180 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
2181 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2182 new_value->operands[0] = args[0];
2183 new_value->operands[1] = args[1];
2184 new_value->operands[2] = args[2];
2185 new_value->operands[3] = args[3];
2186 new_value->next = const_vectors_;
2187 if (const_vectors_ == nullptr) {
2188 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
2189 }
2190 estimated_native_code_size_ += 16; // Space for one vector.
2191 const_vectors_ = new_value;
2192 return new_value;
2193}
2194
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002195// ------------ ABI support: mapping of args to physical registers -------------
Andreas Gampeccc60262014-07-04 18:02:38 -07002196RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide,
2197 bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002198 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
Andreas Gampeccc60262014-07-04 18:02:38 -07002199 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) /
2200 sizeof(SpecialTargetRegister);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002201 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
Andreas Gampeccc60262014-07-04 18:02:38 -07002202 kFArg4, kFArg5, kFArg6, kFArg7};
2203 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) /
2204 sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002205
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002206 if (is_double_or_float) {
2207 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002208 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide ? kWide : kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002209 }
2210 } else {
2211 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002212 return ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++],
2213 is_ref ? kRef : (is_wide ? kWide : kNotWide));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002214 }
2215 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07002216 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002217}
2218
2219RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
2220 DCHECK(IsInitialized());
2221 auto res = mapping_.find(in_position);
2222 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
2223}
2224
Andreas Gampeccc60262014-07-04 18:02:38 -07002225void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count,
2226 InToRegStorageMapper* mapper) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002227 DCHECK(mapper != nullptr);
2228 max_mapped_in_ = -1;
2229 is_there_stack_mapped_ = false;
2230 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07002231 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
2232 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002233 if (reg.Valid()) {
2234 mapping_[in_position] = reg;
2235 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07002236 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002237 // We covered 2 args, so skip the next one
2238 in_position++;
2239 }
2240 } else {
2241 is_there_stack_mapped_ = true;
2242 }
2243 }
2244 initialized_ = true;
2245}
2246
2247RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002248 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002249 return GetCoreArgMappingToPhysicalReg(arg_num);
2250 }
2251
2252 if (!in_to_reg_storage_mapping_.IsInitialized()) {
2253 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2254 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
2255
Chao-ying Fua77ee512014-07-01 17:43:41 -07002256 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002257 in_to_reg_storage_mapping_.Initialize(arg_locs, cu_->num_ins, &mapper);
2258 }
2259 return in_to_reg_storage_mapping_.Get(arg_num);
2260}
2261
2262RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
2263 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
2264 // Not used for 64-bit, TODO: Move X86_32 to the same framework
2265 switch (core_arg_num) {
2266 case 0:
2267 return rs_rX86_ARG1;
2268 case 1:
2269 return rs_rX86_ARG2;
2270 case 2:
2271 return rs_rX86_ARG3;
2272 default:
2273 return RegStorage::InvalidReg();
2274 }
2275}
2276
2277// ---------End of ABI support: mapping of args to physical registers -------------
2278
2279/*
2280 * If there are any ins passed in registers that have not been promoted
2281 * to a callee-save register, flush them to the frame. Perform initial
2282 * assignment of promoted arguments.
2283 *
2284 * ArgLocs is an array of location records describing the incoming arguments
2285 * with one location record per word of argument.
2286 */
2287void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002288 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002289 /*
2290 * Dummy up a RegLocation for the incoming Method*
2291 * It will attempt to keep kArg0 live (or copy it to home location
2292 * if promoted).
2293 */
2294
2295 RegLocation rl_src = rl_method;
2296 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -07002297 rl_src.reg = TargetReg(kArg0, kRef);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002298 rl_src.home = false;
2299 MarkLive(rl_src);
2300 StoreValue(rl_method, rl_src);
2301 // If Method* has been promoted, explicitly flush
2302 if (rl_method.location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002303 StoreRefDisp(rs_rX86_SP, 0, As32BitReg(TargetReg(kArg0, kRef)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002304 }
2305
2306 if (cu_->num_ins == 0) {
2307 return;
2308 }
2309
2310 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2311 /*
2312 * Copy incoming arguments to their proper home locations.
2313 * NOTE: an older version of dx had an issue in which
2314 * it would reuse static method argument registers.
2315 * This could result in the same Dalvik virtual register
2316 * being promoted to both core and fp regs. To account for this,
2317 * we only copy to the corresponding promoted physical register
2318 * if it matches the type of the SSA name for the incoming
2319 * argument. It is also possible that long and double arguments
2320 * end up half-promoted. In those cases, we must flush the promoted
2321 * half to memory as well.
2322 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002323 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002324 for (int i = 0; i < cu_->num_ins; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002325 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002326 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002327
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002328 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002329 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002330 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002331
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002332 // We have already updated the arg location with promoted info
2333 // so we can be based on it.
2334 if (t_loc->location == kLocPhysReg) {
2335 // Just copy it.
2336 OpRegCopy(t_loc->reg, reg);
2337 } else {
2338 // Needs flush.
2339 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002340 StoreRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002341 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002342 StoreBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002343 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002344 }
2345 }
2346 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002347 // If arriving in frame & promoted.
2348 if (t_loc->location == kLocPhysReg) {
2349 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002350 LoadRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002351 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002352 LoadBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002353 t_loc->wide ? k64 : k32, kNotVolatile);
2354 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002355 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002356 }
2357 if (t_loc->wide) {
2358 // Increment i to skip the next one.
2359 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002360 }
2361 }
2362}
2363
2364/*
2365 * Load up to 5 arguments, the first three of which will be in
2366 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2367 * and as part of the load sequence, it must be replaced with
2368 * the target method pointer. Note, this may also be called
2369 * for "range" variants if the number of arguments is 5 or fewer.
2370 */
2371int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2372 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2373 const MethodReference& target_method,
2374 uint32_t vtable_idx, uintptr_t direct_code,
2375 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002376 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002377 return Mir2Lir::GenDalvikArgsNoRange(info,
2378 call_state, pcrLabel, next_call_insn,
2379 target_method,
2380 vtable_idx, direct_code,
2381 direct_method, type, skip_this);
2382 }
2383 return GenDalvikArgsRange(info,
2384 call_state, pcrLabel, next_call_insn,
2385 target_method,
2386 vtable_idx, direct_code,
2387 direct_method, type, skip_this);
2388}
2389
2390/*
2391 * May have 0+ arguments (also used for jumbo). Note that
2392 * source virtual registers may be in physical registers, so may
2393 * need to be flushed to home location before copying. This
2394 * applies to arg3 and above (see below).
2395 *
2396 * Two general strategies:
2397 * If < 20 arguments
2398 * Pass args 3-18 using vldm/vstm block copy
2399 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2400 * If 20+ arguments
2401 * Pass args arg19+ using memcpy block copy
2402 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2403 *
2404 */
2405int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2406 LIR** pcrLabel, NextCallInsn next_call_insn,
2407 const MethodReference& target_method,
2408 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2409 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002410 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002411 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2412 pcrLabel, next_call_insn,
2413 target_method,
2414 vtable_idx, direct_code, direct_method,
2415 type, skip_this);
2416 }
2417
2418 /* If no arguments, just return */
2419 if (info->num_arg_words == 0)
2420 return call_state;
2421
2422 const int start_index = skip_this ? 1 : 0;
2423
Chao-ying Fua77ee512014-07-01 17:43:41 -07002424 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002425 InToRegStorageMapping in_to_reg_storage_mapping;
2426 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2427 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2428 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
Serguei Katkov8e3acdd2014-07-15 12:01:00 +07002429 info->args[last_mapped_in].wide ? 2 : 1;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002430 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2431
2432 // Fisrt of all, check whether it make sense to use bulk copying
2433 // Optimization is aplicable only for range case
2434 // TODO: make a constant instead of 2
2435 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2436 // Scan the rest of the args - if in phys_reg flush to memory
2437 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2438 RegLocation loc = info->args[next_arg];
2439 if (loc.wide) {
2440 loc = UpdateLocWide(loc);
2441 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002442 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002443 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002444 }
2445 next_arg += 2;
2446 } else {
2447 loc = UpdateLoc(loc);
2448 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002449 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002450 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002451 }
2452 next_arg++;
2453 }
2454 }
2455
2456 // Logic below assumes that Method pointer is at offset zero from SP.
2457 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
2458
2459 // The rest can be copied together
2460 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
Andreas Gampeccc60262014-07-04 18:02:38 -07002461 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped,
2462 cu_->instruction_set);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002463
2464 int current_src_offset = start_offset;
2465 int current_dest_offset = outs_offset;
2466
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002467 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2468 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002469 while (regs_left_to_pass_via_stack > 0) {
2470 // This is based on the knowledge that the stack itself is 16-byte aligned.
2471 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2472 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2473 size_t bytes_to_move;
2474
2475 /*
2476 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2477 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2478 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2479 * We do this because we could potentially do a smaller move to align.
2480 */
2481 if (regs_left_to_pass_via_stack == 4 ||
2482 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2483 // Moving 128-bits via xmm register.
2484 bytes_to_move = sizeof(uint32_t) * 4;
2485
2486 // Allocate a free xmm temp. Since we are working through the calling sequence,
2487 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2488 // there are no free registers.
2489 RegStorage temp = AllocTempDouble();
2490
2491 LIR* ld1 = nullptr;
2492 LIR* ld2 = nullptr;
2493 LIR* st1 = nullptr;
2494 LIR* st2 = nullptr;
2495
2496 /*
2497 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2498 * do an aligned move. If we have 8-byte alignment, then do the move in two
2499 * parts. This approach prevents possible cache line splits. Finally, fall back
2500 * to doing an unaligned move. In most cases we likely won't split the cache
2501 * line but we cannot prove it and thus take a conservative approach.
2502 */
2503 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2504 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2505
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002506 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002507 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002508 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002509 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002510 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovLo128FP);
2511 ld2 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002512 kMovHi128FP);
2513 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002514 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002515 }
2516
2517 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002518 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002519 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002520 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovLo128FP);
2521 st2 = OpMovMemReg(rs_rX86_SP, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002522 temp, kMovHi128FP);
2523 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002524 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002525 }
2526
2527 // TODO If we could keep track of aliasing information for memory accesses that are wider
2528 // than 64-bit, we wouldn't need to set up a barrier.
2529 if (ld1 != nullptr) {
2530 if (ld2 != nullptr) {
2531 // For 64-bit load we can actually set up the aliasing information.
2532 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2533 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2534 } else {
2535 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002536 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002537 }
2538 }
2539 if (st1 != nullptr) {
2540 if (st2 != nullptr) {
2541 // For 64-bit store we can actually set up the aliasing information.
2542 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2543 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2544 } else {
2545 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002546 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002547 }
2548 }
2549
2550 // Free the temporary used for the data movement.
2551 FreeTemp(temp);
2552 } else {
2553 // Moving 32-bits via general purpose register.
2554 bytes_to_move = sizeof(uint32_t);
2555
2556 // Instead of allocating a new temp, simply reuse one of the registers being used
2557 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07002558 RegStorage temp = TargetReg(kArg3, kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002559
2560 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002561 Load32Disp(rs_rX86_SP, current_src_offset, temp);
2562 Store32Disp(rs_rX86_SP, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002563 }
2564
2565 current_src_offset += bytes_to_move;
2566 current_dest_offset += bytes_to_move;
2567 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2568 }
2569 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2570 }
2571
2572 // Now handle rest not registers if they are
2573 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002574 RegStorage regSingle = TargetReg(kArg2, kNotWide);
2575 RegStorage regWide = TargetReg(kArg3, kWide);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002576 for (int i = start_index;
2577 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002578 RegLocation rl_arg = info->args[i];
2579 rl_arg = UpdateRawLoc(rl_arg);
2580 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2581 if (!reg.Valid()) {
2582 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2583
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002584 {
2585 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2586 if (rl_arg.wide) {
2587 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002588 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002589 } else {
2590 LoadValueDirectWideFixed(rl_arg, regWide);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002591 StoreBaseDisp(rs_rX86_SP, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002592 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002593 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002594 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002595 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002596 } else {
2597 LoadValueDirectFixed(rl_arg, regSingle);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002598 StoreBaseDisp(rs_rX86_SP, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002599 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002600 }
2601 }
2602 call_state = next_call_insn(cu_, info, call_state, target_method,
2603 vtable_idx, direct_code, direct_method, type);
2604 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002605 if (rl_arg.wide) {
2606 i++;
2607 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002608 }
2609 }
2610
2611 // Finish with mapped registers
2612 for (int i = start_index; i <= last_mapped_in; i++) {
2613 RegLocation rl_arg = info->args[i];
2614 rl_arg = UpdateRawLoc(rl_arg);
2615 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2616 if (reg.Valid()) {
2617 if (rl_arg.wide) {
2618 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002619 } else {
2620 LoadValueDirectFixed(rl_arg, reg);
2621 }
2622 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2623 direct_code, direct_method, type);
2624 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002625 if (rl_arg.wide) {
2626 i++;
2627 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002628 }
2629
2630 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2631 direct_code, direct_method, type);
2632 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00002633 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002634 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002635 } else {
2636 *pcrLabel = nullptr;
2637 // In lieu of generating a check for kArg1 being null, we need to
2638 // perform a load when doing implicit checks.
2639 RegStorage tmp = AllocTemp();
Andreas Gampeccc60262014-07-04 18:02:38 -07002640 Load32Disp(TargetReg(kArg1, kRef), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002641 MarkPossibleNullPointerException(info->opt_flags);
2642 FreeTemp(tmp);
2643 }
2644 }
2645 return call_state;
2646}
2647
Andreas Gampe98430592014-07-27 19:44:50 -07002648bool X86Mir2Lir::GenInlinedCharAt(CallInfo* info) {
2649 // Location of reference to data array
2650 int value_offset = mirror::String::ValueOffset().Int32Value();
2651 // Location of count
2652 int count_offset = mirror::String::CountOffset().Int32Value();
2653 // Starting offset within data array
2654 int offset_offset = mirror::String::OffsetOffset().Int32Value();
2655 // Start of char data with array_
2656 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
2657
2658 RegLocation rl_obj = info->args[0];
2659 RegLocation rl_idx = info->args[1];
2660 rl_obj = LoadValue(rl_obj, kRefReg);
2661 // X86 wants to avoid putting a constant index into a register.
2662 if (!rl_idx.is_const) {
2663 rl_idx = LoadValue(rl_idx, kCoreReg);
2664 }
2665 RegStorage reg_max;
2666 GenNullCheck(rl_obj.reg, info->opt_flags);
2667 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
2668 LIR* range_check_branch = nullptr;
2669 RegStorage reg_off;
2670 RegStorage reg_ptr;
2671 if (range_check) {
2672 // On x86, we can compare to memory directly
2673 // Set up a launch pad to allow retry in case of bounds violation */
2674 if (rl_idx.is_const) {
2675 LIR* comparison;
2676 range_check_branch = OpCmpMemImmBranch(
2677 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
2678 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr, &comparison);
2679 MarkPossibleNullPointerExceptionAfter(0, comparison);
2680 } else {
2681 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
2682 MarkPossibleNullPointerException(0);
2683 range_check_branch = OpCondBranch(kCondUge, nullptr);
2684 }
2685 }
2686 reg_off = AllocTemp();
2687 reg_ptr = AllocTempRef();
2688 Load32Disp(rl_obj.reg, offset_offset, reg_off);
2689 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
2690 if (rl_idx.is_const) {
2691 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
2692 } else {
2693 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
2694 }
2695 FreeTemp(rl_obj.reg);
2696 if (rl_idx.location == kLocPhysReg) {
2697 FreeTemp(rl_idx.reg);
2698 }
2699 RegLocation rl_dest = InlineTarget(info);
2700 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
2701 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
2702 FreeTemp(reg_off);
2703 FreeTemp(reg_ptr);
2704 StoreValue(rl_dest, rl_result);
2705 if (range_check) {
2706 DCHECK(range_check_branch != nullptr);
2707 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
2708 AddIntrinsicSlowPath(info, range_check_branch);
2709 }
2710 return true;
2711}
2712
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002713bool X86Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
2714 RegLocation rl_dest = InlineTarget(info);
2715
2716 // Early exit if the result is unused.
2717 if (rl_dest.orig_sreg < 0) {
2718 return true;
2719 }
2720
2721 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
2722
2723 if (cu_->target64) {
2724 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<8>());
2725 } else {
2726 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<4>());
2727 }
2728
2729 StoreValue(rl_dest, rl_result);
2730 return true;
2731}
2732
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002733} // namespace art