blob: 821179d725e2d174a7f80b2ff7a9a85ab042812f [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 Fue0ccdc02014-06-06 17:32:37 -0700763 if (size == kReference) {
764 return kRefReg;
765 }
766 return kCoreReg;
767 }
768
Vladimir Marko674744e2014-04-24 15:18:26 +0100769 if (UNLIKELY(is_volatile)) {
770 // On x86, atomic 64-bit load/store requires an fp register.
771 // Smaller aligned load/store is atomic for both core and fp registers.
772 if (size == k64 || size == kDouble) {
773 return kFPReg;
774 }
775 }
776 return RegClassBySize(size);
777}
778
Elena Sayapinadd644502014-07-01 18:39:52 +0700779X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800780 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700781 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800782 method_address_insns_(arena, 100, kGrowableArrayMisc),
783 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800784 call_method_insns_(arena, 100, kGrowableArrayMisc),
Elena Sayapinadd644502014-07-01 18:39:52 +0700785 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400786 const_vectors_(nullptr) {
787 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700788 if (kIsDebugBuild) {
789 for (int i = 0; i < kX86Last; i++) {
790 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
791 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400792 << " is wrong: expecting " << i << ", seeing "
793 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700794 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 }
796 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700797 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700798 rs_rX86_SP = rs_rX86_SP_64;
799
800 rs_rX86_ARG0 = rs_rDI;
801 rs_rX86_ARG1 = rs_rSI;
802 rs_rX86_ARG2 = rs_rDX;
803 rs_rX86_ARG3 = rs_rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700804 rs_rX86_ARG4 = rs_r8;
805 rs_rX86_ARG5 = rs_r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700806 rs_rX86_FARG0 = rs_fr0;
807 rs_rX86_FARG1 = rs_fr1;
808 rs_rX86_FARG2 = rs_fr2;
809 rs_rX86_FARG3 = rs_fr3;
810 rs_rX86_FARG4 = rs_fr4;
811 rs_rX86_FARG5 = rs_fr5;
812 rs_rX86_FARG6 = rs_fr6;
813 rs_rX86_FARG7 = rs_fr7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700814 rX86_ARG0 = rDI;
815 rX86_ARG1 = rSI;
816 rX86_ARG2 = rDX;
817 rX86_ARG3 = rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700818 rX86_ARG4 = r8;
819 rX86_ARG5 = r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700820 rX86_FARG0 = fr0;
821 rX86_FARG1 = fr1;
822 rX86_FARG2 = fr2;
823 rX86_FARG3 = fr3;
824 rX86_FARG4 = fr4;
825 rX86_FARG5 = fr5;
826 rX86_FARG6 = fr6;
827 rX86_FARG7 = fr7;
Mark Mendell55884bc2014-06-10 10:21:29 -0400828 rs_rX86_INVOKE_TGT = rs_rDI;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700829 } else {
830 rs_rX86_SP = rs_rX86_SP_32;
831
832 rs_rX86_ARG0 = rs_rAX;
833 rs_rX86_ARG1 = rs_rCX;
834 rs_rX86_ARG2 = rs_rDX;
835 rs_rX86_ARG3 = rs_rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700836 rs_rX86_ARG4 = RegStorage::InvalidReg();
837 rs_rX86_ARG5 = RegStorage::InvalidReg();
838 rs_rX86_FARG0 = rs_rAX;
839 rs_rX86_FARG1 = rs_rCX;
840 rs_rX86_FARG2 = rs_rDX;
841 rs_rX86_FARG3 = rs_rBX;
842 rs_rX86_FARG4 = RegStorage::InvalidReg();
843 rs_rX86_FARG5 = RegStorage::InvalidReg();
844 rs_rX86_FARG6 = RegStorage::InvalidReg();
845 rs_rX86_FARG7 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700846 rX86_ARG0 = rAX;
847 rX86_ARG1 = rCX;
848 rX86_ARG2 = rDX;
849 rX86_ARG3 = rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700850 rX86_FARG0 = rAX;
851 rX86_FARG1 = rCX;
852 rX86_FARG2 = rDX;
853 rX86_FARG3 = rBX;
Mark Mendell55884bc2014-06-10 10:21:29 -0400854 rs_rX86_INVOKE_TGT = rs_rAX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700855 // TODO(64): Initialize with invalid reg
856// rX86_ARG4 = RegStorage::InvalidReg();
857// rX86_ARG5 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700858 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700859 rs_rX86_RET0 = rs_rAX;
860 rs_rX86_RET1 = rs_rDX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700861 rs_rX86_COUNT = rs_rCX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700862 rX86_RET0 = rAX;
863 rX86_RET1 = rDX;
864 rX86_INVOKE_TGT = rAX;
865 rX86_COUNT = rCX;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700866
867 // Initialize the number of reserved vector registers
868 num_reserved_vector_regs_ = -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869}
870
871Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
872 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700873 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874}
875
Andreas Gampe98430592014-07-27 19:44:50 -0700876// Not used in x86(-64)
877RegStorage X86Mir2Lir::LoadHelper(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700878 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
879 return RegStorage::InvalidReg();
880}
881
Dave Allisonb373e092014-02-20 16:06:36 -0800882LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
Dave Allison69dfe512014-07-11 17:11:58 +0000883 // First load the pointer in fs:[suspend-trigger] into eax
884 // Then use a test instruction to indirect via that address.
Dave Allisondfd3b472014-07-16 16:04:32 -0700885 if (cu_->target64) {
886 NewLIR2(kX86Mov64RT, rs_rAX.GetReg(),
887 Thread::ThreadSuspendTriggerOffset<8>().Int32Value());
888 } else {
889 NewLIR2(kX86Mov32RT, rs_rAX.GetReg(),
890 Thread::ThreadSuspendTriggerOffset<4>().Int32Value());
891 }
Dave Allison69dfe512014-07-11 17:11:58 +0000892 return NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rAX.GetReg(), 0);
Dave Allisonb373e092014-02-20 16:06:36 -0800893}
894
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700895uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700896 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 return X86Mir2Lir::EncodingMap[opcode].flags;
898}
899
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700900const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700901 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 return X86Mir2Lir::EncodingMap[opcode].name;
903}
904
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700905const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700906 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 return X86Mir2Lir::EncodingMap[opcode].fmt;
908}
909
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000910void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
911 // Can we do this directly to memory?
912 rl_dest = UpdateLocWide(rl_dest);
913 if ((rl_dest.location == kLocDalvikFrame) ||
914 (rl_dest.location == kLocCompilerTemp)) {
915 int32_t val_lo = Low32Bits(value);
916 int32_t val_hi = High32Bits(value);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700917 int r_base = rs_rX86_SP.GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000918 int displacement = SRegOffset(rl_dest.s_reg_low);
919
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100920 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800921 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000922 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
923 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800924 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000925 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
926 false /* is_load */, true /* is64bit */);
927 return;
928 }
929
930 // Just use the standard code to do the generation.
931 Mir2Lir::GenConstWide(rl_dest, value);
932}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800933
934// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
935void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
936 LOG(INFO) << "location: " << loc.location << ','
937 << (loc.wide ? " w" : " ")
938 << (loc.defined ? " D" : " ")
939 << (loc.is_const ? " c" : " ")
940 << (loc.fp ? " F" : " ")
941 << (loc.core ? " C" : " ")
942 << (loc.ref ? " r" : " ")
943 << (loc.high_word ? " h" : " ")
944 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800945 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000946 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800947 << ", s_reg: " << loc.s_reg_low
948 << ", orig: " << loc.orig_sreg;
949}
950
Mark Mendell67c39c42014-01-31 17:28:00 -0800951void X86Mir2Lir::Materialize() {
952 // A good place to put the analysis before starting.
953 AnalyzeMIR();
954
955 // Now continue with regular code generation.
956 Mir2Lir::Materialize();
957}
958
Jeff Hao49161ce2014-03-12 11:05:25 -0700959void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800960 SpecialTargetRegister symbolic_reg) {
961 /*
962 * For x86, just generate a 32 bit move immediate instruction, that will be filled
963 * in at 'link time'. For now, put a unique value based on target to ensure that
964 * code deduplication works.
965 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700966 int target_method_idx = target_method.dex_method_index;
967 const DexFile* target_dex_file = target_method.dex_file;
968 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
969 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800970
Jeff Hao49161ce2014-03-12 11:05:25 -0700971 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700972 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
973 TargetReg(symbolic_reg, kNotWide).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700974 static_cast<int>(target_method_id_ptr), target_method_idx,
975 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800976 AppendLIR(move);
977 method_address_insns_.Insert(move);
978}
979
Fred Shihe7f82e22014-08-06 10:46:37 -0700980void X86Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
981 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800982 /*
983 * For x86, just generate a 32 bit move immediate instruction, that will be filled
984 * in at 'link time'. For now, put a unique value based on target to ensure that
985 * code deduplication works.
986 */
Fred Shihe7f82e22014-08-06 10:46:37 -0700987 const DexFile::TypeId& id = dex_file.GetTypeId(type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800988 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
989
990 // Generate the move instruction with the unique pointer and save index and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700991 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
992 TargetReg(symbolic_reg, kNotWide).GetReg(),
Fred Shihe7f82e22014-08-06 10:46:37 -0700993 static_cast<int>(ptr), type_idx,
994 WrapPointer(const_cast<DexFile*>(&dex_file)));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800995 AppendLIR(move);
996 class_type_address_insns_.Insert(move);
997}
998
Jeff Hao49161ce2014-03-12 11:05:25 -0700999LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001000 /*
1001 * For x86, just generate a 32 bit call relative instruction, that will be filled
1002 * in at 'link time'. For now, put a unique value based on target to ensure that
1003 * code deduplication works.
1004 */
Jeff Hao49161ce2014-03-12 11:05:25 -07001005 int target_method_idx = target_method.dex_method_index;
1006 const DexFile* target_dex_file = target_method.dex_file;
1007 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
1008 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001009
Jeff Hao49161ce2014-03-12 11:05:25 -07001010 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
1011 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
1012 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001013 AppendLIR(call);
1014 call_method_insns_.Insert(call);
1015 return call;
1016}
1017
1018void X86Mir2Lir::InstallLiteralPools() {
1019 // These are handled differently for x86.
1020 DCHECK(code_literal_list_ == nullptr);
1021 DCHECK(method_literal_list_ == nullptr);
1022 DCHECK(class_literal_list_ == nullptr);
1023
Mark Mendelld65c51a2014-04-29 16:55:20 -04001024 // Align to 16 byte boundary. We have implicit knowledge that the start of the method is
1025 // on a 4 byte boundary. How can I check this if it changes (other than aligned loads
1026 // will fail at runtime)?
1027 if (const_vectors_ != nullptr) {
1028 int align_size = (16-4) - (code_buffer_.size() & 0xF);
1029 if (align_size < 0) {
1030 align_size += 16;
1031 }
1032
1033 while (align_size > 0) {
1034 code_buffer_.push_back(0);
1035 align_size--;
1036 }
1037 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001038 PushWord(&code_buffer_, p->operands[0]);
1039 PushWord(&code_buffer_, p->operands[1]);
1040 PushWord(&code_buffer_, p->operands[2]);
1041 PushWord(&code_buffer_, p->operands[3]);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001042 }
1043 }
1044
Mark Mendell55d0eac2014-02-06 11:02:52 -08001045 // Handle the fixups for methods.
1046 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
1047 LIR* p = method_address_insns_.Get(i);
1048 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001049 uint32_t target_method_idx = p->operands[2];
1050 const DexFile* target_dex_file =
1051 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001052
1053 // The offset to patch is the last 4 bytes of the instruction.
1054 int patch_offset = p->offset + p->flags.size - 4;
1055 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
1056 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -07001057 target_method_idx, target_dex_file,
1058 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001059 patch_offset);
1060 }
1061
1062 // Handle the fixups for class types.
1063 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
1064 LIR* p = class_type_address_insns_.Get(i);
1065 DCHECK_EQ(p->opcode, kX86Mov32RI);
Fred Shihe7f82e22014-08-06 10:46:37 -07001066
1067 const DexFile* class_dex_file =
1068 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Jeff Hao49161ce2014-03-12 11:05:25 -07001069 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001070
1071 // The offset to patch is the last 4 bytes of the instruction.
1072 int patch_offset = p->offset + p->flags.size - 4;
1073 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -07001074 cu_->method_idx, target_method_idx, class_dex_file,
1075 patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001076 }
1077
1078 // And now the PC-relative calls to methods.
1079 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
1080 LIR* p = call_method_insns_.Get(i);
1081 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001082 uint32_t target_method_idx = p->operands[1];
1083 const DexFile* target_dex_file =
1084 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001085
1086 // The offset to patch is the last 4 bytes of the instruction.
1087 int patch_offset = p->offset + p->flags.size - 4;
1088 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001089 cu_->method_idx, cu_->invoke_type,
1090 target_method_idx, target_dex_file,
1091 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001092 patch_offset, -4 /* offset */);
1093 }
1094
1095 // And do the normal processing.
1096 Mir2Lir::InstallLiteralPools();
1097}
1098
DaniilSokolov70c4f062014-06-24 17:34:00 -07001099bool X86Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
DaniilSokolov70c4f062014-06-24 17:34:00 -07001100 RegLocation rl_src = info->args[0];
1101 RegLocation rl_srcPos = info->args[1];
1102 RegLocation rl_dst = info->args[2];
1103 RegLocation rl_dstPos = info->args[3];
1104 RegLocation rl_length = info->args[4];
1105 if (rl_srcPos.is_const && (mir_graph_->ConstantValue(rl_srcPos) < 0)) {
1106 return false;
1107 }
1108 if (rl_dstPos.is_const && (mir_graph_->ConstantValue(rl_dstPos) < 0)) {
1109 return false;
1110 }
1111 ClobberCallerSave();
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001112 LockCallTemps(); // Using fixed registers.
1113 RegStorage tmp_reg = cu_->target64 ? rs_r11 : rs_rBX;
1114 LoadValueDirectFixed(rl_src, rs_rAX);
1115 LoadValueDirectFixed(rl_dst, rs_rCX);
1116 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_rAX, rs_rCX, nullptr);
1117 LIR* src_null_branch = OpCmpImmBranch(kCondEq, rs_rAX, 0, nullptr);
1118 LIR* dst_null_branch = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1119 LoadValueDirectFixed(rl_length, rs_rDX);
1120 // If the length of the copy is > 128 characters (256 bytes) or negative then go slow path.
1121 LIR* len_too_big = OpCmpImmBranch(kCondHi, rs_rDX, 128, nullptr);
1122 LoadValueDirectFixed(rl_src, rs_rAX);
1123 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001124 LIR* src_bad_len = nullptr;
1125 LIR* srcPos_negative = nullptr;
1126 if (!rl_srcPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001127 LoadValueDirectFixed(rl_srcPos, tmp_reg);
1128 srcPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1129 OpRegReg(kOpAdd, tmp_reg, rs_rDX);
1130 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001131 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001132 int32_t pos_val = mir_graph_->ConstantValue(rl_srcPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001133 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001134 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001135 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001136 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1137 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001138 }
1139 }
1140 LIR* dstPos_negative = nullptr;
1141 LIR* dst_bad_len = nullptr;
1142 LoadValueDirectFixed(rl_dst, rs_rAX);
1143 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1144 if (!rl_dstPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001145 LoadValueDirectFixed(rl_dstPos, tmp_reg);
1146 dstPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1147 OpRegRegReg(kOpAdd, tmp_reg, tmp_reg, rs_rDX);
1148 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001149 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001150 int32_t pos_val = mir_graph_->ConstantValue(rl_dstPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001151 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001152 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001153 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001154 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1155 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001156 }
1157 }
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001158 // Everything is checked now.
1159 LoadValueDirectFixed(rl_src, rs_rAX);
1160 LoadValueDirectFixed(rl_dst, tmp_reg);
1161 LoadValueDirectFixed(rl_srcPos, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001162 NewLIR5(kX86Lea32RA, rs_rAX.GetReg(), rs_rAX.GetReg(),
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001163 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value());
1164 // RAX now holds the address of the first src element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001165
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001166 LoadValueDirectFixed(rl_dstPos, rs_rCX);
1167 NewLIR5(kX86Lea32RA, tmp_reg.GetReg(), tmp_reg.GetReg(),
1168 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value() );
1169 // RBX now holds the address of the first dst element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001170
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001171 // Check if the number of elements to be copied is odd or even. If odd
DaniilSokolov70c4f062014-06-24 17:34:00 -07001172 // then copy the first element (so that the remaining number of elements
1173 // is even).
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001174 LoadValueDirectFixed(rl_length, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001175 OpRegImm(kOpAnd, rs_rCX, 1);
1176 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1177 OpRegImm(kOpSub, rs_rDX, 1);
1178 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001179 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001180
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001181 // Since the remaining number of elements is even, we will copy by
DaniilSokolov70c4f062014-06-24 17:34:00 -07001182 // two elements at a time.
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001183 LIR* beginLoop = NewLIR0(kPseudoTargetLabel);
1184 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_rDX, 0, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001185 OpRegImm(kOpSub, rs_rDX, 2);
1186 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001187 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001188 OpUnconditionalBranch(beginLoop);
1189 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1190 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1191 LIR *return_point = NewLIR0(kPseudoTargetLabel);
1192 jmp_to_ret->target = return_point;
1193 jmp_to_begin_loop->target = beginLoop;
1194 src_dst_same->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001195 len_too_big->target = check_failed;
1196 src_null_branch->target = check_failed;
1197 if (srcPos_negative != nullptr)
1198 srcPos_negative ->target = check_failed;
1199 if (src_bad_len != nullptr)
1200 src_bad_len->target = check_failed;
1201 dst_null_branch->target = check_failed;
1202 if (dstPos_negative != nullptr)
1203 dstPos_negative->target = check_failed;
1204 if (dst_bad_len != nullptr)
1205 dst_bad_len->target = check_failed;
1206 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
1207 return true;
1208}
1209
1210
Mark Mendell4028a6c2014-02-19 20:06:20 -08001211/*
1212 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1213 * otherwise bails to standard library code.
1214 */
1215bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001216 RegLocation rl_obj = info->args[0];
1217 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001218 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001219 // RBX is promotable in 64-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001220 RegStorage rs_tmp = cu_->target64 ? rs_r11 : rs_rBX;
1221 int start_value = -1;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001222
1223 uint32_t char_value =
1224 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1225
1226 if (char_value > 0xFFFF) {
1227 // We have to punt to the real String.indexOf.
1228 return false;
1229 }
1230
1231 // Okay, we are commited to inlining this.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001232 // EAX: 16 bit character being searched.
1233 // ECX: count: number of words to be searched.
1234 // EDI: String being searched.
1235 // EDX: temporary during execution.
1236 // EBX or R11: temporary during execution (depending on mode).
1237 // REP SCASW: search instruction.
1238
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001239 FlushAllRegs();
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001240
buzbeea0cd2d72014-06-01 09:33:49 -07001241 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001242 RegLocation rl_dest = InlineTarget(info);
1243
1244 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001245 LoadValueDirectFixed(rl_obj, rs_rDX);
1246 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001247 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001248
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001249 LIR *slowpath_branch = nullptr, *length_compare = nullptr;
1250
1251 // We need the value in EAX.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001252 if (rl_char.is_const) {
buzbee2700f7e2014-03-07 09:46:20 -08001253 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001254 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001255 // Does the character fit in 16 bits? Compare it at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001256 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001257 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001258 }
1259
1260 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001261 // Location of reference to data array within the String object.
1262 int value_offset = mirror::String::ValueOffset().Int32Value();
1263 // Location of count within the String object.
1264 int count_offset = mirror::String::CountOffset().Int32Value();
1265 // Starting offset within data array.
1266 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1267 // Start of char data with array_.
1268 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001269
Dave Allison69dfe512014-07-11 17:11:58 +00001270 // Compute the number of words to search in to rCX.
1271 Load32Disp(rs_rDX, count_offset, rs_rCX);
1272
Dave Allisondfd3b472014-07-16 16:04:32 -07001273 // Possible signal here due to null pointer dereference.
1274 // Note that the signal handler will expect the top word of
1275 // the stack to be the ArtMethod*. If the PUSH edi instruction
1276 // below is ahead of the load above then this will not be true
1277 // and the signal handler will not work.
1278 MarkPossibleNullPointerException(0);
Dave Allison69dfe512014-07-11 17:11:58 +00001279
Dave Allisondfd3b472014-07-16 16:04:32 -07001280 if (!cu_->target64) {
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001281 // EDI is promotable in 32-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001282 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1283 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001284
Mark Mendell4028a6c2014-02-19 20:06:20 -08001285 if (zero_based) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001286 // Start index is not present.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001287 // We have to handle an empty string. Use special instruction JECXZ.
1288 length_compare = NewLIR0(kX86Jecxz8);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001289
1290 // Copy the number of words to search in a temporary register.
1291 // We will use the register at the end to calculate result.
1292 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001293 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001294 // Start index is present.
buzbeea44d4f52014-03-05 11:26:39 -08001295 rl_start = info->args[2];
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001296
Mark Mendell4028a6c2014-02-19 20:06:20 -08001297 // We have to offset by the start index.
1298 if (rl_start.is_const) {
1299 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1300 start_value = std::max(start_value, 0);
1301
1302 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001303 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001304 OpRegImm(kOpMov, rs_rDI, start_value);
1305
1306 // Copy the number of words to search in a temporary register.
1307 // We will use the register at the end to calculate result.
1308 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001309
1310 if (start_value != 0) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001311 // Decrease the number of words to search by the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001312 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001313 }
1314 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001315 // Handle "start index < 0" case.
1316 if (!cu_->target64 && rl_start.location != kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001317 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001318 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001319 {
1320 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001321 Load32Disp(rs_rX86_SP, displacement, rs_rDI);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001322 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001323 } else {
1324 LoadValueDirectFixed(rl_start, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001325 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001326 OpRegReg(kOpXor, rs_tmp, rs_tmp);
1327 OpRegReg(kOpCmp, rs_rDI, rs_tmp);
1328 OpCondRegReg(kOpCmov, kCondLt, rs_rDI, rs_tmp);
1329
1330 // The length of the string should be greater than the start index.
1331 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rDI, nullptr);
1332
1333 // Copy the number of words to search in a temporary register.
1334 // We will use the register at the end to calculate result.
1335 OpRegReg(kOpMov, rs_tmp, rs_rCX);
1336
1337 // Decrease the number of words to search by the start index.
1338 OpRegReg(kOpSub, rs_rCX, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001339 }
1340 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001341
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001342 // Load the address of the string into EDI.
1343 // In case of start index we have to add the address to existing value in EDI.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001344 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001345 if (zero_based || (!zero_based && rl_start.is_const && start_value == 0)) {
1346 Load32Disp(rs_rDX, offset_offset, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001347 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001348 OpRegMem(kOpAdd, rs_rDI, rs_rDX, offset_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001349 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001350 OpRegImm(kOpLsl, rs_rDI, 1);
1351 OpRegMem(kOpAdd, rs_rDI, rs_rDX, value_offset);
1352 OpRegImm(kOpAdd, rs_rDI, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001353
1354 // EDI now contains the start of the string to be searched.
1355 // We are all prepared to do the search for the character.
1356 NewLIR0(kX86RepneScasw);
1357
1358 // Did we find a match?
1359 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1360
1361 // yes, we matched. Compute the index of the result.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001362 OpRegReg(kOpSub, rs_tmp, rs_rCX);
1363 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_tmp.GetReg(), -1);
1364
Mark Mendell4028a6c2014-02-19 20:06:20 -08001365 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1366
1367 // Failed to match; return -1.
1368 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1369 length_compare->target = not_found;
1370 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001371 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001372
1373 // And join up at the end.
1374 all_done->target = NewLIR0(kPseudoTargetLabel);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001375
1376 if (!cu_->target64)
1377 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001378
1379 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001380 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001381 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001382 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001383 }
1384
1385 StoreValue(rl_dest, rl_return);
1386 return true;
1387}
1388
Tong Shen35e1e6a2014-07-30 09:31:22 -07001389static bool ARTRegIDToDWARFRegID(bool is_x86_64, int art_reg_id, int* dwarf_reg_id) {
1390 if (is_x86_64) {
1391 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001392 case 3 : *dwarf_reg_id = 3; return true; // %rbx
Tong Shen35e1e6a2014-07-30 09:31:22 -07001393 // This is the only discrepancy between ART & DWARF register numbering.
Andreas Gampebda27222014-07-30 23:21:36 -07001394 case 5 : *dwarf_reg_id = 6; return true; // %rbp
1395 case 12: *dwarf_reg_id = 12; return true; // %r12
1396 case 13: *dwarf_reg_id = 13; return true; // %r13
1397 case 14: *dwarf_reg_id = 14; return true; // %r14
1398 case 15: *dwarf_reg_id = 15; return true; // %r15
1399 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001400 }
1401 } else {
1402 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001403 case 5: *dwarf_reg_id = 5; return true; // %ebp
1404 case 6: *dwarf_reg_id = 6; return true; // %esi
1405 case 7: *dwarf_reg_id = 7; return true; // %edi
1406 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001407 }
1408 }
1409}
1410
Tong Shen547cdfd2014-08-05 01:54:19 -07001411std::vector<uint8_t>* X86Mir2Lir::ReturnFrameDescriptionEntry() {
1412 std::vector<uint8_t>* cfi_info = new std::vector<uint8_t>;
Mark Mendellae9fd932014-02-10 16:14:35 -08001413
1414 // Generate the FDE for the method.
1415 DCHECK_NE(data_offset_, 0U);
1416
Tong Shen547cdfd2014-08-05 01:54:19 -07001417 WriteFDEHeader(cfi_info);
1418 WriteFDEAddressRange(cfi_info, data_offset_);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001419
Mark Mendellae9fd932014-02-10 16:14:35 -08001420 // The instructions in the FDE.
1421 if (stack_decrement_ != nullptr) {
1422 // Advance LOC to just past the stack decrement.
1423 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001424 DW_CFA_advance_loc(cfi_info, pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001425
1426 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
Tong Shen547cdfd2014-08-05 01:54:19 -07001427 DW_CFA_def_cfa_offset(cfi_info, frame_size_);
Mark Mendellae9fd932014-02-10 16:14:35 -08001428
Tong Shen35e1e6a2014-07-30 09:31:22 -07001429 // Handle register spills
1430 const uint32_t kSpillInstLen = (cu_->target64) ? 5 : 4;
1431 const int kDataAlignmentFactor = (cu_->target64) ? -8 : -4;
1432 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
1433 int offset = -(GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
1434 for (int reg = 0; mask; mask >>= 1, reg++) {
1435 if (mask & 0x1) {
1436 pc += kSpillInstLen;
1437
1438 // Advance LOC to pass this instruction
Tong Shen547cdfd2014-08-05 01:54:19 -07001439 DW_CFA_advance_loc(cfi_info, kSpillInstLen);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001440
1441 int dwarf_reg_id;
1442 if (ARTRegIDToDWARFRegID(cu_->target64, reg, &dwarf_reg_id)) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001443 // DW_CFA_offset_extended_sf reg offset
1444 DW_CFA_offset_extended_sf(cfi_info, dwarf_reg_id, offset / kDataAlignmentFactor);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001445 }
1446
1447 offset += GetInstructionSetPointerSize(cu_->instruction_set);
1448 }
1449 }
1450
Mark Mendellae9fd932014-02-10 16:14:35 -08001451 // We continue with that stack until the epilogue.
1452 if (stack_increment_ != nullptr) {
1453 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001454 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001455
1456 // We probably have code snippets after the epilogue, so save the
1457 // current state: DW_CFA_remember_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001458 DW_CFA_remember_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001459
Tong Shen35e1e6a2014-07-30 09:31:22 -07001460 // We have now popped the stack: DW_CFA_def_cfa_offset 4/8.
1461 // There is only the return PC on the stack now.
Tong Shen547cdfd2014-08-05 01:54:19 -07001462 DW_CFA_def_cfa_offset(cfi_info, GetInstructionSetPointerSize(cu_->instruction_set));
Mark Mendellae9fd932014-02-10 16:14:35 -08001463
1464 // Everything after that is the same as before the epilogue.
1465 // Stack bump was followed by RET instruction.
1466 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1467 if (post_ret_insn != nullptr) {
1468 pc = new_pc;
1469 new_pc = post_ret_insn->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001470 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001471 // Restore the state: DW_CFA_restore_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001472 DW_CFA_restore_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001473 }
1474 }
1475 }
1476
Tong Shen547cdfd2014-08-05 01:54:19 -07001477 PadCFI(cfi_info);
1478 WriteCFILength(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001479
Mark Mendellae9fd932014-02-10 16:14:35 -08001480 return cfi_info;
1481}
1482
Mark Mendelld65c51a2014-04-29 16:55:20 -04001483void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1484 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001485 case kMirOpReserveVectorRegisters:
1486 ReserveVectorRegisters(mir);
1487 break;
1488 case kMirOpReturnVectorRegisters:
1489 ReturnVectorRegisters();
1490 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001491 case kMirOpConstVector:
1492 GenConst128(bb, mir);
1493 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001494 case kMirOpMoveVector:
1495 GenMoveVector(bb, mir);
1496 break;
1497 case kMirOpPackedMultiply:
1498 GenMultiplyVector(bb, mir);
1499 break;
1500 case kMirOpPackedAddition:
1501 GenAddVector(bb, mir);
1502 break;
1503 case kMirOpPackedSubtract:
1504 GenSubtractVector(bb, mir);
1505 break;
1506 case kMirOpPackedShiftLeft:
1507 GenShiftLeftVector(bb, mir);
1508 break;
1509 case kMirOpPackedSignedShiftRight:
1510 GenSignedShiftRightVector(bb, mir);
1511 break;
1512 case kMirOpPackedUnsignedShiftRight:
1513 GenUnsignedShiftRightVector(bb, mir);
1514 break;
1515 case kMirOpPackedAnd:
1516 GenAndVector(bb, mir);
1517 break;
1518 case kMirOpPackedOr:
1519 GenOrVector(bb, mir);
1520 break;
1521 case kMirOpPackedXor:
1522 GenXorVector(bb, mir);
1523 break;
1524 case kMirOpPackedAddReduce:
1525 GenAddReduceVector(bb, mir);
1526 break;
1527 case kMirOpPackedReduce:
1528 GenReduceVector(bb, mir);
1529 break;
1530 case kMirOpPackedSet:
1531 GenSetVector(bb, mir);
1532 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001533 default:
1534 break;
1535 }
1536}
1537
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001538void X86Mir2Lir::ReserveVectorRegisters(MIR* mir) {
1539 // We should not try to reserve twice without returning the registers
1540 DCHECK_NE(num_reserved_vector_regs_, -1);
1541
1542 int num_vector_reg = mir->dalvikInsn.vA;
1543 for (int i = 0; i < num_vector_reg; i++) {
1544 RegStorage xp_reg = RegStorage::Solo128(i);
1545 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1546 Clobber(xp_reg);
1547
1548 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1549 info != nullptr;
1550 info = info->GetAliasChain()) {
1551 if (info->GetReg().IsSingle()) {
1552 reg_pool_->sp_regs_.Delete(info);
1553 } else {
1554 reg_pool_->dp_regs_.Delete(info);
1555 }
1556 }
1557 }
1558
1559 num_reserved_vector_regs_ = num_vector_reg;
1560}
1561
1562void X86Mir2Lir::ReturnVectorRegisters() {
1563 // Return all the reserved registers
1564 for (int i = 0; i < num_reserved_vector_regs_; i++) {
1565 RegStorage xp_reg = RegStorage::Solo128(i);
1566 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1567
1568 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1569 info != nullptr;
1570 info = info->GetAliasChain()) {
1571 if (info->GetReg().IsSingle()) {
1572 reg_pool_->sp_regs_.Insert(info);
1573 } else {
1574 reg_pool_->dp_regs_.Insert(info);
1575 }
1576 }
1577 }
1578
1579 // We don't have anymore reserved vector registers
1580 num_reserved_vector_regs_ = -1;
1581}
1582
Mark Mendelld65c51a2014-04-29 16:55:20 -04001583void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001584 store_method_addr_used_ = true;
1585 int type_size = mir->dalvikInsn.vB;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001586 // We support 128 bit vectors.
1587 DCHECK_EQ(type_size & 0xFFFF, 128);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001588 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001589 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001590 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001591 // Check for all 0 case.
1592 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1593 NewLIR2(kX86XorpsRR, reg, reg);
1594 return;
1595 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001596
1597 // Append the mov const vector to reg opcode.
1598 AppendOpcodeWithConst(kX86MovupsRM, reg, mir);
1599}
1600
1601void X86Mir2Lir::AppendOpcodeWithConst(X86OpCode opcode, int reg, MIR* mir) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04001602 // Okay, load it from the constant vector area.
1603 LIR *data_target = ScanVectorLiteral(mir);
1604 if (data_target == nullptr) {
1605 data_target = AddVectorLiteral(mir);
1606 }
1607
1608 // Address the start of the method.
1609 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001610 if (rl_method.wide) {
1611 rl_method = LoadValueWide(rl_method, kCoreReg);
1612 } else {
1613 rl_method = LoadValue(rl_method, kCoreReg);
1614 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001615
1616 // Load the proper value from the literal area.
1617 // We don't know the proper offset for the value, so pick one that will force
1618 // 4 byte offset. We will fix this up in the assembler later to have the right
1619 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001620 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001621 LIR *load = NewLIR2(opcode, reg, rl_method.reg.GetReg());
Mark Mendelld65c51a2014-04-29 16:55:20 -04001622 load->flags.fixup = kFixupLoad;
1623 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001624}
1625
Mark Mendellfe945782014-05-22 09:52:36 -04001626void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1627 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001628 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1629 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
1630 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001631 NewLIR2(kX86Mova128RR, rs_dest.GetReg(), rs_src.GetReg());
1632}
1633
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001634void X86Mir2Lir::GenMultiplyVectorSignedByte(BasicBlock *bb, MIR *mir) {
1635 const int BYTE_SIZE = 8;
1636 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1637 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
1638 RegStorage rs_src1_high_tmp = Get128BitRegister(AllocTempWide());
1639
1640 /*
1641 * Emulate the behavior of a kSignedByte by separating out the 16 values in the two XMM
1642 * and multiplying 8 at a time before recombining back into one XMM register.
1643 *
1644 * let xmm1, xmm2 be real srcs (keep low bits of 16bit lanes)
1645 * xmm3 is tmp (operate on high bits of 16bit lanes)
1646 *
1647 * xmm3 = xmm1
1648 * xmm1 = xmm1 .* xmm2
1649 * xmm1 = xmm1 & 0x00ff00ff00ff00ff00ff00ff00ff00ff // xmm1 now has low bits
1650 * xmm3 = xmm3 .>> 8
1651 * xmm2 = xmm2 & 0xff00ff00ff00ff00ff00ff00ff00ff00
1652 * xmm2 = xmm2 .* xmm3 // xmm2 now has high bits
1653 * xmm1 = xmm1 | xmm2 // combine results
1654 */
1655
1656 // Copy xmm1.
1657 NewLIR2(kX86Mova128RR, rs_src1_high_tmp.GetReg(), rs_dest_src1.GetReg());
1658
1659 // Multiply low bits.
1660 NewLIR2(kX86PmullwRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1661
1662 // xmm1 now has low bits.
1663 AndMaskVectorRegister(rs_dest_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
1664
1665 // Prepare high bits for multiplication.
1666 NewLIR2(kX86PsrlwRI, rs_src1_high_tmp.GetReg(), BYTE_SIZE);
1667 AndMaskVectorRegister(rs_src2, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1668
1669 // Multiply high bits and xmm2 now has high bits.
1670 NewLIR2(kX86PmullwRR, rs_src2.GetReg(), rs_src1_high_tmp.GetReg());
1671
1672 // Combine back into dest XMM register.
1673 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1674}
1675
Mark Mendellfe945782014-05-22 09:52:36 -04001676void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001677 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1678 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1679 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1680 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001681 int opcode = 0;
1682 switch (opsize) {
1683 case k32:
1684 opcode = kX86PmulldRR;
1685 break;
1686 case kSignedHalf:
1687 opcode = kX86PmullwRR;
1688 break;
1689 case kSingle:
1690 opcode = kX86MulpsRR;
1691 break;
1692 case kDouble:
1693 opcode = kX86MulpdRR;
1694 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001695 case kSignedByte:
1696 // HW doesn't support 16x16 byte multiplication so emulate it.
1697 GenMultiplyVectorSignedByte(bb, mir);
1698 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001699 default:
1700 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1701 break;
1702 }
1703 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1704}
1705
1706void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001707 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1708 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1709 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1710 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001711 int opcode = 0;
1712 switch (opsize) {
1713 case k32:
1714 opcode = kX86PadddRR;
1715 break;
1716 case kSignedHalf:
1717 case kUnsignedHalf:
1718 opcode = kX86PaddwRR;
1719 break;
1720 case kUnsignedByte:
1721 case kSignedByte:
1722 opcode = kX86PaddbRR;
1723 break;
1724 case kSingle:
1725 opcode = kX86AddpsRR;
1726 break;
1727 case kDouble:
1728 opcode = kX86AddpdRR;
1729 break;
1730 default:
1731 LOG(FATAL) << "Unsupported vector addition " << opsize;
1732 break;
1733 }
1734 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1735}
1736
1737void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001738 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1739 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1740 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1741 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001742 int opcode = 0;
1743 switch (opsize) {
1744 case k32:
1745 opcode = kX86PsubdRR;
1746 break;
1747 case kSignedHalf:
1748 case kUnsignedHalf:
1749 opcode = kX86PsubwRR;
1750 break;
1751 case kUnsignedByte:
1752 case kSignedByte:
1753 opcode = kX86PsubbRR;
1754 break;
1755 case kSingle:
1756 opcode = kX86SubpsRR;
1757 break;
1758 case kDouble:
1759 opcode = kX86SubpdRR;
1760 break;
1761 default:
1762 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1763 break;
1764 }
1765 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1766}
1767
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001768void X86Mir2Lir::GenShiftByteVector(BasicBlock *bb, MIR *mir) {
1769 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1770 RegStorage rs_tmp = Get128BitRegister(AllocTempWide());
1771
1772 int opcode = 0;
1773 int imm = mir->dalvikInsn.vB;
1774
1775 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1776 case kMirOpPackedShiftLeft:
1777 opcode = kX86PsllwRI;
1778 break;
1779 case kMirOpPackedSignedShiftRight:
1780 opcode = kX86PsrawRI;
1781 break;
1782 case kMirOpPackedUnsignedShiftRight:
1783 opcode = kX86PsrlwRI;
1784 break;
1785 default:
1786 LOG(FATAL) << "Unsupported shift operation on byte vector " << opcode;
1787 break;
1788 }
1789
1790 /*
1791 * xmm1 will have low bits
1792 * xmm2 will have high bits
1793 *
1794 * xmm2 = xmm1
1795 * xmm1 = xmm1 .<< N
1796 * xmm2 = xmm2 && 0xFF00FF00FF00FF00FF00FF00FF00FF00
1797 * xmm2 = xmm2 .<< N
1798 * xmm1 = xmm1 | xmm2
1799 */
1800
1801 // Copy xmm1.
1802 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_dest_src1.GetReg());
1803
1804 // Shift lower values.
1805 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1806
1807 // Mask bottom bits.
1808 AndMaskVectorRegister(rs_tmp, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1809
1810 // Shift higher values.
1811 NewLIR2(opcode, rs_tmp.GetReg(), imm);
1812
1813 // Combine back into dest XMM register.
1814 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_tmp.GetReg());
1815}
1816
Mark Mendellfe945782014-05-22 09:52:36 -04001817void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001818 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1819 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1820 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1821 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001822 int opcode = 0;
1823 switch (opsize) {
1824 case k32:
1825 opcode = kX86PslldRI;
1826 break;
1827 case k64:
1828 opcode = kX86PsllqRI;
1829 break;
1830 case kSignedHalf:
1831 case kUnsignedHalf:
1832 opcode = kX86PsllwRI;
1833 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001834 case kSignedByte:
1835 case kUnsignedByte:
1836 GenShiftByteVector(bb, mir);
1837 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001838 default:
1839 LOG(FATAL) << "Unsupported vector shift left " << opsize;
1840 break;
1841 }
1842 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1843}
1844
1845void X86Mir2Lir::GenSignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001846 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1847 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1848 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1849 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001850 int opcode = 0;
1851 switch (opsize) {
1852 case k32:
1853 opcode = kX86PsradRI;
1854 break;
1855 case kSignedHalf:
1856 case kUnsignedHalf:
1857 opcode = kX86PsrawRI;
1858 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001859 case kSignedByte:
1860 case kUnsignedByte:
1861 GenShiftByteVector(bb, mir);
1862 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001863 default:
1864 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
1865 break;
1866 }
1867 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1868}
1869
1870void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001871 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1872 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1873 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1874 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001875 int opcode = 0;
1876 switch (opsize) {
1877 case k32:
1878 opcode = kX86PsrldRI;
1879 break;
1880 case k64:
1881 opcode = kX86PsrlqRI;
1882 break;
1883 case kSignedHalf:
1884 case kUnsignedHalf:
1885 opcode = kX86PsrlwRI;
1886 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001887 case kSignedByte:
1888 case kUnsignedByte:
1889 GenShiftByteVector(bb, mir);
1890 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001891 default:
1892 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1893 break;
1894 }
1895 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1896}
1897
1898void X86Mir2Lir::GenAndVector(BasicBlock *bb, MIR *mir) {
1899 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001900 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1901 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1902 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001903 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1904}
1905
1906void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
1907 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001908 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1909 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1910 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001911 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1912}
1913
1914void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
1915 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001916 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1917 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1918 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001919 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1920}
1921
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001922void X86Mir2Lir::AndMaskVectorRegister(RegStorage rs_src1, uint32_t m1, uint32_t m2, uint32_t m3, uint32_t m4) {
1923 MaskVectorRegister(kX86PandRM, rs_src1, m1, m2, m3, m4);
1924}
1925
1926void X86Mir2Lir::MaskVectorRegister(X86OpCode opcode, RegStorage rs_src1, uint32_t m0, uint32_t m1, uint32_t m2, uint32_t m3) {
1927 // Create temporary MIR as container for 128-bit binary mask.
1928 MIR const_mir;
1929 MIR* const_mirp = &const_mir;
1930 const_mirp->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpConstVector);
1931 const_mirp->dalvikInsn.arg[0] = m0;
1932 const_mirp->dalvikInsn.arg[1] = m1;
1933 const_mirp->dalvikInsn.arg[2] = m2;
1934 const_mirp->dalvikInsn.arg[3] = m3;
1935
1936 // Mask vector with const from literal pool.
1937 AppendOpcodeWithConst(opcode, rs_src1.GetReg(), const_mirp);
1938}
1939
Mark Mendellfe945782014-05-22 09:52:36 -04001940void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001941 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1942 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1943 RegLocation rl_dest = mir_graph_->GetDest(mir);
1944 RegStorage rs_tmp;
1945
1946 int vec_bytes = (mir->dalvikInsn.vC & 0xFFFF) / 8;
1947 int vec_unit_size = 0;
Mark Mendellfe945782014-05-22 09:52:36 -04001948 int opcode = 0;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001949 int extr_opcode = 0;
1950 RegLocation rl_result;
1951
Mark Mendellfe945782014-05-22 09:52:36 -04001952 switch (opsize) {
1953 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001954 extr_opcode = kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001955 opcode = kX86PhadddRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001956 vec_unit_size = 4;
1957 break;
1958 case kSignedByte:
1959 case kUnsignedByte:
1960 extr_opcode = kX86PextrbRRI;
1961 opcode = kX86PhaddwRR;
1962 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001963 break;
1964 case kSignedHalf:
1965 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001966 extr_opcode = kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001967 opcode = kX86PhaddwRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001968 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001969 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001970 case kSingle:
1971 rl_result = EvalLoc(rl_dest, kFPReg, true);
1972 vec_unit_size = 4;
1973 for (int i = 0; i < 3; i++) {
1974 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
1975 NewLIR3(kX86ShufpsRRI, rs_src1.GetReg(), rs_src1.GetReg(), 0x39);
1976 }
1977 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
1978 StoreValue(rl_dest, rl_result);
1979
1980 // For single-precision floats, we are done here
1981 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001982 default:
1983 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
1984 break;
1985 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001986
1987 int elems = vec_bytes / vec_unit_size;
1988
1989 // Emulate horizontal add instruction by reducing 2 vectors with 8 values before adding them again
1990 // TODO is overflow handled correctly?
1991 if (opsize == kSignedByte || opsize == kUnsignedByte) {
1992 rs_tmp = Get128BitRegister(AllocTempWide());
1993
1994 // tmp = xmm1 .>> 8.
1995 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_src1.GetReg());
1996 NewLIR2(kX86PsrlwRI, rs_tmp.GetReg(), 8);
1997
1998 // Zero extend low bits in xmm1.
1999 AndMaskVectorRegister(rs_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
2000 }
2001
2002 while (elems > 1) {
2003 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2004 NewLIR2(opcode, rs_tmp.GetReg(), rs_tmp.GetReg());
2005 }
2006 NewLIR2(opcode, rs_src1.GetReg(), rs_src1.GetReg());
2007 elems >>= 1;
2008 }
2009
2010 // Combine the results if we separated them.
2011 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2012 NewLIR2(kX86PaddbRR, rs_src1.GetReg(), rs_tmp.GetReg());
2013 }
2014
2015 // We need to extract to a GPR.
2016 RegStorage temp = AllocTemp();
2017 NewLIR3(extr_opcode, temp.GetReg(), rs_src1.GetReg(), 0);
2018
2019 // Can we do this directly into memory?
2020 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2021 if (rl_result.location == kLocPhysReg) {
2022 // Ensure res is in a core reg
2023 rl_result = EvalLoc(rl_dest, kCoreReg, true);
2024 OpRegReg(kOpAdd, rl_result.reg, temp);
2025 StoreFinalValue(rl_dest, rl_result);
2026 } else {
2027 OpMemReg(kOpAdd, rl_result, temp.GetReg());
2028 }
2029
2030 FreeTemp(temp);
Mark Mendellfe945782014-05-22 09:52:36 -04002031}
2032
2033void X86Mir2Lir::GenReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002034 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2035 RegLocation rl_dest = mir_graph_->GetDest(mir);
2036 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
2037 int extract_index = mir->dalvikInsn.arg[0];
2038 int extr_opcode = 0;
2039 RegLocation rl_result;
2040 bool is_wide = false;
2041
Mark Mendellfe945782014-05-22 09:52:36 -04002042 switch (opsize) {
2043 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002044 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2045 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrdMRI : kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002046 break;
2047 case kSignedHalf:
2048 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002049 rl_result= UpdateLocTyped(rl_dest, kCoreReg);
2050 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrwMRI : kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002051 break;
2052 default:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002053 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2054 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002055 break;
2056 }
Mark Mendellfe945782014-05-22 09:52:36 -04002057
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002058 if (rl_result.location == kLocPhysReg) {
2059 NewLIR3(extr_opcode, rl_result.reg.GetReg(), rs_src1.GetReg(), extract_index);
2060 if (is_wide == true) {
2061 StoreFinalValue(rl_dest, rl_result);
2062 } else {
2063 StoreFinalValueWide(rl_dest, rl_result);
2064 }
2065 } else {
2066 int displacement = SRegOffset(rl_result.s_reg_low);
2067 LIR *l = NewLIR3(extr_opcode, rs_rX86_SP.GetReg(), displacement, rs_src1.GetReg());
2068 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is_wide /* is_64bit */);
2069 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is_wide /* is_64bit */);
2070 }
Mark Mendellfe945782014-05-22 09:52:36 -04002071}
2072
2073void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002074 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2075 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2076 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
2077 int op_low = 0, op_high = 0, imm = 0, op_mov = kX86MovdxrRR;
2078 RegisterClass reg_type = kCoreReg;
2079
Mark Mendellfe945782014-05-22 09:52:36 -04002080 switch (opsize) {
2081 case k32:
2082 op_low = kX86PshufdRRI;
2083 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002084 case kSingle:
2085 op_low = kX86PshufdRRI;
2086 op_mov = kX86Mova128RR;
2087 reg_type = kFPReg;
2088 break;
2089 case k64:
2090 op_low = kX86PshufdRRI;
2091 imm = 0x44;
2092 break;
2093 case kDouble:
2094 op_low = kX86PshufdRRI;
2095 op_mov = kX86Mova128RR;
2096 reg_type = kFPReg;
2097 imm = 0x44;
2098 break;
2099 case kSignedByte:
2100 case kUnsignedByte:
2101 // Shuffle 8 bit value into 16 bit word.
2102 // We set val = val + (val << 8) below and use 16 bit shuffle.
Mark Mendellfe945782014-05-22 09:52:36 -04002103 case kSignedHalf:
2104 case kUnsignedHalf:
2105 // Handles low quadword.
2106 op_low = kX86PshuflwRRI;
2107 // Handles upper quadword.
2108 op_high = kX86PshufdRRI;
2109 break;
2110 default:
2111 LOG(FATAL) << "Unsupported vector set " << opsize;
2112 break;
2113 }
2114
Mark Mendellfe945782014-05-22 09:52:36 -04002115 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002116
2117 // Load the value from the VR into the reg.
2118 if (rl_src.wide == 0) {
2119 rl_src = LoadValue(rl_src, reg_type);
2120 } else {
2121 rl_src = LoadValueWide(rl_src, reg_type);
2122 }
2123
2124 // If opsize is 8 bits wide then double value and use 16 bit shuffle instead.
2125 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2126 RegStorage temp = AllocTemp();
2127 // val = val + (val << 8).
2128 NewLIR2(kX86Mov32RR, temp.GetReg(), rl_src.reg.GetReg());
2129 NewLIR2(kX86Sal32RI, temp.GetReg(), 8);
2130 NewLIR2(kX86Or32RR, rl_src.reg.GetReg(), temp.GetReg());
2131 FreeTemp(temp);
2132 }
Mark Mendellfe945782014-05-22 09:52:36 -04002133
2134 // Load the value into the XMM register.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002135 NewLIR2(op_mov, rs_dest.GetReg(), rl_src.reg.GetReg());
Mark Mendellfe945782014-05-22 09:52:36 -04002136
2137 // Now shuffle the value across the destination.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002138 NewLIR3(op_low, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002139
2140 // And then repeat as needed.
2141 if (op_high != 0) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002142 NewLIR3(op_high, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002143 }
2144}
2145
Mark Mendelld65c51a2014-04-29 16:55:20 -04002146LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
2147 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2148 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
2149 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
2150 args[2] == p->operands[2] && args[3] == p->operands[3]) {
2151 return p;
2152 }
2153 }
2154 return nullptr;
2155}
2156
2157LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
2158 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
2159 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2160 new_value->operands[0] = args[0];
2161 new_value->operands[1] = args[1];
2162 new_value->operands[2] = args[2];
2163 new_value->operands[3] = args[3];
2164 new_value->next = const_vectors_;
2165 if (const_vectors_ == nullptr) {
2166 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
2167 }
2168 estimated_native_code_size_ += 16; // Space for one vector.
2169 const_vectors_ = new_value;
2170 return new_value;
2171}
2172
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002173// ------------ ABI support: mapping of args to physical registers -------------
Andreas Gampeccc60262014-07-04 18:02:38 -07002174RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide,
2175 bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002176 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
Andreas Gampeccc60262014-07-04 18:02:38 -07002177 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) /
2178 sizeof(SpecialTargetRegister);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002179 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
Andreas Gampeccc60262014-07-04 18:02:38 -07002180 kFArg4, kFArg5, kFArg6, kFArg7};
2181 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) /
2182 sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002183
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002184 if (is_double_or_float) {
2185 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002186 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide ? kWide : kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002187 }
2188 } else {
2189 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002190 return ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++],
2191 is_ref ? kRef : (is_wide ? kWide : kNotWide));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002192 }
2193 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07002194 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002195}
2196
2197RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
2198 DCHECK(IsInitialized());
2199 auto res = mapping_.find(in_position);
2200 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
2201}
2202
Andreas Gampeccc60262014-07-04 18:02:38 -07002203void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count,
2204 InToRegStorageMapper* mapper) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002205 DCHECK(mapper != nullptr);
2206 max_mapped_in_ = -1;
2207 is_there_stack_mapped_ = false;
2208 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07002209 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
2210 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002211 if (reg.Valid()) {
2212 mapping_[in_position] = reg;
2213 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07002214 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002215 // We covered 2 args, so skip the next one
2216 in_position++;
2217 }
2218 } else {
2219 is_there_stack_mapped_ = true;
2220 }
2221 }
2222 initialized_ = true;
2223}
2224
2225RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002226 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002227 return GetCoreArgMappingToPhysicalReg(arg_num);
2228 }
2229
2230 if (!in_to_reg_storage_mapping_.IsInitialized()) {
2231 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2232 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
2233
Chao-ying Fua77ee512014-07-01 17:43:41 -07002234 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002235 in_to_reg_storage_mapping_.Initialize(arg_locs, cu_->num_ins, &mapper);
2236 }
2237 return in_to_reg_storage_mapping_.Get(arg_num);
2238}
2239
2240RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
2241 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
2242 // Not used for 64-bit, TODO: Move X86_32 to the same framework
2243 switch (core_arg_num) {
2244 case 0:
2245 return rs_rX86_ARG1;
2246 case 1:
2247 return rs_rX86_ARG2;
2248 case 2:
2249 return rs_rX86_ARG3;
2250 default:
2251 return RegStorage::InvalidReg();
2252 }
2253}
2254
2255// ---------End of ABI support: mapping of args to physical registers -------------
2256
2257/*
2258 * If there are any ins passed in registers that have not been promoted
2259 * to a callee-save register, flush them to the frame. Perform initial
2260 * assignment of promoted arguments.
2261 *
2262 * ArgLocs is an array of location records describing the incoming arguments
2263 * with one location record per word of argument.
2264 */
2265void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002266 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002267 /*
2268 * Dummy up a RegLocation for the incoming Method*
2269 * It will attempt to keep kArg0 live (or copy it to home location
2270 * if promoted).
2271 */
2272
2273 RegLocation rl_src = rl_method;
2274 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -07002275 rl_src.reg = TargetReg(kArg0, kRef);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002276 rl_src.home = false;
2277 MarkLive(rl_src);
2278 StoreValue(rl_method, rl_src);
2279 // If Method* has been promoted, explicitly flush
2280 if (rl_method.location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002281 StoreRefDisp(rs_rX86_SP, 0, As32BitReg(TargetReg(kArg0, kRef)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002282 }
2283
2284 if (cu_->num_ins == 0) {
2285 return;
2286 }
2287
2288 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2289 /*
2290 * Copy incoming arguments to their proper home locations.
2291 * NOTE: an older version of dx had an issue in which
2292 * it would reuse static method argument registers.
2293 * This could result in the same Dalvik virtual register
2294 * being promoted to both core and fp regs. To account for this,
2295 * we only copy to the corresponding promoted physical register
2296 * if it matches the type of the SSA name for the incoming
2297 * argument. It is also possible that long and double arguments
2298 * end up half-promoted. In those cases, we must flush the promoted
2299 * half to memory as well.
2300 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002301 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002302 for (int i = 0; i < cu_->num_ins; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002303 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002304 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002305
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002306 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002307 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002308 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002309
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002310 // We have already updated the arg location with promoted info
2311 // so we can be based on it.
2312 if (t_loc->location == kLocPhysReg) {
2313 // Just copy it.
2314 OpRegCopy(t_loc->reg, reg);
2315 } else {
2316 // Needs flush.
2317 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002318 StoreRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002319 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002320 StoreBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002321 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002322 }
2323 }
2324 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002325 // If arriving in frame & promoted.
2326 if (t_loc->location == kLocPhysReg) {
2327 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002328 LoadRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002329 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002330 LoadBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002331 t_loc->wide ? k64 : k32, kNotVolatile);
2332 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002333 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002334 }
2335 if (t_loc->wide) {
2336 // Increment i to skip the next one.
2337 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002338 }
2339 }
2340}
2341
2342/*
2343 * Load up to 5 arguments, the first three of which will be in
2344 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2345 * and as part of the load sequence, it must be replaced with
2346 * the target method pointer. Note, this may also be called
2347 * for "range" variants if the number of arguments is 5 or fewer.
2348 */
2349int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2350 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2351 const MethodReference& target_method,
2352 uint32_t vtable_idx, uintptr_t direct_code,
2353 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002354 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002355 return Mir2Lir::GenDalvikArgsNoRange(info,
2356 call_state, pcrLabel, next_call_insn,
2357 target_method,
2358 vtable_idx, direct_code,
2359 direct_method, type, skip_this);
2360 }
2361 return GenDalvikArgsRange(info,
2362 call_state, pcrLabel, next_call_insn,
2363 target_method,
2364 vtable_idx, direct_code,
2365 direct_method, type, skip_this);
2366}
2367
2368/*
2369 * May have 0+ arguments (also used for jumbo). Note that
2370 * source virtual registers may be in physical registers, so may
2371 * need to be flushed to home location before copying. This
2372 * applies to arg3 and above (see below).
2373 *
2374 * Two general strategies:
2375 * If < 20 arguments
2376 * Pass args 3-18 using vldm/vstm block copy
2377 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2378 * If 20+ arguments
2379 * Pass args arg19+ using memcpy block copy
2380 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2381 *
2382 */
2383int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2384 LIR** pcrLabel, NextCallInsn next_call_insn,
2385 const MethodReference& target_method,
2386 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2387 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002388 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002389 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2390 pcrLabel, next_call_insn,
2391 target_method,
2392 vtable_idx, direct_code, direct_method,
2393 type, skip_this);
2394 }
2395
2396 /* If no arguments, just return */
2397 if (info->num_arg_words == 0)
2398 return call_state;
2399
2400 const int start_index = skip_this ? 1 : 0;
2401
Chao-ying Fua77ee512014-07-01 17:43:41 -07002402 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002403 InToRegStorageMapping in_to_reg_storage_mapping;
2404 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2405 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2406 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
Serguei Katkov8e3acdd2014-07-15 12:01:00 +07002407 info->args[last_mapped_in].wide ? 2 : 1;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002408 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2409
2410 // Fisrt of all, check whether it make sense to use bulk copying
2411 // Optimization is aplicable only for range case
2412 // TODO: make a constant instead of 2
2413 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2414 // Scan the rest of the args - if in phys_reg flush to memory
2415 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2416 RegLocation loc = info->args[next_arg];
2417 if (loc.wide) {
2418 loc = UpdateLocWide(loc);
2419 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002420 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002421 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002422 }
2423 next_arg += 2;
2424 } else {
2425 loc = UpdateLoc(loc);
2426 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002427 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002428 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002429 }
2430 next_arg++;
2431 }
2432 }
2433
2434 // Logic below assumes that Method pointer is at offset zero from SP.
2435 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
2436
2437 // The rest can be copied together
2438 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
Andreas Gampeccc60262014-07-04 18:02:38 -07002439 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped,
2440 cu_->instruction_set);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002441
2442 int current_src_offset = start_offset;
2443 int current_dest_offset = outs_offset;
2444
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002445 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2446 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002447 while (regs_left_to_pass_via_stack > 0) {
2448 // This is based on the knowledge that the stack itself is 16-byte aligned.
2449 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2450 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2451 size_t bytes_to_move;
2452
2453 /*
2454 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2455 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2456 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2457 * We do this because we could potentially do a smaller move to align.
2458 */
2459 if (regs_left_to_pass_via_stack == 4 ||
2460 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2461 // Moving 128-bits via xmm register.
2462 bytes_to_move = sizeof(uint32_t) * 4;
2463
2464 // Allocate a free xmm temp. Since we are working through the calling sequence,
2465 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2466 // there are no free registers.
2467 RegStorage temp = AllocTempDouble();
2468
2469 LIR* ld1 = nullptr;
2470 LIR* ld2 = nullptr;
2471 LIR* st1 = nullptr;
2472 LIR* st2 = nullptr;
2473
2474 /*
2475 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2476 * do an aligned move. If we have 8-byte alignment, then do the move in two
2477 * parts. This approach prevents possible cache line splits. Finally, fall back
2478 * to doing an unaligned move. In most cases we likely won't split the cache
2479 * line but we cannot prove it and thus take a conservative approach.
2480 */
2481 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2482 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2483
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002484 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002485 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002486 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002487 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002488 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovLo128FP);
2489 ld2 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002490 kMovHi128FP);
2491 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002492 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002493 }
2494
2495 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002496 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002497 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002498 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovLo128FP);
2499 st2 = OpMovMemReg(rs_rX86_SP, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002500 temp, kMovHi128FP);
2501 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002502 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002503 }
2504
2505 // TODO If we could keep track of aliasing information for memory accesses that are wider
2506 // than 64-bit, we wouldn't need to set up a barrier.
2507 if (ld1 != nullptr) {
2508 if (ld2 != nullptr) {
2509 // For 64-bit load we can actually set up the aliasing information.
2510 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2511 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2512 } else {
2513 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002514 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002515 }
2516 }
2517 if (st1 != nullptr) {
2518 if (st2 != nullptr) {
2519 // For 64-bit store we can actually set up the aliasing information.
2520 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2521 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2522 } else {
2523 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002524 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002525 }
2526 }
2527
2528 // Free the temporary used for the data movement.
2529 FreeTemp(temp);
2530 } else {
2531 // Moving 32-bits via general purpose register.
2532 bytes_to_move = sizeof(uint32_t);
2533
2534 // Instead of allocating a new temp, simply reuse one of the registers being used
2535 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07002536 RegStorage temp = TargetReg(kArg3, kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002537
2538 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002539 Load32Disp(rs_rX86_SP, current_src_offset, temp);
2540 Store32Disp(rs_rX86_SP, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002541 }
2542
2543 current_src_offset += bytes_to_move;
2544 current_dest_offset += bytes_to_move;
2545 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2546 }
2547 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2548 }
2549
2550 // Now handle rest not registers if they are
2551 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002552 RegStorage regSingle = TargetReg(kArg2, kNotWide);
2553 RegStorage regWide = TargetReg(kArg3, kWide);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002554 for (int i = start_index;
2555 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002556 RegLocation rl_arg = info->args[i];
2557 rl_arg = UpdateRawLoc(rl_arg);
2558 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2559 if (!reg.Valid()) {
2560 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2561
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002562 {
2563 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2564 if (rl_arg.wide) {
2565 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002566 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002567 } else {
2568 LoadValueDirectWideFixed(rl_arg, regWide);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002569 StoreBaseDisp(rs_rX86_SP, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002570 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002571 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002572 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002573 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002574 } else {
2575 LoadValueDirectFixed(rl_arg, regSingle);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002576 StoreBaseDisp(rs_rX86_SP, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002577 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002578 }
2579 }
2580 call_state = next_call_insn(cu_, info, call_state, target_method,
2581 vtable_idx, direct_code, direct_method, type);
2582 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002583 if (rl_arg.wide) {
2584 i++;
2585 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002586 }
2587 }
2588
2589 // Finish with mapped registers
2590 for (int i = start_index; i <= last_mapped_in; i++) {
2591 RegLocation rl_arg = info->args[i];
2592 rl_arg = UpdateRawLoc(rl_arg);
2593 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2594 if (reg.Valid()) {
2595 if (rl_arg.wide) {
2596 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002597 } else {
2598 LoadValueDirectFixed(rl_arg, reg);
2599 }
2600 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2601 direct_code, direct_method, type);
2602 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002603 if (rl_arg.wide) {
2604 i++;
2605 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002606 }
2607
2608 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2609 direct_code, direct_method, type);
2610 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00002611 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002612 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002613 } else {
2614 *pcrLabel = nullptr;
2615 // In lieu of generating a check for kArg1 being null, we need to
2616 // perform a load when doing implicit checks.
2617 RegStorage tmp = AllocTemp();
Andreas Gampeccc60262014-07-04 18:02:38 -07002618 Load32Disp(TargetReg(kArg1, kRef), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002619 MarkPossibleNullPointerException(info->opt_flags);
2620 FreeTemp(tmp);
2621 }
2622 }
2623 return call_state;
2624}
2625
Andreas Gampe98430592014-07-27 19:44:50 -07002626bool X86Mir2Lir::GenInlinedCharAt(CallInfo* info) {
2627 // Location of reference to data array
2628 int value_offset = mirror::String::ValueOffset().Int32Value();
2629 // Location of count
2630 int count_offset = mirror::String::CountOffset().Int32Value();
2631 // Starting offset within data array
2632 int offset_offset = mirror::String::OffsetOffset().Int32Value();
2633 // Start of char data with array_
2634 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
2635
2636 RegLocation rl_obj = info->args[0];
2637 RegLocation rl_idx = info->args[1];
2638 rl_obj = LoadValue(rl_obj, kRefReg);
2639 // X86 wants to avoid putting a constant index into a register.
2640 if (!rl_idx.is_const) {
2641 rl_idx = LoadValue(rl_idx, kCoreReg);
2642 }
2643 RegStorage reg_max;
2644 GenNullCheck(rl_obj.reg, info->opt_flags);
2645 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
2646 LIR* range_check_branch = nullptr;
2647 RegStorage reg_off;
2648 RegStorage reg_ptr;
2649 if (range_check) {
2650 // On x86, we can compare to memory directly
2651 // Set up a launch pad to allow retry in case of bounds violation */
2652 if (rl_idx.is_const) {
2653 LIR* comparison;
2654 range_check_branch = OpCmpMemImmBranch(
2655 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
2656 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr, &comparison);
2657 MarkPossibleNullPointerExceptionAfter(0, comparison);
2658 } else {
2659 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
2660 MarkPossibleNullPointerException(0);
2661 range_check_branch = OpCondBranch(kCondUge, nullptr);
2662 }
2663 }
2664 reg_off = AllocTemp();
2665 reg_ptr = AllocTempRef();
2666 Load32Disp(rl_obj.reg, offset_offset, reg_off);
2667 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
2668 if (rl_idx.is_const) {
2669 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
2670 } else {
2671 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
2672 }
2673 FreeTemp(rl_obj.reg);
2674 if (rl_idx.location == kLocPhysReg) {
2675 FreeTemp(rl_idx.reg);
2676 }
2677 RegLocation rl_dest = InlineTarget(info);
2678 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
2679 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
2680 FreeTemp(reg_off);
2681 FreeTemp(reg_ptr);
2682 StoreValue(rl_dest, rl_result);
2683 if (range_check) {
2684 DCHECK(range_check_branch != nullptr);
2685 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
2686 AddIntrinsicSlowPath(info, range_check_branch);
2687 }
2688 return true;
2689}
2690
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002691bool X86Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
2692 RegLocation rl_dest = InlineTarget(info);
2693
2694 // Early exit if the result is unused.
2695 if (rl_dest.orig_sreg < 0) {
2696 return true;
2697 }
2698
2699 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
2700
2701 if (cu_->target64) {
2702 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<8>());
2703 } else {
2704 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<4>());
2705 }
2706
2707 StoreValue(rl_dest, rl_result);
2708 return true;
2709}
2710
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002711} // namespace art