blob: 2731343b2baed92f0899d908644baa94bc4b0110 [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};
Vladimir Marko089142c2014-06-05 10:57:05 +010055static constexpr RegStorage reserved_regs_arr_32[] = {rs_rX86_SP_32};
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070056static constexpr RegStorage reserved_regs_arr_64[] = {rs_rX86_SP_32};
Vladimir Marko089142c2014-06-05 10:57:05 +010057static constexpr RegStorage reserved_regs_arr_64q[] = {rs_rX86_SP_64};
58static constexpr RegStorage core_temps_arr_32[] = {rs_rAX, rs_rCX, rs_rDX, rs_rBX};
59static constexpr RegStorage core_temps_arr_64[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070060 rs_rAX, rs_rCX, rs_rDX, rs_rSI, rs_rDI,
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070061 rs_r8, rs_r9, rs_r10, rs_r11
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070062};
Vladimir Marko089142c2014-06-05 10:57:05 +010063static constexpr RegStorage core_temps_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070064 rs_r0q, rs_r1q, rs_r2q, rs_r6q, rs_r7q,
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070065 rs_r8q, rs_r9q, rs_r10q, rs_r11q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070066};
Vladimir Marko089142c2014-06-05 10:57:05 +010067static constexpr RegStorage sp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070068 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
69};
Vladimir Marko089142c2014-06-05 10:57:05 +010070static constexpr RegStorage sp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070071 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
buzbee091cc402014-03-31 10:14:40 -070072 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
buzbee091cc402014-03-31 10:14:40 -070073};
Vladimir Marko089142c2014-06-05 10:57:05 +010074static constexpr RegStorage dp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070075 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
76};
Vladimir Marko089142c2014-06-05 10:57:05 +010077static constexpr RegStorage dp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070078 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
buzbee091cc402014-03-31 10:14:40 -070079 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
buzbee091cc402014-03-31 10:14:40 -070080};
81
Vladimir Marko089142c2014-06-05 10:57:05 +010082static constexpr RegStorage xp_temps_arr_32[] = {
Mark Mendellfe945782014-05-22 09:52:36 -040083 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
84};
Vladimir Marko089142c2014-06-05 10:57:05 +010085static constexpr RegStorage xp_temps_arr_64[] = {
Mark Mendellfe945782014-05-22 09:52:36 -040086 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
Mark Mendellfe945782014-05-22 09:52:36 -040087 rs_xr8, rs_xr9, rs_xr10, rs_xr11, rs_xr12, rs_xr13, rs_xr14, rs_xr15
Mark Mendellfe945782014-05-22 09:52:36 -040088};
89
Vladimir Marko089142c2014-06-05 10:57:05 +010090static constexpr ArrayRef<const RegStorage> empty_pool;
91static constexpr ArrayRef<const RegStorage> core_regs_32(core_regs_arr_32);
92static constexpr ArrayRef<const RegStorage> core_regs_64(core_regs_arr_64);
93static constexpr ArrayRef<const RegStorage> core_regs_64q(core_regs_arr_64q);
94static constexpr ArrayRef<const RegStorage> sp_regs_32(sp_regs_arr_32);
95static constexpr ArrayRef<const RegStorage> sp_regs_64(sp_regs_arr_64);
96static constexpr ArrayRef<const RegStorage> dp_regs_32(dp_regs_arr_32);
97static constexpr ArrayRef<const RegStorage> dp_regs_64(dp_regs_arr_64);
98static constexpr ArrayRef<const RegStorage> reserved_regs_32(reserved_regs_arr_32);
99static constexpr ArrayRef<const RegStorage> reserved_regs_64(reserved_regs_arr_64);
100static constexpr ArrayRef<const RegStorage> reserved_regs_64q(reserved_regs_arr_64q);
101static constexpr ArrayRef<const RegStorage> core_temps_32(core_temps_arr_32);
102static constexpr ArrayRef<const RegStorage> core_temps_64(core_temps_arr_64);
103static constexpr ArrayRef<const RegStorage> core_temps_64q(core_temps_arr_64q);
104static constexpr ArrayRef<const RegStorage> sp_temps_32(sp_temps_arr_32);
105static constexpr ArrayRef<const RegStorage> sp_temps_64(sp_temps_arr_64);
106static constexpr ArrayRef<const RegStorage> dp_temps_32(dp_temps_arr_32);
107static constexpr ArrayRef<const RegStorage> dp_temps_64(dp_temps_arr_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700108
Vladimir Marko089142c2014-06-05 10:57:05 +0100109static constexpr ArrayRef<const RegStorage> xp_temps_32(xp_temps_arr_32);
110static constexpr ArrayRef<const RegStorage> xp_temps_64(xp_temps_arr_64);
Mark Mendellfe945782014-05-22 09:52:36 -0400111
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700112RegStorage rs_rX86_SP;
113
114X86NativeRegisterPool rX86_ARG0;
115X86NativeRegisterPool rX86_ARG1;
116X86NativeRegisterPool rX86_ARG2;
117X86NativeRegisterPool rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700118X86NativeRegisterPool rX86_ARG4;
119X86NativeRegisterPool rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700120X86NativeRegisterPool rX86_FARG0;
121X86NativeRegisterPool rX86_FARG1;
122X86NativeRegisterPool rX86_FARG2;
123X86NativeRegisterPool rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700124X86NativeRegisterPool rX86_FARG4;
125X86NativeRegisterPool rX86_FARG5;
126X86NativeRegisterPool rX86_FARG6;
127X86NativeRegisterPool rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700128X86NativeRegisterPool rX86_RET0;
129X86NativeRegisterPool rX86_RET1;
130X86NativeRegisterPool rX86_INVOKE_TGT;
131X86NativeRegisterPool rX86_COUNT;
132
133RegStorage rs_rX86_ARG0;
134RegStorage rs_rX86_ARG1;
135RegStorage rs_rX86_ARG2;
136RegStorage rs_rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700137RegStorage rs_rX86_ARG4;
138RegStorage rs_rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700139RegStorage rs_rX86_FARG0;
140RegStorage rs_rX86_FARG1;
141RegStorage rs_rX86_FARG2;
142RegStorage rs_rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700143RegStorage rs_rX86_FARG4;
144RegStorage rs_rX86_FARG5;
145RegStorage rs_rX86_FARG6;
146RegStorage rs_rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700147RegStorage rs_rX86_RET0;
148RegStorage rs_rX86_RET1;
149RegStorage rs_rX86_INVOKE_TGT;
150RegStorage rs_rX86_COUNT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700152RegLocation X86Mir2Lir::LocCReturn() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000153 return x86_loc_c_return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154}
155
buzbeea0cd2d72014-06-01 09:33:49 -0700156RegLocation X86Mir2Lir::LocCReturnRef() {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700157 return cu_->target64 ? x86_64_loc_c_return_ref : x86_loc_c_return_ref;
buzbeea0cd2d72014-06-01 09:33:49 -0700158}
159
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700160RegLocation X86Mir2Lir::LocCReturnWide() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700161 return cu_->target64 ? x86_64_loc_c_return_wide : x86_loc_c_return_wide;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162}
163
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700164RegLocation X86Mir2Lir::LocCReturnFloat() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000165 return x86_loc_c_return_float;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166}
167
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700168RegLocation X86Mir2Lir::LocCReturnDouble() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000169 return x86_loc_c_return_double;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170}
171
Chao-ying Fua77ee512014-07-01 17:43:41 -0700172// Return a target-dependent special register for 32-bit.
173RegStorage X86Mir2Lir::TargetReg32(SpecialTargetRegister reg) {
buzbee091cc402014-03-31 10:14:40 -0700174 RegStorage res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 switch (reg) {
buzbee091cc402014-03-31 10:14:40 -0700176 case kSelf: res_reg = RegStorage::InvalidReg(); break;
177 case kSuspend: res_reg = RegStorage::InvalidReg(); break;
178 case kLr: res_reg = RegStorage::InvalidReg(); break;
179 case kPc: res_reg = RegStorage::InvalidReg(); break;
180 case kSp: res_reg = rs_rX86_SP; break;
181 case kArg0: res_reg = rs_rX86_ARG0; break;
182 case kArg1: res_reg = rs_rX86_ARG1; break;
183 case kArg2: res_reg = rs_rX86_ARG2; break;
184 case kArg3: res_reg = rs_rX86_ARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700185 case kArg4: res_reg = rs_rX86_ARG4; break;
186 case kArg5: res_reg = rs_rX86_ARG5; break;
buzbee091cc402014-03-31 10:14:40 -0700187 case kFArg0: res_reg = rs_rX86_FARG0; break;
188 case kFArg1: res_reg = rs_rX86_FARG1; break;
189 case kFArg2: res_reg = rs_rX86_FARG2; break;
190 case kFArg3: res_reg = rs_rX86_FARG3; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700191 case kFArg4: res_reg = rs_rX86_FARG4; break;
192 case kFArg5: res_reg = rs_rX86_FARG5; break;
193 case kFArg6: res_reg = rs_rX86_FARG6; break;
194 case kFArg7: res_reg = rs_rX86_FARG7; break;
buzbee091cc402014-03-31 10:14:40 -0700195 case kRet0: res_reg = rs_rX86_RET0; break;
196 case kRet1: res_reg = rs_rX86_RET1; break;
197 case kInvokeTgt: res_reg = rs_rX86_INVOKE_TGT; break;
198 case kHiddenArg: res_reg = rs_rAX; break;
Elena Sayapinadd644502014-07-01 18:39:52 +0700199 case kHiddenFpArg: DCHECK(!cu_->target64); res_reg = rs_fr0; break;
buzbee091cc402014-03-31 10:14:40 -0700200 case kCount: res_reg = rs_rX86_COUNT; break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700201 default: res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 }
buzbee091cc402014-03-31 10:14:40 -0700203 return res_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204}
205
Chao-ying Fua77ee512014-07-01 17:43:41 -0700206RegStorage X86Mir2Lir::TargetReg(SpecialTargetRegister reg) {
207 LOG(FATAL) << "Do not use this function!!!";
208 return RegStorage::InvalidReg();
209}
210
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211/*
212 * Decode the register id.
213 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100214ResourceMask X86Mir2Lir::GetRegMaskCommon(const RegStorage& reg) const {
215 /* Double registers in x86 are just a single FP register. This is always just a single bit. */
216 return ResourceMask::Bit(
217 /* FP register starts at bit position 16 */
218 ((reg.IsFloat() || reg.StorageSize() > 8) ? kX86FPReg0 : 0) + reg.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219}
220
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100221ResourceMask X86Mir2Lir::GetPCUseDefEncoding() const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 /*
223 * FIXME: might make sense to use a virtual resource encoding bit for pc. Might be
224 * able to clean up some of the x86/Arm_Mips differences
225 */
226 LOG(FATAL) << "Unexpected call to GetPCUseDefEncoding for x86";
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100227 return kEncodeNone;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228}
229
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100230void X86Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags,
231 ResourceMask* use_mask, ResourceMask* def_mask) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700232 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbeeb48819d2013-09-14 16:15:25 -0700233 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234
235 // X86-specific resource map setup here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 if (flags & REG_USE_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100237 use_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 }
239
240 if (flags & REG_DEF_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100241 def_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 }
243
244 if (flags & REG_DEFA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100245 SetupRegMask(def_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 }
247
248 if (flags & REG_DEFD) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100249 SetupRegMask(def_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 }
251 if (flags & REG_USEA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100252 SetupRegMask(use_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 }
254
255 if (flags & REG_USEC) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100256 SetupRegMask(use_mask, rs_rCX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 }
258
259 if (flags & REG_USED) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100260 SetupRegMask(use_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 }
Vladimir Marko70b797d2013-12-03 15:25:24 +0000262
263 if (flags & REG_USEB) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100264 SetupRegMask(use_mask, rs_rBX.GetReg());
Vladimir Marko70b797d2013-12-03 15:25:24 +0000265 }
Mark Mendell4028a6c2014-02-19 20:06:20 -0800266
267 // Fixup hard to describe instruction: Uses rAX, rCX, rDI; sets rDI.
268 if (lir->opcode == kX86RepneScasw) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100269 SetupRegMask(use_mask, rs_rAX.GetReg());
270 SetupRegMask(use_mask, rs_rCX.GetReg());
271 SetupRegMask(use_mask, rs_rDI.GetReg());
272 SetupRegMask(def_mask, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800273 }
Serguei Katkove90501d2014-03-12 15:56:54 +0700274
275 if (flags & USE_FP_STACK) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100276 use_mask->SetBit(kX86FPStack);
277 def_mask->SetBit(kX86FPStack);
Serguei Katkove90501d2014-03-12 15:56:54 +0700278 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279}
280
281/* For dumping instructions */
282static const char* x86RegName[] = {
283 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
284 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
285};
286
287static const char* x86CondName[] = {
288 "O",
289 "NO",
290 "B/NAE/C",
291 "NB/AE/NC",
292 "Z/EQ",
293 "NZ/NE",
294 "BE/NA",
295 "NBE/A",
296 "S",
297 "NS",
298 "P/PE",
299 "NP/PO",
300 "L/NGE",
301 "NL/GE",
302 "LE/NG",
303 "NLE/G"
304};
305
306/*
307 * Interpret a format string and build a string no longer than size
308 * See format key in Assemble.cc.
309 */
310std::string X86Mir2Lir::BuildInsnString(const char *fmt, LIR *lir, unsigned char* base_addr) {
311 std::string buf;
312 size_t i = 0;
313 size_t fmt_len = strlen(fmt);
314 while (i < fmt_len) {
315 if (fmt[i] != '!') {
316 buf += fmt[i];
317 i++;
318 } else {
319 i++;
320 DCHECK_LT(i, fmt_len);
321 char operand_number_ch = fmt[i];
322 i++;
323 if (operand_number_ch == '!') {
324 buf += "!";
325 } else {
326 int operand_number = operand_number_ch - '0';
327 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
328 DCHECK_LT(i, fmt_len);
329 int operand = lir->operands[operand_number];
330 switch (fmt[i]) {
331 case 'c':
332 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
333 buf += x86CondName[operand];
334 break;
335 case 'd':
336 buf += StringPrintf("%d", operand);
337 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400338 case 'q': {
339 int64_t value = static_cast<int64_t>(static_cast<int64_t>(operand) << 32 |
340 static_cast<uint32_t>(lir->operands[operand_number+1]));
341 buf +=StringPrintf("%" PRId64, value);
342 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 case 'p': {
buzbee0d829482013-10-11 15:24:55 -0700344 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(operand));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 buf += StringPrintf("0x%08x", tab_rec->offset);
346 break;
347 }
348 case 'r':
buzbee091cc402014-03-31 10:14:40 -0700349 if (RegStorage::IsFloat(operand)) {
350 int fp_reg = RegStorage::RegNum(operand);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 buf += StringPrintf("xmm%d", fp_reg);
352 } else {
buzbee091cc402014-03-31 10:14:40 -0700353 int reg_num = RegStorage::RegNum(operand);
354 DCHECK_LT(static_cast<size_t>(reg_num), sizeof(x86RegName));
355 buf += x86RegName[reg_num];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 }
357 break;
358 case 't':
Ian Rogers107c31e2014-01-23 20:55:29 -0800359 buf += StringPrintf("0x%08" PRIxPTR " (L%p)",
360 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + operand,
361 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 break;
363 default:
364 buf += StringPrintf("DecodeError '%c'", fmt[i]);
365 break;
366 }
367 i++;
368 }
369 }
370 }
371 return buf;
372}
373
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100374void X86Mir2Lir::DumpResourceMask(LIR *x86LIR, const ResourceMask& mask, const char *prefix) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 char buf[256];
376 buf[0] = 0;
377
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100378 if (mask.Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 strcpy(buf, "all");
380 } else {
381 char num[8];
382 int i;
383
384 for (i = 0; i < kX86RegEnd; i++) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100385 if (mask.HasBit(i)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800386 snprintf(num, arraysize(num), "%d ", i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 strcat(buf, num);
388 }
389 }
390
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100391 if (mask.HasBit(ResourceMask::kCCode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 strcat(buf, "cc ");
393 }
394 /* Memory bits */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100395 if (x86LIR && (mask.HasBit(ResourceMask::kDalvikReg))) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800396 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
397 DECODE_ALIAS_INFO_REG(x86LIR->flags.alias_info),
398 (DECODE_ALIAS_INFO_WIDE(x86LIR->flags.alias_info)) ? "(+1)" : "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100400 if (mask.HasBit(ResourceMask::kLiteral)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 strcat(buf, "lit ");
402 }
403
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100404 if (mask.HasBit(ResourceMask::kHeapRef)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 strcat(buf, "heap ");
406 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100407 if (mask.HasBit(ResourceMask::kMustNotAlias)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 strcat(buf, "noalias ");
409 }
410 }
411 if (buf[0]) {
412 LOG(INFO) << prefix << ": " << buf;
413 }
414}
415
416void X86Mir2Lir::AdjustSpillMask() {
417 // Adjustment for LR spilling, x86 has no LR so nothing to do here
buzbee091cc402014-03-31 10:14:40 -0700418 core_spill_mask_ |= (1 << rs_rRET.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 num_core_spills_++;
420}
421
Mark Mendelle87f9b52014-04-30 14:13:18 -0400422RegStorage X86Mir2Lir::AllocateByteRegister() {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700423 RegStorage reg = AllocTypedTemp(false, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +0700424 if (!cu_->target64) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700425 DCHECK_LT(reg.GetRegNum(), rs_rX86_SP.GetRegNum());
426 }
427 return reg;
428}
429
430bool X86Mir2Lir::IsByteRegister(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700431 return cu_->target64 || reg.GetRegNum() < rs_rX86_SP.GetRegNum();
Mark Mendelle87f9b52014-04-30 14:13:18 -0400432}
433
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434/* Clobber all regs that might be used by an external C call */
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000435void X86Mir2Lir::ClobberCallerSave() {
buzbee091cc402014-03-31 10:14:40 -0700436 Clobber(rs_rAX);
437 Clobber(rs_rCX);
438 Clobber(rs_rDX);
439 Clobber(rs_rBX);
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700440
441 Clobber(rs_fr0);
442 Clobber(rs_fr1);
443 Clobber(rs_fr2);
444 Clobber(rs_fr3);
445 Clobber(rs_fr4);
446 Clobber(rs_fr5);
447 Clobber(rs_fr6);
448 Clobber(rs_fr7);
449
Elena Sayapinadd644502014-07-01 18:39:52 +0700450 if (cu_->target64) {
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700451 Clobber(rs_r8);
452 Clobber(rs_r9);
453 Clobber(rs_r10);
454 Clobber(rs_r11);
455
456 Clobber(rs_fr8);
457 Clobber(rs_fr9);
458 Clobber(rs_fr10);
459 Clobber(rs_fr11);
460 Clobber(rs_fr12);
461 Clobber(rs_fr13);
462 Clobber(rs_fr14);
463 Clobber(rs_fr15);
464 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465}
466
467RegLocation X86Mir2Lir::GetReturnWideAlt() {
468 RegLocation res = LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -0700469 DCHECK(res.reg.GetLowReg() == rs_rAX.GetReg());
470 DCHECK(res.reg.GetHighReg() == rs_rDX.GetReg());
471 Clobber(rs_rAX);
472 Clobber(rs_rDX);
473 MarkInUse(rs_rAX);
474 MarkInUse(rs_rDX);
475 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 return res;
477}
478
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700479RegLocation X86Mir2Lir::GetReturnAlt() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 RegLocation res = LocCReturn();
buzbee091cc402014-03-31 10:14:40 -0700481 res.reg.SetReg(rs_rDX.GetReg());
482 Clobber(rs_rDX);
483 MarkInUse(rs_rDX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 return res;
485}
486
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700488void X86Mir2Lir::LockCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700489 LockTemp(rs_rX86_ARG0);
490 LockTemp(rs_rX86_ARG1);
491 LockTemp(rs_rX86_ARG2);
492 LockTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700493 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700494 LockTemp(rs_rX86_ARG4);
495 LockTemp(rs_rX86_ARG5);
496 LockTemp(rs_rX86_FARG0);
497 LockTemp(rs_rX86_FARG1);
498 LockTemp(rs_rX86_FARG2);
499 LockTemp(rs_rX86_FARG3);
500 LockTemp(rs_rX86_FARG4);
501 LockTemp(rs_rX86_FARG5);
502 LockTemp(rs_rX86_FARG6);
503 LockTemp(rs_rX86_FARG7);
504 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505}
506
507/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700508void X86Mir2Lir::FreeCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700509 FreeTemp(rs_rX86_ARG0);
510 FreeTemp(rs_rX86_ARG1);
511 FreeTemp(rs_rX86_ARG2);
512 FreeTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700513 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700514 FreeTemp(rs_rX86_ARG4);
515 FreeTemp(rs_rX86_ARG5);
516 FreeTemp(rs_rX86_FARG0);
517 FreeTemp(rs_rX86_FARG1);
518 FreeTemp(rs_rX86_FARG2);
519 FreeTemp(rs_rX86_FARG3);
520 FreeTemp(rs_rX86_FARG4);
521 FreeTemp(rs_rX86_FARG5);
522 FreeTemp(rs_rX86_FARG6);
523 FreeTemp(rs_rX86_FARG7);
524 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525}
526
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800527bool X86Mir2Lir::ProvidesFullMemoryBarrier(X86OpCode opcode) {
528 switch (opcode) {
529 case kX86LockCmpxchgMR:
530 case kX86LockCmpxchgAR:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700531 case kX86LockCmpxchg64M:
532 case kX86LockCmpxchg64A:
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800533 case kX86XchgMR:
534 case kX86Mfence:
535 // Atomic memory instructions provide full barrier.
536 return true;
537 default:
538 break;
539 }
540
541 // Conservative if cannot prove it provides full barrier.
542 return false;
543}
544
Andreas Gampeb14329f2014-05-15 11:16:06 -0700545bool X86Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800547 // Start off with using the last LIR as the barrier. If it is not enough, then we will update it.
548 LIR* mem_barrier = last_lir_insn_;
549
Andreas Gampeb14329f2014-05-15 11:16:06 -0700550 bool ret = false;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800551 /*
552 * According to the JSR-133 Cookbook, for x86 only StoreLoad barriers need memory fence. All other barriers
553 * (LoadLoad, LoadStore, StoreStore) are nops due to the x86 memory model. For those cases, all we need
554 * to ensure is that there is a scheduling barrier in place.
555 */
556 if (barrier_kind == kStoreLoad) {
557 // If no LIR exists already that can be used a barrier, then generate an mfence.
558 if (mem_barrier == nullptr) {
559 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700560 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800561 }
562
563 // If last instruction does not provide full barrier, then insert an mfence.
564 if (ProvidesFullMemoryBarrier(static_cast<X86OpCode>(mem_barrier->opcode)) == false) {
565 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700566 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800567 }
568 }
569
570 // Now ensure that a scheduling barrier is in place.
571 if (mem_barrier == nullptr) {
572 GenBarrier();
573 } else {
574 // Mark as a scheduling barrier.
575 DCHECK(!mem_barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100576 mem_barrier->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800577 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700578 return ret;
579#else
580 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581#endif
582}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000583
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584void X86Mir2Lir::CompilerInitializeRegAlloc() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700585 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700586 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_64, core_regs_64q, sp_regs_64,
587 dp_regs_64, reserved_regs_64, reserved_regs_64q,
588 core_temps_64, core_temps_64q, sp_temps_64, dp_temps_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700589 } else {
buzbeeb01bf152014-05-13 15:59:07 -0700590 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
591 dp_regs_32, reserved_regs_32, empty_pool,
592 core_temps_32, empty_pool, sp_temps_32, dp_temps_32);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700593 }
buzbee091cc402014-03-31 10:14:40 -0700594
595 // Target-specific adjustments.
596
Mark Mendellfe945782014-05-22 09:52:36 -0400597 // Add in XMM registers.
Elena Sayapinadd644502014-07-01 18:39:52 +0700598 const ArrayRef<const RegStorage> *xp_temps = cu_->target64 ? &xp_temps_64 : &xp_temps_32;
Mark Mendellfe945782014-05-22 09:52:36 -0400599 for (RegStorage reg : *xp_temps) {
600 RegisterInfo* info = new (arena_) RegisterInfo(reg, GetRegMaskCommon(reg));
601 reginfo_map_.Put(reg.GetReg(), info);
602 info->SetIsTemp(true);
603 }
604
buzbee091cc402014-03-31 10:14:40 -0700605 // Alias single precision xmm to double xmms.
606 // TODO: as needed, add larger vector sizes - alias all to the largest.
607 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
608 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
609 int sp_reg_num = info->GetReg().GetRegNum();
Mark Mendellfe945782014-05-22 09:52:36 -0400610 RegStorage xp_reg = RegStorage::Solo128(sp_reg_num);
611 RegisterInfo* xp_reg_info = GetRegInfo(xp_reg);
612 // 128-bit xmm vector register's master storage should refer to itself.
613 DCHECK_EQ(xp_reg_info, xp_reg_info->Master());
614
615 // Redirect 32-bit vector's master storage to 128-bit vector.
616 info->SetMaster(xp_reg_info);
617
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700618 RegStorage dp_reg = RegStorage::FloatSolo64(sp_reg_num);
buzbee091cc402014-03-31 10:14:40 -0700619 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
Mark Mendellfe945782014-05-22 09:52:36 -0400620 // Redirect 64-bit vector's master storage to 128-bit vector.
621 dp_reg_info->SetMaster(xp_reg_info);
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700622 // Singles should show a single 32-bit mask bit, at first referring to the low half.
623 DCHECK_EQ(info->StorageMask(), 0x1U);
624 }
625
Elena Sayapinadd644502014-07-01 18:39:52 +0700626 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700627 // Alias 32bit W registers to corresponding 64bit X registers.
628 GrowableArray<RegisterInfo*>::Iterator w_it(&reg_pool_->core_regs_);
629 for (RegisterInfo* info = w_it.Next(); info != nullptr; info = w_it.Next()) {
630 int x_reg_num = info->GetReg().GetRegNum();
631 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
632 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
633 // 64bit X register's master storage should refer to itself.
634 DCHECK_EQ(x_reg_info, x_reg_info->Master());
635 // Redirect 32bit W master storage to 64bit X.
636 info->SetMaster(x_reg_info);
637 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
638 DCHECK_EQ(info->StorageMask(), 0x1U);
639 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 }
buzbee091cc402014-03-31 10:14:40 -0700641
642 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
643 // TODO: adjust for x86/hard float calling convention.
644 reg_pool_->next_core_reg_ = 2;
645 reg_pool_->next_sp_reg_ = 2;
646 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647}
648
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649void X86Mir2Lir::SpillCoreRegs() {
650 if (num_core_spills_ == 0) {
651 return;
652 }
653 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700654 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700655 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 for (int reg = 0; mask; mask >>= 1, reg++) {
657 if (mask & 0x1) {
buzbee2700f7e2014-03-07 09:46:20 -0800658 StoreWordDisp(rs_rX86_SP, offset, RegStorage::Solo32(reg));
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700659 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 }
661 }
662}
663
664void X86Mir2Lir::UnSpillCoreRegs() {
665 if (num_core_spills_ == 0) {
666 return;
667 }
668 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700669 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700670 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 for (int reg = 0; mask; mask >>= 1, reg++) {
672 if (mask & 0x1) {
buzbee2700f7e2014-03-07 09:46:20 -0800673 LoadWordDisp(rs_rX86_SP, offset, RegStorage::Solo32(reg));
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700674 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 }
676 }
677}
678
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700679bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
681}
682
Vladimir Marko674744e2014-04-24 15:18:26 +0100683bool X86Mir2Lir::SupportsVolatileLoadStore(OpSize size) {
684 return true;
685}
686
687RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700688 // X86_64 can handle any size.
Elena Sayapinadd644502014-07-01 18:39:52 +0700689 if (cu_->target64) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700690 if (size == kReference) {
691 return kRefReg;
692 }
693 return kCoreReg;
694 }
695
Vladimir Marko674744e2014-04-24 15:18:26 +0100696 if (UNLIKELY(is_volatile)) {
697 // On x86, atomic 64-bit load/store requires an fp register.
698 // Smaller aligned load/store is atomic for both core and fp registers.
699 if (size == k64 || size == kDouble) {
700 return kFPReg;
701 }
702 }
703 return RegClassBySize(size);
704}
705
Elena Sayapinadd644502014-07-01 18:39:52 +0700706X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800707 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700708 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800709 method_address_insns_(arena, 100, kGrowableArrayMisc),
710 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800711 call_method_insns_(arena, 100, kGrowableArrayMisc),
Elena Sayapinadd644502014-07-01 18:39:52 +0700712 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400713 const_vectors_(nullptr) {
714 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700715 if (kIsDebugBuild) {
716 for (int i = 0; i < kX86Last; i++) {
717 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
718 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400719 << " is wrong: expecting " << i << ", seeing "
720 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700721 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 }
723 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700724 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700725 rs_rX86_SP = rs_rX86_SP_64;
726
727 rs_rX86_ARG0 = rs_rDI;
728 rs_rX86_ARG1 = rs_rSI;
729 rs_rX86_ARG2 = rs_rDX;
730 rs_rX86_ARG3 = rs_rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700731 rs_rX86_ARG4 = rs_r8;
732 rs_rX86_ARG5 = rs_r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700733 rs_rX86_FARG0 = rs_fr0;
734 rs_rX86_FARG1 = rs_fr1;
735 rs_rX86_FARG2 = rs_fr2;
736 rs_rX86_FARG3 = rs_fr3;
737 rs_rX86_FARG4 = rs_fr4;
738 rs_rX86_FARG5 = rs_fr5;
739 rs_rX86_FARG6 = rs_fr6;
740 rs_rX86_FARG7 = rs_fr7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700741 rX86_ARG0 = rDI;
742 rX86_ARG1 = rSI;
743 rX86_ARG2 = rDX;
744 rX86_ARG3 = rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700745 rX86_ARG4 = r8;
746 rX86_ARG5 = r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700747 rX86_FARG0 = fr0;
748 rX86_FARG1 = fr1;
749 rX86_FARG2 = fr2;
750 rX86_FARG3 = fr3;
751 rX86_FARG4 = fr4;
752 rX86_FARG5 = fr5;
753 rX86_FARG6 = fr6;
754 rX86_FARG7 = fr7;
Mark Mendell55884bc2014-06-10 10:21:29 -0400755 rs_rX86_INVOKE_TGT = rs_rDI;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700756 } else {
757 rs_rX86_SP = rs_rX86_SP_32;
758
759 rs_rX86_ARG0 = rs_rAX;
760 rs_rX86_ARG1 = rs_rCX;
761 rs_rX86_ARG2 = rs_rDX;
762 rs_rX86_ARG3 = rs_rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700763 rs_rX86_ARG4 = RegStorage::InvalidReg();
764 rs_rX86_ARG5 = RegStorage::InvalidReg();
765 rs_rX86_FARG0 = rs_rAX;
766 rs_rX86_FARG1 = rs_rCX;
767 rs_rX86_FARG2 = rs_rDX;
768 rs_rX86_FARG3 = rs_rBX;
769 rs_rX86_FARG4 = RegStorage::InvalidReg();
770 rs_rX86_FARG5 = RegStorage::InvalidReg();
771 rs_rX86_FARG6 = RegStorage::InvalidReg();
772 rs_rX86_FARG7 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700773 rX86_ARG0 = rAX;
774 rX86_ARG1 = rCX;
775 rX86_ARG2 = rDX;
776 rX86_ARG3 = rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700777 rX86_FARG0 = rAX;
778 rX86_FARG1 = rCX;
779 rX86_FARG2 = rDX;
780 rX86_FARG3 = rBX;
Mark Mendell55884bc2014-06-10 10:21:29 -0400781 rs_rX86_INVOKE_TGT = rs_rAX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700782 // TODO(64): Initialize with invalid reg
783// rX86_ARG4 = RegStorage::InvalidReg();
784// rX86_ARG5 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700785 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700786 rs_rX86_RET0 = rs_rAX;
787 rs_rX86_RET1 = rs_rDX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700788 rs_rX86_COUNT = rs_rCX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700789 rX86_RET0 = rAX;
790 rX86_RET1 = rDX;
791 rX86_INVOKE_TGT = rAX;
792 rX86_COUNT = rCX;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793}
794
795Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
796 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700797 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798}
799
800// Not used in x86
Ian Rogersdd7624d2014-03-14 17:43:00 -0700801RegStorage X86Mir2Lir::LoadHelper(ThreadOffset<4> offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
buzbee2700f7e2014-03-07 09:46:20 -0800803 return RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700804}
805
Andreas Gampe2f244e92014-05-08 03:35:25 -0700806// Not used in x86
807RegStorage X86Mir2Lir::LoadHelper(ThreadOffset<8> offset) {
808 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
809 return RegStorage::InvalidReg();
810}
811
Dave Allisonb373e092014-02-20 16:06:36 -0800812LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
813 LOG(FATAL) << "Unexpected use of CheckSuspendUsingLoad in x86";
814 return nullptr;
815}
816
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700817uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700818 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 return X86Mir2Lir::EncodingMap[opcode].flags;
820}
821
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700822const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700823 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824 return X86Mir2Lir::EncodingMap[opcode].name;
825}
826
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700827const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700828 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 return X86Mir2Lir::EncodingMap[opcode].fmt;
830}
831
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000832void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
833 // Can we do this directly to memory?
834 rl_dest = UpdateLocWide(rl_dest);
835 if ((rl_dest.location == kLocDalvikFrame) ||
836 (rl_dest.location == kLocCompilerTemp)) {
837 int32_t val_lo = Low32Bits(value);
838 int32_t val_hi = High32Bits(value);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700839 int r_base = rs_rX86_SP.GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000840 int displacement = SRegOffset(rl_dest.s_reg_low);
841
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100842 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800843 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000844 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
845 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800846 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000847 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
848 false /* is_load */, true /* is64bit */);
849 return;
850 }
851
852 // Just use the standard code to do the generation.
853 Mir2Lir::GenConstWide(rl_dest, value);
854}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800855
856// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
857void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
858 LOG(INFO) << "location: " << loc.location << ','
859 << (loc.wide ? " w" : " ")
860 << (loc.defined ? " D" : " ")
861 << (loc.is_const ? " c" : " ")
862 << (loc.fp ? " F" : " ")
863 << (loc.core ? " C" : " ")
864 << (loc.ref ? " r" : " ")
865 << (loc.high_word ? " h" : " ")
866 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800867 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000868 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800869 << ", s_reg: " << loc.s_reg_low
870 << ", orig: " << loc.orig_sreg;
871}
872
Mark Mendell67c39c42014-01-31 17:28:00 -0800873void X86Mir2Lir::Materialize() {
874 // A good place to put the analysis before starting.
875 AnalyzeMIR();
876
877 // Now continue with regular code generation.
878 Mir2Lir::Materialize();
879}
880
Jeff Hao49161ce2014-03-12 11:05:25 -0700881void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800882 SpecialTargetRegister symbolic_reg) {
883 /*
884 * For x86, just generate a 32 bit move immediate instruction, that will be filled
885 * in at 'link time'. For now, put a unique value based on target to ensure that
886 * code deduplication works.
887 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700888 int target_method_idx = target_method.dex_method_index;
889 const DexFile* target_dex_file = target_method.dex_file;
890 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
891 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800892
Jeff Hao49161ce2014-03-12 11:05:25 -0700893 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700894 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI, TargetReg(symbolic_reg, false).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700895 static_cast<int>(target_method_id_ptr), target_method_idx,
896 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800897 AppendLIR(move);
898 method_address_insns_.Insert(move);
899}
900
901void X86Mir2Lir::LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg) {
902 /*
903 * For x86, just generate a 32 bit move immediate instruction, that will be filled
904 * in at 'link time'. For now, put a unique value based on target to ensure that
905 * code deduplication works.
906 */
907 const DexFile::TypeId& id = cu_->dex_file->GetTypeId(type_idx);
908 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
909
910 // Generate the move instruction with the unique pointer and save index and type.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700911 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI, TargetReg(symbolic_reg, false).GetReg(),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800912 static_cast<int>(ptr), type_idx);
913 AppendLIR(move);
914 class_type_address_insns_.Insert(move);
915}
916
Jeff Hao49161ce2014-03-12 11:05:25 -0700917LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800918 /*
919 * For x86, just generate a 32 bit call relative instruction, that will be filled
920 * in at 'link time'. For now, put a unique value based on target to ensure that
921 * code deduplication works.
922 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700923 int target_method_idx = target_method.dex_method_index;
924 const DexFile* target_dex_file = target_method.dex_file;
925 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
926 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800927
Jeff Hao49161ce2014-03-12 11:05:25 -0700928 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
929 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
930 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800931 AppendLIR(call);
932 call_method_insns_.Insert(call);
933 return call;
934}
935
Mark Mendelld65c51a2014-04-29 16:55:20 -0400936/*
937 * @brief Enter a 32 bit quantity into a buffer
938 * @param buf buffer.
939 * @param data Data value.
940 */
941
942static void PushWord(std::vector<uint8_t>&buf, int32_t data) {
943 buf.push_back(data & 0xff);
944 buf.push_back((data >> 8) & 0xff);
945 buf.push_back((data >> 16) & 0xff);
946 buf.push_back((data >> 24) & 0xff);
947}
948
Mark Mendell55d0eac2014-02-06 11:02:52 -0800949void X86Mir2Lir::InstallLiteralPools() {
950 // These are handled differently for x86.
951 DCHECK(code_literal_list_ == nullptr);
952 DCHECK(method_literal_list_ == nullptr);
953 DCHECK(class_literal_list_ == nullptr);
954
Mark Mendelld65c51a2014-04-29 16:55:20 -0400955 // Align to 16 byte boundary. We have implicit knowledge that the start of the method is
956 // on a 4 byte boundary. How can I check this if it changes (other than aligned loads
957 // will fail at runtime)?
958 if (const_vectors_ != nullptr) {
959 int align_size = (16-4) - (code_buffer_.size() & 0xF);
960 if (align_size < 0) {
961 align_size += 16;
962 }
963
964 while (align_size > 0) {
965 code_buffer_.push_back(0);
966 align_size--;
967 }
968 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
969 PushWord(code_buffer_, p->operands[0]);
970 PushWord(code_buffer_, p->operands[1]);
971 PushWord(code_buffer_, p->operands[2]);
972 PushWord(code_buffer_, p->operands[3]);
973 }
974 }
975
Mark Mendell55d0eac2014-02-06 11:02:52 -0800976 // Handle the fixups for methods.
977 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
978 LIR* p = method_address_insns_.Get(i);
979 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -0700980 uint32_t target_method_idx = p->operands[2];
981 const DexFile* target_dex_file =
982 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800983
984 // The offset to patch is the last 4 bytes of the instruction.
985 int patch_offset = p->offset + p->flags.size - 4;
986 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
987 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700988 target_method_idx, target_dex_file,
989 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800990 patch_offset);
991 }
992
993 // Handle the fixups for class types.
994 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
995 LIR* p = class_type_address_insns_.Get(i);
996 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -0700997 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -0800998
999 // The offset to patch is the last 4 bytes of the instruction.
1000 int patch_offset = p->offset + p->flags.size - 4;
1001 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001002 cu_->method_idx, target_method_idx, patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001003 }
1004
1005 // And now the PC-relative calls to methods.
1006 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
1007 LIR* p = call_method_insns_.Get(i);
1008 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001009 uint32_t target_method_idx = p->operands[1];
1010 const DexFile* target_dex_file =
1011 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001012
1013 // The offset to patch is the last 4 bytes of the instruction.
1014 int patch_offset = p->offset + p->flags.size - 4;
1015 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001016 cu_->method_idx, cu_->invoke_type,
1017 target_method_idx, target_dex_file,
1018 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001019 patch_offset, -4 /* offset */);
1020 }
1021
1022 // And do the normal processing.
1023 Mir2Lir::InstallLiteralPools();
1024}
1025
DaniilSokolov70c4f062014-06-24 17:34:00 -07001026bool X86Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
1027 if (cu_->target64) {
1028 // TODO: Implement ArrayCOpy intrinsic for x86_64
1029 return false;
1030 }
1031
1032 RegLocation rl_src = info->args[0];
1033 RegLocation rl_srcPos = info->args[1];
1034 RegLocation rl_dst = info->args[2];
1035 RegLocation rl_dstPos = info->args[3];
1036 RegLocation rl_length = info->args[4];
1037 if (rl_srcPos.is_const && (mir_graph_->ConstantValue(rl_srcPos) < 0)) {
1038 return false;
1039 }
1040 if (rl_dstPos.is_const && (mir_graph_->ConstantValue(rl_dstPos) < 0)) {
1041 return false;
1042 }
1043 ClobberCallerSave();
1044 LockCallTemps(); // Using fixed registers
1045 LoadValueDirectFixed(rl_src , rs_rAX);
1046 LoadValueDirectFixed(rl_dst , rs_rCX);
1047 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_rAX , rs_rCX, nullptr);
1048 LIR* src_null_branch = OpCmpImmBranch(kCondEq, rs_rAX , 0, nullptr);
1049 LIR* dst_null_branch = OpCmpImmBranch(kCondEq, rs_rCX , 0, nullptr);
1050 LoadValueDirectFixed(rl_length , rs_rDX);
1051 LIR* len_negative = OpCmpImmBranch(kCondLt, rs_rDX , 0, nullptr);
1052 LIR* len_too_big = OpCmpImmBranch(kCondGt, rs_rDX , 128, nullptr);
1053 LoadValueDirectFixed(rl_src , rs_rAX);
1054 LoadWordDisp(rs_rAX , mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1055 LIR* src_bad_len = nullptr;
1056 LIR* srcPos_negative = nullptr;
1057 if (!rl_srcPos.is_const) {
1058 LoadValueDirectFixed(rl_srcPos , rs_rBX);
1059 srcPos_negative = OpCmpImmBranch(kCondLt, rs_rBX , 0, nullptr);
1060 OpRegReg(kOpAdd, rs_rBX, rs_rDX);
1061 src_bad_len = OpCmpBranch(kCondLt, rs_rAX , rs_rBX, nullptr);
1062 } else {
1063 int pos_val = mir_graph_->ConstantValue(rl_srcPos.orig_sreg);
1064 if (pos_val == 0) {
1065 src_bad_len = OpCmpBranch(kCondLt, rs_rAX , rs_rDX, nullptr);
1066 } else {
1067 OpRegRegImm(kOpAdd, rs_rBX, rs_rDX, pos_val);
1068 src_bad_len = OpCmpBranch(kCondLt, rs_rAX , rs_rBX, nullptr);
1069 }
1070 }
1071 LIR* dstPos_negative = nullptr;
1072 LIR* dst_bad_len = nullptr;
1073 LoadValueDirectFixed(rl_dst, rs_rAX);
1074 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1075 if (!rl_dstPos.is_const) {
1076 LoadValueDirectFixed(rl_dstPos , rs_rBX);
1077 dstPos_negative = OpCmpImmBranch(kCondLt, rs_rBX , 0, nullptr);
1078 OpRegRegReg(kOpAdd, rs_rBX, rs_rBX, rs_rDX);
1079 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX , rs_rBX, nullptr);
1080 } else {
1081 int pos_val = mir_graph_->ConstantValue(rl_dstPos.orig_sreg);
1082 if (pos_val == 0) {
1083 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX , rs_rDX, nullptr);
1084 } else {
1085 OpRegRegImm(kOpAdd, rs_rBX, rs_rDX, pos_val);
1086 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX , rs_rBX, nullptr);
1087 }
1088 }
1089 // everything is checked now
1090 LoadValueDirectFixed(rl_src , rs_rAX);
1091 LoadValueDirectFixed(rl_dst , rs_rBX);
1092 LoadValueDirectFixed(rl_srcPos , rs_rCX);
1093 NewLIR5(kX86Lea32RA, rs_rAX.GetReg(), rs_rAX.GetReg(),
1094 rs_rCX.GetReg() , 1, mirror::Array::DataOffset(2).Int32Value());
1095 // RAX now holds the address of the first src element to be copied
1096
1097 LoadValueDirectFixed(rl_dstPos , rs_rCX);
1098 NewLIR5(kX86Lea32RA, rs_rBX.GetReg(), rs_rBX.GetReg(),
1099 rs_rCX.GetReg() , 1, mirror::Array::DataOffset(2).Int32Value() );
1100 // RBX now holds the address of the first dst element to be copied
1101
1102 // check if the number of elements to be copied is odd or even. If odd
1103 // then copy the first element (so that the remaining number of elements
1104 // is even).
1105 LoadValueDirectFixed(rl_length , rs_rCX);
1106 OpRegImm(kOpAnd, rs_rCX, 1);
1107 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1108 OpRegImm(kOpSub, rs_rDX, 1);
1109 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
1110 StoreBaseIndexedDisp(rs_rBX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
1111
1112 // since the remaining number of elements is even, we will copy by
1113 // two elements at a time.
1114 LIR *beginLoop = NewLIR0(kPseudoTargetLabel);
1115 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_rDX , 0, nullptr);
1116 OpRegImm(kOpSub, rs_rDX, 2);
1117 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSingle);
1118 StoreBaseIndexedDisp(rs_rBX, rs_rDX, 1, 0, rs_rCX, kSingle);
1119 OpUnconditionalBranch(beginLoop);
1120 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1121 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1122 LIR *return_point = NewLIR0(kPseudoTargetLabel);
1123 jmp_to_ret->target = return_point;
1124 jmp_to_begin_loop->target = beginLoop;
1125 src_dst_same->target = check_failed;
1126 len_negative->target = check_failed;
1127 len_too_big->target = check_failed;
1128 src_null_branch->target = check_failed;
1129 if (srcPos_negative != nullptr)
1130 srcPos_negative ->target = check_failed;
1131 if (src_bad_len != nullptr)
1132 src_bad_len->target = check_failed;
1133 dst_null_branch->target = check_failed;
1134 if (dstPos_negative != nullptr)
1135 dstPos_negative->target = check_failed;
1136 if (dst_bad_len != nullptr)
1137 dst_bad_len->target = check_failed;
1138 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
1139 return true;
1140}
1141
1142
Mark Mendell4028a6c2014-02-19 20:06:20 -08001143/*
1144 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1145 * otherwise bails to standard library code.
1146 */
1147bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
1148 ClobberCallerSave();
1149 LockCallTemps(); // Using fixed registers
1150
1151 // EAX: 16 bit character being searched.
1152 // ECX: count: number of words to be searched.
1153 // EDI: String being searched.
1154 // EDX: temporary during execution.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001155 // EBX or R11: temporary during execution (depending on mode).
Mark Mendell4028a6c2014-02-19 20:06:20 -08001156
1157 RegLocation rl_obj = info->args[0];
1158 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001159 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001160 RegStorage tmpReg = cu_->target64 ? rs_r11 : rs_rBX;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001161
1162 uint32_t char_value =
1163 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1164
1165 if (char_value > 0xFFFF) {
1166 // We have to punt to the real String.indexOf.
1167 return false;
1168 }
1169
1170 // Okay, we are commited to inlining this.
buzbeea0cd2d72014-06-01 09:33:49 -07001171 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001172 RegLocation rl_dest = InlineTarget(info);
1173
1174 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001175 LoadValueDirectFixed(rl_obj, rs_rDX);
1176 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001177 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001178
1179 // Does the character fit in 16 bits?
Mingyao Yang3a74d152014-04-21 15:39:44 -07001180 LIR* slowpath_branch = nullptr;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001181 if (rl_char.is_const) {
1182 // We need the value in EAX.
buzbee2700f7e2014-03-07 09:46:20 -08001183 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001184 } else {
1185 // Character is not a constant; compare at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001186 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001187 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001188 }
1189
1190 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001191 // Location of reference to data array within the String object.
1192 int value_offset = mirror::String::ValueOffset().Int32Value();
1193 // Location of count within the String object.
1194 int count_offset = mirror::String::CountOffset().Int32Value();
1195 // Starting offset within data array.
1196 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1197 // Start of char data with array_.
1198 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001199
1200 // Character is in EAX.
1201 // Object pointer is in EDX.
1202
1203 // We need to preserve EDI, but have no spare registers, so push it on the stack.
1204 // We have to remember that all stack addresses after this are offset by sizeof(EDI).
buzbee091cc402014-03-31 10:14:40 -07001205 NewLIR1(kX86Push32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001206
1207 // Compute the number of words to search in to rCX.
buzbee695d13a2014-04-19 13:32:20 -07001208 Load32Disp(rs_rDX, count_offset, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001209 LIR *length_compare = nullptr;
1210 int start_value = 0;
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001211 bool is_index_on_stack = false;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001212 if (zero_based) {
1213 // We have to handle an empty string. Use special instruction JECXZ.
1214 length_compare = NewLIR0(kX86Jecxz8);
1215 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001216 rl_start = info->args[2];
Mark Mendell4028a6c2014-02-19 20:06:20 -08001217 // We have to offset by the start index.
1218 if (rl_start.is_const) {
1219 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1220 start_value = std::max(start_value, 0);
1221
1222 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001223 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001224
1225 if (start_value != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001226 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001227 }
1228 } else {
1229 // Runtime start index.
buzbee30adc732014-05-09 15:10:18 -07001230 rl_start = UpdateLocTyped(rl_start, kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001231 if (rl_start.location == kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001232 // Handle "start index < 0" case.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001233 OpRegReg(kOpXor, tmpReg, tmpReg);
1234 OpRegReg(kOpCmp, rl_start.reg, tmpReg);
1235 OpCondRegReg(kOpCmov, kCondLt, rl_start.reg, tmpReg);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001236
1237 // The length of the string should be greater than the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001238 length_compare = OpCmpBranch(kCondLe, rs_rCX, rl_start.reg, nullptr);
1239 OpRegReg(kOpSub, rs_rCX, rl_start.reg);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001240 if (rl_start.reg == rs_rDI) {
1241 // The special case. We will use EDI further, so lets put start index to stack.
buzbee091cc402014-03-31 10:14:40 -07001242 NewLIR1(kX86Push32R, rs_rDI.GetReg());
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001243 is_index_on_stack = true;
1244 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001245 } else {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001246 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001247 int displacement = SRegOffset(rl_start.s_reg_low) + (cu_->target64 ? 2 : 1) * sizeof(uint32_t);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001248 {
1249 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001250 Load32Disp(rs_rX86_SP, displacement, tmpReg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001251 }
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001252 OpRegReg(kOpXor, rs_rDI, rs_rDI);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001253 OpRegReg(kOpCmp, tmpReg, rs_rDI);
1254 OpCondRegReg(kOpCmov, kCondLt, tmpReg, rs_rDI);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001255
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001256 length_compare = OpCmpBranch(kCondLe, rs_rCX, tmpReg, nullptr);
1257 OpRegReg(kOpSub, rs_rCX, tmpReg);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001258 // Put the start index to stack.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001259 NewLIR1(kX86Push32R, tmpReg.GetReg());
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001260 is_index_on_stack = true;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001261 }
1262 }
1263 }
1264 DCHECK(length_compare != nullptr);
1265
1266 // ECX now contains the count in words to be searched.
1267
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001268 // Load the address of the string into R11 or EBX (depending on mode).
Mark Mendelle19c91f2014-02-25 08:19:08 -08001269 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
buzbee695d13a2014-04-19 13:32:20 -07001270 Load32Disp(rs_rDX, value_offset, rs_rDI);
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001271 Load32Disp(rs_rDX, offset_offset, tmpReg);
1272 OpLea(tmpReg, rs_rDI, tmpReg, 1, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001273
1274 // Now compute into EDI where the search will start.
1275 if (zero_based || rl_start.is_const) {
1276 if (start_value == 0) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001277 OpRegCopy(rs_rDI, tmpReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001278 } else {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001279 NewLIR3(kX86Lea32RM, rs_rDI.GetReg(), tmpReg.GetReg(), 2 * start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001280 }
1281 } else {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001282 if (is_index_on_stack == true) {
1283 // Load the start index from stack.
buzbee091cc402014-03-31 10:14:40 -07001284 NewLIR1(kX86Pop32R, rs_rDX.GetReg());
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001285 OpLea(rs_rDI, tmpReg, rs_rDX, 1, 0);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001286 } else {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001287 OpLea(rs_rDI, tmpReg, rl_start.reg, 1, 0);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001288 }
1289 }
1290
1291 // EDI now contains the start of the string to be searched.
1292 // We are all prepared to do the search for the character.
1293 NewLIR0(kX86RepneScasw);
1294
1295 // Did we find a match?
1296 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1297
1298 // yes, we matched. Compute the index of the result.
1299 // index = ((curr_ptr - orig_ptr) / 2) - 1.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001300 OpRegReg(kOpSub, rs_rDI, tmpReg);
buzbee2700f7e2014-03-07 09:46:20 -08001301 OpRegImm(kOpAsr, rs_rDI, 1);
buzbee091cc402014-03-31 10:14:40 -07001302 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_rDI.GetReg(), -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001303 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1304
1305 // Failed to match; return -1.
1306 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1307 length_compare->target = not_found;
1308 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001309 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001310
1311 // And join up at the end.
1312 all_done->target = NewLIR0(kPseudoTargetLabel);
1313 // Restore EDI from the stack.
buzbee091cc402014-03-31 10:14:40 -07001314 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001315
1316 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001317 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001318 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001319 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001320 }
1321
1322 StoreValue(rl_dest, rl_return);
1323 return true;
1324}
1325
Mark Mendellae9fd932014-02-10 16:14:35 -08001326/*
Mark Mendellae9fd932014-02-10 16:14:35 -08001327 * @brief Enter an 'advance LOC' into the FDE buffer
1328 * @param buf FDE buffer.
1329 * @param increment Amount by which to increase the current location.
1330 */
1331static void AdvanceLoc(std::vector<uint8_t>&buf, uint32_t increment) {
1332 if (increment < 64) {
1333 // Encoding in opcode.
1334 buf.push_back(0x1 << 6 | increment);
1335 } else if (increment < 256) {
1336 // Single byte delta.
1337 buf.push_back(0x02);
1338 buf.push_back(increment);
1339 } else if (increment < 256 * 256) {
1340 // Two byte delta.
1341 buf.push_back(0x03);
1342 buf.push_back(increment & 0xff);
1343 buf.push_back((increment >> 8) & 0xff);
1344 } else {
1345 // Four byte delta.
1346 buf.push_back(0x04);
1347 PushWord(buf, increment);
1348 }
1349}
1350
1351
1352std::vector<uint8_t>* X86CFIInitialization() {
1353 return X86Mir2Lir::ReturnCommonCallFrameInformation();
1354}
1355
1356std::vector<uint8_t>* X86Mir2Lir::ReturnCommonCallFrameInformation() {
1357 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
1358
1359 // Length of the CIE (except for this field).
1360 PushWord(*cfi_info, 16);
1361
1362 // CIE id.
1363 PushWord(*cfi_info, 0xFFFFFFFFU);
1364
1365 // Version: 3.
1366 cfi_info->push_back(0x03);
1367
1368 // Augmentation: empty string.
1369 cfi_info->push_back(0x0);
1370
1371 // Code alignment: 1.
1372 cfi_info->push_back(0x01);
1373
1374 // Data alignment: -4.
1375 cfi_info->push_back(0x7C);
1376
1377 // Return address register (R8).
1378 cfi_info->push_back(0x08);
1379
1380 // Initial return PC is 4(ESP): DW_CFA_def_cfa R4 4.
1381 cfi_info->push_back(0x0C);
1382 cfi_info->push_back(0x04);
1383 cfi_info->push_back(0x04);
1384
1385 // Return address location: 0(SP): DW_CFA_offset R8 1 (* -4);.
1386 cfi_info->push_back(0x2 << 6 | 0x08);
1387 cfi_info->push_back(0x01);
1388
1389 // And 2 Noops to align to 4 byte boundary.
1390 cfi_info->push_back(0x0);
1391 cfi_info->push_back(0x0);
1392
1393 DCHECK_EQ(cfi_info->size() & 3, 0U);
1394 return cfi_info;
1395}
1396
1397static void EncodeUnsignedLeb128(std::vector<uint8_t>& buf, uint32_t value) {
1398 uint8_t buffer[12];
1399 uint8_t *ptr = EncodeUnsignedLeb128(buffer, value);
1400 for (uint8_t *p = buffer; p < ptr; p++) {
1401 buf.push_back(*p);
1402 }
1403}
1404
1405std::vector<uint8_t>* X86Mir2Lir::ReturnCallFrameInformation() {
1406 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
1407
1408 // Generate the FDE for the method.
1409 DCHECK_NE(data_offset_, 0U);
1410
1411 // Length (will be filled in later in this routine).
1412 PushWord(*cfi_info, 0);
1413
1414 // CIE_pointer (can be filled in by linker); might be left at 0 if there is only
1415 // one CIE for the whole debug_frame section.
1416 PushWord(*cfi_info, 0);
1417
1418 // 'initial_location' (filled in by linker).
1419 PushWord(*cfi_info, 0);
1420
1421 // 'address_range' (number of bytes in the method).
1422 PushWord(*cfi_info, data_offset_);
1423
1424 // The instructions in the FDE.
1425 if (stack_decrement_ != nullptr) {
1426 // Advance LOC to just past the stack decrement.
1427 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
1428 AdvanceLoc(*cfi_info, pc);
1429
1430 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
1431 cfi_info->push_back(0x0e);
1432 EncodeUnsignedLeb128(*cfi_info, frame_size_);
1433
1434 // We continue with that stack until the epilogue.
1435 if (stack_increment_ != nullptr) {
1436 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
1437 AdvanceLoc(*cfi_info, new_pc - pc);
1438
1439 // We probably have code snippets after the epilogue, so save the
1440 // current state: DW_CFA_remember_state.
1441 cfi_info->push_back(0x0a);
1442
1443 // We have now popped the stack: DW_CFA_def_cfa_offset 4. There is only the return
1444 // PC on the stack now.
1445 cfi_info->push_back(0x0e);
1446 EncodeUnsignedLeb128(*cfi_info, 4);
1447
1448 // Everything after that is the same as before the epilogue.
1449 // Stack bump was followed by RET instruction.
1450 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1451 if (post_ret_insn != nullptr) {
1452 pc = new_pc;
1453 new_pc = post_ret_insn->offset;
1454 AdvanceLoc(*cfi_info, new_pc - pc);
1455 // Restore the state: DW_CFA_restore_state.
1456 cfi_info->push_back(0x0b);
1457 }
1458 }
1459 }
1460
1461 // Padding to a multiple of 4
1462 while ((cfi_info->size() & 3) != 0) {
1463 // DW_CFA_nop is encoded as 0.
1464 cfi_info->push_back(0);
1465 }
1466
1467 // Set the length of the FDE inside the generated bytes.
1468 uint32_t length = cfi_info->size() - 4;
1469 (*cfi_info)[0] = length;
1470 (*cfi_info)[1] = length >> 8;
1471 (*cfi_info)[2] = length >> 16;
1472 (*cfi_info)[3] = length >> 24;
1473 return cfi_info;
1474}
1475
Mark Mendelld65c51a2014-04-29 16:55:20 -04001476void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1477 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1478 case kMirOpConstVector:
1479 GenConst128(bb, mir);
1480 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001481 case kMirOpMoveVector:
1482 GenMoveVector(bb, mir);
1483 break;
1484 case kMirOpPackedMultiply:
1485 GenMultiplyVector(bb, mir);
1486 break;
1487 case kMirOpPackedAddition:
1488 GenAddVector(bb, mir);
1489 break;
1490 case kMirOpPackedSubtract:
1491 GenSubtractVector(bb, mir);
1492 break;
1493 case kMirOpPackedShiftLeft:
1494 GenShiftLeftVector(bb, mir);
1495 break;
1496 case kMirOpPackedSignedShiftRight:
1497 GenSignedShiftRightVector(bb, mir);
1498 break;
1499 case kMirOpPackedUnsignedShiftRight:
1500 GenUnsignedShiftRightVector(bb, mir);
1501 break;
1502 case kMirOpPackedAnd:
1503 GenAndVector(bb, mir);
1504 break;
1505 case kMirOpPackedOr:
1506 GenOrVector(bb, mir);
1507 break;
1508 case kMirOpPackedXor:
1509 GenXorVector(bb, mir);
1510 break;
1511 case kMirOpPackedAddReduce:
1512 GenAddReduceVector(bb, mir);
1513 break;
1514 case kMirOpPackedReduce:
1515 GenReduceVector(bb, mir);
1516 break;
1517 case kMirOpPackedSet:
1518 GenSetVector(bb, mir);
1519 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001520 default:
1521 break;
1522 }
1523}
1524
1525void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
1526 int type_size = mir->dalvikInsn.vA;
1527 // We support 128 bit vectors.
1528 DCHECK_EQ(type_size & 0xFFFF, 128);
Mark Mendellfe945782014-05-22 09:52:36 -04001529 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001530 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001531 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001532 // Check for all 0 case.
1533 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1534 NewLIR2(kX86XorpsRR, reg, reg);
1535 return;
1536 }
1537 // Okay, load it from the constant vector area.
1538 LIR *data_target = ScanVectorLiteral(mir);
1539 if (data_target == nullptr) {
1540 data_target = AddVectorLiteral(mir);
1541 }
1542
1543 // Address the start of the method.
1544 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001545 if (rl_method.wide) {
1546 rl_method = LoadValueWide(rl_method, kCoreReg);
1547 } else {
1548 rl_method = LoadValue(rl_method, kCoreReg);
1549 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001550
1551 // Load the proper value from the literal area.
1552 // We don't know the proper offset for the value, so pick one that will force
1553 // 4 byte offset. We will fix this up in the assembler later to have the right
1554 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001555 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001556 LIR *load = NewLIR3(kX86Mova128RM, reg, rl_method.reg.GetReg(), 256 /* bogus */);
1557 load->flags.fixup = kFixupLoad;
1558 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001559}
1560
Mark Mendellfe945782014-05-22 09:52:36 -04001561void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1562 // We only support 128 bit registers.
1563 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1564 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vB);
1565 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vC);
1566 NewLIR2(kX86Mova128RR, rs_dest.GetReg(), rs_src.GetReg());
1567}
1568
1569void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
1570 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1571 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1572 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1573 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1574 int opcode = 0;
1575 switch (opsize) {
1576 case k32:
1577 opcode = kX86PmulldRR;
1578 break;
1579 case kSignedHalf:
1580 opcode = kX86PmullwRR;
1581 break;
1582 case kSingle:
1583 opcode = kX86MulpsRR;
1584 break;
1585 case kDouble:
1586 opcode = kX86MulpdRR;
1587 break;
1588 default:
1589 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1590 break;
1591 }
1592 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1593}
1594
1595void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
1596 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1597 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1598 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1599 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1600 int opcode = 0;
1601 switch (opsize) {
1602 case k32:
1603 opcode = kX86PadddRR;
1604 break;
1605 case kSignedHalf:
1606 case kUnsignedHalf:
1607 opcode = kX86PaddwRR;
1608 break;
1609 case kUnsignedByte:
1610 case kSignedByte:
1611 opcode = kX86PaddbRR;
1612 break;
1613 case kSingle:
1614 opcode = kX86AddpsRR;
1615 break;
1616 case kDouble:
1617 opcode = kX86AddpdRR;
1618 break;
1619 default:
1620 LOG(FATAL) << "Unsupported vector addition " << opsize;
1621 break;
1622 }
1623 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1624}
1625
1626void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
1627 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1628 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1629 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1630 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1631 int opcode = 0;
1632 switch (opsize) {
1633 case k32:
1634 opcode = kX86PsubdRR;
1635 break;
1636 case kSignedHalf:
1637 case kUnsignedHalf:
1638 opcode = kX86PsubwRR;
1639 break;
1640 case kUnsignedByte:
1641 case kSignedByte:
1642 opcode = kX86PsubbRR;
1643 break;
1644 case kSingle:
1645 opcode = kX86SubpsRR;
1646 break;
1647 case kDouble:
1648 opcode = kX86SubpdRR;
1649 break;
1650 default:
1651 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1652 break;
1653 }
1654 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1655}
1656
1657void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
1658 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1659 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1660 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1661 int imm = mir->dalvikInsn.vC;
1662 int opcode = 0;
1663 switch (opsize) {
1664 case k32:
1665 opcode = kX86PslldRI;
1666 break;
1667 case k64:
1668 opcode = kX86PsllqRI;
1669 break;
1670 case kSignedHalf:
1671 case kUnsignedHalf:
1672 opcode = kX86PsllwRI;
1673 break;
1674 default:
1675 LOG(FATAL) << "Unsupported vector shift left " << opsize;
1676 break;
1677 }
1678 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1679}
1680
1681void X86Mir2Lir::GenSignedShiftRightVector(BasicBlock *bb, MIR *mir) {
1682 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1683 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1684 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1685 int imm = mir->dalvikInsn.vC;
1686 int opcode = 0;
1687 switch (opsize) {
1688 case k32:
1689 opcode = kX86PsradRI;
1690 break;
1691 case kSignedHalf:
1692 case kUnsignedHalf:
1693 opcode = kX86PsrawRI;
1694 break;
1695 default:
1696 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
1697 break;
1698 }
1699 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1700}
1701
1702void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
1703 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1704 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1705 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1706 int imm = mir->dalvikInsn.vC;
1707 int opcode = 0;
1708 switch (opsize) {
1709 case k32:
1710 opcode = kX86PsrldRI;
1711 break;
1712 case k64:
1713 opcode = kX86PsrlqRI;
1714 break;
1715 case kSignedHalf:
1716 case kUnsignedHalf:
1717 opcode = kX86PsrlwRI;
1718 break;
1719 default:
1720 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1721 break;
1722 }
1723 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1724}
1725
1726void X86Mir2Lir::GenAndVector(BasicBlock *bb, MIR *mir) {
1727 // We only support 128 bit registers.
1728 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1729 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1730 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1731 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1732}
1733
1734void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
1735 // We only support 128 bit registers.
1736 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1737 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1738 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1739 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1740}
1741
1742void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
1743 // We only support 128 bit registers.
1744 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1745 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1746 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1747 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1748}
1749
1750void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
1751 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1752 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1753 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1754 int imm = mir->dalvikInsn.vC;
1755 int opcode = 0;
1756 switch (opsize) {
1757 case k32:
1758 opcode = kX86PhadddRR;
1759 break;
1760 case kSignedHalf:
1761 case kUnsignedHalf:
1762 opcode = kX86PhaddwRR;
1763 break;
1764 default:
1765 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
1766 break;
1767 }
1768 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1769}
1770
1771void X86Mir2Lir::GenReduceVector(BasicBlock *bb, MIR *mir) {
1772 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1773 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1774 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
1775 int index = mir->dalvikInsn.arg[0];
1776 int opcode = 0;
1777 switch (opsize) {
1778 case k32:
1779 opcode = kX86PextrdRRI;
1780 break;
1781 case kSignedHalf:
1782 case kUnsignedHalf:
1783 opcode = kX86PextrwRRI;
1784 break;
1785 case kUnsignedByte:
1786 case kSignedByte:
1787 opcode = kX86PextrbRRI;
1788 break;
1789 default:
1790 LOG(FATAL) << "Unsupported vector reduce " << opsize;
1791 break;
1792 }
1793 // We need to extract to a GPR.
1794 RegStorage temp = AllocTemp();
1795 NewLIR3(opcode, temp.GetReg(), rs_src.GetReg(), index);
1796
1797 // Assume that the destination VR is in the def for the mir.
1798 RegLocation rl_dest = mir_graph_->GetDest(mir);
1799 RegLocation rl_temp =
1800 {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, temp, INVALID_SREG, INVALID_SREG};
1801 StoreValue(rl_dest, rl_temp);
1802}
1803
1804void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
1805 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1806 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1807 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vB);
1808 int op_low = 0, op_high = 0;
1809 switch (opsize) {
1810 case k32:
1811 op_low = kX86PshufdRRI;
1812 break;
1813 case kSignedHalf:
1814 case kUnsignedHalf:
1815 // Handles low quadword.
1816 op_low = kX86PshuflwRRI;
1817 // Handles upper quadword.
1818 op_high = kX86PshufdRRI;
1819 break;
1820 default:
1821 LOG(FATAL) << "Unsupported vector set " << opsize;
1822 break;
1823 }
1824
1825 // Load the value from the VR into a GPR.
1826 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
1827 rl_src = LoadValue(rl_src, kCoreReg);
1828
1829 // Load the value into the XMM register.
1830 NewLIR2(kX86MovdxrRR, rs_dest.GetReg(), rl_src.reg.GetReg());
1831
1832 // Now shuffle the value across the destination.
1833 NewLIR3(op_low, rs_dest.GetReg(), rs_dest.GetReg(), 0);
1834
1835 // And then repeat as needed.
1836 if (op_high != 0) {
1837 NewLIR3(op_high, rs_dest.GetReg(), rs_dest.GetReg(), 0);
1838 }
1839}
1840
1841
Mark Mendelld65c51a2014-04-29 16:55:20 -04001842LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
1843 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
1844 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1845 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
1846 args[2] == p->operands[2] && args[3] == p->operands[3]) {
1847 return p;
1848 }
1849 }
1850 return nullptr;
1851}
1852
1853LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
1854 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
1855 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
1856 new_value->operands[0] = args[0];
1857 new_value->operands[1] = args[1];
1858 new_value->operands[2] = args[2];
1859 new_value->operands[3] = args[3];
1860 new_value->next = const_vectors_;
1861 if (const_vectors_ == nullptr) {
1862 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
1863 }
1864 estimated_native_code_size_ += 16; // Space for one vector.
1865 const_vectors_ = new_value;
1866 return new_value;
1867}
1868
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001869// ------------ ABI support: mapping of args to physical registers -------------
Serguei Katkov407a9d22014-07-05 03:09:32 +07001870RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide, bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001871 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
1872 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) / sizeof(SpecialTargetRegister);
1873 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
1874 kFArg4, kFArg5, kFArg6, kFArg7};
1875 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) / sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001876
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001877 if (is_double_or_float) {
1878 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001879 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001880 }
1881 } else {
1882 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07001883 return is_ref ? ml_->TargetRefReg(coreArgMappingToPhysicalReg[cur_core_reg_++]) :
1884 ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++], is_wide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001885 }
1886 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001887 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001888}
1889
1890RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
1891 DCHECK(IsInitialized());
1892 auto res = mapping_.find(in_position);
1893 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
1894}
1895
1896void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count, InToRegStorageMapper* mapper) {
1897 DCHECK(mapper != nullptr);
1898 max_mapped_in_ = -1;
1899 is_there_stack_mapped_ = false;
1900 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07001901 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
1902 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001903 if (reg.Valid()) {
1904 mapping_[in_position] = reg;
1905 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07001906 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001907 // We covered 2 args, so skip the next one
1908 in_position++;
1909 }
1910 } else {
1911 is_there_stack_mapped_ = true;
1912 }
1913 }
1914 initialized_ = true;
1915}
1916
1917RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001918 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001919 return GetCoreArgMappingToPhysicalReg(arg_num);
1920 }
1921
1922 if (!in_to_reg_storage_mapping_.IsInitialized()) {
1923 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
1924 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
1925
Chao-ying Fua77ee512014-07-01 17:43:41 -07001926 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001927 in_to_reg_storage_mapping_.Initialize(arg_locs, cu_->num_ins, &mapper);
1928 }
1929 return in_to_reg_storage_mapping_.Get(arg_num);
1930}
1931
1932RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
1933 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
1934 // Not used for 64-bit, TODO: Move X86_32 to the same framework
1935 switch (core_arg_num) {
1936 case 0:
1937 return rs_rX86_ARG1;
1938 case 1:
1939 return rs_rX86_ARG2;
1940 case 2:
1941 return rs_rX86_ARG3;
1942 default:
1943 return RegStorage::InvalidReg();
1944 }
1945}
1946
1947// ---------End of ABI support: mapping of args to physical registers -------------
1948
1949/*
1950 * If there are any ins passed in registers that have not been promoted
1951 * to a callee-save register, flush them to the frame. Perform initial
1952 * assignment of promoted arguments.
1953 *
1954 * ArgLocs is an array of location records describing the incoming arguments
1955 * with one location record per word of argument.
1956 */
1957void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001958 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001959 /*
1960 * Dummy up a RegLocation for the incoming Method*
1961 * It will attempt to keep kArg0 live (or copy it to home location
1962 * if promoted).
1963 */
1964
1965 RegLocation rl_src = rl_method;
1966 rl_src.location = kLocPhysReg;
Chao-ying Fua77ee512014-07-01 17:43:41 -07001967 rl_src.reg = TargetRefReg(kArg0);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001968 rl_src.home = false;
1969 MarkLive(rl_src);
1970 StoreValue(rl_method, rl_src);
1971 // If Method* has been promoted, explicitly flush
1972 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001973 StoreRefDisp(rs_rX86_SP, 0, As32BitReg(TargetRefReg(kArg0)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001974 }
1975
1976 if (cu_->num_ins == 0) {
1977 return;
1978 }
1979
1980 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
1981 /*
1982 * Copy incoming arguments to their proper home locations.
1983 * NOTE: an older version of dx had an issue in which
1984 * it would reuse static method argument registers.
1985 * This could result in the same Dalvik virtual register
1986 * being promoted to both core and fp regs. To account for this,
1987 * we only copy to the corresponding promoted physical register
1988 * if it matches the type of the SSA name for the incoming
1989 * argument. It is also possible that long and double arguments
1990 * end up half-promoted. In those cases, we must flush the promoted
1991 * half to memory as well.
1992 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001993 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001994 for (int i = 0; i < cu_->num_ins; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001995 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001996 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001997
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001998 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001999 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002000 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002001
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002002 // We have already updated the arg location with promoted info
2003 // so we can be based on it.
2004 if (t_loc->location == kLocPhysReg) {
2005 // Just copy it.
2006 OpRegCopy(t_loc->reg, reg);
2007 } else {
2008 // Needs flush.
2009 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002010 StoreRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002011 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002012 StoreBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002013 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002014 }
2015 }
2016 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002017 // If arriving in frame & promoted.
2018 if (t_loc->location == kLocPhysReg) {
2019 if (t_loc->ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002020 LoadRefDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002021 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002022 LoadBaseDisp(rs_rX86_SP, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002023 t_loc->wide ? k64 : k32, kNotVolatile);
2024 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002025 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002026 }
2027 if (t_loc->wide) {
2028 // Increment i to skip the next one.
2029 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002030 }
2031 }
2032}
2033
2034/*
2035 * Load up to 5 arguments, the first three of which will be in
2036 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2037 * and as part of the load sequence, it must be replaced with
2038 * the target method pointer. Note, this may also be called
2039 * for "range" variants if the number of arguments is 5 or fewer.
2040 */
2041int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2042 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2043 const MethodReference& target_method,
2044 uint32_t vtable_idx, uintptr_t direct_code,
2045 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002046 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002047 return Mir2Lir::GenDalvikArgsNoRange(info,
2048 call_state, pcrLabel, next_call_insn,
2049 target_method,
2050 vtable_idx, direct_code,
2051 direct_method, type, skip_this);
2052 }
2053 return GenDalvikArgsRange(info,
2054 call_state, pcrLabel, next_call_insn,
2055 target_method,
2056 vtable_idx, direct_code,
2057 direct_method, type, skip_this);
2058}
2059
2060/*
2061 * May have 0+ arguments (also used for jumbo). Note that
2062 * source virtual registers may be in physical registers, so may
2063 * need to be flushed to home location before copying. This
2064 * applies to arg3 and above (see below).
2065 *
2066 * Two general strategies:
2067 * If < 20 arguments
2068 * Pass args 3-18 using vldm/vstm block copy
2069 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2070 * If 20+ arguments
2071 * Pass args arg19+ using memcpy block copy
2072 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2073 *
2074 */
2075int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2076 LIR** pcrLabel, NextCallInsn next_call_insn,
2077 const MethodReference& target_method,
2078 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2079 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002080 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002081 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2082 pcrLabel, next_call_insn,
2083 target_method,
2084 vtable_idx, direct_code, direct_method,
2085 type, skip_this);
2086 }
2087
2088 /* If no arguments, just return */
2089 if (info->num_arg_words == 0)
2090 return call_state;
2091
2092 const int start_index = skip_this ? 1 : 0;
2093
Chao-ying Fua77ee512014-07-01 17:43:41 -07002094 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002095 InToRegStorageMapping in_to_reg_storage_mapping;
2096 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2097 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2098 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
2099 in_to_reg_storage_mapping.Get(last_mapped_in).Is64BitSolo() ? 2 : 1;
2100 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2101
2102 // Fisrt of all, check whether it make sense to use bulk copying
2103 // Optimization is aplicable only for range case
2104 // TODO: make a constant instead of 2
2105 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2106 // Scan the rest of the args - if in phys_reg flush to memory
2107 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2108 RegLocation loc = info->args[next_arg];
2109 if (loc.wide) {
2110 loc = UpdateLocWide(loc);
2111 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002112 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002113 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002114 }
2115 next_arg += 2;
2116 } else {
2117 loc = UpdateLoc(loc);
2118 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002119 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002120 StoreBaseDisp(rs_rX86_SP, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002121 }
2122 next_arg++;
2123 }
2124 }
2125
2126 // Logic below assumes that Method pointer is at offset zero from SP.
2127 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
2128
2129 // The rest can be copied together
2130 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
2131 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped, cu_->instruction_set);
2132
2133 int current_src_offset = start_offset;
2134 int current_dest_offset = outs_offset;
2135
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002136 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2137 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002138 while (regs_left_to_pass_via_stack > 0) {
2139 // This is based on the knowledge that the stack itself is 16-byte aligned.
2140 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2141 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2142 size_t bytes_to_move;
2143
2144 /*
2145 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2146 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2147 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2148 * We do this because we could potentially do a smaller move to align.
2149 */
2150 if (regs_left_to_pass_via_stack == 4 ||
2151 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2152 // Moving 128-bits via xmm register.
2153 bytes_to_move = sizeof(uint32_t) * 4;
2154
2155 // Allocate a free xmm temp. Since we are working through the calling sequence,
2156 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2157 // there are no free registers.
2158 RegStorage temp = AllocTempDouble();
2159
2160 LIR* ld1 = nullptr;
2161 LIR* ld2 = nullptr;
2162 LIR* st1 = nullptr;
2163 LIR* st2 = nullptr;
2164
2165 /*
2166 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2167 * do an aligned move. If we have 8-byte alignment, then do the move in two
2168 * parts. This approach prevents possible cache line splits. Finally, fall back
2169 * to doing an unaligned move. In most cases we likely won't split the cache
2170 * line but we cannot prove it and thus take a conservative approach.
2171 */
2172 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2173 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2174
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002175 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002176 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002177 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002178 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002179 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovLo128FP);
2180 ld2 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002181 kMovHi128FP);
2182 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002183 ld1 = OpMovRegMem(temp, rs_rX86_SP, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002184 }
2185
2186 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002187 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002188 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002189 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovLo128FP);
2190 st2 = OpMovMemReg(rs_rX86_SP, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002191 temp, kMovHi128FP);
2192 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002193 st1 = OpMovMemReg(rs_rX86_SP, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002194 }
2195
2196 // TODO If we could keep track of aliasing information for memory accesses that are wider
2197 // than 64-bit, we wouldn't need to set up a barrier.
2198 if (ld1 != nullptr) {
2199 if (ld2 != nullptr) {
2200 // For 64-bit load we can actually set up the aliasing information.
2201 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2202 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2203 } else {
2204 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002205 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002206 }
2207 }
2208 if (st1 != nullptr) {
2209 if (st2 != nullptr) {
2210 // For 64-bit store we can actually set up the aliasing information.
2211 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2212 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2213 } else {
2214 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002215 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002216 }
2217 }
2218
2219 // Free the temporary used for the data movement.
2220 FreeTemp(temp);
2221 } else {
2222 // Moving 32-bits via general purpose register.
2223 bytes_to_move = sizeof(uint32_t);
2224
2225 // Instead of allocating a new temp, simply reuse one of the registers being used
2226 // for argument passing.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002227 RegStorage temp = TargetReg(kArg3, false);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002228
2229 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07002230 Load32Disp(rs_rX86_SP, current_src_offset, temp);
2231 Store32Disp(rs_rX86_SP, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002232 }
2233
2234 current_src_offset += bytes_to_move;
2235 current_dest_offset += bytes_to_move;
2236 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2237 }
2238 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2239 }
2240
2241 // Now handle rest not registers if they are
2242 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002243 RegStorage regSingle = TargetReg(kArg2, false);
2244 RegStorage regWide = TargetReg(kArg3, true);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002245 for (int i = start_index;
2246 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002247 RegLocation rl_arg = info->args[i];
2248 rl_arg = UpdateRawLoc(rl_arg);
2249 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2250 if (!reg.Valid()) {
2251 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2252
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002253 {
2254 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2255 if (rl_arg.wide) {
2256 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002257 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002258 } else {
2259 LoadValueDirectWideFixed(rl_arg, regWide);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002260 StoreBaseDisp(rs_rX86_SP, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002261 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002262 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002263 if (rl_arg.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002264 StoreBaseDisp(rs_rX86_SP, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002265 } else {
2266 LoadValueDirectFixed(rl_arg, regSingle);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002267 StoreBaseDisp(rs_rX86_SP, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002268 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002269 }
2270 }
2271 call_state = next_call_insn(cu_, info, call_state, target_method,
2272 vtable_idx, direct_code, direct_method, type);
2273 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002274 if (rl_arg.wide) {
2275 i++;
2276 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002277 }
2278 }
2279
2280 // Finish with mapped registers
2281 for (int i = start_index; i <= last_mapped_in; i++) {
2282 RegLocation rl_arg = info->args[i];
2283 rl_arg = UpdateRawLoc(rl_arg);
2284 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2285 if (reg.Valid()) {
2286 if (rl_arg.wide) {
2287 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002288 } else {
2289 LoadValueDirectFixed(rl_arg, reg);
2290 }
2291 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2292 direct_code, direct_method, type);
2293 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002294 if (rl_arg.wide) {
2295 i++;
2296 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002297 }
2298
2299 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2300 direct_code, direct_method, type);
2301 if (pcrLabel) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002302 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002303 *pcrLabel = GenExplicitNullCheck(TargetRefReg(kArg1), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002304 } else {
2305 *pcrLabel = nullptr;
2306 // In lieu of generating a check for kArg1 being null, we need to
2307 // perform a load when doing implicit checks.
2308 RegStorage tmp = AllocTemp();
Chao-ying Fua77ee512014-07-01 17:43:41 -07002309 Load32Disp(TargetRefReg(kArg1), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002310 MarkPossibleNullPointerException(info->opt_flags);
2311 FreeTemp(tmp);
2312 }
2313 }
2314 return call_state;
2315}
2316
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002317} // namespace art