blob: 95941e04180ee558f954d0f10deab7ef0e6a8b01 [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 serdjukc3561ae2014-07-18 12:35:46 +07001219 // RBX is callee-save register in 64-bit mode.
1220 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
1239 FlushReg(rs_rAX);
1240 Clobber(rs_rAX);
1241 LockTemp(rs_rAX);
1242 FlushReg(rs_rCX);
1243 Clobber(rs_rCX);
1244 LockTemp(rs_rCX);
1245 FlushReg(rs_rDX);
1246 Clobber(rs_rDX);
1247 LockTemp(rs_rDX);
1248 FlushReg(rs_tmp);
1249 Clobber(rs_tmp);
1250 LockTemp(rs_tmp);
1251 if (cu_->target64) {
1252 FlushReg(rs_rDI);
1253 Clobber(rs_rDI);
1254 LockTemp(rs_rDI);
1255 }
1256
buzbeea0cd2d72014-06-01 09:33:49 -07001257 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001258 RegLocation rl_dest = InlineTarget(info);
1259
1260 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001261 LoadValueDirectFixed(rl_obj, rs_rDX);
1262 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001263 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001264
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001265 LIR *slowpath_branch = nullptr, *length_compare = nullptr;
1266
1267 // We need the value in EAX.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001268 if (rl_char.is_const) {
buzbee2700f7e2014-03-07 09:46:20 -08001269 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001270 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001271 // Does the character fit in 16 bits? Compare it at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001272 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001273 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001274 }
1275
1276 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001277 // Location of reference to data array within the String object.
1278 int value_offset = mirror::String::ValueOffset().Int32Value();
1279 // Location of count within the String object.
1280 int count_offset = mirror::String::CountOffset().Int32Value();
1281 // Starting offset within data array.
1282 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1283 // Start of char data with array_.
1284 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001285
Dave Allison69dfe512014-07-11 17:11:58 +00001286 // Compute the number of words to search in to rCX.
1287 Load32Disp(rs_rDX, count_offset, rs_rCX);
1288
Dave Allisondfd3b472014-07-16 16:04:32 -07001289 // Possible signal here due to null pointer dereference.
1290 // Note that the signal handler will expect the top word of
1291 // the stack to be the ArtMethod*. If the PUSH edi instruction
1292 // below is ahead of the load above then this will not be true
1293 // and the signal handler will not work.
1294 MarkPossibleNullPointerException(0);
Dave Allison69dfe512014-07-11 17:11:58 +00001295
Dave Allisondfd3b472014-07-16 16:04:32 -07001296 if (!cu_->target64) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001297 // EDI is callee-save register in 32-bit mode.
1298 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1299 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001300
Mark Mendell4028a6c2014-02-19 20:06:20 -08001301 if (zero_based) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001302 // Start index is not present.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001303 // We have to handle an empty string. Use special instruction JECXZ.
1304 length_compare = NewLIR0(kX86Jecxz8);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001305
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 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001310 // Start index is present.
buzbeea44d4f52014-03-05 11:26:39 -08001311 rl_start = info->args[2];
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001312
Mark Mendell4028a6c2014-02-19 20:06:20 -08001313 // We have to offset by the start index.
1314 if (rl_start.is_const) {
1315 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1316 start_value = std::max(start_value, 0);
1317
1318 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001319 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001320 OpRegImm(kOpMov, rs_rDI, start_value);
1321
1322 // Copy the number of words to search in a temporary register.
1323 // We will use the register at the end to calculate result.
1324 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001325
1326 if (start_value != 0) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001327 // Decrease the number of words to search by the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001328 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001329 }
1330 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001331 // Handle "start index < 0" case.
1332 if (!cu_->target64 && rl_start.location != kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001333 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001334 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001335 {
1336 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001337 Load32Disp(rs_rX86_SP, displacement, rs_rDI);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001338 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001339 } else {
1340 LoadValueDirectFixed(rl_start, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001341 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001342 OpRegReg(kOpXor, rs_tmp, rs_tmp);
1343 OpRegReg(kOpCmp, rs_rDI, rs_tmp);
1344 OpCondRegReg(kOpCmov, kCondLt, rs_rDI, rs_tmp);
1345
1346 // The length of the string should be greater than the start index.
1347 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rDI, nullptr);
1348
1349 // Copy the number of words to search in a temporary register.
1350 // We will use the register at the end to calculate result.
1351 OpRegReg(kOpMov, rs_tmp, rs_rCX);
1352
1353 // Decrease the number of words to search by the start index.
1354 OpRegReg(kOpSub, rs_rCX, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001355 }
1356 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001357
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001358 // Load the address of the string into EDI.
1359 // In case of start index we have to add the address to existing value in EDI.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001360 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001361 if (zero_based || (!zero_based && rl_start.is_const && start_value == 0)) {
1362 Load32Disp(rs_rDX, offset_offset, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001363 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001364 OpRegMem(kOpAdd, rs_rDI, rs_rDX, offset_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001365 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001366 OpRegImm(kOpLsl, rs_rDI, 1);
1367 OpRegMem(kOpAdd, rs_rDI, rs_rDX, value_offset);
1368 OpRegImm(kOpAdd, rs_rDI, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001369
1370 // EDI now contains the start of the string to be searched.
1371 // We are all prepared to do the search for the character.
1372 NewLIR0(kX86RepneScasw);
1373
1374 // Did we find a match?
1375 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1376
1377 // yes, we matched. Compute the index of the result.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001378 OpRegReg(kOpSub, rs_tmp, rs_rCX);
1379 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_tmp.GetReg(), -1);
1380
Mark Mendell4028a6c2014-02-19 20:06:20 -08001381 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1382
1383 // Failed to match; return -1.
1384 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1385 length_compare->target = not_found;
1386 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001387 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001388
1389 // And join up at the end.
1390 all_done->target = NewLIR0(kPseudoTargetLabel);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001391
1392 if (!cu_->target64)
1393 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001394
1395 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001396 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001397 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001398 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001399 }
1400
1401 StoreValue(rl_dest, rl_return);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001402
1403 FreeTemp(rs_rAX);
1404 FreeTemp(rs_rCX);
1405 FreeTemp(rs_rDX);
1406 FreeTemp(rs_tmp);
1407 if (cu_->target64) {
1408 FreeTemp(rs_rDI);
1409 }
1410
Mark Mendell4028a6c2014-02-19 20:06:20 -08001411 return true;
1412}
1413
Tong Shen35e1e6a2014-07-30 09:31:22 -07001414static bool ARTRegIDToDWARFRegID(bool is_x86_64, int art_reg_id, int* dwarf_reg_id) {
1415 if (is_x86_64) {
1416 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001417 case 3 : *dwarf_reg_id = 3; return true; // %rbx
Tong Shen35e1e6a2014-07-30 09:31:22 -07001418 // This is the only discrepancy between ART & DWARF register numbering.
Andreas Gampebda27222014-07-30 23:21:36 -07001419 case 5 : *dwarf_reg_id = 6; return true; // %rbp
1420 case 12: *dwarf_reg_id = 12; return true; // %r12
1421 case 13: *dwarf_reg_id = 13; return true; // %r13
1422 case 14: *dwarf_reg_id = 14; return true; // %r14
1423 case 15: *dwarf_reg_id = 15; return true; // %r15
1424 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001425 }
1426 } else {
1427 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001428 case 5: *dwarf_reg_id = 5; return true; // %ebp
1429 case 6: *dwarf_reg_id = 6; return true; // %esi
1430 case 7: *dwarf_reg_id = 7; return true; // %edi
1431 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001432 }
1433 }
1434}
1435
Tong Shen547cdfd2014-08-05 01:54:19 -07001436std::vector<uint8_t>* X86Mir2Lir::ReturnFrameDescriptionEntry() {
1437 std::vector<uint8_t>* cfi_info = new std::vector<uint8_t>;
Mark Mendellae9fd932014-02-10 16:14:35 -08001438
1439 // Generate the FDE for the method.
1440 DCHECK_NE(data_offset_, 0U);
1441
Tong Shen547cdfd2014-08-05 01:54:19 -07001442 WriteFDEHeader(cfi_info);
1443 WriteFDEAddressRange(cfi_info, data_offset_);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001444
Mark Mendellae9fd932014-02-10 16:14:35 -08001445 // The instructions in the FDE.
1446 if (stack_decrement_ != nullptr) {
1447 // Advance LOC to just past the stack decrement.
1448 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001449 DW_CFA_advance_loc(cfi_info, pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001450
1451 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
Tong Shen547cdfd2014-08-05 01:54:19 -07001452 DW_CFA_def_cfa_offset(cfi_info, frame_size_);
Mark Mendellae9fd932014-02-10 16:14:35 -08001453
Tong Shen35e1e6a2014-07-30 09:31:22 -07001454 // Handle register spills
1455 const uint32_t kSpillInstLen = (cu_->target64) ? 5 : 4;
1456 const int kDataAlignmentFactor = (cu_->target64) ? -8 : -4;
1457 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
1458 int offset = -(GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
1459 for (int reg = 0; mask; mask >>= 1, reg++) {
1460 if (mask & 0x1) {
1461 pc += kSpillInstLen;
1462
1463 // Advance LOC to pass this instruction
Tong Shen547cdfd2014-08-05 01:54:19 -07001464 DW_CFA_advance_loc(cfi_info, kSpillInstLen);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001465
1466 int dwarf_reg_id;
1467 if (ARTRegIDToDWARFRegID(cu_->target64, reg, &dwarf_reg_id)) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001468 // DW_CFA_offset_extended_sf reg offset
1469 DW_CFA_offset_extended_sf(cfi_info, dwarf_reg_id, offset / kDataAlignmentFactor);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001470 }
1471
1472 offset += GetInstructionSetPointerSize(cu_->instruction_set);
1473 }
1474 }
1475
Mark Mendellae9fd932014-02-10 16:14:35 -08001476 // We continue with that stack until the epilogue.
1477 if (stack_increment_ != nullptr) {
1478 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001479 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001480
1481 // We probably have code snippets after the epilogue, so save the
1482 // current state: DW_CFA_remember_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001483 DW_CFA_remember_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001484
Tong Shen35e1e6a2014-07-30 09:31:22 -07001485 // We have now popped the stack: DW_CFA_def_cfa_offset 4/8.
1486 // There is only the return PC on the stack now.
Tong Shen547cdfd2014-08-05 01:54:19 -07001487 DW_CFA_def_cfa_offset(cfi_info, GetInstructionSetPointerSize(cu_->instruction_set));
Mark Mendellae9fd932014-02-10 16:14:35 -08001488
1489 // Everything after that is the same as before the epilogue.
1490 // Stack bump was followed by RET instruction.
1491 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1492 if (post_ret_insn != nullptr) {
1493 pc = new_pc;
1494 new_pc = post_ret_insn->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001495 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001496 // Restore the state: DW_CFA_restore_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001497 DW_CFA_restore_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001498 }
1499 }
1500 }
1501
Tong Shen547cdfd2014-08-05 01:54:19 -07001502 PadCFI(cfi_info);
1503 WriteCFILength(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001504
Mark Mendellae9fd932014-02-10 16:14:35 -08001505 return cfi_info;
1506}
1507
Mark Mendelld65c51a2014-04-29 16:55:20 -04001508void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1509 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001510 case kMirOpReserveVectorRegisters:
1511 ReserveVectorRegisters(mir);
1512 break;
1513 case kMirOpReturnVectorRegisters:
1514 ReturnVectorRegisters();
1515 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001516 case kMirOpConstVector:
1517 GenConst128(bb, mir);
1518 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001519 case kMirOpMoveVector:
1520 GenMoveVector(bb, mir);
1521 break;
1522 case kMirOpPackedMultiply:
1523 GenMultiplyVector(bb, mir);
1524 break;
1525 case kMirOpPackedAddition:
1526 GenAddVector(bb, mir);
1527 break;
1528 case kMirOpPackedSubtract:
1529 GenSubtractVector(bb, mir);
1530 break;
1531 case kMirOpPackedShiftLeft:
1532 GenShiftLeftVector(bb, mir);
1533 break;
1534 case kMirOpPackedSignedShiftRight:
1535 GenSignedShiftRightVector(bb, mir);
1536 break;
1537 case kMirOpPackedUnsignedShiftRight:
1538 GenUnsignedShiftRightVector(bb, mir);
1539 break;
1540 case kMirOpPackedAnd:
1541 GenAndVector(bb, mir);
1542 break;
1543 case kMirOpPackedOr:
1544 GenOrVector(bb, mir);
1545 break;
1546 case kMirOpPackedXor:
1547 GenXorVector(bb, mir);
1548 break;
1549 case kMirOpPackedAddReduce:
1550 GenAddReduceVector(bb, mir);
1551 break;
1552 case kMirOpPackedReduce:
1553 GenReduceVector(bb, mir);
1554 break;
1555 case kMirOpPackedSet:
1556 GenSetVector(bb, mir);
1557 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001558 default:
1559 break;
1560 }
1561}
1562
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001563void X86Mir2Lir::ReserveVectorRegisters(MIR* mir) {
1564 // We should not try to reserve twice without returning the registers
1565 DCHECK_NE(num_reserved_vector_regs_, -1);
1566
1567 int num_vector_reg = mir->dalvikInsn.vA;
1568 for (int i = 0; i < num_vector_reg; i++) {
1569 RegStorage xp_reg = RegStorage::Solo128(i);
1570 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1571 Clobber(xp_reg);
1572
1573 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1574 info != nullptr;
1575 info = info->GetAliasChain()) {
1576 if (info->GetReg().IsSingle()) {
1577 reg_pool_->sp_regs_.Delete(info);
1578 } else {
1579 reg_pool_->dp_regs_.Delete(info);
1580 }
1581 }
1582 }
1583
1584 num_reserved_vector_regs_ = num_vector_reg;
1585}
1586
1587void X86Mir2Lir::ReturnVectorRegisters() {
1588 // Return all the reserved registers
1589 for (int i = 0; i < num_reserved_vector_regs_; i++) {
1590 RegStorage xp_reg = RegStorage::Solo128(i);
1591 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1592
1593 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1594 info != nullptr;
1595 info = info->GetAliasChain()) {
1596 if (info->GetReg().IsSingle()) {
1597 reg_pool_->sp_regs_.Insert(info);
1598 } else {
1599 reg_pool_->dp_regs_.Insert(info);
1600 }
1601 }
1602 }
1603
1604 // We don't have anymore reserved vector registers
1605 num_reserved_vector_regs_ = -1;
1606}
1607
Mark Mendelld65c51a2014-04-29 16:55:20 -04001608void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001609 store_method_addr_used_ = true;
1610 int type_size = mir->dalvikInsn.vB;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001611 // We support 128 bit vectors.
1612 DCHECK_EQ(type_size & 0xFFFF, 128);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001613 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001614 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001615 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001616 // Check for all 0 case.
1617 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1618 NewLIR2(kX86XorpsRR, reg, reg);
1619 return;
1620 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001621
1622 // Append the mov const vector to reg opcode.
1623 AppendOpcodeWithConst(kX86MovupsRM, reg, mir);
1624}
1625
1626void X86Mir2Lir::AppendOpcodeWithConst(X86OpCode opcode, int reg, MIR* mir) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04001627 // Okay, load it from the constant vector area.
1628 LIR *data_target = ScanVectorLiteral(mir);
1629 if (data_target == nullptr) {
1630 data_target = AddVectorLiteral(mir);
1631 }
1632
1633 // Address the start of the method.
1634 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001635 if (rl_method.wide) {
1636 rl_method = LoadValueWide(rl_method, kCoreReg);
1637 } else {
1638 rl_method = LoadValue(rl_method, kCoreReg);
1639 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001640
1641 // Load the proper value from the literal area.
1642 // We don't know the proper offset for the value, so pick one that will force
1643 // 4 byte offset. We will fix this up in the assembler later to have the right
1644 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001645 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001646 LIR *load = NewLIR2(opcode, reg, rl_method.reg.GetReg());
Mark Mendelld65c51a2014-04-29 16:55:20 -04001647 load->flags.fixup = kFixupLoad;
1648 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001649}
1650
Mark Mendellfe945782014-05-22 09:52:36 -04001651void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1652 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001653 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1654 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
1655 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001656 NewLIR2(kX86Mova128RR, rs_dest.GetReg(), rs_src.GetReg());
1657}
1658
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001659void X86Mir2Lir::GenMultiplyVectorSignedByte(BasicBlock *bb, MIR *mir) {
1660 const int BYTE_SIZE = 8;
1661 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1662 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
1663 RegStorage rs_src1_high_tmp = Get128BitRegister(AllocTempWide());
1664
1665 /*
1666 * Emulate the behavior of a kSignedByte by separating out the 16 values in the two XMM
1667 * and multiplying 8 at a time before recombining back into one XMM register.
1668 *
1669 * let xmm1, xmm2 be real srcs (keep low bits of 16bit lanes)
1670 * xmm3 is tmp (operate on high bits of 16bit lanes)
1671 *
1672 * xmm3 = xmm1
1673 * xmm1 = xmm1 .* xmm2
1674 * xmm1 = xmm1 & 0x00ff00ff00ff00ff00ff00ff00ff00ff // xmm1 now has low bits
1675 * xmm3 = xmm3 .>> 8
1676 * xmm2 = xmm2 & 0xff00ff00ff00ff00ff00ff00ff00ff00
1677 * xmm2 = xmm2 .* xmm3 // xmm2 now has high bits
1678 * xmm1 = xmm1 | xmm2 // combine results
1679 */
1680
1681 // Copy xmm1.
1682 NewLIR2(kX86Mova128RR, rs_src1_high_tmp.GetReg(), rs_dest_src1.GetReg());
1683
1684 // Multiply low bits.
1685 NewLIR2(kX86PmullwRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1686
1687 // xmm1 now has low bits.
1688 AndMaskVectorRegister(rs_dest_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
1689
1690 // Prepare high bits for multiplication.
1691 NewLIR2(kX86PsrlwRI, rs_src1_high_tmp.GetReg(), BYTE_SIZE);
1692 AndMaskVectorRegister(rs_src2, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1693
1694 // Multiply high bits and xmm2 now has high bits.
1695 NewLIR2(kX86PmullwRR, rs_src2.GetReg(), rs_src1_high_tmp.GetReg());
1696
1697 // Combine back into dest XMM register.
1698 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1699}
1700
Mark Mendellfe945782014-05-22 09:52:36 -04001701void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001702 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1703 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1704 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1705 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001706 int opcode = 0;
1707 switch (opsize) {
1708 case k32:
1709 opcode = kX86PmulldRR;
1710 break;
1711 case kSignedHalf:
1712 opcode = kX86PmullwRR;
1713 break;
1714 case kSingle:
1715 opcode = kX86MulpsRR;
1716 break;
1717 case kDouble:
1718 opcode = kX86MulpdRR;
1719 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001720 case kSignedByte:
1721 // HW doesn't support 16x16 byte multiplication so emulate it.
1722 GenMultiplyVectorSignedByte(bb, mir);
1723 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001724 default:
1725 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1726 break;
1727 }
1728 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1729}
1730
1731void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001732 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1733 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1734 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1735 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001736 int opcode = 0;
1737 switch (opsize) {
1738 case k32:
1739 opcode = kX86PadddRR;
1740 break;
1741 case kSignedHalf:
1742 case kUnsignedHalf:
1743 opcode = kX86PaddwRR;
1744 break;
1745 case kUnsignedByte:
1746 case kSignedByte:
1747 opcode = kX86PaddbRR;
1748 break;
1749 case kSingle:
1750 opcode = kX86AddpsRR;
1751 break;
1752 case kDouble:
1753 opcode = kX86AddpdRR;
1754 break;
1755 default:
1756 LOG(FATAL) << "Unsupported vector addition " << opsize;
1757 break;
1758 }
1759 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1760}
1761
1762void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001763 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1764 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1765 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1766 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001767 int opcode = 0;
1768 switch (opsize) {
1769 case k32:
1770 opcode = kX86PsubdRR;
1771 break;
1772 case kSignedHalf:
1773 case kUnsignedHalf:
1774 opcode = kX86PsubwRR;
1775 break;
1776 case kUnsignedByte:
1777 case kSignedByte:
1778 opcode = kX86PsubbRR;
1779 break;
1780 case kSingle:
1781 opcode = kX86SubpsRR;
1782 break;
1783 case kDouble:
1784 opcode = kX86SubpdRR;
1785 break;
1786 default:
1787 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1788 break;
1789 }
1790 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1791}
1792
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001793void X86Mir2Lir::GenShiftByteVector(BasicBlock *bb, MIR *mir) {
1794 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1795 RegStorage rs_tmp = Get128BitRegister(AllocTempWide());
1796
1797 int opcode = 0;
1798 int imm = mir->dalvikInsn.vB;
1799
1800 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1801 case kMirOpPackedShiftLeft:
1802 opcode = kX86PsllwRI;
1803 break;
1804 case kMirOpPackedSignedShiftRight:
1805 opcode = kX86PsrawRI;
1806 break;
1807 case kMirOpPackedUnsignedShiftRight:
1808 opcode = kX86PsrlwRI;
1809 break;
1810 default:
1811 LOG(FATAL) << "Unsupported shift operation on byte vector " << opcode;
1812 break;
1813 }
1814
1815 /*
1816 * xmm1 will have low bits
1817 * xmm2 will have high bits
1818 *
1819 * xmm2 = xmm1
1820 * xmm1 = xmm1 .<< N
1821 * xmm2 = xmm2 && 0xFF00FF00FF00FF00FF00FF00FF00FF00
1822 * xmm2 = xmm2 .<< N
1823 * xmm1 = xmm1 | xmm2
1824 */
1825
1826 // Copy xmm1.
1827 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_dest_src1.GetReg());
1828
1829 // Shift lower values.
1830 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1831
1832 // Mask bottom bits.
1833 AndMaskVectorRegister(rs_tmp, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1834
1835 // Shift higher values.
1836 NewLIR2(opcode, rs_tmp.GetReg(), imm);
1837
1838 // Combine back into dest XMM register.
1839 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_tmp.GetReg());
1840}
1841
Mark Mendellfe945782014-05-22 09:52:36 -04001842void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001843 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1844 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1845 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1846 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001847 int opcode = 0;
1848 switch (opsize) {
1849 case k32:
1850 opcode = kX86PslldRI;
1851 break;
1852 case k64:
1853 opcode = kX86PsllqRI;
1854 break;
1855 case kSignedHalf:
1856 case kUnsignedHalf:
1857 opcode = kX86PsllwRI;
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 shift left " << opsize;
1865 break;
1866 }
1867 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1868}
1869
1870void X86Mir2Lir::GenSignedShiftRightVector(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 = kX86PsradRI;
1879 break;
1880 case kSignedHalf:
1881 case kUnsignedHalf:
1882 opcode = kX86PsrawRI;
1883 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001884 case kSignedByte:
1885 case kUnsignedByte:
1886 GenShiftByteVector(bb, mir);
1887 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001888 default:
1889 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
1890 break;
1891 }
1892 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1893}
1894
1895void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001896 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1897 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1898 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1899 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001900 int opcode = 0;
1901 switch (opsize) {
1902 case k32:
1903 opcode = kX86PsrldRI;
1904 break;
1905 case k64:
1906 opcode = kX86PsrlqRI;
1907 break;
1908 case kSignedHalf:
1909 case kUnsignedHalf:
1910 opcode = kX86PsrlwRI;
1911 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001912 case kSignedByte:
1913 case kUnsignedByte:
1914 GenShiftByteVector(bb, mir);
1915 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001916 default:
1917 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1918 break;
1919 }
1920 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1921}
1922
1923void X86Mir2Lir::GenAndVector(BasicBlock *bb, MIR *mir) {
1924 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001925 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1926 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1927 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001928 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1929}
1930
1931void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
1932 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001933 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1934 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1935 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001936 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1937}
1938
1939void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
1940 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001941 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1942 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1943 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001944 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1945}
1946
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001947void X86Mir2Lir::AndMaskVectorRegister(RegStorage rs_src1, uint32_t m1, uint32_t m2, uint32_t m3, uint32_t m4) {
1948 MaskVectorRegister(kX86PandRM, rs_src1, m1, m2, m3, m4);
1949}
1950
1951void X86Mir2Lir::MaskVectorRegister(X86OpCode opcode, RegStorage rs_src1, uint32_t m0, uint32_t m1, uint32_t m2, uint32_t m3) {
1952 // Create temporary MIR as container for 128-bit binary mask.
1953 MIR const_mir;
1954 MIR* const_mirp = &const_mir;
1955 const_mirp->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpConstVector);
1956 const_mirp->dalvikInsn.arg[0] = m0;
1957 const_mirp->dalvikInsn.arg[1] = m1;
1958 const_mirp->dalvikInsn.arg[2] = m2;
1959 const_mirp->dalvikInsn.arg[3] = m3;
1960
1961 // Mask vector with const from literal pool.
1962 AppendOpcodeWithConst(opcode, rs_src1.GetReg(), const_mirp);
1963}
1964
Mark Mendellfe945782014-05-22 09:52:36 -04001965void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001966 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1967 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1968 RegLocation rl_dest = mir_graph_->GetDest(mir);
1969 RegStorage rs_tmp;
1970
1971 int vec_bytes = (mir->dalvikInsn.vC & 0xFFFF) / 8;
1972 int vec_unit_size = 0;
Mark Mendellfe945782014-05-22 09:52:36 -04001973 int opcode = 0;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001974 int extr_opcode = 0;
1975 RegLocation rl_result;
1976
Mark Mendellfe945782014-05-22 09:52:36 -04001977 switch (opsize) {
1978 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001979 extr_opcode = kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001980 opcode = kX86PhadddRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001981 vec_unit_size = 4;
1982 break;
1983 case kSignedByte:
1984 case kUnsignedByte:
1985 extr_opcode = kX86PextrbRRI;
1986 opcode = kX86PhaddwRR;
1987 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001988 break;
1989 case kSignedHalf:
1990 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001991 extr_opcode = kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04001992 opcode = kX86PhaddwRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001993 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04001994 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001995 case kSingle:
1996 rl_result = EvalLoc(rl_dest, kFPReg, true);
1997 vec_unit_size = 4;
1998 for (int i = 0; i < 3; i++) {
1999 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
2000 NewLIR3(kX86ShufpsRRI, rs_src1.GetReg(), rs_src1.GetReg(), 0x39);
2001 }
2002 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
2003 StoreValue(rl_dest, rl_result);
2004
2005 // For single-precision floats, we are done here
2006 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002007 default:
2008 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2009 break;
2010 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002011
2012 int elems = vec_bytes / vec_unit_size;
2013
2014 // Emulate horizontal add instruction by reducing 2 vectors with 8 values before adding them again
2015 // TODO is overflow handled correctly?
2016 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2017 rs_tmp = Get128BitRegister(AllocTempWide());
2018
2019 // tmp = xmm1 .>> 8.
2020 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_src1.GetReg());
2021 NewLIR2(kX86PsrlwRI, rs_tmp.GetReg(), 8);
2022
2023 // Zero extend low bits in xmm1.
2024 AndMaskVectorRegister(rs_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
2025 }
2026
2027 while (elems > 1) {
2028 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2029 NewLIR2(opcode, rs_tmp.GetReg(), rs_tmp.GetReg());
2030 }
2031 NewLIR2(opcode, rs_src1.GetReg(), rs_src1.GetReg());
2032 elems >>= 1;
2033 }
2034
2035 // Combine the results if we separated them.
2036 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2037 NewLIR2(kX86PaddbRR, rs_src1.GetReg(), rs_tmp.GetReg());
2038 }
2039
2040 // We need to extract to a GPR.
2041 RegStorage temp = AllocTemp();
2042 NewLIR3(extr_opcode, temp.GetReg(), rs_src1.GetReg(), 0);
2043
2044 // Can we do this directly into memory?
2045 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2046 if (rl_result.location == kLocPhysReg) {
2047 // Ensure res is in a core reg
2048 rl_result = EvalLoc(rl_dest, kCoreReg, true);
2049 OpRegReg(kOpAdd, rl_result.reg, temp);
2050 StoreFinalValue(rl_dest, rl_result);
2051 } else {
2052 OpMemReg(kOpAdd, rl_result, temp.GetReg());
2053 }
2054
2055 FreeTemp(temp);
Mark Mendellfe945782014-05-22 09:52:36 -04002056}
2057
2058void X86Mir2Lir::GenReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002059 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2060 RegLocation rl_dest = mir_graph_->GetDest(mir);
2061 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
2062 int extract_index = mir->dalvikInsn.arg[0];
2063 int extr_opcode = 0;
2064 RegLocation rl_result;
2065 bool is_wide = false;
2066
Mark Mendellfe945782014-05-22 09:52:36 -04002067 switch (opsize) {
2068 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002069 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2070 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrdMRI : kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002071 break;
2072 case kSignedHalf:
2073 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002074 rl_result= UpdateLocTyped(rl_dest, kCoreReg);
2075 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrwMRI : kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002076 break;
2077 default:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002078 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2079 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002080 break;
2081 }
Mark Mendellfe945782014-05-22 09:52:36 -04002082
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002083 if (rl_result.location == kLocPhysReg) {
2084 NewLIR3(extr_opcode, rl_result.reg.GetReg(), rs_src1.GetReg(), extract_index);
2085 if (is_wide == true) {
2086 StoreFinalValue(rl_dest, rl_result);
2087 } else {
2088 StoreFinalValueWide(rl_dest, rl_result);
2089 }
2090 } else {
2091 int displacement = SRegOffset(rl_result.s_reg_low);
2092 LIR *l = NewLIR3(extr_opcode, rs_rX86_SP.GetReg(), displacement, rs_src1.GetReg());
2093 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is_wide /* is_64bit */);
2094 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is_wide /* is_64bit */);
2095 }
Mark Mendellfe945782014-05-22 09:52:36 -04002096}
2097
2098void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002099 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2100 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2101 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
2102 int op_low = 0, op_high = 0, imm = 0, op_mov = kX86MovdxrRR;
2103 RegisterClass reg_type = kCoreReg;
2104
Mark Mendellfe945782014-05-22 09:52:36 -04002105 switch (opsize) {
2106 case k32:
2107 op_low = kX86PshufdRRI;
2108 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002109 case kSingle:
2110 op_low = kX86PshufdRRI;
2111 op_mov = kX86Mova128RR;
2112 reg_type = kFPReg;
2113 break;
2114 case k64:
2115 op_low = kX86PshufdRRI;
2116 imm = 0x44;
2117 break;
2118 case kDouble:
2119 op_low = kX86PshufdRRI;
2120 op_mov = kX86Mova128RR;
2121 reg_type = kFPReg;
2122 imm = 0x44;
2123 break;
2124 case kSignedByte:
2125 case kUnsignedByte:
2126 // Shuffle 8 bit value into 16 bit word.
2127 // We set val = val + (val << 8) below and use 16 bit shuffle.
Mark Mendellfe945782014-05-22 09:52:36 -04002128 case kSignedHalf:
2129 case kUnsignedHalf:
2130 // Handles low quadword.
2131 op_low = kX86PshuflwRRI;
2132 // Handles upper quadword.
2133 op_high = kX86PshufdRRI;
2134 break;
2135 default:
2136 LOG(FATAL) << "Unsupported vector set " << opsize;
2137 break;
2138 }
2139
Mark Mendellfe945782014-05-22 09:52:36 -04002140 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002141
2142 // Load the value from the VR into the reg.
2143 if (rl_src.wide == 0) {
2144 rl_src = LoadValue(rl_src, reg_type);
2145 } else {
2146 rl_src = LoadValueWide(rl_src, reg_type);
2147 }
2148
2149 // If opsize is 8 bits wide then double value and use 16 bit shuffle instead.
2150 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2151 RegStorage temp = AllocTemp();
2152 // val = val + (val << 8).
2153 NewLIR2(kX86Mov32RR, temp.GetReg(), rl_src.reg.GetReg());
2154 NewLIR2(kX86Sal32RI, temp.GetReg(), 8);
2155 NewLIR2(kX86Or32RR, rl_src.reg.GetReg(), temp.GetReg());
2156 FreeTemp(temp);
2157 }
Mark Mendellfe945782014-05-22 09:52:36 -04002158
2159 // Load the value into the XMM register.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002160 NewLIR2(op_mov, rs_dest.GetReg(), rl_src.reg.GetReg());
Mark Mendellfe945782014-05-22 09:52:36 -04002161
2162 // Now shuffle the value across the destination.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002163 NewLIR3(op_low, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002164
2165 // And then repeat as needed.
2166 if (op_high != 0) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002167 NewLIR3(op_high, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002168 }
2169}
2170
Mark Mendelld65c51a2014-04-29 16:55:20 -04002171LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
2172 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2173 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
2174 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
2175 args[2] == p->operands[2] && args[3] == p->operands[3]) {
2176 return p;
2177 }
2178 }
2179 return nullptr;
2180}
2181
2182LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
2183 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
2184 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2185 new_value->operands[0] = args[0];
2186 new_value->operands[1] = args[1];
2187 new_value->operands[2] = args[2];
2188 new_value->operands[3] = args[3];
2189 new_value->next = const_vectors_;
2190 if (const_vectors_ == nullptr) {
2191 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
2192 }
2193 estimated_native_code_size_ += 16; // Space for one vector.
2194 const_vectors_ = new_value;
2195 return new_value;
2196}
2197
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002198// ------------ ABI support: mapping of args to physical registers -------------
Andreas Gampeccc60262014-07-04 18:02:38 -07002199RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide,
2200 bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002201 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
Andreas Gampeccc60262014-07-04 18:02:38 -07002202 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) /
2203 sizeof(SpecialTargetRegister);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002204 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
Andreas Gampeccc60262014-07-04 18:02:38 -07002205 kFArg4, kFArg5, kFArg6, kFArg7};
2206 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) /
2207 sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002208
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002209 if (is_double_or_float) {
2210 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002211 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide ? kWide : kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002212 }
2213 } else {
2214 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002215 return ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++],
2216 is_ref ? kRef : (is_wide ? kWide : kNotWide));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002217 }
2218 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07002219 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002220}
2221
2222RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
2223 DCHECK(IsInitialized());
2224 auto res = mapping_.find(in_position);
2225 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
2226}
2227
Andreas Gampeccc60262014-07-04 18:02:38 -07002228void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count,
2229 InToRegStorageMapper* mapper) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002230 DCHECK(mapper != nullptr);
2231 max_mapped_in_ = -1;
2232 is_there_stack_mapped_ = false;
2233 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07002234 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
2235 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002236 if (reg.Valid()) {
2237 mapping_[in_position] = reg;
2238 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07002239 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002240 // We covered 2 args, so skip the next one
2241 in_position++;
2242 }
2243 } else {
2244 is_there_stack_mapped_ = true;
2245 }
2246 }
2247 initialized_ = true;
2248}
2249
2250RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002251 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002252 return GetCoreArgMappingToPhysicalReg(arg_num);
2253 }
2254
2255 if (!in_to_reg_storage_mapping_.IsInitialized()) {
2256 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2257 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
2258
Chao-ying Fua77ee512014-07-01 17:43:41 -07002259 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002260 in_to_reg_storage_mapping_.Initialize(arg_locs, cu_->num_ins, &mapper);
2261 }
2262 return in_to_reg_storage_mapping_.Get(arg_num);
2263}
2264
2265RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
2266 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
2267 // Not used for 64-bit, TODO: Move X86_32 to the same framework
2268 switch (core_arg_num) {
2269 case 0:
2270 return rs_rX86_ARG1;
2271 case 1:
2272 return rs_rX86_ARG2;
2273 case 2:
2274 return rs_rX86_ARG3;
2275 default:
2276 return RegStorage::InvalidReg();
2277 }
2278}
2279
2280// ---------End of ABI support: mapping of args to physical registers -------------
2281
2282/*
2283 * If there are any ins passed in registers that have not been promoted
2284 * to a callee-save register, flush them to the frame. Perform initial
2285 * assignment of promoted arguments.
2286 *
2287 * ArgLocs is an array of location records describing the incoming arguments
2288 * with one location record per word of argument.
2289 */
2290void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002291 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002292 /*
2293 * Dummy up a RegLocation for the incoming Method*
2294 * It will attempt to keep kArg0 live (or copy it to home location
2295 * if promoted).
2296 */
2297
2298 RegLocation rl_src = rl_method;
2299 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -07002300 rl_src.reg = TargetReg(kArg0, kRef);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002301 rl_src.home = false;
2302 MarkLive(rl_src);
2303 StoreValue(rl_method, rl_src);
2304 // If Method* has been promoted, explicitly flush
2305 if (rl_method.location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002306 StoreRefDisp(rs_rX86_SP, 0, As32BitReg(TargetReg(kArg0, kRef)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002307 }
2308
2309 if (cu_->num_ins == 0) {
2310 return;
2311 }
2312
2313 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2314 /*
2315 * Copy incoming arguments to their proper home locations.
2316 * NOTE: an older version of dx had an issue in which
2317 * it would reuse static method argument registers.
2318 * This could result in the same Dalvik virtual register
2319 * being promoted to both core and fp regs. To account for this,
2320 * we only copy to the corresponding promoted physical register
2321 * if it matches the type of the SSA name for the incoming
2322 * argument. It is also possible that long and double arguments
2323 * end up half-promoted. In those cases, we must flush the promoted
2324 * half to memory as well.
2325 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002326 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002327 for (int i = 0; i < cu_->num_ins; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002328 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002329 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002330
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002331 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002332 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002333 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002334
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002335 // We have already updated the arg location with promoted info
2336 // so we can be based on it.
2337 if (t_loc->location == kLocPhysReg) {
2338 // Just copy it.
2339 OpRegCopy(t_loc->reg, reg);
2340 } else {
2341 // Needs flush.
2342 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002343 StoreRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002344 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002345 StoreBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002346 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002347 }
2348 }
2349 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002350 // If arriving in frame & promoted.
2351 if (t_loc->location == kLocPhysReg) {
2352 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002353 LoadRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002354 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002355 LoadBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002356 t_loc->wide ? k64 : k32, kNotVolatile);
2357 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002358 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002359 }
2360 if (t_loc->wide) {
2361 // Increment i to skip the next one.
2362 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002363 }
2364 }
2365}
2366
2367/*
2368 * Load up to 5 arguments, the first three of which will be in
2369 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2370 * and as part of the load sequence, it must be replaced with
2371 * the target method pointer. Note, this may also be called
2372 * for "range" variants if the number of arguments is 5 or fewer.
2373 */
2374int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2375 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2376 const MethodReference& target_method,
2377 uint32_t vtable_idx, uintptr_t direct_code,
2378 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002379 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002380 return Mir2Lir::GenDalvikArgsNoRange(info,
2381 call_state, pcrLabel, next_call_insn,
2382 target_method,
2383 vtable_idx, direct_code,
2384 direct_method, type, skip_this);
2385 }
2386 return GenDalvikArgsRange(info,
2387 call_state, pcrLabel, next_call_insn,
2388 target_method,
2389 vtable_idx, direct_code,
2390 direct_method, type, skip_this);
2391}
2392
2393/*
2394 * May have 0+ arguments (also used for jumbo). Note that
2395 * source virtual registers may be in physical registers, so may
2396 * need to be flushed to home location before copying. This
2397 * applies to arg3 and above (see below).
2398 *
2399 * Two general strategies:
2400 * If < 20 arguments
2401 * Pass args 3-18 using vldm/vstm block copy
2402 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2403 * If 20+ arguments
2404 * Pass args arg19+ using memcpy block copy
2405 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2406 *
2407 */
2408int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2409 LIR** pcrLabel, NextCallInsn next_call_insn,
2410 const MethodReference& target_method,
2411 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2412 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002413 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002414 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2415 pcrLabel, next_call_insn,
2416 target_method,
2417 vtable_idx, direct_code, direct_method,
2418 type, skip_this);
2419 }
2420
2421 /* If no arguments, just return */
2422 if (info->num_arg_words == 0)
2423 return call_state;
2424
2425 const int start_index = skip_this ? 1 : 0;
2426
Chao-ying Fua77ee512014-07-01 17:43:41 -07002427 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002428 InToRegStorageMapping in_to_reg_storage_mapping;
2429 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2430 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2431 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
Serguei Katkov8e3acdd2014-07-15 12:01:00 +07002432 info->args[last_mapped_in].wide ? 2 : 1;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002433 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2434
2435 // Fisrt of all, check whether it make sense to use bulk copying
2436 // Optimization is aplicable only for range case
2437 // TODO: make a constant instead of 2
2438 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2439 // Scan the rest of the args - if in phys_reg flush to memory
2440 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2441 RegLocation loc = info->args[next_arg];
2442 if (loc.wide) {
2443 loc = UpdateLocWide(loc);
2444 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002445 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002446 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002447 }
2448 next_arg += 2;
2449 } else {
2450 loc = UpdateLoc(loc);
2451 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002452 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002453 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002454 }
2455 next_arg++;
2456 }
2457 }
2458
2459 // Logic below assumes that Method pointer is at offset zero from SP.
2460 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
2461
2462 // The rest can be copied together
2463 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
Andreas Gampeccc60262014-07-04 18:02:38 -07002464 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped,
2465 cu_->instruction_set);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002466
2467 int current_src_offset = start_offset;
2468 int current_dest_offset = outs_offset;
2469
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002470 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2471 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002472 while (regs_left_to_pass_via_stack > 0) {
2473 // This is based on the knowledge that the stack itself is 16-byte aligned.
2474 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2475 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2476 size_t bytes_to_move;
2477
2478 /*
2479 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2480 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2481 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2482 * We do this because we could potentially do a smaller move to align.
2483 */
2484 if (regs_left_to_pass_via_stack == 4 ||
2485 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2486 // Moving 128-bits via xmm register.
2487 bytes_to_move = sizeof(uint32_t) * 4;
2488
2489 // Allocate a free xmm temp. Since we are working through the calling sequence,
2490 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2491 // there are no free registers.
2492 RegStorage temp = AllocTempDouble();
2493
2494 LIR* ld1 = nullptr;
2495 LIR* ld2 = nullptr;
2496 LIR* st1 = nullptr;
2497 LIR* st2 = nullptr;
2498
2499 /*
2500 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2501 * do an aligned move. If we have 8-byte alignment, then do the move in two
2502 * parts. This approach prevents possible cache line splits. Finally, fall back
2503 * to doing an unaligned move. In most cases we likely won't split the cache
2504 * line but we cannot prove it and thus take a conservative approach.
2505 */
2506 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2507 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2508
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002509 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002510 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002511 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002512 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002513 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovLo128FP);
2514 ld2 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002515 kMovHi128FP);
2516 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002517 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002518 }
2519
2520 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002521 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002522 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002523 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovLo128FP);
2524 st2 = OpMovMemReg(rs_rX86_SP, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002525 temp, kMovHi128FP);
2526 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002527 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002528 }
2529
2530 // TODO If we could keep track of aliasing information for memory accesses that are wider
2531 // than 64-bit, we wouldn't need to set up a barrier.
2532 if (ld1 != nullptr) {
2533 if (ld2 != nullptr) {
2534 // For 64-bit load we can actually set up the aliasing information.
2535 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2536 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2537 } else {
2538 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002539 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002540 }
2541 }
2542 if (st1 != nullptr) {
2543 if (st2 != nullptr) {
2544 // For 64-bit store we can actually set up the aliasing information.
2545 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2546 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2547 } else {
2548 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002549 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002550 }
2551 }
2552
2553 // Free the temporary used for the data movement.
2554 FreeTemp(temp);
2555 } else {
2556 // Moving 32-bits via general purpose register.
2557 bytes_to_move = sizeof(uint32_t);
2558
2559 // Instead of allocating a new temp, simply reuse one of the registers being used
2560 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07002561 RegStorage temp = TargetReg(kArg3, kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002562
2563 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002564 Load32Disp(rs_rX86_SP, current_src_offset, temp);
2565 Store32Disp(rs_rX86_SP, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002566 }
2567
2568 current_src_offset += bytes_to_move;
2569 current_dest_offset += bytes_to_move;
2570 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2571 }
2572 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2573 }
2574
2575 // Now handle rest not registers if they are
2576 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002577 RegStorage regSingle = TargetReg(kArg2, kNotWide);
2578 RegStorage regWide = TargetReg(kArg3, kWide);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002579 for (int i = start_index;
2580 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002581 RegLocation rl_arg = info->args[i];
2582 rl_arg = UpdateRawLoc(rl_arg);
2583 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2584 if (!reg.Valid()) {
2585 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2586
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002587 {
2588 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2589 if (rl_arg.wide) {
2590 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002591 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002592 } else {
2593 LoadValueDirectWideFixed(rl_arg, regWide);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002594 StoreBaseDisp(rs_rX86_SP, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002595 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002596 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002597 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002598 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002599 } else {
2600 LoadValueDirectFixed(rl_arg, regSingle);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002601 StoreBaseDisp(rs_rX86_SP, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002602 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002603 }
2604 }
2605 call_state = next_call_insn(cu_, info, call_state, target_method,
2606 vtable_idx, direct_code, direct_method, type);
2607 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002608 if (rl_arg.wide) {
2609 i++;
2610 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002611 }
2612 }
2613
2614 // Finish with mapped registers
2615 for (int i = start_index; i <= last_mapped_in; i++) {
2616 RegLocation rl_arg = info->args[i];
2617 rl_arg = UpdateRawLoc(rl_arg);
2618 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2619 if (reg.Valid()) {
2620 if (rl_arg.wide) {
2621 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002622 } else {
2623 LoadValueDirectFixed(rl_arg, reg);
2624 }
2625 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2626 direct_code, direct_method, type);
2627 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002628 if (rl_arg.wide) {
2629 i++;
2630 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002631 }
2632
2633 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2634 direct_code, direct_method, type);
2635 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00002636 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002637 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002638 } else {
2639 *pcrLabel = nullptr;
2640 // In lieu of generating a check for kArg1 being null, we need to
2641 // perform a load when doing implicit checks.
2642 RegStorage tmp = AllocTemp();
Andreas Gampeccc60262014-07-04 18:02:38 -07002643 Load32Disp(TargetReg(kArg1, kRef), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002644 MarkPossibleNullPointerException(info->opt_flags);
2645 FreeTemp(tmp);
2646 }
2647 }
2648 return call_state;
2649}
2650
Andreas Gampe98430592014-07-27 19:44:50 -07002651bool X86Mir2Lir::GenInlinedCharAt(CallInfo* info) {
2652 // Location of reference to data array
2653 int value_offset = mirror::String::ValueOffset().Int32Value();
2654 // Location of count
2655 int count_offset = mirror::String::CountOffset().Int32Value();
2656 // Starting offset within data array
2657 int offset_offset = mirror::String::OffsetOffset().Int32Value();
2658 // Start of char data with array_
2659 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
2660
2661 RegLocation rl_obj = info->args[0];
2662 RegLocation rl_idx = info->args[1];
2663 rl_obj = LoadValue(rl_obj, kRefReg);
2664 // X86 wants to avoid putting a constant index into a register.
2665 if (!rl_idx.is_const) {
2666 rl_idx = LoadValue(rl_idx, kCoreReg);
2667 }
2668 RegStorage reg_max;
2669 GenNullCheck(rl_obj.reg, info->opt_flags);
2670 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
2671 LIR* range_check_branch = nullptr;
2672 RegStorage reg_off;
2673 RegStorage reg_ptr;
2674 if (range_check) {
2675 // On x86, we can compare to memory directly
2676 // Set up a launch pad to allow retry in case of bounds violation */
2677 if (rl_idx.is_const) {
2678 LIR* comparison;
2679 range_check_branch = OpCmpMemImmBranch(
2680 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
2681 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr, &comparison);
2682 MarkPossibleNullPointerExceptionAfter(0, comparison);
2683 } else {
2684 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
2685 MarkPossibleNullPointerException(0);
2686 range_check_branch = OpCondBranch(kCondUge, nullptr);
2687 }
2688 }
2689 reg_off = AllocTemp();
2690 reg_ptr = AllocTempRef();
2691 Load32Disp(rl_obj.reg, offset_offset, reg_off);
2692 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
2693 if (rl_idx.is_const) {
2694 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
2695 } else {
2696 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
2697 }
2698 FreeTemp(rl_obj.reg);
2699 if (rl_idx.location == kLocPhysReg) {
2700 FreeTemp(rl_idx.reg);
2701 }
2702 RegLocation rl_dest = InlineTarget(info);
2703 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
2704 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
2705 FreeTemp(reg_off);
2706 FreeTemp(reg_ptr);
2707 StoreValue(rl_dest, rl_result);
2708 if (range_check) {
2709 DCHECK(range_check_branch != nullptr);
2710 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
2711 AddIntrinsicSlowPath(info, range_check_branch);
2712 }
2713 return true;
2714}
2715
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002716bool X86Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
2717 RegLocation rl_dest = InlineTarget(info);
2718
2719 // Early exit if the result is unused.
2720 if (rl_dest.orig_sreg < 0) {
2721 return true;
2722 }
2723
2724 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
2725
2726 if (cu_->target64) {
2727 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<8>());
2728 } else {
2729 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<4>());
2730 }
2731
2732 StoreValue(rl_dest, rl_result);
2733 return true;
2734}
2735
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002736} // namespace art