blob: 901d627799a657143db727b7c68be518a0f92544 [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"
27
Brian Carlstrom7940e442013-07-12 13:46:57 -070028namespace art {
29
Vladimir Marko089142c2014-06-05 10:57:05 +010030static constexpr RegStorage core_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070031 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
32};
Vladimir Marko089142c2014-06-05 10:57:05 +010033static constexpr RegStorage core_regs_arr_64[] = {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070034 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
buzbee091cc402014-03-31 10:14:40 -070035 rs_r8, rs_r9, rs_r10, rs_r11, rs_r12, rs_r13, rs_r14, rs_r15
Brian Carlstrom7940e442013-07-12 13:46:57 -070036};
Vladimir Marko089142c2014-06-05 10:57:05 +010037static constexpr RegStorage core_regs_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070038 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 +070039 rs_r8q, rs_r9q, rs_r10q, rs_r11q, rs_r12q, rs_r13q, rs_r14q, rs_r15q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070040};
Vladimir Marko089142c2014-06-05 10:57:05 +010041static constexpr RegStorage sp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070042 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
43};
Vladimir Marko089142c2014-06-05 10:57:05 +010044static constexpr RegStorage sp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070045 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
buzbee091cc402014-03-31 10:14:40 -070046 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070047};
Vladimir Marko089142c2014-06-05 10:57:05 +010048static constexpr RegStorage dp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070049 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
50};
Vladimir Marko089142c2014-06-05 10:57:05 +010051static constexpr RegStorage dp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070052 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
buzbee091cc402014-03-31 10:14:40 -070053 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070054};
Serguei Katkovc3801912014-07-08 17:21:53 +070055static constexpr RegStorage xp_regs_arr_32[] = {
56 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
57};
58static constexpr RegStorage xp_regs_arr_64[] = {
59 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
60 rs_xr8, rs_xr9, rs_xr10, rs_xr11, rs_xr12, rs_xr13, rs_xr14, rs_xr15
61};
Vladimir Marko089142c2014-06-05 10:57:05 +010062static constexpr RegStorage reserved_regs_arr_32[] = {rs_rX86_SP_32};
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070063static constexpr RegStorage reserved_regs_arr_64[] = {rs_rX86_SP_32};
Vladimir Marko089142c2014-06-05 10:57:05 +010064static constexpr RegStorage reserved_regs_arr_64q[] = {rs_rX86_SP_64};
65static constexpr RegStorage core_temps_arr_32[] = {rs_rAX, rs_rCX, rs_rDX, rs_rBX};
66static constexpr RegStorage core_temps_arr_64[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070067 rs_rAX, rs_rCX, rs_rDX, rs_rSI, rs_rDI,
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070068 rs_r8, rs_r9, rs_r10, rs_r11
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070069};
Serguei Katkovc3801912014-07-08 17:21:53 +070070
71// How to add register to be available for promotion:
72// 1) Remove register from array defining temp
73// 2) Update ClobberCallerSave
74// 3) Update JNI compiler ABI:
75// 3.1) add reg in JniCallingConvention method
76// 3.2) update CoreSpillMask/FpSpillMask
77// 4) Update entrypoints
78// 4.1) Update constants in asm_support_x86_64.h for new frame size
79// 4.2) Remove entry in SmashCallerSaves
80// 4.3) Update jni_entrypoints to spill/unspill new callee save reg
81// 4.4) Update quick_entrypoints to spill/unspill new callee save reg
82// 5) Update runtime ABI
83// 5.1) Update quick_method_frame_info with new required spills
84// 5.2) Update QuickArgumentVisitor with new offsets to gprs and xmms
85// Note that you cannot use register corresponding to incoming args
86// according to ABI and QCG needs one additional XMM temp for
87// bulk copy in preparation to call.
Vladimir Marko089142c2014-06-05 10:57:05 +010088static constexpr RegStorage core_temps_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070089 rs_r0q, rs_r1q, rs_r2q, rs_r6q, rs_r7q,
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070090 rs_r8q, rs_r9q, rs_r10q, rs_r11q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070091};
Vladimir Marko089142c2014-06-05 10:57:05 +010092static constexpr RegStorage sp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070093 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
94};
Vladimir Marko089142c2014-06-05 10:57:05 +010095static constexpr RegStorage sp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070096 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
Serguei Katkovc3801912014-07-08 17:21:53 +070097 rs_fr8, rs_fr9, rs_fr10, rs_fr11
buzbee091cc402014-03-31 10:14:40 -070098};
Vladimir Marko089142c2014-06-05 10:57:05 +010099static constexpr RegStorage dp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700100 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
101};
Vladimir Marko089142c2014-06-05 10:57:05 +0100102static constexpr RegStorage dp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -0700103 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700104 rs_dr8, rs_dr9, rs_dr10, rs_dr11
buzbee091cc402014-03-31 10:14:40 -0700105};
106
Vladimir Marko089142c2014-06-05 10:57:05 +0100107static constexpr RegStorage xp_temps_arr_32[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400108 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
109};
Vladimir Marko089142c2014-06-05 10:57:05 +0100110static constexpr RegStorage xp_temps_arr_64[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400111 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700112 rs_xr8, rs_xr9, rs_xr10, rs_xr11
Mark Mendellfe945782014-05-22 09:52:36 -0400113};
114
Vladimir Marko089142c2014-06-05 10:57:05 +0100115static constexpr ArrayRef<const RegStorage> empty_pool;
116static constexpr ArrayRef<const RegStorage> core_regs_32(core_regs_arr_32);
117static constexpr ArrayRef<const RegStorage> core_regs_64(core_regs_arr_64);
118static constexpr ArrayRef<const RegStorage> core_regs_64q(core_regs_arr_64q);
119static constexpr ArrayRef<const RegStorage> sp_regs_32(sp_regs_arr_32);
120static constexpr ArrayRef<const RegStorage> sp_regs_64(sp_regs_arr_64);
121static constexpr ArrayRef<const RegStorage> dp_regs_32(dp_regs_arr_32);
122static constexpr ArrayRef<const RegStorage> dp_regs_64(dp_regs_arr_64);
Serguei Katkovc3801912014-07-08 17:21:53 +0700123static constexpr ArrayRef<const RegStorage> xp_regs_32(xp_regs_arr_32);
124static constexpr ArrayRef<const RegStorage> xp_regs_64(xp_regs_arr_64);
Vladimir Marko089142c2014-06-05 10:57:05 +0100125static constexpr ArrayRef<const RegStorage> reserved_regs_32(reserved_regs_arr_32);
126static constexpr ArrayRef<const RegStorage> reserved_regs_64(reserved_regs_arr_64);
127static constexpr ArrayRef<const RegStorage> reserved_regs_64q(reserved_regs_arr_64q);
128static constexpr ArrayRef<const RegStorage> core_temps_32(core_temps_arr_32);
129static constexpr ArrayRef<const RegStorage> core_temps_64(core_temps_arr_64);
130static constexpr ArrayRef<const RegStorage> core_temps_64q(core_temps_arr_64q);
131static constexpr ArrayRef<const RegStorage> sp_temps_32(sp_temps_arr_32);
132static constexpr ArrayRef<const RegStorage> sp_temps_64(sp_temps_arr_64);
133static constexpr ArrayRef<const RegStorage> dp_temps_32(dp_temps_arr_32);
134static constexpr ArrayRef<const RegStorage> dp_temps_64(dp_temps_arr_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700135
Vladimir Marko089142c2014-06-05 10:57:05 +0100136static constexpr ArrayRef<const RegStorage> xp_temps_32(xp_temps_arr_32);
137static constexpr ArrayRef<const RegStorage> xp_temps_64(xp_temps_arr_64);
Mark Mendellfe945782014-05-22 09:52:36 -0400138
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700139RegStorage rs_rX86_SP;
140
141X86NativeRegisterPool rX86_ARG0;
142X86NativeRegisterPool rX86_ARG1;
143X86NativeRegisterPool rX86_ARG2;
144X86NativeRegisterPool rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700145X86NativeRegisterPool rX86_ARG4;
146X86NativeRegisterPool rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700147X86NativeRegisterPool rX86_FARG0;
148X86NativeRegisterPool rX86_FARG1;
149X86NativeRegisterPool rX86_FARG2;
150X86NativeRegisterPool rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700151X86NativeRegisterPool rX86_FARG4;
152X86NativeRegisterPool rX86_FARG5;
153X86NativeRegisterPool rX86_FARG6;
154X86NativeRegisterPool rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700155X86NativeRegisterPool rX86_RET0;
156X86NativeRegisterPool rX86_RET1;
157X86NativeRegisterPool rX86_INVOKE_TGT;
158X86NativeRegisterPool rX86_COUNT;
159
160RegStorage rs_rX86_ARG0;
161RegStorage rs_rX86_ARG1;
162RegStorage rs_rX86_ARG2;
163RegStorage rs_rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700164RegStorage rs_rX86_ARG4;
165RegStorage rs_rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700166RegStorage rs_rX86_FARG0;
167RegStorage rs_rX86_FARG1;
168RegStorage rs_rX86_FARG2;
169RegStorage rs_rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700170RegStorage rs_rX86_FARG4;
171RegStorage rs_rX86_FARG5;
172RegStorage rs_rX86_FARG6;
173RegStorage rs_rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700174RegStorage rs_rX86_RET0;
175RegStorage rs_rX86_RET1;
176RegStorage rs_rX86_INVOKE_TGT;
177RegStorage rs_rX86_COUNT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700179RegLocation X86Mir2Lir::LocCReturn() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000180 return x86_loc_c_return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181}
182
buzbeea0cd2d72014-06-01 09:33:49 -0700183RegLocation X86Mir2Lir::LocCReturnRef() {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700184 return cu_->target64 ? x86_64_loc_c_return_ref : x86_loc_c_return_ref;
buzbeea0cd2d72014-06-01 09:33:49 -0700185}
186
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700187RegLocation X86Mir2Lir::LocCReturnWide() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700188 return cu_->target64 ? x86_64_loc_c_return_wide : x86_loc_c_return_wide;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189}
190
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700191RegLocation X86Mir2Lir::LocCReturnFloat() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000192 return x86_loc_c_return_float;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193}
194
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700195RegLocation X86Mir2Lir::LocCReturnDouble() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000196 return x86_loc_c_return_double;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197}
198
Chao-ying Fua77ee512014-07-01 17:43:41 -0700199// Return a target-dependent special register for 32-bit.
200RegStorage X86Mir2Lir::TargetReg32(SpecialTargetRegister reg) {
buzbee091cc402014-03-31 10:14:40 -0700201 RegStorage res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 switch (reg) {
buzbee091cc402014-03-31 10:14:40 -0700203 case kSelf: res_reg = RegStorage::InvalidReg(); break;
204 case kSuspend: res_reg = RegStorage::InvalidReg(); break;
205 case kLr: res_reg = RegStorage::InvalidReg(); break;
206 case kPc: res_reg = RegStorage::InvalidReg(); break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700207 case kSp: res_reg = rs_rX86_SP_32; break; // This must be the concrete one, as _SP is target-
208 // specific size.
buzbee091cc402014-03-31 10:14:40 -0700209 case kArg0: res_reg = rs_rX86_ARG0; break;
210 case kArg1: res_reg = rs_rX86_ARG1; break;
211 case kArg2: res_reg = rs_rX86_ARG2; break;
212 case kArg3: res_reg = rs_rX86_ARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700213 case kArg4: res_reg = rs_rX86_ARG4; break;
214 case kArg5: res_reg = rs_rX86_ARG5; break;
buzbee091cc402014-03-31 10:14:40 -0700215 case kFArg0: res_reg = rs_rX86_FARG0; break;
216 case kFArg1: res_reg = rs_rX86_FARG1; break;
217 case kFArg2: res_reg = rs_rX86_FARG2; break;
218 case kFArg3: res_reg = rs_rX86_FARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700219 case kFArg4: res_reg = rs_rX86_FARG4; break;
220 case kFArg5: res_reg = rs_rX86_FARG5; break;
221 case kFArg6: res_reg = rs_rX86_FARG6; break;
222 case kFArg7: res_reg = rs_rX86_FARG7; break;
buzbee091cc402014-03-31 10:14:40 -0700223 case kRet0: res_reg = rs_rX86_RET0; break;
224 case kRet1: res_reg = rs_rX86_RET1; break;
225 case kInvokeTgt: res_reg = rs_rX86_INVOKE_TGT; break;
226 case kHiddenArg: res_reg = rs_rAX; break;
Elena Sayapinadd644502014-07-01 18:39:52 +0700227 case kHiddenFpArg: DCHECK(!cu_->target64); res_reg = rs_fr0; break;
buzbee091cc402014-03-31 10:14:40 -0700228 case kCount: res_reg = rs_rX86_COUNT; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700229 default: res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 }
buzbee091cc402014-03-31 10:14:40 -0700231 return res_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232}
233
Chao-ying Fua77ee512014-07-01 17:43:41 -0700234RegStorage X86Mir2Lir::TargetReg(SpecialTargetRegister reg) {
235 LOG(FATAL) << "Do not use this function!!!";
236 return RegStorage::InvalidReg();
237}
238
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239/*
240 * Decode the register id.
241 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100242ResourceMask X86Mir2Lir::GetRegMaskCommon(const RegStorage& reg) const {
243 /* Double registers in x86 are just a single FP register. This is always just a single bit. */
244 return ResourceMask::Bit(
245 /* FP register starts at bit position 16 */
246 ((reg.IsFloat() || reg.StorageSize() > 8) ? kX86FPReg0 : 0) + reg.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247}
248
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100249ResourceMask X86Mir2Lir::GetPCUseDefEncoding() const {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100250 return kEncodeNone;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251}
252
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100253void X86Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags,
254 ResourceMask* use_mask, ResourceMask* def_mask) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700255 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbeeb48819d2013-09-14 16:15:25 -0700256 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257
258 // X86-specific resource map setup here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 if (flags & REG_USE_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100260 use_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 }
262
263 if (flags & REG_DEF_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100264 def_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 }
266
267 if (flags & REG_DEFA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100268 SetupRegMask(def_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 }
270
271 if (flags & REG_DEFD) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100272 SetupRegMask(def_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 }
274 if (flags & REG_USEA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100275 SetupRegMask(use_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 }
277
278 if (flags & REG_USEC) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100279 SetupRegMask(use_mask, rs_rCX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 }
281
282 if (flags & REG_USED) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100283 SetupRegMask(use_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 }
Vladimir Marko70b797d2013-12-03 15:25:24 +0000285
286 if (flags & REG_USEB) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100287 SetupRegMask(use_mask, rs_rBX.GetReg());
Vladimir Marko70b797d2013-12-03 15:25:24 +0000288 }
Mark Mendell4028a6c2014-02-19 20:06:20 -0800289
290 // Fixup hard to describe instruction: Uses rAX, rCX, rDI; sets rDI.
291 if (lir->opcode == kX86RepneScasw) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100292 SetupRegMask(use_mask, rs_rAX.GetReg());
293 SetupRegMask(use_mask, rs_rCX.GetReg());
294 SetupRegMask(use_mask, rs_rDI.GetReg());
295 SetupRegMask(def_mask, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800296 }
Serguei Katkove90501d2014-03-12 15:56:54 +0700297
298 if (flags & USE_FP_STACK) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100299 use_mask->SetBit(kX86FPStack);
300 def_mask->SetBit(kX86FPStack);
Serguei Katkove90501d2014-03-12 15:56:54 +0700301 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302}
303
304/* For dumping instructions */
305static const char* x86RegName[] = {
306 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
307 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
308};
309
310static const char* x86CondName[] = {
311 "O",
312 "NO",
313 "B/NAE/C",
314 "NB/AE/NC",
315 "Z/EQ",
316 "NZ/NE",
317 "BE/NA",
318 "NBE/A",
319 "S",
320 "NS",
321 "P/PE",
322 "NP/PO",
323 "L/NGE",
324 "NL/GE",
325 "LE/NG",
326 "NLE/G"
327};
328
329/*
330 * Interpret a format string and build a string no longer than size
331 * See format key in Assemble.cc.
332 */
333std::string X86Mir2Lir::BuildInsnString(const char *fmt, LIR *lir, unsigned char* base_addr) {
334 std::string buf;
335 size_t i = 0;
336 size_t fmt_len = strlen(fmt);
337 while (i < fmt_len) {
338 if (fmt[i] != '!') {
339 buf += fmt[i];
340 i++;
341 } else {
342 i++;
343 DCHECK_LT(i, fmt_len);
344 char operand_number_ch = fmt[i];
345 i++;
346 if (operand_number_ch == '!') {
347 buf += "!";
348 } else {
349 int operand_number = operand_number_ch - '0';
350 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
351 DCHECK_LT(i, fmt_len);
352 int operand = lir->operands[operand_number];
353 switch (fmt[i]) {
354 case 'c':
355 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
356 buf += x86CondName[operand];
357 break;
358 case 'd':
359 buf += StringPrintf("%d", operand);
360 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400361 case 'q': {
362 int64_t value = static_cast<int64_t>(static_cast<int64_t>(operand) << 32 |
363 static_cast<uint32_t>(lir->operands[operand_number+1]));
364 buf +=StringPrintf("%" PRId64, value);
365 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 case 'p': {
buzbee0d829482013-10-11 15:24:55 -0700367 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(operand));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 buf += StringPrintf("0x%08x", tab_rec->offset);
369 break;
370 }
371 case 'r':
buzbee091cc402014-03-31 10:14:40 -0700372 if (RegStorage::IsFloat(operand)) {
373 int fp_reg = RegStorage::RegNum(operand);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 buf += StringPrintf("xmm%d", fp_reg);
375 } else {
buzbee091cc402014-03-31 10:14:40 -0700376 int reg_num = RegStorage::RegNum(operand);
377 DCHECK_LT(static_cast<size_t>(reg_num), sizeof(x86RegName));
378 buf += x86RegName[reg_num];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 }
380 break;
381 case 't':
Ian Rogers107c31e2014-01-23 20:55:29 -0800382 buf += StringPrintf("0x%08" PRIxPTR " (L%p)",
383 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + operand,
384 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 break;
386 default:
387 buf += StringPrintf("DecodeError '%c'", fmt[i]);
388 break;
389 }
390 i++;
391 }
392 }
393 }
394 return buf;
395}
396
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100397void X86Mir2Lir::DumpResourceMask(LIR *x86LIR, const ResourceMask& mask, const char *prefix) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 char buf[256];
399 buf[0] = 0;
400
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100401 if (mask.Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 strcpy(buf, "all");
403 } else {
404 char num[8];
405 int i;
406
407 for (i = 0; i < kX86RegEnd; i++) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100408 if (mask.HasBit(i)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800409 snprintf(num, arraysize(num), "%d ", i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 strcat(buf, num);
411 }
412 }
413
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100414 if (mask.HasBit(ResourceMask::kCCode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 strcat(buf, "cc ");
416 }
417 /* Memory bits */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100418 if (x86LIR && (mask.HasBit(ResourceMask::kDalvikReg))) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800419 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
420 DECODE_ALIAS_INFO_REG(x86LIR->flags.alias_info),
421 (DECODE_ALIAS_INFO_WIDE(x86LIR->flags.alias_info)) ? "(+1)" : "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100423 if (mask.HasBit(ResourceMask::kLiteral)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 strcat(buf, "lit ");
425 }
426
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100427 if (mask.HasBit(ResourceMask::kHeapRef)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 strcat(buf, "heap ");
429 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100430 if (mask.HasBit(ResourceMask::kMustNotAlias)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 strcat(buf, "noalias ");
432 }
433 }
434 if (buf[0]) {
435 LOG(INFO) << prefix << ": " << buf;
436 }
437}
438
439void X86Mir2Lir::AdjustSpillMask() {
440 // Adjustment for LR spilling, x86 has no LR so nothing to do here
buzbee091cc402014-03-31 10:14:40 -0700441 core_spill_mask_ |= (1 << rs_rRET.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 num_core_spills_++;
443}
444
Mark Mendelle87f9b52014-04-30 14:13:18 -0400445RegStorage X86Mir2Lir::AllocateByteRegister() {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700446 RegStorage reg = AllocTypedTemp(false, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +0700447 if (!cu_->target64) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700448 DCHECK_LT(reg.GetRegNum(), rs_rX86_SP.GetRegNum());
449 }
450 return reg;
451}
452
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700453RegStorage X86Mir2Lir::Get128BitRegister(RegStorage reg) {
454 return GetRegInfo(reg)->FindMatchingView(RegisterInfo::k128SoloStorageMask)->GetReg();
455}
456
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700457bool X86Mir2Lir::IsByteRegister(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700458 return cu_->target64 || reg.GetRegNum() < rs_rX86_SP.GetRegNum();
Mark Mendelle87f9b52014-04-30 14:13:18 -0400459}
460
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461/* Clobber all regs that might be used by an external C call */
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000462void X86Mir2Lir::ClobberCallerSave() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700463 if (cu_->target64) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700464 Clobber(rs_rAX);
465 Clobber(rs_rCX);
466 Clobber(rs_rDX);
467 Clobber(rs_rSI);
468 Clobber(rs_rDI);
469
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700470 Clobber(rs_r8);
471 Clobber(rs_r9);
472 Clobber(rs_r10);
473 Clobber(rs_r11);
474
475 Clobber(rs_fr8);
476 Clobber(rs_fr9);
477 Clobber(rs_fr10);
478 Clobber(rs_fr11);
Serguei Katkovc3801912014-07-08 17:21:53 +0700479 } else {
480 Clobber(rs_rAX);
481 Clobber(rs_rCX);
482 Clobber(rs_rDX);
483 Clobber(rs_rBX);
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700484 }
Serguei Katkovc3801912014-07-08 17:21:53 +0700485
486 Clobber(rs_fr0);
487 Clobber(rs_fr1);
488 Clobber(rs_fr2);
489 Clobber(rs_fr3);
490 Clobber(rs_fr4);
491 Clobber(rs_fr5);
492 Clobber(rs_fr6);
493 Clobber(rs_fr7);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494}
495
496RegLocation X86Mir2Lir::GetReturnWideAlt() {
497 RegLocation res = LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -0700498 DCHECK(res.reg.GetLowReg() == rs_rAX.GetReg());
499 DCHECK(res.reg.GetHighReg() == rs_rDX.GetReg());
500 Clobber(rs_rAX);
501 Clobber(rs_rDX);
502 MarkInUse(rs_rAX);
503 MarkInUse(rs_rDX);
504 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 return res;
506}
507
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700508RegLocation X86Mir2Lir::GetReturnAlt() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 RegLocation res = LocCReturn();
buzbee091cc402014-03-31 10:14:40 -0700510 res.reg.SetReg(rs_rDX.GetReg());
511 Clobber(rs_rDX);
512 MarkInUse(rs_rDX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 return res;
514}
515
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700517void X86Mir2Lir::LockCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700518 LockTemp(rs_rX86_ARG0);
519 LockTemp(rs_rX86_ARG1);
520 LockTemp(rs_rX86_ARG2);
521 LockTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700522 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700523 LockTemp(rs_rX86_ARG4);
524 LockTemp(rs_rX86_ARG5);
525 LockTemp(rs_rX86_FARG0);
526 LockTemp(rs_rX86_FARG1);
527 LockTemp(rs_rX86_FARG2);
528 LockTemp(rs_rX86_FARG3);
529 LockTemp(rs_rX86_FARG4);
530 LockTemp(rs_rX86_FARG5);
531 LockTemp(rs_rX86_FARG6);
532 LockTemp(rs_rX86_FARG7);
533 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534}
535
536/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700537void X86Mir2Lir::FreeCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700538 FreeTemp(rs_rX86_ARG0);
539 FreeTemp(rs_rX86_ARG1);
540 FreeTemp(rs_rX86_ARG2);
541 FreeTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700542 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700543 FreeTemp(rs_rX86_ARG4);
544 FreeTemp(rs_rX86_ARG5);
545 FreeTemp(rs_rX86_FARG0);
546 FreeTemp(rs_rX86_FARG1);
547 FreeTemp(rs_rX86_FARG2);
548 FreeTemp(rs_rX86_FARG3);
549 FreeTemp(rs_rX86_FARG4);
550 FreeTemp(rs_rX86_FARG5);
551 FreeTemp(rs_rX86_FARG6);
552 FreeTemp(rs_rX86_FARG7);
553 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554}
555
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800556bool X86Mir2Lir::ProvidesFullMemoryBarrier(X86OpCode opcode) {
557 switch (opcode) {
558 case kX86LockCmpxchgMR:
559 case kX86LockCmpxchgAR:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700560 case kX86LockCmpxchg64M:
561 case kX86LockCmpxchg64A:
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800562 case kX86XchgMR:
563 case kX86Mfence:
564 // Atomic memory instructions provide full barrier.
565 return true;
566 default:
567 break;
568 }
569
570 // Conservative if cannot prove it provides full barrier.
571 return false;
572}
573
Andreas Gampeb14329f2014-05-15 11:16:06 -0700574bool X86Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800576 // Start off with using the last LIR as the barrier. If it is not enough, then we will update it.
577 LIR* mem_barrier = last_lir_insn_;
578
Andreas Gampeb14329f2014-05-15 11:16:06 -0700579 bool ret = false;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800580 /*
Hans Boehm48f5c472014-06-27 14:50:10 -0700581 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
582 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
583 * 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 -0800584 */
Hans Boehm48f5c472014-06-27 14:50:10 -0700585 if (barrier_kind == kAnyAny) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800586 // If no LIR exists already that can be used a barrier, then generate an mfence.
587 if (mem_barrier == nullptr) {
588 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700589 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800590 }
591
592 // If last instruction does not provide full barrier, then insert an mfence.
593 if (ProvidesFullMemoryBarrier(static_cast<X86OpCode>(mem_barrier->opcode)) == false) {
594 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700595 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800596 }
597 }
598
599 // Now ensure that a scheduling barrier is in place.
600 if (mem_barrier == nullptr) {
601 GenBarrier();
602 } else {
603 // Mark as a scheduling barrier.
604 DCHECK(!mem_barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100605 mem_barrier->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800606 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700607 return ret;
608#else
609 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610#endif
611}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000612
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613void X86Mir2Lir::CompilerInitializeRegAlloc() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700614 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700615 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_64, core_regs_64q, sp_regs_64,
616 dp_regs_64, reserved_regs_64, reserved_regs_64q,
617 core_temps_64, core_temps_64q, sp_temps_64, dp_temps_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700618 } else {
buzbeeb01bf152014-05-13 15:59:07 -0700619 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
620 dp_regs_32, reserved_regs_32, empty_pool,
621 core_temps_32, empty_pool, sp_temps_32, dp_temps_32);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700622 }
buzbee091cc402014-03-31 10:14:40 -0700623
624 // Target-specific adjustments.
625
Mark Mendellfe945782014-05-22 09:52:36 -0400626 // Add in XMM registers.
Serguei Katkovc3801912014-07-08 17:21:53 +0700627 const ArrayRef<const RegStorage> *xp_regs = cu_->target64 ? &xp_regs_64 : &xp_regs_32;
628 for (RegStorage reg : *xp_regs) {
Mark Mendellfe945782014-05-22 09:52:36 -0400629 RegisterInfo* info = new (arena_) RegisterInfo(reg, GetRegMaskCommon(reg));
630 reginfo_map_.Put(reg.GetReg(), info);
Serguei Katkovc3801912014-07-08 17:21:53 +0700631 }
632 const ArrayRef<const RegStorage> *xp_temps = cu_->target64 ? &xp_temps_64 : &xp_temps_32;
633 for (RegStorage reg : *xp_temps) {
634 RegisterInfo* xp_reg_info = GetRegInfo(reg);
635 xp_reg_info->SetIsTemp(true);
Mark Mendellfe945782014-05-22 09:52:36 -0400636 }
637
buzbee091cc402014-03-31 10:14:40 -0700638 // Alias single precision xmm to double xmms.
639 // TODO: as needed, add larger vector sizes - alias all to the largest.
640 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
641 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
642 int sp_reg_num = info->GetReg().GetRegNum();
Mark Mendellfe945782014-05-22 09:52:36 -0400643 RegStorage xp_reg = RegStorage::Solo128(sp_reg_num);
644 RegisterInfo* xp_reg_info = GetRegInfo(xp_reg);
645 // 128-bit xmm vector register's master storage should refer to itself.
646 DCHECK_EQ(xp_reg_info, xp_reg_info->Master());
647
648 // Redirect 32-bit vector's master storage to 128-bit vector.
649 info->SetMaster(xp_reg_info);
650
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700651 RegStorage dp_reg = RegStorage::FloatSolo64(sp_reg_num);
buzbee091cc402014-03-31 10:14:40 -0700652 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
Mark Mendellfe945782014-05-22 09:52:36 -0400653 // Redirect 64-bit vector's master storage to 128-bit vector.
654 dp_reg_info->SetMaster(xp_reg_info);
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700655 // Singles should show a single 32-bit mask bit, at first referring to the low half.
656 DCHECK_EQ(info->StorageMask(), 0x1U);
657 }
658
Elena Sayapinadd644502014-07-01 18:39:52 +0700659 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700660 // Alias 32bit W registers to corresponding 64bit X registers.
661 GrowableArray<RegisterInfo*>::Iterator w_it(&reg_pool_->core_regs_);
662 for (RegisterInfo* info = w_it.Next(); info != nullptr; info = w_it.Next()) {
663 int x_reg_num = info->GetReg().GetRegNum();
664 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
665 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
666 // 64bit X register's master storage should refer to itself.
667 DCHECK_EQ(x_reg_info, x_reg_info->Master());
668 // Redirect 32bit W master storage to 64bit X.
669 info->SetMaster(x_reg_info);
670 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
671 DCHECK_EQ(info->StorageMask(), 0x1U);
672 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 }
buzbee091cc402014-03-31 10:14:40 -0700674
675 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
676 // TODO: adjust for x86/hard float calling convention.
677 reg_pool_->next_core_reg_ = 2;
678 reg_pool_->next_sp_reg_ = 2;
679 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680}
681
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700682int X86Mir2Lir::VectorRegisterSize() {
683 return 128;
684}
685
686int X86Mir2Lir::NumReservableVectorRegisters(bool fp_used) {
687 return fp_used ? 5 : 7;
688}
689
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690void X86Mir2Lir::SpillCoreRegs() {
691 if (num_core_spills_ == 0) {
692 return;
693 }
694 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700695 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700696 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700697 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 for (int reg = 0; mask; mask >>= 1, reg++) {
699 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700700 StoreBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
701 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700702 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 }
704 }
705}
706
707void X86Mir2Lir::UnSpillCoreRegs() {
708 if (num_core_spills_ == 0) {
709 return;
710 }
711 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700712 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700713 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700714 OpSize size = cu_->target64 ? k64 : k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 for (int reg = 0; mask; mask >>= 1, reg++) {
716 if (mask & 0x1) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700717 LoadBaseDisp(rs_rX86_SP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
718 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700719 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 }
721 }
722}
723
Serguei Katkovc3801912014-07-08 17:21:53 +0700724void X86Mir2Lir::SpillFPRegs() {
725 if (num_fp_spills_ == 0) {
726 return;
727 }
728 uint32_t mask = fp_spill_mask_;
729 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
730 for (int reg = 0; mask; mask >>= 1, reg++) {
731 if (mask & 0x1) {
732 StoreBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
733 k64, kNotVolatile);
734 offset += sizeof(double);
735 }
736 }
737}
738void X86Mir2Lir::UnSpillFPRegs() {
739 if (num_fp_spills_ == 0) {
740 return;
741 }
742 uint32_t mask = fp_spill_mask_;
743 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
744 for (int reg = 0; mask; mask >>= 1, reg++) {
745 if (mask & 0x1) {
746 LoadBaseDisp(rs_rX86_SP, offset, RegStorage::FloatSolo64(reg),
747 k64, kNotVolatile);
748 offset += sizeof(double);
749 }
750 }
751}
752
753
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700754bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700755 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
756}
757
Vladimir Marko674744e2014-04-24 15:18:26 +0100758RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700759 // X86_64 can handle any size.
Elena Sayapinadd644502014-07-01 18:39:52 +0700760 if (cu_->target64) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700761 if (size == kReference) {
762 return kRefReg;
763 }
764 return kCoreReg;
765 }
766
Vladimir Marko674744e2014-04-24 15:18:26 +0100767 if (UNLIKELY(is_volatile)) {
768 // On x86, atomic 64-bit load/store requires an fp register.
769 // Smaller aligned load/store is atomic for both core and fp registers.
770 if (size == k64 || size == kDouble) {
771 return kFPReg;
772 }
773 }
774 return RegClassBySize(size);
775}
776
Elena Sayapinadd644502014-07-01 18:39:52 +0700777X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800778 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700779 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800780 method_address_insns_(arena, 100, kGrowableArrayMisc),
781 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800782 call_method_insns_(arena, 100, kGrowableArrayMisc),
Elena Sayapinadd644502014-07-01 18:39:52 +0700783 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400784 const_vectors_(nullptr) {
785 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700786 if (kIsDebugBuild) {
787 for (int i = 0; i < kX86Last; i++) {
788 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
789 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400790 << " is wrong: expecting " << i << ", seeing "
791 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700792 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 }
794 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700795 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700796 rs_rX86_SP = rs_rX86_SP_64;
797
798 rs_rX86_ARG0 = rs_rDI;
799 rs_rX86_ARG1 = rs_rSI;
800 rs_rX86_ARG2 = rs_rDX;
801 rs_rX86_ARG3 = rs_rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700802 rs_rX86_ARG4 = rs_r8;
803 rs_rX86_ARG5 = rs_r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700804 rs_rX86_FARG0 = rs_fr0;
805 rs_rX86_FARG1 = rs_fr1;
806 rs_rX86_FARG2 = rs_fr2;
807 rs_rX86_FARG3 = rs_fr3;
808 rs_rX86_FARG4 = rs_fr4;
809 rs_rX86_FARG5 = rs_fr5;
810 rs_rX86_FARG6 = rs_fr6;
811 rs_rX86_FARG7 = rs_fr7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700812 rX86_ARG0 = rDI;
813 rX86_ARG1 = rSI;
814 rX86_ARG2 = rDX;
815 rX86_ARG3 = rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700816 rX86_ARG4 = r8;
817 rX86_ARG5 = r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700818 rX86_FARG0 = fr0;
819 rX86_FARG1 = fr1;
820 rX86_FARG2 = fr2;
821 rX86_FARG3 = fr3;
822 rX86_FARG4 = fr4;
823 rX86_FARG5 = fr5;
824 rX86_FARG6 = fr6;
825 rX86_FARG7 = fr7;
Mark Mendell55884bc2014-06-10 10:21:29 -0400826 rs_rX86_INVOKE_TGT = rs_rDI;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700827 } else {
828 rs_rX86_SP = rs_rX86_SP_32;
829
830 rs_rX86_ARG0 = rs_rAX;
831 rs_rX86_ARG1 = rs_rCX;
832 rs_rX86_ARG2 = rs_rDX;
833 rs_rX86_ARG3 = rs_rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700834 rs_rX86_ARG4 = RegStorage::InvalidReg();
835 rs_rX86_ARG5 = RegStorage::InvalidReg();
836 rs_rX86_FARG0 = rs_rAX;
837 rs_rX86_FARG1 = rs_rCX;
838 rs_rX86_FARG2 = rs_rDX;
839 rs_rX86_FARG3 = rs_rBX;
840 rs_rX86_FARG4 = RegStorage::InvalidReg();
841 rs_rX86_FARG5 = RegStorage::InvalidReg();
842 rs_rX86_FARG6 = RegStorage::InvalidReg();
843 rs_rX86_FARG7 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700844 rX86_ARG0 = rAX;
845 rX86_ARG1 = rCX;
846 rX86_ARG2 = rDX;
847 rX86_ARG3 = rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700848 rX86_FARG0 = rAX;
849 rX86_FARG1 = rCX;
850 rX86_FARG2 = rDX;
851 rX86_FARG3 = rBX;
Mark Mendell55884bc2014-06-10 10:21:29 -0400852 rs_rX86_INVOKE_TGT = rs_rAX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700853 // TODO(64): Initialize with invalid reg
854// rX86_ARG4 = RegStorage::InvalidReg();
855// rX86_ARG5 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700856 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700857 rs_rX86_RET0 = rs_rAX;
858 rs_rX86_RET1 = rs_rDX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700859 rs_rX86_COUNT = rs_rCX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700860 rX86_RET0 = rAX;
861 rX86_RET1 = rDX;
862 rX86_INVOKE_TGT = rAX;
863 rX86_COUNT = rCX;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700864
865 // Initialize the number of reserved vector registers
866 num_reserved_vector_regs_ = -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867}
868
869Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
870 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700871 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872}
873
Andreas Gampe98430592014-07-27 19:44:50 -0700874// Not used in x86(-64)
875RegStorage X86Mir2Lir::LoadHelper(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700876 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
877 return RegStorage::InvalidReg();
878}
879
Dave Allisonb373e092014-02-20 16:06:36 -0800880LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
Dave Allison69dfe512014-07-11 17:11:58 +0000881 // First load the pointer in fs:[suspend-trigger] into eax
882 // Then use a test instruction to indirect via that address.
883 NewLIR2(kX86Mov32RT, rs_rAX.GetReg(), cu_->target64 ?
884 Thread::ThreadSuspendTriggerOffset<8>().Int32Value() :
885 Thread::ThreadSuspendTriggerOffset<4>().Int32Value());
886 return NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rAX.GetReg(), 0);
Dave Allisonb373e092014-02-20 16:06:36 -0800887}
888
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700889uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700890 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 return X86Mir2Lir::EncodingMap[opcode].flags;
892}
893
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700894const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700895 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 return X86Mir2Lir::EncodingMap[opcode].name;
897}
898
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700899const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700900 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 return X86Mir2Lir::EncodingMap[opcode].fmt;
902}
903
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000904void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
905 // Can we do this directly to memory?
906 rl_dest = UpdateLocWide(rl_dest);
907 if ((rl_dest.location == kLocDalvikFrame) ||
908 (rl_dest.location == kLocCompilerTemp)) {
909 int32_t val_lo = Low32Bits(value);
910 int32_t val_hi = High32Bits(value);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700911 int r_base = rs_rX86_SP.GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000912 int displacement = SRegOffset(rl_dest.s_reg_low);
913
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100914 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800915 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000916 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
917 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800918 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000919 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
920 false /* is_load */, true /* is64bit */);
921 return;
922 }
923
924 // Just use the standard code to do the generation.
925 Mir2Lir::GenConstWide(rl_dest, value);
926}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800927
928// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
929void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
930 LOG(INFO) << "location: " << loc.location << ','
931 << (loc.wide ? " w" : " ")
932 << (loc.defined ? " D" : " ")
933 << (loc.is_const ? " c" : " ")
934 << (loc.fp ? " F" : " ")
935 << (loc.core ? " C" : " ")
936 << (loc.ref ? " r" : " ")
937 << (loc.high_word ? " h" : " ")
938 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800939 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000940 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800941 << ", s_reg: " << loc.s_reg_low
942 << ", orig: " << loc.orig_sreg;
943}
944
Mark Mendell67c39c42014-01-31 17:28:00 -0800945void X86Mir2Lir::Materialize() {
946 // A good place to put the analysis before starting.
947 AnalyzeMIR();
948
949 // Now continue with regular code generation.
950 Mir2Lir::Materialize();
951}
952
Jeff Hao49161ce2014-03-12 11:05:25 -0700953void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800954 SpecialTargetRegister symbolic_reg) {
955 /*
956 * For x86, just generate a 32 bit move immediate instruction, that will be filled
957 * in at 'link time'. For now, put a unique value based on target to ensure that
958 * code deduplication works.
959 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700960 int target_method_idx = target_method.dex_method_index;
961 const DexFile* target_dex_file = target_method.dex_file;
962 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
963 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800964
Jeff Hao49161ce2014-03-12 11:05:25 -0700965 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700966 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
967 TargetReg(symbolic_reg, kNotWide).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700968 static_cast<int>(target_method_id_ptr), target_method_idx,
969 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800970 AppendLIR(move);
971 method_address_insns_.Insert(move);
972}
973
Fred Shihe7f82e22014-08-06 10:46:37 -0700974void X86Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
975 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800976 /*
977 * For x86, just generate a 32 bit move immediate instruction, that will be filled
978 * in at 'link time'. For now, put a unique value based on target to ensure that
979 * code deduplication works.
980 */
Fred Shihe7f82e22014-08-06 10:46:37 -0700981 const DexFile::TypeId& id = dex_file.GetTypeId(type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800982 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
983
984 // Generate the move instruction with the unique pointer and save index and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700985 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
986 TargetReg(symbolic_reg, kNotWide).GetReg(),
Fred Shihe7f82e22014-08-06 10:46:37 -0700987 static_cast<int>(ptr), type_idx,
988 WrapPointer(const_cast<DexFile*>(&dex_file)));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800989 AppendLIR(move);
990 class_type_address_insns_.Insert(move);
991}
992
Jeff Hao49161ce2014-03-12 11:05:25 -0700993LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800994 /*
995 * For x86, just generate a 32 bit call relative instruction, that will be filled
996 * in at 'link time'. For now, put a unique value based on target to ensure that
997 * code deduplication works.
998 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700999 int target_method_idx = target_method.dex_method_index;
1000 const DexFile* target_dex_file = target_method.dex_file;
1001 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
1002 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001003
Jeff Hao49161ce2014-03-12 11:05:25 -07001004 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
1005 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
1006 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001007 AppendLIR(call);
1008 call_method_insns_.Insert(call);
1009 return call;
1010}
1011
Mark Mendelld65c51a2014-04-29 16:55:20 -04001012/*
1013 * @brief Enter a 32 bit quantity into a buffer
1014 * @param buf buffer.
1015 * @param data Data value.
1016 */
1017
1018static void PushWord(std::vector<uint8_t>&buf, int32_t data) {
1019 buf.push_back(data & 0xff);
1020 buf.push_back((data >> 8) & 0xff);
1021 buf.push_back((data >> 16) & 0xff);
1022 buf.push_back((data >> 24) & 0xff);
1023}
1024
Mark Mendell55d0eac2014-02-06 11:02:52 -08001025void X86Mir2Lir::InstallLiteralPools() {
1026 // These are handled differently for x86.
1027 DCHECK(code_literal_list_ == nullptr);
1028 DCHECK(method_literal_list_ == nullptr);
1029 DCHECK(class_literal_list_ == nullptr);
1030
Mark Mendelld65c51a2014-04-29 16:55:20 -04001031 // Align to 16 byte boundary. We have implicit knowledge that the start of the method is
1032 // on a 4 byte boundary. How can I check this if it changes (other than aligned loads
1033 // will fail at runtime)?
1034 if (const_vectors_ != nullptr) {
1035 int align_size = (16-4) - (code_buffer_.size() & 0xF);
1036 if (align_size < 0) {
1037 align_size += 16;
1038 }
1039
1040 while (align_size > 0) {
1041 code_buffer_.push_back(0);
1042 align_size--;
1043 }
1044 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1045 PushWord(code_buffer_, p->operands[0]);
1046 PushWord(code_buffer_, p->operands[1]);
1047 PushWord(code_buffer_, p->operands[2]);
1048 PushWord(code_buffer_, p->operands[3]);
1049 }
1050 }
1051
Mark Mendell55d0eac2014-02-06 11:02:52 -08001052 // Handle the fixups for methods.
1053 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
1054 LIR* p = method_address_insns_.Get(i);
1055 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001056 uint32_t target_method_idx = p->operands[2];
1057 const DexFile* target_dex_file =
1058 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001059
1060 // The offset to patch is the last 4 bytes of the instruction.
1061 int patch_offset = p->offset + p->flags.size - 4;
1062 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
1063 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -07001064 target_method_idx, target_dex_file,
1065 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001066 patch_offset);
1067 }
1068
1069 // Handle the fixups for class types.
1070 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
1071 LIR* p = class_type_address_insns_.Get(i);
1072 DCHECK_EQ(p->opcode, kX86Mov32RI);
Fred Shihe7f82e22014-08-06 10:46:37 -07001073
1074 const DexFile* class_dex_file =
1075 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Jeff Hao49161ce2014-03-12 11:05:25 -07001076 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001077
1078 // The offset to patch is the last 4 bytes of the instruction.
1079 int patch_offset = p->offset + p->flags.size - 4;
1080 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Fred Shihe7f82e22014-08-06 10:46:37 -07001081 cu_->method_idx, target_method_idx, class_dex_file,
1082 patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001083 }
1084
1085 // And now the PC-relative calls to methods.
1086 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
1087 LIR* p = call_method_insns_.Get(i);
1088 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001089 uint32_t target_method_idx = p->operands[1];
1090 const DexFile* target_dex_file =
1091 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001092
1093 // The offset to patch is the last 4 bytes of the instruction.
1094 int patch_offset = p->offset + p->flags.size - 4;
1095 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001096 cu_->method_idx, cu_->invoke_type,
1097 target_method_idx, target_dex_file,
1098 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001099 patch_offset, -4 /* offset */);
1100 }
1101
1102 // And do the normal processing.
1103 Mir2Lir::InstallLiteralPools();
1104}
1105
DaniilSokolov70c4f062014-06-24 17:34:00 -07001106bool X86Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
DaniilSokolov70c4f062014-06-24 17:34:00 -07001107 RegLocation rl_src = info->args[0];
1108 RegLocation rl_srcPos = info->args[1];
1109 RegLocation rl_dst = info->args[2];
1110 RegLocation rl_dstPos = info->args[3];
1111 RegLocation rl_length = info->args[4];
1112 if (rl_srcPos.is_const && (mir_graph_->ConstantValue(rl_srcPos) < 0)) {
1113 return false;
1114 }
1115 if (rl_dstPos.is_const && (mir_graph_->ConstantValue(rl_dstPos) < 0)) {
1116 return false;
1117 }
1118 ClobberCallerSave();
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001119 LockCallTemps(); // Using fixed registers.
1120 RegStorage tmp_reg = cu_->target64 ? rs_r11 : rs_rBX;
1121 LoadValueDirectFixed(rl_src, rs_rAX);
1122 LoadValueDirectFixed(rl_dst, rs_rCX);
1123 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_rAX, rs_rCX, nullptr);
1124 LIR* src_null_branch = OpCmpImmBranch(kCondEq, rs_rAX, 0, nullptr);
1125 LIR* dst_null_branch = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1126 LoadValueDirectFixed(rl_length, rs_rDX);
1127 // If the length of the copy is > 128 characters (256 bytes) or negative then go slow path.
1128 LIR* len_too_big = OpCmpImmBranch(kCondHi, rs_rDX, 128, nullptr);
1129 LoadValueDirectFixed(rl_src, rs_rAX);
1130 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001131 LIR* src_bad_len = nullptr;
1132 LIR* srcPos_negative = nullptr;
1133 if (!rl_srcPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001134 LoadValueDirectFixed(rl_srcPos, tmp_reg);
1135 srcPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1136 OpRegReg(kOpAdd, tmp_reg, rs_rDX);
1137 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001138 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001139 int32_t pos_val = mir_graph_->ConstantValue(rl_srcPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001140 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001141 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001142 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001143 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1144 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001145 }
1146 }
1147 LIR* dstPos_negative = nullptr;
1148 LIR* dst_bad_len = nullptr;
1149 LoadValueDirectFixed(rl_dst, rs_rAX);
1150 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1151 if (!rl_dstPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001152 LoadValueDirectFixed(rl_dstPos, tmp_reg);
1153 dstPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
1154 OpRegRegReg(kOpAdd, tmp_reg, tmp_reg, rs_rDX);
1155 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001156 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001157 int32_t pos_val = mir_graph_->ConstantValue(rl_dstPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001158 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001159 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001160 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001161 OpRegRegImm(kOpAdd, tmp_reg, rs_rDX, pos_val);
1162 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001163 }
1164 }
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001165 // Everything is checked now.
1166 LoadValueDirectFixed(rl_src, rs_rAX);
1167 LoadValueDirectFixed(rl_dst, tmp_reg);
1168 LoadValueDirectFixed(rl_srcPos, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001169 NewLIR5(kX86Lea32RA, rs_rAX.GetReg(), rs_rAX.GetReg(),
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001170 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value());
1171 // RAX now holds the address of the first src element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001172
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001173 LoadValueDirectFixed(rl_dstPos, rs_rCX);
1174 NewLIR5(kX86Lea32RA, tmp_reg.GetReg(), tmp_reg.GetReg(),
1175 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value() );
1176 // RBX now holds the address of the first dst element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001177
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001178 // Check if the number of elements to be copied is odd or even. If odd
DaniilSokolov70c4f062014-06-24 17:34:00 -07001179 // then copy the first element (so that the remaining number of elements
1180 // is even).
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001181 LoadValueDirectFixed(rl_length, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001182 OpRegImm(kOpAnd, rs_rCX, 1);
1183 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1184 OpRegImm(kOpSub, rs_rDX, 1);
1185 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001186 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001187
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001188 // Since the remaining number of elements is even, we will copy by
DaniilSokolov70c4f062014-06-24 17:34:00 -07001189 // two elements at a time.
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001190 LIR* beginLoop = NewLIR0(kPseudoTargetLabel);
1191 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_rDX, 0, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001192 OpRegImm(kOpSub, rs_rDX, 2);
1193 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001194 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001195 OpUnconditionalBranch(beginLoop);
1196 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1197 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1198 LIR *return_point = NewLIR0(kPseudoTargetLabel);
1199 jmp_to_ret->target = return_point;
1200 jmp_to_begin_loop->target = beginLoop;
1201 src_dst_same->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001202 len_too_big->target = check_failed;
1203 src_null_branch->target = check_failed;
1204 if (srcPos_negative != nullptr)
1205 srcPos_negative ->target = check_failed;
1206 if (src_bad_len != nullptr)
1207 src_bad_len->target = check_failed;
1208 dst_null_branch->target = check_failed;
1209 if (dstPos_negative != nullptr)
1210 dstPos_negative->target = check_failed;
1211 if (dst_bad_len != nullptr)
1212 dst_bad_len->target = check_failed;
1213 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
1214 return true;
1215}
1216
1217
Mark Mendell4028a6c2014-02-19 20:06:20 -08001218/*
1219 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1220 * otherwise bails to standard library code.
1221 */
1222bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001223 RegLocation rl_obj = info->args[0];
1224 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001225 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001226 // RBX is callee-save register in 64-bit mode.
1227 RegStorage rs_tmp = cu_->target64 ? rs_r11 : rs_rBX;
1228 int start_value = -1;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001229
1230 uint32_t char_value =
1231 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1232
1233 if (char_value > 0xFFFF) {
1234 // We have to punt to the real String.indexOf.
1235 return false;
1236 }
1237
1238 // Okay, we are commited to inlining this.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001239 // EAX: 16 bit character being searched.
1240 // ECX: count: number of words to be searched.
1241 // EDI: String being searched.
1242 // EDX: temporary during execution.
1243 // EBX or R11: temporary during execution (depending on mode).
1244 // REP SCASW: search instruction.
1245
1246 FlushReg(rs_rAX);
1247 Clobber(rs_rAX);
1248 LockTemp(rs_rAX);
1249 FlushReg(rs_rCX);
1250 Clobber(rs_rCX);
1251 LockTemp(rs_rCX);
1252 FlushReg(rs_rDX);
1253 Clobber(rs_rDX);
1254 LockTemp(rs_rDX);
1255 FlushReg(rs_tmp);
1256 Clobber(rs_tmp);
1257 LockTemp(rs_tmp);
1258 if (cu_->target64) {
1259 FlushReg(rs_rDI);
1260 Clobber(rs_rDI);
1261 LockTemp(rs_rDI);
1262 }
1263
buzbeea0cd2d72014-06-01 09:33:49 -07001264 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001265 RegLocation rl_dest = InlineTarget(info);
1266
1267 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001268 LoadValueDirectFixed(rl_obj, rs_rDX);
1269 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001270 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001271
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001272 LIR *slowpath_branch = nullptr, *length_compare = nullptr;
1273
1274 // We need the value in EAX.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001275 if (rl_char.is_const) {
buzbee2700f7e2014-03-07 09:46:20 -08001276 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001277 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001278 // Does the character fit in 16 bits? Compare it at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001279 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001280 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001281 }
1282
1283 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001284 // Location of reference to data array within the String object.
1285 int value_offset = mirror::String::ValueOffset().Int32Value();
1286 // Location of count within the String object.
1287 int count_offset = mirror::String::CountOffset().Int32Value();
1288 // Starting offset within data array.
1289 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1290 // Start of char data with array_.
1291 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001292
Dave Allison69dfe512014-07-11 17:11:58 +00001293 // Compute the number of words to search in to rCX.
1294 Load32Disp(rs_rDX, count_offset, rs_rCX);
1295
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001296 if (!cu_->target64) {
1297 // Possible signal here due to null pointer dereference.
1298 // Note that the signal handler will expect the top word of
1299 // the stack to be the ArtMethod*. If the PUSH edi instruction
1300 // below is ahead of the load above then this will not be true
1301 // and the signal handler will not work.
1302 MarkPossibleNullPointerException(0);
Dave Allison69dfe512014-07-11 17:11:58 +00001303
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001304 // EDI is callee-save register in 32-bit mode.
1305 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1306 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001307
Mark Mendell4028a6c2014-02-19 20:06:20 -08001308 if (zero_based) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001309 // Start index is not present.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001310 // We have to handle an empty string. Use special instruction JECXZ.
1311 length_compare = NewLIR0(kX86Jecxz8);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001312
1313 // Copy the number of words to search in a temporary register.
1314 // We will use the register at the end to calculate result.
1315 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001316 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001317 // Start index is present.
buzbeea44d4f52014-03-05 11:26:39 -08001318 rl_start = info->args[2];
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001319
Mark Mendell4028a6c2014-02-19 20:06:20 -08001320 // We have to offset by the start index.
1321 if (rl_start.is_const) {
1322 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1323 start_value = std::max(start_value, 0);
1324
1325 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001326 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001327 OpRegImm(kOpMov, rs_rDI, start_value);
1328
1329 // Copy the number of words to search in a temporary register.
1330 // We will use the register at the end to calculate result.
1331 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001332
1333 if (start_value != 0) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001334 // Decrease the number of words to search by the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001335 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001336 }
1337 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001338 // Handle "start index < 0" case.
1339 if (!cu_->target64 && rl_start.location != kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001340 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001341 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001342 {
1343 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001344 Load32Disp(rs_rX86_SP, displacement, rs_rDI);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001345 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001346 } else {
1347 LoadValueDirectFixed(rl_start, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001348 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001349 OpRegReg(kOpXor, rs_tmp, rs_tmp);
1350 OpRegReg(kOpCmp, rs_rDI, rs_tmp);
1351 OpCondRegReg(kOpCmov, kCondLt, rs_rDI, rs_tmp);
1352
1353 // The length of the string should be greater than the start index.
1354 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rDI, nullptr);
1355
1356 // Copy the number of words to search in a temporary register.
1357 // We will use the register at the end to calculate result.
1358 OpRegReg(kOpMov, rs_tmp, rs_rCX);
1359
1360 // Decrease the number of words to search by the start index.
1361 OpRegReg(kOpSub, rs_rCX, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001362 }
1363 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001364
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001365 // Load the address of the string into EDI.
1366 // In case of start index we have to add the address to existing value in EDI.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001367 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001368 if (zero_based || (!zero_based && rl_start.is_const && start_value == 0)) {
1369 Load32Disp(rs_rDX, offset_offset, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001370 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001371 OpRegMem(kOpAdd, rs_rDI, rs_rDX, offset_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001372 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001373 OpRegImm(kOpLsl, rs_rDI, 1);
1374 OpRegMem(kOpAdd, rs_rDI, rs_rDX, value_offset);
1375 OpRegImm(kOpAdd, rs_rDI, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001376
1377 // EDI now contains the start of the string to be searched.
1378 // We are all prepared to do the search for the character.
1379 NewLIR0(kX86RepneScasw);
1380
1381 // Did we find a match?
1382 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1383
1384 // yes, we matched. Compute the index of the result.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001385 OpRegReg(kOpSub, rs_tmp, rs_rCX);
1386 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_tmp.GetReg(), -1);
1387
Mark Mendell4028a6c2014-02-19 20:06:20 -08001388 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1389
1390 // Failed to match; return -1.
1391 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1392 length_compare->target = not_found;
1393 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001394 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001395
1396 // And join up at the end.
1397 all_done->target = NewLIR0(kPseudoTargetLabel);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001398
1399 if (!cu_->target64)
1400 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001401
1402 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001403 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001404 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001405 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001406 }
1407
1408 StoreValue(rl_dest, rl_return);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001409
1410 FreeTemp(rs_rAX);
1411 FreeTemp(rs_rCX);
1412 FreeTemp(rs_rDX);
1413 FreeTemp(rs_tmp);
1414 if (cu_->target64) {
1415 FreeTemp(rs_rDI);
1416 }
1417
Mark Mendell4028a6c2014-02-19 20:06:20 -08001418 return true;
1419}
1420
Mark Mendellae9fd932014-02-10 16:14:35 -08001421/*
Mark Mendellae9fd932014-02-10 16:14:35 -08001422 * @brief Enter an 'advance LOC' into the FDE buffer
1423 * @param buf FDE buffer.
1424 * @param increment Amount by which to increase the current location.
1425 */
1426static void AdvanceLoc(std::vector<uint8_t>&buf, uint32_t increment) {
1427 if (increment < 64) {
1428 // Encoding in opcode.
1429 buf.push_back(0x1 << 6 | increment);
1430 } else if (increment < 256) {
1431 // Single byte delta.
1432 buf.push_back(0x02);
1433 buf.push_back(increment);
1434 } else if (increment < 256 * 256) {
1435 // Two byte delta.
1436 buf.push_back(0x03);
1437 buf.push_back(increment & 0xff);
1438 buf.push_back((increment >> 8) & 0xff);
1439 } else {
1440 // Four byte delta.
1441 buf.push_back(0x04);
1442 PushWord(buf, increment);
1443 }
1444}
1445
1446
Tong Shen35e1e6a2014-07-30 09:31:22 -07001447std::vector<uint8_t>* X86CFIInitialization(bool is_x86_64) {
1448 return X86Mir2Lir::ReturnCommonCallFrameInformation(is_x86_64);
Mark Mendellae9fd932014-02-10 16:14:35 -08001449}
1450
1451static void EncodeUnsignedLeb128(std::vector<uint8_t>& buf, uint32_t value) {
1452 uint8_t buffer[12];
1453 uint8_t *ptr = EncodeUnsignedLeb128(buffer, value);
1454 for (uint8_t *p = buffer; p < ptr; p++) {
1455 buf.push_back(*p);
1456 }
1457}
1458
Tong Shen35e1e6a2014-07-30 09:31:22 -07001459static void EncodeSignedLeb128(std::vector<uint8_t>& buf, int32_t value) {
1460 uint8_t buffer[12];
1461 uint8_t *ptr = EncodeSignedLeb128(buffer, value);
1462 for (uint8_t *p = buffer; p < ptr; p++) {
1463 buf.push_back(*p);
1464 }
1465}
1466
1467std::vector<uint8_t>* X86Mir2Lir::ReturnCommonCallFrameInformation(bool is_x86_64) {
1468 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
1469
1470 // Length (will be filled in later in this routine).
1471 PushWord(*cfi_info, 0);
1472
1473 // CIE id: always 0.
1474 PushWord(*cfi_info, 0);
1475
1476 // Version: always 1.
1477 cfi_info->push_back(0x01);
1478
1479 // Augmentation: 'zR\0'
1480 cfi_info->push_back(0x7a);
1481 cfi_info->push_back(0x52);
1482 cfi_info->push_back(0x0);
1483
1484 // Code alignment: 1.
1485 EncodeUnsignedLeb128(*cfi_info, 1);
1486
1487 // Data alignment.
1488 if (is_x86_64) {
1489 EncodeSignedLeb128(*cfi_info, -8);
1490 } else {
1491 EncodeSignedLeb128(*cfi_info, -4);
1492 }
1493
1494 // Return address register.
1495 if (is_x86_64) {
1496 // R16(RIP)
1497 cfi_info->push_back(0x10);
1498 } else {
1499 // R8(EIP)
1500 cfi_info->push_back(0x08);
1501 }
1502
1503 // Augmentation length: 1.
1504 cfi_info->push_back(1);
1505
1506 // Augmentation data: 0x03 ((DW_EH_PE_absptr << 4) | DW_EH_PE_udata4).
1507 cfi_info->push_back(0x03);
1508
1509 // Initial instructions.
1510 if (is_x86_64) {
1511 // DW_CFA_def_cfa R7(RSP) 8.
1512 cfi_info->push_back(0x0c);
1513 cfi_info->push_back(0x07);
1514 cfi_info->push_back(0x08);
1515
1516 // DW_CFA_offset R16(RIP) 1 (* -8).
1517 cfi_info->push_back(0x90);
1518 cfi_info->push_back(0x01);
1519 } else {
1520 // DW_CFA_def_cfa R4(ESP) 4.
1521 cfi_info->push_back(0x0c);
1522 cfi_info->push_back(0x04);
1523 cfi_info->push_back(0x04);
1524
1525 // DW_CFA_offset R8(EIP) 1 (* -4).
1526 cfi_info->push_back(0x88);
1527 cfi_info->push_back(0x01);
1528 }
1529
1530 // Padding to a multiple of 4
1531 while ((cfi_info->size() & 3) != 0) {
1532 // DW_CFA_nop is encoded as 0.
1533 cfi_info->push_back(0);
1534 }
1535
1536 // Set the length of the CIE inside the generated bytes.
1537 uint32_t length = cfi_info->size() - 4;
1538 (*cfi_info)[0] = length;
1539 (*cfi_info)[1] = length >> 8;
1540 (*cfi_info)[2] = length >> 16;
1541 (*cfi_info)[3] = length >> 24;
1542 return cfi_info;
1543}
1544
1545static bool ARTRegIDToDWARFRegID(bool is_x86_64, int art_reg_id, int* dwarf_reg_id) {
1546 if (is_x86_64) {
1547 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001548 case 3 : *dwarf_reg_id = 3; return true; // %rbx
Tong Shen35e1e6a2014-07-30 09:31:22 -07001549 // This is the only discrepancy between ART & DWARF register numbering.
Andreas Gampebda27222014-07-30 23:21:36 -07001550 case 5 : *dwarf_reg_id = 6; return true; // %rbp
1551 case 12: *dwarf_reg_id = 12; return true; // %r12
1552 case 13: *dwarf_reg_id = 13; return true; // %r13
1553 case 14: *dwarf_reg_id = 14; return true; // %r14
1554 case 15: *dwarf_reg_id = 15; return true; // %r15
1555 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001556 }
1557 } else {
1558 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001559 case 5: *dwarf_reg_id = 5; return true; // %ebp
1560 case 6: *dwarf_reg_id = 6; return true; // %esi
1561 case 7: *dwarf_reg_id = 7; return true; // %edi
1562 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001563 }
1564 }
1565}
1566
Mark Mendellae9fd932014-02-10 16:14:35 -08001567std::vector<uint8_t>* X86Mir2Lir::ReturnCallFrameInformation() {
1568 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
1569
1570 // Generate the FDE for the method.
1571 DCHECK_NE(data_offset_, 0U);
1572
1573 // Length (will be filled in later in this routine).
1574 PushWord(*cfi_info, 0);
1575
Tong Shen35e1e6a2014-07-30 09:31:22 -07001576 // 'CIE_pointer' (filled in by linker).
Mark Mendellae9fd932014-02-10 16:14:35 -08001577 PushWord(*cfi_info, 0);
1578
1579 // 'initial_location' (filled in by linker).
1580 PushWord(*cfi_info, 0);
1581
1582 // 'address_range' (number of bytes in the method).
1583 PushWord(*cfi_info, data_offset_);
1584
Tong Shen35e1e6a2014-07-30 09:31:22 -07001585 // Augmentation length: 0
1586 cfi_info->push_back(0);
1587
Mark Mendellae9fd932014-02-10 16:14:35 -08001588 // The instructions in the FDE.
1589 if (stack_decrement_ != nullptr) {
1590 // Advance LOC to just past the stack decrement.
1591 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
1592 AdvanceLoc(*cfi_info, pc);
1593
1594 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
1595 cfi_info->push_back(0x0e);
1596 EncodeUnsignedLeb128(*cfi_info, frame_size_);
1597
Tong Shen35e1e6a2014-07-30 09:31:22 -07001598 // Handle register spills
1599 const uint32_t kSpillInstLen = (cu_->target64) ? 5 : 4;
1600 const int kDataAlignmentFactor = (cu_->target64) ? -8 : -4;
1601 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
1602 int offset = -(GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
1603 for (int reg = 0; mask; mask >>= 1, reg++) {
1604 if (mask & 0x1) {
1605 pc += kSpillInstLen;
1606
1607 // Advance LOC to pass this instruction
1608 AdvanceLoc(*cfi_info, kSpillInstLen);
1609
1610 int dwarf_reg_id;
1611 if (ARTRegIDToDWARFRegID(cu_->target64, reg, &dwarf_reg_id)) {
1612 // DW_CFA_offset_extended_sf reg_no offset
1613 cfi_info->push_back(0x11);
1614 EncodeUnsignedLeb128(*cfi_info, dwarf_reg_id);
1615 EncodeSignedLeb128(*cfi_info, offset / kDataAlignmentFactor);
1616 }
1617
1618 offset += GetInstructionSetPointerSize(cu_->instruction_set);
1619 }
1620 }
1621
Mark Mendellae9fd932014-02-10 16:14:35 -08001622 // We continue with that stack until the epilogue.
1623 if (stack_increment_ != nullptr) {
1624 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
1625 AdvanceLoc(*cfi_info, new_pc - pc);
1626
1627 // We probably have code snippets after the epilogue, so save the
1628 // current state: DW_CFA_remember_state.
1629 cfi_info->push_back(0x0a);
1630
Tong Shen35e1e6a2014-07-30 09:31:22 -07001631 // We have now popped the stack: DW_CFA_def_cfa_offset 4/8.
1632 // There is only the return PC on the stack now.
Mark Mendellae9fd932014-02-10 16:14:35 -08001633 cfi_info->push_back(0x0e);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001634 EncodeUnsignedLeb128(*cfi_info, GetInstructionSetPointerSize(cu_->instruction_set));
Mark Mendellae9fd932014-02-10 16:14:35 -08001635
1636 // Everything after that is the same as before the epilogue.
1637 // Stack bump was followed by RET instruction.
1638 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1639 if (post_ret_insn != nullptr) {
1640 pc = new_pc;
1641 new_pc = post_ret_insn->offset;
1642 AdvanceLoc(*cfi_info, new_pc - pc);
1643 // Restore the state: DW_CFA_restore_state.
1644 cfi_info->push_back(0x0b);
1645 }
1646 }
1647 }
1648
1649 // Padding to a multiple of 4
1650 while ((cfi_info->size() & 3) != 0) {
1651 // DW_CFA_nop is encoded as 0.
1652 cfi_info->push_back(0);
1653 }
1654
1655 // Set the length of the FDE inside the generated bytes.
1656 uint32_t length = cfi_info->size() - 4;
1657 (*cfi_info)[0] = length;
1658 (*cfi_info)[1] = length >> 8;
1659 (*cfi_info)[2] = length >> 16;
1660 (*cfi_info)[3] = length >> 24;
1661 return cfi_info;
1662}
1663
Mark Mendelld65c51a2014-04-29 16:55:20 -04001664void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1665 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001666 case kMirOpReserveVectorRegisters:
1667 ReserveVectorRegisters(mir);
1668 break;
1669 case kMirOpReturnVectorRegisters:
1670 ReturnVectorRegisters();
1671 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001672 case kMirOpConstVector:
1673 GenConst128(bb, mir);
1674 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001675 case kMirOpMoveVector:
1676 GenMoveVector(bb, mir);
1677 break;
1678 case kMirOpPackedMultiply:
1679 GenMultiplyVector(bb, mir);
1680 break;
1681 case kMirOpPackedAddition:
1682 GenAddVector(bb, mir);
1683 break;
1684 case kMirOpPackedSubtract:
1685 GenSubtractVector(bb, mir);
1686 break;
1687 case kMirOpPackedShiftLeft:
1688 GenShiftLeftVector(bb, mir);
1689 break;
1690 case kMirOpPackedSignedShiftRight:
1691 GenSignedShiftRightVector(bb, mir);
1692 break;
1693 case kMirOpPackedUnsignedShiftRight:
1694 GenUnsignedShiftRightVector(bb, mir);
1695 break;
1696 case kMirOpPackedAnd:
1697 GenAndVector(bb, mir);
1698 break;
1699 case kMirOpPackedOr:
1700 GenOrVector(bb, mir);
1701 break;
1702 case kMirOpPackedXor:
1703 GenXorVector(bb, mir);
1704 break;
1705 case kMirOpPackedAddReduce:
1706 GenAddReduceVector(bb, mir);
1707 break;
1708 case kMirOpPackedReduce:
1709 GenReduceVector(bb, mir);
1710 break;
1711 case kMirOpPackedSet:
1712 GenSetVector(bb, mir);
1713 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001714 default:
1715 break;
1716 }
1717}
1718
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001719void X86Mir2Lir::ReserveVectorRegisters(MIR* mir) {
1720 // We should not try to reserve twice without returning the registers
1721 DCHECK_NE(num_reserved_vector_regs_, -1);
1722
1723 int num_vector_reg = mir->dalvikInsn.vA;
1724 for (int i = 0; i < num_vector_reg; i++) {
1725 RegStorage xp_reg = RegStorage::Solo128(i);
1726 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1727 Clobber(xp_reg);
1728
1729 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1730 info != nullptr;
1731 info = info->GetAliasChain()) {
1732 if (info->GetReg().IsSingle()) {
1733 reg_pool_->sp_regs_.Delete(info);
1734 } else {
1735 reg_pool_->dp_regs_.Delete(info);
1736 }
1737 }
1738 }
1739
1740 num_reserved_vector_regs_ = num_vector_reg;
1741}
1742
1743void X86Mir2Lir::ReturnVectorRegisters() {
1744 // Return all the reserved registers
1745 for (int i = 0; i < num_reserved_vector_regs_; i++) {
1746 RegStorage xp_reg = RegStorage::Solo128(i);
1747 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1748
1749 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1750 info != nullptr;
1751 info = info->GetAliasChain()) {
1752 if (info->GetReg().IsSingle()) {
1753 reg_pool_->sp_regs_.Insert(info);
1754 } else {
1755 reg_pool_->dp_regs_.Insert(info);
1756 }
1757 }
1758 }
1759
1760 // We don't have anymore reserved vector registers
1761 num_reserved_vector_regs_ = -1;
1762}
1763
Mark Mendelld65c51a2014-04-29 16:55:20 -04001764void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001765 store_method_addr_used_ = true;
1766 int type_size = mir->dalvikInsn.vB;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001767 // We support 128 bit vectors.
1768 DCHECK_EQ(type_size & 0xFFFF, 128);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001769 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001770 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001771 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001772 // Check for all 0 case.
1773 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1774 NewLIR2(kX86XorpsRR, reg, reg);
1775 return;
1776 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001777
1778 // Append the mov const vector to reg opcode.
1779 AppendOpcodeWithConst(kX86MovupsRM, reg, mir);
1780}
1781
1782void X86Mir2Lir::AppendOpcodeWithConst(X86OpCode opcode, int reg, MIR* mir) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04001783 // Okay, load it from the constant vector area.
1784 LIR *data_target = ScanVectorLiteral(mir);
1785 if (data_target == nullptr) {
1786 data_target = AddVectorLiteral(mir);
1787 }
1788
1789 // Address the start of the method.
1790 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001791 if (rl_method.wide) {
1792 rl_method = LoadValueWide(rl_method, kCoreReg);
1793 } else {
1794 rl_method = LoadValue(rl_method, kCoreReg);
1795 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001796
1797 // Load the proper value from the literal area.
1798 // We don't know the proper offset for the value, so pick one that will force
1799 // 4 byte offset. We will fix this up in the assembler later to have the right
1800 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001801 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001802 LIR *load = NewLIR2(opcode, reg, rl_method.reg.GetReg());
Mark Mendelld65c51a2014-04-29 16:55:20 -04001803 load->flags.fixup = kFixupLoad;
1804 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001805}
1806
Mark Mendellfe945782014-05-22 09:52:36 -04001807void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1808 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001809 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1810 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
1811 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001812 NewLIR2(kX86Mova128RR, rs_dest.GetReg(), rs_src.GetReg());
1813}
1814
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001815void X86Mir2Lir::GenMultiplyVectorSignedByte(BasicBlock *bb, MIR *mir) {
1816 const int BYTE_SIZE = 8;
1817 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1818 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
1819 RegStorage rs_src1_high_tmp = Get128BitRegister(AllocTempWide());
1820
1821 /*
1822 * Emulate the behavior of a kSignedByte by separating out the 16 values in the two XMM
1823 * and multiplying 8 at a time before recombining back into one XMM register.
1824 *
1825 * let xmm1, xmm2 be real srcs (keep low bits of 16bit lanes)
1826 * xmm3 is tmp (operate on high bits of 16bit lanes)
1827 *
1828 * xmm3 = xmm1
1829 * xmm1 = xmm1 .* xmm2
1830 * xmm1 = xmm1 & 0x00ff00ff00ff00ff00ff00ff00ff00ff // xmm1 now has low bits
1831 * xmm3 = xmm3 .>> 8
1832 * xmm2 = xmm2 & 0xff00ff00ff00ff00ff00ff00ff00ff00
1833 * xmm2 = xmm2 .* xmm3 // xmm2 now has high bits
1834 * xmm1 = xmm1 | xmm2 // combine results
1835 */
1836
1837 // Copy xmm1.
1838 NewLIR2(kX86Mova128RR, rs_src1_high_tmp.GetReg(), rs_dest_src1.GetReg());
1839
1840 // Multiply low bits.
1841 NewLIR2(kX86PmullwRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1842
1843 // xmm1 now has low bits.
1844 AndMaskVectorRegister(rs_dest_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
1845
1846 // Prepare high bits for multiplication.
1847 NewLIR2(kX86PsrlwRI, rs_src1_high_tmp.GetReg(), BYTE_SIZE);
1848 AndMaskVectorRegister(rs_src2, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1849
1850 // Multiply high bits and xmm2 now has high bits.
1851 NewLIR2(kX86PmullwRR, rs_src2.GetReg(), rs_src1_high_tmp.GetReg());
1852
1853 // Combine back into dest XMM register.
1854 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1855}
1856
Mark Mendellfe945782014-05-22 09:52:36 -04001857void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001858 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1859 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1860 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1861 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001862 int opcode = 0;
1863 switch (opsize) {
1864 case k32:
1865 opcode = kX86PmulldRR;
1866 break;
1867 case kSignedHalf:
1868 opcode = kX86PmullwRR;
1869 break;
1870 case kSingle:
1871 opcode = kX86MulpsRR;
1872 break;
1873 case kDouble:
1874 opcode = kX86MulpdRR;
1875 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001876 case kSignedByte:
1877 // HW doesn't support 16x16 byte multiplication so emulate it.
1878 GenMultiplyVectorSignedByte(bb, mir);
1879 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001880 default:
1881 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1882 break;
1883 }
1884 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1885}
1886
1887void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001888 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1889 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1890 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1891 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001892 int opcode = 0;
1893 switch (opsize) {
1894 case k32:
1895 opcode = kX86PadddRR;
1896 break;
1897 case kSignedHalf:
1898 case kUnsignedHalf:
1899 opcode = kX86PaddwRR;
1900 break;
1901 case kUnsignedByte:
1902 case kSignedByte:
1903 opcode = kX86PaddbRR;
1904 break;
1905 case kSingle:
1906 opcode = kX86AddpsRR;
1907 break;
1908 case kDouble:
1909 opcode = kX86AddpdRR;
1910 break;
1911 default:
1912 LOG(FATAL) << "Unsupported vector addition " << opsize;
1913 break;
1914 }
1915 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1916}
1917
1918void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001919 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1920 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1921 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1922 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001923 int opcode = 0;
1924 switch (opsize) {
1925 case k32:
1926 opcode = kX86PsubdRR;
1927 break;
1928 case kSignedHalf:
1929 case kUnsignedHalf:
1930 opcode = kX86PsubwRR;
1931 break;
1932 case kUnsignedByte:
1933 case kSignedByte:
1934 opcode = kX86PsubbRR;
1935 break;
1936 case kSingle:
1937 opcode = kX86SubpsRR;
1938 break;
1939 case kDouble:
1940 opcode = kX86SubpdRR;
1941 break;
1942 default:
1943 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1944 break;
1945 }
1946 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1947}
1948
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001949void X86Mir2Lir::GenShiftByteVector(BasicBlock *bb, MIR *mir) {
1950 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
1951 RegStorage rs_tmp = Get128BitRegister(AllocTempWide());
1952
1953 int opcode = 0;
1954 int imm = mir->dalvikInsn.vB;
1955
1956 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1957 case kMirOpPackedShiftLeft:
1958 opcode = kX86PsllwRI;
1959 break;
1960 case kMirOpPackedSignedShiftRight:
1961 opcode = kX86PsrawRI;
1962 break;
1963 case kMirOpPackedUnsignedShiftRight:
1964 opcode = kX86PsrlwRI;
1965 break;
1966 default:
1967 LOG(FATAL) << "Unsupported shift operation on byte vector " << opcode;
1968 break;
1969 }
1970
1971 /*
1972 * xmm1 will have low bits
1973 * xmm2 will have high bits
1974 *
1975 * xmm2 = xmm1
1976 * xmm1 = xmm1 .<< N
1977 * xmm2 = xmm2 && 0xFF00FF00FF00FF00FF00FF00FF00FF00
1978 * xmm2 = xmm2 .<< N
1979 * xmm1 = xmm1 | xmm2
1980 */
1981
1982 // Copy xmm1.
1983 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_dest_src1.GetReg());
1984
1985 // Shift lower values.
1986 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1987
1988 // Mask bottom bits.
1989 AndMaskVectorRegister(rs_tmp, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
1990
1991 // Shift higher values.
1992 NewLIR2(opcode, rs_tmp.GetReg(), imm);
1993
1994 // Combine back into dest XMM register.
1995 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_tmp.GetReg());
1996}
1997
Mark Mendellfe945782014-05-22 09:52:36 -04001998void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001999 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2000 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2001 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
2002 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04002003 int opcode = 0;
2004 switch (opsize) {
2005 case k32:
2006 opcode = kX86PslldRI;
2007 break;
2008 case k64:
2009 opcode = kX86PsllqRI;
2010 break;
2011 case kSignedHalf:
2012 case kUnsignedHalf:
2013 opcode = kX86PsllwRI;
2014 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002015 case kSignedByte:
2016 case kUnsignedByte:
2017 GenShiftByteVector(bb, mir);
2018 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002019 default:
2020 LOG(FATAL) << "Unsupported vector shift left " << opsize;
2021 break;
2022 }
2023 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
2024}
2025
2026void X86Mir2Lir::GenSignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002027 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2028 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2029 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
2030 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04002031 int opcode = 0;
2032 switch (opsize) {
2033 case k32:
2034 opcode = kX86PsradRI;
2035 break;
2036 case kSignedHalf:
2037 case kUnsignedHalf:
2038 opcode = kX86PsrawRI;
2039 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002040 case kSignedByte:
2041 case kUnsignedByte:
2042 GenShiftByteVector(bb, mir);
2043 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002044 default:
2045 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
2046 break;
2047 }
2048 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
2049}
2050
2051void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002052 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2053 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2054 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
2055 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04002056 int opcode = 0;
2057 switch (opsize) {
2058 case k32:
2059 opcode = kX86PsrldRI;
2060 break;
2061 case k64:
2062 opcode = kX86PsrlqRI;
2063 break;
2064 case kSignedHalf:
2065 case kUnsignedHalf:
2066 opcode = kX86PsrlwRI;
2067 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002068 case kSignedByte:
2069 case kUnsignedByte:
2070 GenShiftByteVector(bb, mir);
2071 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002072 default:
2073 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
2074 break;
2075 }
2076 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
2077}
2078
2079void X86Mir2Lir::GenAndVector(BasicBlock *bb, MIR *mir) {
2080 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002081 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2082 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
2083 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04002084 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
2085}
2086
2087void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
2088 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002089 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2090 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
2091 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04002092 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
2093}
2094
2095void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
2096 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002097 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2098 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
2099 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04002100 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
2101}
2102
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002103void X86Mir2Lir::AndMaskVectorRegister(RegStorage rs_src1, uint32_t m1, uint32_t m2, uint32_t m3, uint32_t m4) {
2104 MaskVectorRegister(kX86PandRM, rs_src1, m1, m2, m3, m4);
2105}
2106
2107void X86Mir2Lir::MaskVectorRegister(X86OpCode opcode, RegStorage rs_src1, uint32_t m0, uint32_t m1, uint32_t m2, uint32_t m3) {
2108 // Create temporary MIR as container for 128-bit binary mask.
2109 MIR const_mir;
2110 MIR* const_mirp = &const_mir;
2111 const_mirp->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpConstVector);
2112 const_mirp->dalvikInsn.arg[0] = m0;
2113 const_mirp->dalvikInsn.arg[1] = m1;
2114 const_mirp->dalvikInsn.arg[2] = m2;
2115 const_mirp->dalvikInsn.arg[3] = m3;
2116
2117 // Mask vector with const from literal pool.
2118 AppendOpcodeWithConst(opcode, rs_src1.GetReg(), const_mirp);
2119}
2120
Mark Mendellfe945782014-05-22 09:52:36 -04002121void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002122 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2123 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
2124 RegLocation rl_dest = mir_graph_->GetDest(mir);
2125 RegStorage rs_tmp;
2126
2127 int vec_bytes = (mir->dalvikInsn.vC & 0xFFFF) / 8;
2128 int vec_unit_size = 0;
Mark Mendellfe945782014-05-22 09:52:36 -04002129 int opcode = 0;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002130 int extr_opcode = 0;
2131 RegLocation rl_result;
2132
Mark Mendellfe945782014-05-22 09:52:36 -04002133 switch (opsize) {
2134 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002135 extr_opcode = kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002136 opcode = kX86PhadddRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002137 vec_unit_size = 4;
2138 break;
2139 case kSignedByte:
2140 case kUnsignedByte:
2141 extr_opcode = kX86PextrbRRI;
2142 opcode = kX86PhaddwRR;
2143 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04002144 break;
2145 case kSignedHalf:
2146 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002147 extr_opcode = kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002148 opcode = kX86PhaddwRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002149 vec_unit_size = 2;
Mark Mendellfe945782014-05-22 09:52:36 -04002150 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002151 case kSingle:
2152 rl_result = EvalLoc(rl_dest, kFPReg, true);
2153 vec_unit_size = 4;
2154 for (int i = 0; i < 3; i++) {
2155 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
2156 NewLIR3(kX86ShufpsRRI, rs_src1.GetReg(), rs_src1.GetReg(), 0x39);
2157 }
2158 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), rs_src1.GetReg());
2159 StoreValue(rl_dest, rl_result);
2160
2161 // For single-precision floats, we are done here
2162 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002163 default:
2164 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2165 break;
2166 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002167
2168 int elems = vec_bytes / vec_unit_size;
2169
2170 // Emulate horizontal add instruction by reducing 2 vectors with 8 values before adding them again
2171 // TODO is overflow handled correctly?
2172 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2173 rs_tmp = Get128BitRegister(AllocTempWide());
2174
2175 // tmp = xmm1 .>> 8.
2176 NewLIR2(kX86Mova128RR, rs_tmp.GetReg(), rs_src1.GetReg());
2177 NewLIR2(kX86PsrlwRI, rs_tmp.GetReg(), 8);
2178
2179 // Zero extend low bits in xmm1.
2180 AndMaskVectorRegister(rs_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
2181 }
2182
2183 while (elems > 1) {
2184 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2185 NewLIR2(opcode, rs_tmp.GetReg(), rs_tmp.GetReg());
2186 }
2187 NewLIR2(opcode, rs_src1.GetReg(), rs_src1.GetReg());
2188 elems >>= 1;
2189 }
2190
2191 // Combine the results if we separated them.
2192 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2193 NewLIR2(kX86PaddbRR, rs_src1.GetReg(), rs_tmp.GetReg());
2194 }
2195
2196 // We need to extract to a GPR.
2197 RegStorage temp = AllocTemp();
2198 NewLIR3(extr_opcode, temp.GetReg(), rs_src1.GetReg(), 0);
2199
2200 // Can we do this directly into memory?
2201 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2202 if (rl_result.location == kLocPhysReg) {
2203 // Ensure res is in a core reg
2204 rl_result = EvalLoc(rl_dest, kCoreReg, true);
2205 OpRegReg(kOpAdd, rl_result.reg, temp);
2206 StoreFinalValue(rl_dest, rl_result);
2207 } else {
2208 OpMemReg(kOpAdd, rl_result, temp.GetReg());
2209 }
2210
2211 FreeTemp(temp);
Mark Mendellfe945782014-05-22 09:52:36 -04002212}
2213
2214void X86Mir2Lir::GenReduceVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002215 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2216 RegLocation rl_dest = mir_graph_->GetDest(mir);
2217 RegStorage rs_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
2218 int extract_index = mir->dalvikInsn.arg[0];
2219 int extr_opcode = 0;
2220 RegLocation rl_result;
2221 bool is_wide = false;
2222
Mark Mendellfe945782014-05-22 09:52:36 -04002223 switch (opsize) {
2224 case k32:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002225 rl_result = UpdateLocTyped(rl_dest, kCoreReg);
2226 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrdMRI : kX86PextrdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002227 break;
2228 case kSignedHalf:
2229 case kUnsignedHalf:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002230 rl_result= UpdateLocTyped(rl_dest, kCoreReg);
2231 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrwMRI : kX86PextrwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002232 break;
2233 default:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002234 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2235 return;
Mark Mendellfe945782014-05-22 09:52:36 -04002236 break;
2237 }
Mark Mendellfe945782014-05-22 09:52:36 -04002238
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002239 if (rl_result.location == kLocPhysReg) {
2240 NewLIR3(extr_opcode, rl_result.reg.GetReg(), rs_src1.GetReg(), extract_index);
2241 if (is_wide == true) {
2242 StoreFinalValue(rl_dest, rl_result);
2243 } else {
2244 StoreFinalValueWide(rl_dest, rl_result);
2245 }
2246 } else {
2247 int displacement = SRegOffset(rl_result.s_reg_low);
2248 LIR *l = NewLIR3(extr_opcode, rs_rX86_SP.GetReg(), displacement, rs_src1.GetReg());
2249 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is_wide /* is_64bit */);
2250 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is_wide /* is_64bit */);
2251 }
Mark Mendellfe945782014-05-22 09:52:36 -04002252}
2253
2254void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002255 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2256 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2257 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
2258 int op_low = 0, op_high = 0, imm = 0, op_mov = kX86MovdxrRR;
2259 RegisterClass reg_type = kCoreReg;
2260
Mark Mendellfe945782014-05-22 09:52:36 -04002261 switch (opsize) {
2262 case k32:
2263 op_low = kX86PshufdRRI;
2264 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002265 case kSingle:
2266 op_low = kX86PshufdRRI;
2267 op_mov = kX86Mova128RR;
2268 reg_type = kFPReg;
2269 break;
2270 case k64:
2271 op_low = kX86PshufdRRI;
2272 imm = 0x44;
2273 break;
2274 case kDouble:
2275 op_low = kX86PshufdRRI;
2276 op_mov = kX86Mova128RR;
2277 reg_type = kFPReg;
2278 imm = 0x44;
2279 break;
2280 case kSignedByte:
2281 case kUnsignedByte:
2282 // Shuffle 8 bit value into 16 bit word.
2283 // We set val = val + (val << 8) below and use 16 bit shuffle.
Mark Mendellfe945782014-05-22 09:52:36 -04002284 case kSignedHalf:
2285 case kUnsignedHalf:
2286 // Handles low quadword.
2287 op_low = kX86PshuflwRRI;
2288 // Handles upper quadword.
2289 op_high = kX86PshufdRRI;
2290 break;
2291 default:
2292 LOG(FATAL) << "Unsupported vector set " << opsize;
2293 break;
2294 }
2295
Mark Mendellfe945782014-05-22 09:52:36 -04002296 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002297
2298 // Load the value from the VR into the reg.
2299 if (rl_src.wide == 0) {
2300 rl_src = LoadValue(rl_src, reg_type);
2301 } else {
2302 rl_src = LoadValueWide(rl_src, reg_type);
2303 }
2304
2305 // If opsize is 8 bits wide then double value and use 16 bit shuffle instead.
2306 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2307 RegStorage temp = AllocTemp();
2308 // val = val + (val << 8).
2309 NewLIR2(kX86Mov32RR, temp.GetReg(), rl_src.reg.GetReg());
2310 NewLIR2(kX86Sal32RI, temp.GetReg(), 8);
2311 NewLIR2(kX86Or32RR, rl_src.reg.GetReg(), temp.GetReg());
2312 FreeTemp(temp);
2313 }
Mark Mendellfe945782014-05-22 09:52:36 -04002314
2315 // Load the value into the XMM register.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002316 NewLIR2(op_mov, rs_dest.GetReg(), rl_src.reg.GetReg());
Mark Mendellfe945782014-05-22 09:52:36 -04002317
2318 // Now shuffle the value across the destination.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002319 NewLIR3(op_low, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002320
2321 // And then repeat as needed.
2322 if (op_high != 0) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002323 NewLIR3(op_high, rs_dest.GetReg(), rs_dest.GetReg(), imm);
Mark Mendellfe945782014-05-22 09:52:36 -04002324 }
2325}
2326
Mark Mendelld65c51a2014-04-29 16:55:20 -04002327LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
2328 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2329 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
2330 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
2331 args[2] == p->operands[2] && args[3] == p->operands[3]) {
2332 return p;
2333 }
2334 }
2335 return nullptr;
2336}
2337
2338LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
2339 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
2340 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
2341 new_value->operands[0] = args[0];
2342 new_value->operands[1] = args[1];
2343 new_value->operands[2] = args[2];
2344 new_value->operands[3] = args[3];
2345 new_value->next = const_vectors_;
2346 if (const_vectors_ == nullptr) {
2347 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
2348 }
2349 estimated_native_code_size_ += 16; // Space for one vector.
2350 const_vectors_ = new_value;
2351 return new_value;
2352}
2353
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002354// ------------ ABI support: mapping of args to physical registers -------------
Andreas Gampeccc60262014-07-04 18:02:38 -07002355RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide,
2356 bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002357 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
Andreas Gampeccc60262014-07-04 18:02:38 -07002358 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) /
2359 sizeof(SpecialTargetRegister);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002360 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
Andreas Gampeccc60262014-07-04 18:02:38 -07002361 kFArg4, kFArg5, kFArg6, kFArg7};
2362 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) /
2363 sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002364
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002365 if (is_double_or_float) {
2366 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002367 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide ? kWide : kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002368 }
2369 } else {
2370 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002371 return ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++],
2372 is_ref ? kRef : (is_wide ? kWide : kNotWide));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002373 }
2374 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07002375 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002376}
2377
2378RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
2379 DCHECK(IsInitialized());
2380 auto res = mapping_.find(in_position);
2381 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
2382}
2383
Andreas Gampeccc60262014-07-04 18:02:38 -07002384void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count,
2385 InToRegStorageMapper* mapper) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002386 DCHECK(mapper != nullptr);
2387 max_mapped_in_ = -1;
2388 is_there_stack_mapped_ = false;
2389 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07002390 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
2391 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002392 if (reg.Valid()) {
2393 mapping_[in_position] = reg;
2394 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07002395 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002396 // We covered 2 args, so skip the next one
2397 in_position++;
2398 }
2399 } else {
2400 is_there_stack_mapped_ = true;
2401 }
2402 }
2403 initialized_ = true;
2404}
2405
2406RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002407 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002408 return GetCoreArgMappingToPhysicalReg(arg_num);
2409 }
2410
2411 if (!in_to_reg_storage_mapping_.IsInitialized()) {
2412 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2413 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
2414
Chao-ying Fua77ee512014-07-01 17:43:41 -07002415 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002416 in_to_reg_storage_mapping_.Initialize(arg_locs, cu_->num_ins, &mapper);
2417 }
2418 return in_to_reg_storage_mapping_.Get(arg_num);
2419}
2420
2421RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
2422 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
2423 // Not used for 64-bit, TODO: Move X86_32 to the same framework
2424 switch (core_arg_num) {
2425 case 0:
2426 return rs_rX86_ARG1;
2427 case 1:
2428 return rs_rX86_ARG2;
2429 case 2:
2430 return rs_rX86_ARG3;
2431 default:
2432 return RegStorage::InvalidReg();
2433 }
2434}
2435
2436// ---------End of ABI support: mapping of args to physical registers -------------
2437
2438/*
2439 * If there are any ins passed in registers that have not been promoted
2440 * to a callee-save register, flush them to the frame. Perform initial
2441 * assignment of promoted arguments.
2442 *
2443 * ArgLocs is an array of location records describing the incoming arguments
2444 * with one location record per word of argument.
2445 */
2446void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002447 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002448 /*
2449 * Dummy up a RegLocation for the incoming Method*
2450 * It will attempt to keep kArg0 live (or copy it to home location
2451 * if promoted).
2452 */
2453
2454 RegLocation rl_src = rl_method;
2455 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -07002456 rl_src.reg = TargetReg(kArg0, kRef);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002457 rl_src.home = false;
2458 MarkLive(rl_src);
2459 StoreValue(rl_method, rl_src);
2460 // If Method* has been promoted, explicitly flush
2461 if (rl_method.location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002462 StoreRefDisp(rs_rX86_SP, 0, As32BitReg(TargetReg(kArg0, kRef)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002463 }
2464
2465 if (cu_->num_ins == 0) {
2466 return;
2467 }
2468
2469 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
2470 /*
2471 * Copy incoming arguments to their proper home locations.
2472 * NOTE: an older version of dx had an issue in which
2473 * it would reuse static method argument registers.
2474 * This could result in the same Dalvik virtual register
2475 * being promoted to both core and fp regs. To account for this,
2476 * we only copy to the corresponding promoted physical register
2477 * if it matches the type of the SSA name for the incoming
2478 * argument. It is also possible that long and double arguments
2479 * end up half-promoted. In those cases, we must flush the promoted
2480 * half to memory as well.
2481 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002482 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002483 for (int i = 0; i < cu_->num_ins; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002484 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002485 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002486
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002487 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002488 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002489 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002490
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002491 // We have already updated the arg location with promoted info
2492 // so we can be based on it.
2493 if (t_loc->location == kLocPhysReg) {
2494 // Just copy it.
2495 OpRegCopy(t_loc->reg, reg);
2496 } else {
2497 // Needs flush.
2498 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002499 StoreRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002500 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002501 StoreBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002502 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002503 }
2504 }
2505 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002506 // If arriving in frame & promoted.
2507 if (t_loc->location == kLocPhysReg) {
2508 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002509 LoadRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002510 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002511 LoadBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002512 t_loc->wide ? k64 : k32, kNotVolatile);
2513 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002514 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002515 }
2516 if (t_loc->wide) {
2517 // Increment i to skip the next one.
2518 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002519 }
2520 }
2521}
2522
2523/*
2524 * Load up to 5 arguments, the first three of which will be in
2525 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2526 * and as part of the load sequence, it must be replaced with
2527 * the target method pointer. Note, this may also be called
2528 * for "range" variants if the number of arguments is 5 or fewer.
2529 */
2530int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2531 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2532 const MethodReference& target_method,
2533 uint32_t vtable_idx, uintptr_t direct_code,
2534 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002535 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002536 return Mir2Lir::GenDalvikArgsNoRange(info,
2537 call_state, pcrLabel, next_call_insn,
2538 target_method,
2539 vtable_idx, direct_code,
2540 direct_method, type, skip_this);
2541 }
2542 return GenDalvikArgsRange(info,
2543 call_state, pcrLabel, next_call_insn,
2544 target_method,
2545 vtable_idx, direct_code,
2546 direct_method, type, skip_this);
2547}
2548
2549/*
2550 * May have 0+ arguments (also used for jumbo). Note that
2551 * source virtual registers may be in physical registers, so may
2552 * need to be flushed to home location before copying. This
2553 * applies to arg3 and above (see below).
2554 *
2555 * Two general strategies:
2556 * If < 20 arguments
2557 * Pass args 3-18 using vldm/vstm block copy
2558 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2559 * If 20+ arguments
2560 * Pass args arg19+ using memcpy block copy
2561 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2562 *
2563 */
2564int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2565 LIR** pcrLabel, NextCallInsn next_call_insn,
2566 const MethodReference& target_method,
2567 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2568 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002569 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002570 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2571 pcrLabel, next_call_insn,
2572 target_method,
2573 vtable_idx, direct_code, direct_method,
2574 type, skip_this);
2575 }
2576
2577 /* If no arguments, just return */
2578 if (info->num_arg_words == 0)
2579 return call_state;
2580
2581 const int start_index = skip_this ? 1 : 0;
2582
Chao-ying Fua77ee512014-07-01 17:43:41 -07002583 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002584 InToRegStorageMapping in_to_reg_storage_mapping;
2585 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2586 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2587 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
Serguei Katkov8e3acdd2014-07-15 12:01:00 +07002588 info->args[last_mapped_in].wide ? 2 : 1;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002589 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2590
2591 // Fisrt of all, check whether it make sense to use bulk copying
2592 // Optimization is aplicable only for range case
2593 // TODO: make a constant instead of 2
2594 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2595 // Scan the rest of the args - if in phys_reg flush to memory
2596 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2597 RegLocation loc = info->args[next_arg];
2598 if (loc.wide) {
2599 loc = UpdateLocWide(loc);
2600 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002601 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002602 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002603 }
2604 next_arg += 2;
2605 } else {
2606 loc = UpdateLoc(loc);
2607 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002608 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002609 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002610 }
2611 next_arg++;
2612 }
2613 }
2614
2615 // Logic below assumes that Method pointer is at offset zero from SP.
2616 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
2617
2618 // The rest can be copied together
2619 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
Andreas Gampeccc60262014-07-04 18:02:38 -07002620 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped,
2621 cu_->instruction_set);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002622
2623 int current_src_offset = start_offset;
2624 int current_dest_offset = outs_offset;
2625
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002626 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2627 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002628 while (regs_left_to_pass_via_stack > 0) {
2629 // This is based on the knowledge that the stack itself is 16-byte aligned.
2630 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2631 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2632 size_t bytes_to_move;
2633
2634 /*
2635 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2636 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2637 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2638 * We do this because we could potentially do a smaller move to align.
2639 */
2640 if (regs_left_to_pass_via_stack == 4 ||
2641 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2642 // Moving 128-bits via xmm register.
2643 bytes_to_move = sizeof(uint32_t) * 4;
2644
2645 // Allocate a free xmm temp. Since we are working through the calling sequence,
2646 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2647 // there are no free registers.
2648 RegStorage temp = AllocTempDouble();
2649
2650 LIR* ld1 = nullptr;
2651 LIR* ld2 = nullptr;
2652 LIR* st1 = nullptr;
2653 LIR* st2 = nullptr;
2654
2655 /*
2656 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2657 * do an aligned move. If we have 8-byte alignment, then do the move in two
2658 * parts. This approach prevents possible cache line splits. Finally, fall back
2659 * to doing an unaligned move. In most cases we likely won't split the cache
2660 * line but we cannot prove it and thus take a conservative approach.
2661 */
2662 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2663 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2664
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002665 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002666 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002667 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002668 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002669 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovLo128FP);
2670 ld2 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002671 kMovHi128FP);
2672 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002673 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002674 }
2675
2676 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002677 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002678 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002679 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovLo128FP);
2680 st2 = OpMovMemReg(rs_rX86_SP, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002681 temp, kMovHi128FP);
2682 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002683 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002684 }
2685
2686 // TODO If we could keep track of aliasing information for memory accesses that are wider
2687 // than 64-bit, we wouldn't need to set up a barrier.
2688 if (ld1 != nullptr) {
2689 if (ld2 != nullptr) {
2690 // For 64-bit load we can actually set up the aliasing information.
2691 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2692 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2693 } else {
2694 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002695 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002696 }
2697 }
2698 if (st1 != nullptr) {
2699 if (st2 != nullptr) {
2700 // For 64-bit store we can actually set up the aliasing information.
2701 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2702 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2703 } else {
2704 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002705 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002706 }
2707 }
2708
2709 // Free the temporary used for the data movement.
2710 FreeTemp(temp);
2711 } else {
2712 // Moving 32-bits via general purpose register.
2713 bytes_to_move = sizeof(uint32_t);
2714
2715 // Instead of allocating a new temp, simply reuse one of the registers being used
2716 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07002717 RegStorage temp = TargetReg(kArg3, kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002718
2719 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002720 Load32Disp(rs_rX86_SP, current_src_offset, temp);
2721 Store32Disp(rs_rX86_SP, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002722 }
2723
2724 current_src_offset += bytes_to_move;
2725 current_dest_offset += bytes_to_move;
2726 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2727 }
2728 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2729 }
2730
2731 // Now handle rest not registers if they are
2732 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002733 RegStorage regSingle = TargetReg(kArg2, kNotWide);
2734 RegStorage regWide = TargetReg(kArg3, kWide);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002735 for (int i = start_index;
2736 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002737 RegLocation rl_arg = info->args[i];
2738 rl_arg = UpdateRawLoc(rl_arg);
2739 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2740 if (!reg.Valid()) {
2741 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2742
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002743 {
2744 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2745 if (rl_arg.wide) {
2746 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002747 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002748 } else {
2749 LoadValueDirectWideFixed(rl_arg, regWide);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002750 StoreBaseDisp(rs_rX86_SP, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002751 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002752 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002753 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002754 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002755 } else {
2756 LoadValueDirectFixed(rl_arg, regSingle);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002757 StoreBaseDisp(rs_rX86_SP, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002758 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002759 }
2760 }
2761 call_state = next_call_insn(cu_, info, call_state, target_method,
2762 vtable_idx, direct_code, direct_method, type);
2763 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002764 if (rl_arg.wide) {
2765 i++;
2766 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002767 }
2768 }
2769
2770 // Finish with mapped registers
2771 for (int i = start_index; i <= last_mapped_in; i++) {
2772 RegLocation rl_arg = info->args[i];
2773 rl_arg = UpdateRawLoc(rl_arg);
2774 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2775 if (reg.Valid()) {
2776 if (rl_arg.wide) {
2777 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002778 } else {
2779 LoadValueDirectFixed(rl_arg, reg);
2780 }
2781 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2782 direct_code, direct_method, type);
2783 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002784 if (rl_arg.wide) {
2785 i++;
2786 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002787 }
2788
2789 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2790 direct_code, direct_method, type);
2791 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00002792 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002793 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002794 } else {
2795 *pcrLabel = nullptr;
2796 // In lieu of generating a check for kArg1 being null, we need to
2797 // perform a load when doing implicit checks.
2798 RegStorage tmp = AllocTemp();
Andreas Gampeccc60262014-07-04 18:02:38 -07002799 Load32Disp(TargetReg(kArg1, kRef), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002800 MarkPossibleNullPointerException(info->opt_flags);
2801 FreeTemp(tmp);
2802 }
2803 }
2804 return call_state;
2805}
2806
Andreas Gampe98430592014-07-27 19:44:50 -07002807bool X86Mir2Lir::GenInlinedCharAt(CallInfo* info) {
2808 // Location of reference to data array
2809 int value_offset = mirror::String::ValueOffset().Int32Value();
2810 // Location of count
2811 int count_offset = mirror::String::CountOffset().Int32Value();
2812 // Starting offset within data array
2813 int offset_offset = mirror::String::OffsetOffset().Int32Value();
2814 // Start of char data with array_
2815 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
2816
2817 RegLocation rl_obj = info->args[0];
2818 RegLocation rl_idx = info->args[1];
2819 rl_obj = LoadValue(rl_obj, kRefReg);
2820 // X86 wants to avoid putting a constant index into a register.
2821 if (!rl_idx.is_const) {
2822 rl_idx = LoadValue(rl_idx, kCoreReg);
2823 }
2824 RegStorage reg_max;
2825 GenNullCheck(rl_obj.reg, info->opt_flags);
2826 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
2827 LIR* range_check_branch = nullptr;
2828 RegStorage reg_off;
2829 RegStorage reg_ptr;
2830 if (range_check) {
2831 // On x86, we can compare to memory directly
2832 // Set up a launch pad to allow retry in case of bounds violation */
2833 if (rl_idx.is_const) {
2834 LIR* comparison;
2835 range_check_branch = OpCmpMemImmBranch(
2836 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
2837 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr, &comparison);
2838 MarkPossibleNullPointerExceptionAfter(0, comparison);
2839 } else {
2840 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
2841 MarkPossibleNullPointerException(0);
2842 range_check_branch = OpCondBranch(kCondUge, nullptr);
2843 }
2844 }
2845 reg_off = AllocTemp();
2846 reg_ptr = AllocTempRef();
2847 Load32Disp(rl_obj.reg, offset_offset, reg_off);
2848 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
2849 if (rl_idx.is_const) {
2850 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
2851 } else {
2852 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
2853 }
2854 FreeTemp(rl_obj.reg);
2855 if (rl_idx.location == kLocPhysReg) {
2856 FreeTemp(rl_idx.reg);
2857 }
2858 RegLocation rl_dest = InlineTarget(info);
2859 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
2860 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
2861 FreeTemp(reg_off);
2862 FreeTemp(reg_ptr);
2863 StoreValue(rl_dest, rl_result);
2864 if (range_check) {
2865 DCHECK(range_check_branch != nullptr);
2866 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
2867 AddIntrinsicSlowPath(info, range_check_branch);
2868 }
2869 return true;
2870}
2871
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002872bool X86Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
2873 RegLocation rl_dest = InlineTarget(info);
2874
2875 // Early exit if the result is unused.
2876 if (rl_dest.orig_sreg < 0) {
2877 return true;
2878 }
2879
2880 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
2881
2882 if (cu_->target64) {
2883 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<8>());
2884 } else {
2885 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<4>());
2886 }
2887
2888 StoreValue(rl_dest, rl_result);
2889 return true;
2890}
2891
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002892} // namespace art