blob: 64b4af86a2bc4deeb8d0164be3e5743aa3bf934c [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"
Mark Mendelle19c91f2014-02-25 08:19:08 -080023#include "mirror/array.h"
24#include "mirror/string.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "x86_lir.h"
26
Brian Carlstrom7940e442013-07-12 13:46:57 -070027namespace art {
28
Vladimir Marko089142c2014-06-05 10:57:05 +010029static constexpr RegStorage core_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070030 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
31};
Vladimir Marko089142c2014-06-05 10:57:05 +010032static constexpr RegStorage core_regs_arr_64[] = {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070033 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
buzbee091cc402014-03-31 10:14:40 -070034 rs_r8, rs_r9, rs_r10, rs_r11, rs_r12, rs_r13, rs_r14, rs_r15
Brian Carlstrom7940e442013-07-12 13:46:57 -070035};
Vladimir Marko089142c2014-06-05 10:57:05 +010036static constexpr RegStorage core_regs_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070037 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 +070038 rs_r8q, rs_r9q, rs_r10q, rs_r11q, rs_r12q, rs_r13q, rs_r14q, rs_r15q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070039};
Vladimir Marko089142c2014-06-05 10:57:05 +010040static constexpr RegStorage sp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070041 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
42};
Vladimir Marko089142c2014-06-05 10:57:05 +010043static constexpr RegStorage sp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070044 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
buzbee091cc402014-03-31 10:14:40 -070045 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070046};
Vladimir Marko089142c2014-06-05 10:57:05 +010047static constexpr RegStorage dp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070048 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
49};
Vladimir Marko089142c2014-06-05 10:57:05 +010050static constexpr RegStorage dp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070051 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
buzbee091cc402014-03-31 10:14:40 -070052 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070053};
Vladimir Marko089142c2014-06-05 10:57:05 +010054static constexpr RegStorage reserved_regs_arr_32[] = {rs_rX86_SP_32};
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070055static constexpr RegStorage reserved_regs_arr_64[] = {rs_rX86_SP_32};
Vladimir Marko089142c2014-06-05 10:57:05 +010056static constexpr RegStorage reserved_regs_arr_64q[] = {rs_rX86_SP_64};
57static constexpr RegStorage core_temps_arr_32[] = {rs_rAX, rs_rCX, rs_rDX, rs_rBX};
58static constexpr RegStorage core_temps_arr_64[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070059 rs_rAX, rs_rCX, rs_rDX, rs_rSI, rs_rDI,
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070060 rs_r8, rs_r9, rs_r10, rs_r11
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070061};
Vladimir Marko089142c2014-06-05 10:57:05 +010062static constexpr RegStorage core_temps_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070063 rs_r0q, rs_r1q, rs_r2q, rs_r6q, rs_r7q,
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070064 rs_r8q, rs_r9q, rs_r10q, rs_r11q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070065};
Vladimir Marko089142c2014-06-05 10:57:05 +010066static constexpr RegStorage sp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070067 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
68};
Vladimir Marko089142c2014-06-05 10:57:05 +010069static constexpr RegStorage sp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070070 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
buzbee091cc402014-03-31 10:14:40 -070071 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
buzbee091cc402014-03-31 10:14:40 -070072};
Vladimir Marko089142c2014-06-05 10:57:05 +010073static constexpr RegStorage dp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070074 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
75};
Vladimir Marko089142c2014-06-05 10:57:05 +010076static constexpr RegStorage dp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070077 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
buzbee091cc402014-03-31 10:14:40 -070078 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
buzbee091cc402014-03-31 10:14:40 -070079};
80
Vladimir Marko089142c2014-06-05 10:57:05 +010081static constexpr RegStorage xp_temps_arr_32[] = {
Mark Mendellfe945782014-05-22 09:52:36 -040082 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
83};
Vladimir Marko089142c2014-06-05 10:57:05 +010084static constexpr RegStorage xp_temps_arr_64[] = {
Mark Mendellfe945782014-05-22 09:52:36 -040085 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
Mark Mendellfe945782014-05-22 09:52:36 -040086 rs_xr8, rs_xr9, rs_xr10, rs_xr11, rs_xr12, rs_xr13, rs_xr14, rs_xr15
Mark Mendellfe945782014-05-22 09:52:36 -040087};
88
Vladimir Marko089142c2014-06-05 10:57:05 +010089static constexpr ArrayRef<const RegStorage> empty_pool;
90static constexpr ArrayRef<const RegStorage> core_regs_32(core_regs_arr_32);
91static constexpr ArrayRef<const RegStorage> core_regs_64(core_regs_arr_64);
92static constexpr ArrayRef<const RegStorage> core_regs_64q(core_regs_arr_64q);
93static constexpr ArrayRef<const RegStorage> sp_regs_32(sp_regs_arr_32);
94static constexpr ArrayRef<const RegStorage> sp_regs_64(sp_regs_arr_64);
95static constexpr ArrayRef<const RegStorage> dp_regs_32(dp_regs_arr_32);
96static constexpr ArrayRef<const RegStorage> dp_regs_64(dp_regs_arr_64);
97static constexpr ArrayRef<const RegStorage> reserved_regs_32(reserved_regs_arr_32);
98static constexpr ArrayRef<const RegStorage> reserved_regs_64(reserved_regs_arr_64);
99static constexpr ArrayRef<const RegStorage> reserved_regs_64q(reserved_regs_arr_64q);
100static constexpr ArrayRef<const RegStorage> core_temps_32(core_temps_arr_32);
101static constexpr ArrayRef<const RegStorage> core_temps_64(core_temps_arr_64);
102static constexpr ArrayRef<const RegStorage> core_temps_64q(core_temps_arr_64q);
103static constexpr ArrayRef<const RegStorage> sp_temps_32(sp_temps_arr_32);
104static constexpr ArrayRef<const RegStorage> sp_temps_64(sp_temps_arr_64);
105static constexpr ArrayRef<const RegStorage> dp_temps_32(dp_temps_arr_32);
106static constexpr ArrayRef<const RegStorage> dp_temps_64(dp_temps_arr_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700107
Vladimir Marko089142c2014-06-05 10:57:05 +0100108static constexpr ArrayRef<const RegStorage> xp_temps_32(xp_temps_arr_32);
109static constexpr ArrayRef<const RegStorage> xp_temps_64(xp_temps_arr_64);
Mark Mendellfe945782014-05-22 09:52:36 -0400110
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700111RegStorage rs_rX86_SP;
112
113X86NativeRegisterPool rX86_ARG0;
114X86NativeRegisterPool rX86_ARG1;
115X86NativeRegisterPool rX86_ARG2;
116X86NativeRegisterPool rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700117X86NativeRegisterPool rX86_ARG4;
118X86NativeRegisterPool rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700119X86NativeRegisterPool rX86_FARG0;
120X86NativeRegisterPool rX86_FARG1;
121X86NativeRegisterPool rX86_FARG2;
122X86NativeRegisterPool rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700123X86NativeRegisterPool rX86_FARG4;
124X86NativeRegisterPool rX86_FARG5;
125X86NativeRegisterPool rX86_FARG6;
126X86NativeRegisterPool rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700127X86NativeRegisterPool rX86_RET0;
128X86NativeRegisterPool rX86_RET1;
129X86NativeRegisterPool rX86_INVOKE_TGT;
130X86NativeRegisterPool rX86_COUNT;
131
132RegStorage rs_rX86_ARG0;
133RegStorage rs_rX86_ARG1;
134RegStorage rs_rX86_ARG2;
135RegStorage rs_rX86_ARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700136RegStorage rs_rX86_ARG4;
137RegStorage rs_rX86_ARG5;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700138RegStorage rs_rX86_FARG0;
139RegStorage rs_rX86_FARG1;
140RegStorage rs_rX86_FARG2;
141RegStorage rs_rX86_FARG3;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700142RegStorage rs_rX86_FARG4;
143RegStorage rs_rX86_FARG5;
144RegStorage rs_rX86_FARG6;
145RegStorage rs_rX86_FARG7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700146RegStorage rs_rX86_RET0;
147RegStorage rs_rX86_RET1;
148RegStorage rs_rX86_INVOKE_TGT;
149RegStorage rs_rX86_COUNT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700151RegLocation X86Mir2Lir::LocCReturn() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000152 return x86_loc_c_return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153}
154
buzbeea0cd2d72014-06-01 09:33:49 -0700155RegLocation X86Mir2Lir::LocCReturnRef() {
156 // FIXME: return x86_loc_c_return_wide for x86_64 when wide refs supported.
157 return x86_loc_c_return;
158}
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
172// Return a target-dependent special register.
buzbee2700f7e2014-03-07 09:46:20 -0800173RegStorage X86Mir2Lir::TargetReg(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
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206/*
207 * Decode the register id.
208 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100209ResourceMask X86Mir2Lir::GetRegMaskCommon(const RegStorage& reg) const {
210 /* Double registers in x86 are just a single FP register. This is always just a single bit. */
211 return ResourceMask::Bit(
212 /* FP register starts at bit position 16 */
213 ((reg.IsFloat() || reg.StorageSize() > 8) ? kX86FPReg0 : 0) + reg.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214}
215
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100216ResourceMask X86Mir2Lir::GetPCUseDefEncoding() const {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 /*
218 * FIXME: might make sense to use a virtual resource encoding bit for pc. Might be
219 * able to clean up some of the x86/Arm_Mips differences
220 */
221 LOG(FATAL) << "Unexpected call to GetPCUseDefEncoding for x86";
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100222 return kEncodeNone;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223}
224
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100225void X86Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags,
226 ResourceMask* use_mask, ResourceMask* def_mask) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700227 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbeeb48819d2013-09-14 16:15:25 -0700228 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229
230 // X86-specific resource map setup here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 if (flags & REG_USE_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100232 use_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 }
234
235 if (flags & REG_DEF_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100236 def_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 }
238
239 if (flags & REG_DEFA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100240 SetupRegMask(def_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 }
242
243 if (flags & REG_DEFD) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100244 SetupRegMask(def_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 }
246 if (flags & REG_USEA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100247 SetupRegMask(use_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 }
249
250 if (flags & REG_USEC) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100251 SetupRegMask(use_mask, rs_rCX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 }
253
254 if (flags & REG_USED) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100255 SetupRegMask(use_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 }
Vladimir Marko70b797d2013-12-03 15:25:24 +0000257
258 if (flags & REG_USEB) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100259 SetupRegMask(use_mask, rs_rBX.GetReg());
Vladimir Marko70b797d2013-12-03 15:25:24 +0000260 }
Mark Mendell4028a6c2014-02-19 20:06:20 -0800261
262 // Fixup hard to describe instruction: Uses rAX, rCX, rDI; sets rDI.
263 if (lir->opcode == kX86RepneScasw) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100264 SetupRegMask(use_mask, rs_rAX.GetReg());
265 SetupRegMask(use_mask, rs_rCX.GetReg());
266 SetupRegMask(use_mask, rs_rDI.GetReg());
267 SetupRegMask(def_mask, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800268 }
Serguei Katkove90501d2014-03-12 15:56:54 +0700269
270 if (flags & USE_FP_STACK) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100271 use_mask->SetBit(kX86FPStack);
272 def_mask->SetBit(kX86FPStack);
Serguei Katkove90501d2014-03-12 15:56:54 +0700273 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274}
275
276/* For dumping instructions */
277static const char* x86RegName[] = {
278 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
279 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
280};
281
282static const char* x86CondName[] = {
283 "O",
284 "NO",
285 "B/NAE/C",
286 "NB/AE/NC",
287 "Z/EQ",
288 "NZ/NE",
289 "BE/NA",
290 "NBE/A",
291 "S",
292 "NS",
293 "P/PE",
294 "NP/PO",
295 "L/NGE",
296 "NL/GE",
297 "LE/NG",
298 "NLE/G"
299};
300
301/*
302 * Interpret a format string and build a string no longer than size
303 * See format key in Assemble.cc.
304 */
305std::string X86Mir2Lir::BuildInsnString(const char *fmt, LIR *lir, unsigned char* base_addr) {
306 std::string buf;
307 size_t i = 0;
308 size_t fmt_len = strlen(fmt);
309 while (i < fmt_len) {
310 if (fmt[i] != '!') {
311 buf += fmt[i];
312 i++;
313 } else {
314 i++;
315 DCHECK_LT(i, fmt_len);
316 char operand_number_ch = fmt[i];
317 i++;
318 if (operand_number_ch == '!') {
319 buf += "!";
320 } else {
321 int operand_number = operand_number_ch - '0';
322 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
323 DCHECK_LT(i, fmt_len);
324 int operand = lir->operands[operand_number];
325 switch (fmt[i]) {
326 case 'c':
327 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
328 buf += x86CondName[operand];
329 break;
330 case 'd':
331 buf += StringPrintf("%d", operand);
332 break;
333 case 'p': {
buzbee0d829482013-10-11 15:24:55 -0700334 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(operand));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 buf += StringPrintf("0x%08x", tab_rec->offset);
336 break;
337 }
338 case 'r':
buzbee091cc402014-03-31 10:14:40 -0700339 if (RegStorage::IsFloat(operand)) {
340 int fp_reg = RegStorage::RegNum(operand);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 buf += StringPrintf("xmm%d", fp_reg);
342 } else {
buzbee091cc402014-03-31 10:14:40 -0700343 int reg_num = RegStorage::RegNum(operand);
344 DCHECK_LT(static_cast<size_t>(reg_num), sizeof(x86RegName));
345 buf += x86RegName[reg_num];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 }
347 break;
348 case 't':
Ian Rogers107c31e2014-01-23 20:55:29 -0800349 buf += StringPrintf("0x%08" PRIxPTR " (L%p)",
350 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + operand,
351 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 break;
353 default:
354 buf += StringPrintf("DecodeError '%c'", fmt[i]);
355 break;
356 }
357 i++;
358 }
359 }
360 }
361 return buf;
362}
363
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100364void X86Mir2Lir::DumpResourceMask(LIR *x86LIR, const ResourceMask& mask, const char *prefix) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 char buf[256];
366 buf[0] = 0;
367
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100368 if (mask.Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 strcpy(buf, "all");
370 } else {
371 char num[8];
372 int i;
373
374 for (i = 0; i < kX86RegEnd; i++) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100375 if (mask.HasBit(i)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800376 snprintf(num, arraysize(num), "%d ", i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 strcat(buf, num);
378 }
379 }
380
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100381 if (mask.HasBit(ResourceMask::kCCode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 strcat(buf, "cc ");
383 }
384 /* Memory bits */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100385 if (x86LIR && (mask.HasBit(ResourceMask::kDalvikReg))) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800386 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
387 DECODE_ALIAS_INFO_REG(x86LIR->flags.alias_info),
388 (DECODE_ALIAS_INFO_WIDE(x86LIR->flags.alias_info)) ? "(+1)" : "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100390 if (mask.HasBit(ResourceMask::kLiteral)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 strcat(buf, "lit ");
392 }
393
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100394 if (mask.HasBit(ResourceMask::kHeapRef)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 strcat(buf, "heap ");
396 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100397 if (mask.HasBit(ResourceMask::kMustNotAlias)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 strcat(buf, "noalias ");
399 }
400 }
401 if (buf[0]) {
402 LOG(INFO) << prefix << ": " << buf;
403 }
404}
405
406void X86Mir2Lir::AdjustSpillMask() {
407 // Adjustment for LR spilling, x86 has no LR so nothing to do here
buzbee091cc402014-03-31 10:14:40 -0700408 core_spill_mask_ |= (1 << rs_rRET.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 num_core_spills_++;
410}
411
412/*
413 * Mark a callee-save fp register as promoted. Note that
414 * vpush/vpop uses contiguous register lists so we must
415 * include any holes in the mask. Associate holes with
416 * Dalvik register INVALID_VREG (0xFFFFU).
417 */
buzbee091cc402014-03-31 10:14:40 -0700418void X86Mir2Lir::MarkPreservedSingle(int v_reg, RegStorage reg) {
419 UNIMPLEMENTED(FATAL) << "MarkPreservedSingle";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420}
421
buzbee091cc402014-03-31 10:14:40 -0700422void X86Mir2Lir::MarkPreservedDouble(int v_reg, RegStorage reg) {
423 UNIMPLEMENTED(FATAL) << "MarkPreservedDouble";
buzbee2700f7e2014-03-07 09:46:20 -0800424}
425
Mark Mendelle87f9b52014-04-30 14:13:18 -0400426RegStorage X86Mir2Lir::AllocateByteRegister() {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700427 RegStorage reg = AllocTypedTemp(false, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +0700428 if (!cu_->target64) {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700429 DCHECK_LT(reg.GetRegNum(), rs_rX86_SP.GetRegNum());
430 }
431 return reg;
432}
433
434bool X86Mir2Lir::IsByteRegister(RegStorage reg) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700435 return cu_->target64 || reg.GetRegNum() < rs_rX86_SP.GetRegNum();
Mark Mendelle87f9b52014-04-30 14:13:18 -0400436}
437
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438/* Clobber all regs that might be used by an external C call */
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000439void X86Mir2Lir::ClobberCallerSave() {
buzbee091cc402014-03-31 10:14:40 -0700440 Clobber(rs_rAX);
441 Clobber(rs_rCX);
442 Clobber(rs_rDX);
443 Clobber(rs_rBX);
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700444
445 Clobber(rs_fr0);
446 Clobber(rs_fr1);
447 Clobber(rs_fr2);
448 Clobber(rs_fr3);
449 Clobber(rs_fr4);
450 Clobber(rs_fr5);
451 Clobber(rs_fr6);
452 Clobber(rs_fr7);
453
Elena Sayapinadd644502014-07-01 18:39:52 +0700454 if (cu_->target64) {
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700455 Clobber(rs_r8);
456 Clobber(rs_r9);
457 Clobber(rs_r10);
458 Clobber(rs_r11);
459
460 Clobber(rs_fr8);
461 Clobber(rs_fr9);
462 Clobber(rs_fr10);
463 Clobber(rs_fr11);
464 Clobber(rs_fr12);
465 Clobber(rs_fr13);
466 Clobber(rs_fr14);
467 Clobber(rs_fr15);
468 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469}
470
471RegLocation X86Mir2Lir::GetReturnWideAlt() {
472 RegLocation res = LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -0700473 DCHECK(res.reg.GetLowReg() == rs_rAX.GetReg());
474 DCHECK(res.reg.GetHighReg() == rs_rDX.GetReg());
475 Clobber(rs_rAX);
476 Clobber(rs_rDX);
477 MarkInUse(rs_rAX);
478 MarkInUse(rs_rDX);
479 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 return res;
481}
482
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700483RegLocation X86Mir2Lir::GetReturnAlt() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 RegLocation res = LocCReturn();
buzbee091cc402014-03-31 10:14:40 -0700485 res.reg.SetReg(rs_rDX.GetReg());
486 Clobber(rs_rDX);
487 MarkInUse(rs_rDX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 return res;
489}
490
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700492void X86Mir2Lir::LockCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700493 LockTemp(rs_rX86_ARG0);
494 LockTemp(rs_rX86_ARG1);
495 LockTemp(rs_rX86_ARG2);
496 LockTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700497 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700498 LockTemp(rs_rX86_ARG4);
499 LockTemp(rs_rX86_ARG5);
500 LockTemp(rs_rX86_FARG0);
501 LockTemp(rs_rX86_FARG1);
502 LockTemp(rs_rX86_FARG2);
503 LockTemp(rs_rX86_FARG3);
504 LockTemp(rs_rX86_FARG4);
505 LockTemp(rs_rX86_FARG5);
506 LockTemp(rs_rX86_FARG6);
507 LockTemp(rs_rX86_FARG7);
508 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509}
510
511/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700512void X86Mir2Lir::FreeCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700513 FreeTemp(rs_rX86_ARG0);
514 FreeTemp(rs_rX86_ARG1);
515 FreeTemp(rs_rX86_ARG2);
516 FreeTemp(rs_rX86_ARG3);
Elena Sayapinadd644502014-07-01 18:39:52 +0700517 if (cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700518 FreeTemp(rs_rX86_ARG4);
519 FreeTemp(rs_rX86_ARG5);
520 FreeTemp(rs_rX86_FARG0);
521 FreeTemp(rs_rX86_FARG1);
522 FreeTemp(rs_rX86_FARG2);
523 FreeTemp(rs_rX86_FARG3);
524 FreeTemp(rs_rX86_FARG4);
525 FreeTemp(rs_rX86_FARG5);
526 FreeTemp(rs_rX86_FARG6);
527 FreeTemp(rs_rX86_FARG7);
528 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529}
530
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800531bool X86Mir2Lir::ProvidesFullMemoryBarrier(X86OpCode opcode) {
532 switch (opcode) {
533 case kX86LockCmpxchgMR:
534 case kX86LockCmpxchgAR:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700535 case kX86LockCmpxchg64M:
536 case kX86LockCmpxchg64A:
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800537 case kX86XchgMR:
538 case kX86Mfence:
539 // Atomic memory instructions provide full barrier.
540 return true;
541 default:
542 break;
543 }
544
545 // Conservative if cannot prove it provides full barrier.
546 return false;
547}
548
Andreas Gampeb14329f2014-05-15 11:16:06 -0700549bool X86Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800551 // Start off with using the last LIR as the barrier. If it is not enough, then we will update it.
552 LIR* mem_barrier = last_lir_insn_;
553
Andreas Gampeb14329f2014-05-15 11:16:06 -0700554 bool ret = false;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800555 /*
556 * According to the JSR-133 Cookbook, for x86 only StoreLoad barriers need memory fence. All other barriers
557 * (LoadLoad, LoadStore, StoreStore) are nops due to the x86 memory model. For those cases, all we need
558 * to ensure is that there is a scheduling barrier in place.
559 */
560 if (barrier_kind == kStoreLoad) {
561 // If no LIR exists already that can be used a barrier, then generate an mfence.
562 if (mem_barrier == nullptr) {
563 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700564 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800565 }
566
567 // If last instruction does not provide full barrier, then insert an mfence.
568 if (ProvidesFullMemoryBarrier(static_cast<X86OpCode>(mem_barrier->opcode)) == false) {
569 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700570 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800571 }
572 }
573
574 // Now ensure that a scheduling barrier is in place.
575 if (mem_barrier == nullptr) {
576 GenBarrier();
577 } else {
578 // Mark as a scheduling barrier.
579 DCHECK(!mem_barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100580 mem_barrier->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800581 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700582 return ret;
583#else
584 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585#endif
586}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000587
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588void X86Mir2Lir::CompilerInitializeRegAlloc() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700589 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700590 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_64, core_regs_64q, sp_regs_64,
591 dp_regs_64, reserved_regs_64, reserved_regs_64q,
592 core_temps_64, core_temps_64q, sp_temps_64, dp_temps_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700593 } else {
buzbeeb01bf152014-05-13 15:59:07 -0700594 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
595 dp_regs_32, reserved_regs_32, empty_pool,
596 core_temps_32, empty_pool, sp_temps_32, dp_temps_32);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700597 }
buzbee091cc402014-03-31 10:14:40 -0700598
599 // Target-specific adjustments.
600
Mark Mendellfe945782014-05-22 09:52:36 -0400601 // Add in XMM registers.
Elena Sayapinadd644502014-07-01 18:39:52 +0700602 const ArrayRef<const RegStorage> *xp_temps = cu_->target64 ? &xp_temps_64 : &xp_temps_32;
Mark Mendellfe945782014-05-22 09:52:36 -0400603 for (RegStorage reg : *xp_temps) {
604 RegisterInfo* info = new (arena_) RegisterInfo(reg, GetRegMaskCommon(reg));
605 reginfo_map_.Put(reg.GetReg(), info);
606 info->SetIsTemp(true);
607 }
608
buzbee091cc402014-03-31 10:14:40 -0700609 // Alias single precision xmm to double xmms.
610 // TODO: as needed, add larger vector sizes - alias all to the largest.
611 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
612 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
613 int sp_reg_num = info->GetReg().GetRegNum();
Mark Mendellfe945782014-05-22 09:52:36 -0400614 RegStorage xp_reg = RegStorage::Solo128(sp_reg_num);
615 RegisterInfo* xp_reg_info = GetRegInfo(xp_reg);
616 // 128-bit xmm vector register's master storage should refer to itself.
617 DCHECK_EQ(xp_reg_info, xp_reg_info->Master());
618
619 // Redirect 32-bit vector's master storage to 128-bit vector.
620 info->SetMaster(xp_reg_info);
621
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700622 RegStorage dp_reg = RegStorage::FloatSolo64(sp_reg_num);
buzbee091cc402014-03-31 10:14:40 -0700623 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
Mark Mendellfe945782014-05-22 09:52:36 -0400624 // Redirect 64-bit vector's master storage to 128-bit vector.
625 dp_reg_info->SetMaster(xp_reg_info);
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700626 // Singles should show a single 32-bit mask bit, at first referring to the low half.
627 DCHECK_EQ(info->StorageMask(), 0x1U);
628 }
629
Elena Sayapinadd644502014-07-01 18:39:52 +0700630 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700631 // Alias 32bit W registers to corresponding 64bit X registers.
632 GrowableArray<RegisterInfo*>::Iterator w_it(&reg_pool_->core_regs_);
633 for (RegisterInfo* info = w_it.Next(); info != nullptr; info = w_it.Next()) {
634 int x_reg_num = info->GetReg().GetRegNum();
635 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
636 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
637 // 64bit X register's master storage should refer to itself.
638 DCHECK_EQ(x_reg_info, x_reg_info->Master());
639 // Redirect 32bit W master storage to 64bit X.
640 info->SetMaster(x_reg_info);
641 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
642 DCHECK_EQ(info->StorageMask(), 0x1U);
643 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 }
buzbee091cc402014-03-31 10:14:40 -0700645
646 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
647 // TODO: adjust for x86/hard float calling convention.
648 reg_pool_->next_core_reg_ = 2;
649 reg_pool_->next_sp_reg_ = 2;
650 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651}
652
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653void X86Mir2Lir::SpillCoreRegs() {
654 if (num_core_spills_ == 0) {
655 return;
656 }
657 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700658 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700659 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 for (int reg = 0; mask; mask >>= 1, reg++) {
661 if (mask & 0x1) {
buzbee2700f7e2014-03-07 09:46:20 -0800662 StoreWordDisp(rs_rX86_SP, offset, RegStorage::Solo32(reg));
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700663 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 }
665 }
666}
667
668void X86Mir2Lir::UnSpillCoreRegs() {
669 if (num_core_spills_ == 0) {
670 return;
671 }
672 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700673 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700674 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 for (int reg = 0; mask; mask >>= 1, reg++) {
676 if (mask & 0x1) {
buzbee2700f7e2014-03-07 09:46:20 -0800677 LoadWordDisp(rs_rX86_SP, offset, RegStorage::Solo32(reg));
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700678 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 }
680 }
681}
682
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700683bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
685}
686
Vladimir Marko674744e2014-04-24 15:18:26 +0100687bool X86Mir2Lir::SupportsVolatileLoadStore(OpSize size) {
688 return true;
689}
690
691RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700692 // X86_64 can handle any size.
Elena Sayapinadd644502014-07-01 18:39:52 +0700693 if (cu_->target64) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700694 if (size == kReference) {
695 return kRefReg;
696 }
697 return kCoreReg;
698 }
699
Vladimir Marko674744e2014-04-24 15:18:26 +0100700 if (UNLIKELY(is_volatile)) {
701 // On x86, atomic 64-bit load/store requires an fp register.
702 // Smaller aligned load/store is atomic for both core and fp registers.
703 if (size == k64 || size == kDouble) {
704 return kFPReg;
705 }
706 }
707 return RegClassBySize(size);
708}
709
Elena Sayapinadd644502014-07-01 18:39:52 +0700710X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800711 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700712 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800713 method_address_insns_(arena, 100, kGrowableArrayMisc),
714 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800715 call_method_insns_(arena, 100, kGrowableArrayMisc),
Elena Sayapinadd644502014-07-01 18:39:52 +0700716 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400717 const_vectors_(nullptr) {
718 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700719 if (kIsDebugBuild) {
720 for (int i = 0; i < kX86Last; i++) {
721 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
722 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400723 << " is wrong: expecting " << i << ", seeing "
724 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700725 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 }
727 }
Elena Sayapinadd644502014-07-01 18:39:52 +0700728 if (cu_->target64) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700729 rs_rX86_SP = rs_rX86_SP_64;
730
731 rs_rX86_ARG0 = rs_rDI;
732 rs_rX86_ARG1 = rs_rSI;
733 rs_rX86_ARG2 = rs_rDX;
734 rs_rX86_ARG3 = rs_rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700735 rs_rX86_ARG4 = rs_r8;
736 rs_rX86_ARG5 = rs_r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700737 rs_rX86_FARG0 = rs_fr0;
738 rs_rX86_FARG1 = rs_fr1;
739 rs_rX86_FARG2 = rs_fr2;
740 rs_rX86_FARG3 = rs_fr3;
741 rs_rX86_FARG4 = rs_fr4;
742 rs_rX86_FARG5 = rs_fr5;
743 rs_rX86_FARG6 = rs_fr6;
744 rs_rX86_FARG7 = rs_fr7;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700745 rX86_ARG0 = rDI;
746 rX86_ARG1 = rSI;
747 rX86_ARG2 = rDX;
748 rX86_ARG3 = rCX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700749 rX86_ARG4 = r8;
750 rX86_ARG5 = r9;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700751 rX86_FARG0 = fr0;
752 rX86_FARG1 = fr1;
753 rX86_FARG2 = fr2;
754 rX86_FARG3 = fr3;
755 rX86_FARG4 = fr4;
756 rX86_FARG5 = fr5;
757 rX86_FARG6 = fr6;
758 rX86_FARG7 = fr7;
Mark Mendell55884bc2014-06-10 10:21:29 -0400759 rs_rX86_INVOKE_TGT = rs_rDI;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700760 } else {
761 rs_rX86_SP = rs_rX86_SP_32;
762
763 rs_rX86_ARG0 = rs_rAX;
764 rs_rX86_ARG1 = rs_rCX;
765 rs_rX86_ARG2 = rs_rDX;
766 rs_rX86_ARG3 = rs_rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700767 rs_rX86_ARG4 = RegStorage::InvalidReg();
768 rs_rX86_ARG5 = RegStorage::InvalidReg();
769 rs_rX86_FARG0 = rs_rAX;
770 rs_rX86_FARG1 = rs_rCX;
771 rs_rX86_FARG2 = rs_rDX;
772 rs_rX86_FARG3 = rs_rBX;
773 rs_rX86_FARG4 = RegStorage::InvalidReg();
774 rs_rX86_FARG5 = RegStorage::InvalidReg();
775 rs_rX86_FARG6 = RegStorage::InvalidReg();
776 rs_rX86_FARG7 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700777 rX86_ARG0 = rAX;
778 rX86_ARG1 = rCX;
779 rX86_ARG2 = rDX;
780 rX86_ARG3 = rBX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700781 rX86_FARG0 = rAX;
782 rX86_FARG1 = rCX;
783 rX86_FARG2 = rDX;
784 rX86_FARG3 = rBX;
Mark Mendell55884bc2014-06-10 10:21:29 -0400785 rs_rX86_INVOKE_TGT = rs_rAX;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700786 // TODO(64): Initialize with invalid reg
787// rX86_ARG4 = RegStorage::InvalidReg();
788// rX86_ARG5 = RegStorage::InvalidReg();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700789 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700790 rs_rX86_RET0 = rs_rAX;
791 rs_rX86_RET1 = rs_rDX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700792 rs_rX86_COUNT = rs_rCX;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700793 rX86_RET0 = rAX;
794 rX86_RET1 = rDX;
795 rX86_INVOKE_TGT = rAX;
796 rX86_COUNT = rCX;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797}
798
799Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
800 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700801 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802}
803
804// Not used in x86
Ian Rogersdd7624d2014-03-14 17:43:00 -0700805RegStorage X86Mir2Lir::LoadHelper(ThreadOffset<4> offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
buzbee2700f7e2014-03-07 09:46:20 -0800807 return RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808}
809
Andreas Gampe2f244e92014-05-08 03:35:25 -0700810// Not used in x86
811RegStorage X86Mir2Lir::LoadHelper(ThreadOffset<8> offset) {
812 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
813 return RegStorage::InvalidReg();
814}
815
Dave Allisonb373e092014-02-20 16:06:36 -0800816LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
817 LOG(FATAL) << "Unexpected use of CheckSuspendUsingLoad in x86";
818 return nullptr;
819}
820
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700821uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700822 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 return X86Mir2Lir::EncodingMap[opcode].flags;
824}
825
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700826const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700827 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700828 return X86Mir2Lir::EncodingMap[opcode].name;
829}
830
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700831const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700832 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 return X86Mir2Lir::EncodingMap[opcode].fmt;
834}
835
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000836void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
837 // Can we do this directly to memory?
838 rl_dest = UpdateLocWide(rl_dest);
839 if ((rl_dest.location == kLocDalvikFrame) ||
840 (rl_dest.location == kLocCompilerTemp)) {
841 int32_t val_lo = Low32Bits(value);
842 int32_t val_hi = High32Bits(value);
buzbee2700f7e2014-03-07 09:46:20 -0800843 int r_base = TargetReg(kSp).GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000844 int displacement = SRegOffset(rl_dest.s_reg_low);
845
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100846 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800847 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000848 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
849 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800850 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000851 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
852 false /* is_load */, true /* is64bit */);
853 return;
854 }
855
856 // Just use the standard code to do the generation.
857 Mir2Lir::GenConstWide(rl_dest, value);
858}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800859
860// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
861void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
862 LOG(INFO) << "location: " << loc.location << ','
863 << (loc.wide ? " w" : " ")
864 << (loc.defined ? " D" : " ")
865 << (loc.is_const ? " c" : " ")
866 << (loc.fp ? " F" : " ")
867 << (loc.core ? " C" : " ")
868 << (loc.ref ? " r" : " ")
869 << (loc.high_word ? " h" : " ")
870 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800871 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000872 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800873 << ", s_reg: " << loc.s_reg_low
874 << ", orig: " << loc.orig_sreg;
875}
876
Mark Mendell67c39c42014-01-31 17:28:00 -0800877void X86Mir2Lir::Materialize() {
878 // A good place to put the analysis before starting.
879 AnalyzeMIR();
880
881 // Now continue with regular code generation.
882 Mir2Lir::Materialize();
883}
884
Jeff Hao49161ce2014-03-12 11:05:25 -0700885void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800886 SpecialTargetRegister symbolic_reg) {
887 /*
888 * For x86, just generate a 32 bit move immediate instruction, that will be filled
889 * in at 'link time'. For now, put a unique value based on target to ensure that
890 * code deduplication works.
891 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700892 int target_method_idx = target_method.dex_method_index;
893 const DexFile* target_dex_file = target_method.dex_file;
894 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
895 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800896
Jeff Hao49161ce2014-03-12 11:05:25 -0700897 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
buzbee2700f7e2014-03-07 09:46:20 -0800898 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI, TargetReg(symbolic_reg).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700899 static_cast<int>(target_method_id_ptr), target_method_idx,
900 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800901 AppendLIR(move);
902 method_address_insns_.Insert(move);
903}
904
905void X86Mir2Lir::LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg) {
906 /*
907 * For x86, just generate a 32 bit move immediate instruction, that will be filled
908 * in at 'link time'. For now, put a unique value based on target to ensure that
909 * code deduplication works.
910 */
911 const DexFile::TypeId& id = cu_->dex_file->GetTypeId(type_idx);
912 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
913
914 // Generate the move instruction with the unique pointer and save index and type.
buzbee2700f7e2014-03-07 09:46:20 -0800915 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI, TargetReg(symbolic_reg).GetReg(),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800916 static_cast<int>(ptr), type_idx);
917 AppendLIR(move);
918 class_type_address_insns_.Insert(move);
919}
920
Jeff Hao49161ce2014-03-12 11:05:25 -0700921LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800922 /*
923 * For x86, just generate a 32 bit call relative instruction, that will be filled
924 * in at 'link time'. For now, put a unique value based on target to ensure that
925 * code deduplication works.
926 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700927 int target_method_idx = target_method.dex_method_index;
928 const DexFile* target_dex_file = target_method.dex_file;
929 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
930 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800931
Jeff Hao49161ce2014-03-12 11:05:25 -0700932 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
933 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
934 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800935 AppendLIR(call);
936 call_method_insns_.Insert(call);
937 return call;
938}
939
Mark Mendelld65c51a2014-04-29 16:55:20 -0400940/*
941 * @brief Enter a 32 bit quantity into a buffer
942 * @param buf buffer.
943 * @param data Data value.
944 */
945
946static void PushWord(std::vector<uint8_t>&buf, int32_t data) {
947 buf.push_back(data & 0xff);
948 buf.push_back((data >> 8) & 0xff);
949 buf.push_back((data >> 16) & 0xff);
950 buf.push_back((data >> 24) & 0xff);
951}
952
Mark Mendell55d0eac2014-02-06 11:02:52 -0800953void X86Mir2Lir::InstallLiteralPools() {
954 // These are handled differently for x86.
955 DCHECK(code_literal_list_ == nullptr);
956 DCHECK(method_literal_list_ == nullptr);
957 DCHECK(class_literal_list_ == nullptr);
958
Mark Mendelld65c51a2014-04-29 16:55:20 -0400959 // Align to 16 byte boundary. We have implicit knowledge that the start of the method is
960 // on a 4 byte boundary. How can I check this if it changes (other than aligned loads
961 // will fail at runtime)?
962 if (const_vectors_ != nullptr) {
963 int align_size = (16-4) - (code_buffer_.size() & 0xF);
964 if (align_size < 0) {
965 align_size += 16;
966 }
967
968 while (align_size > 0) {
969 code_buffer_.push_back(0);
970 align_size--;
971 }
972 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
973 PushWord(code_buffer_, p->operands[0]);
974 PushWord(code_buffer_, p->operands[1]);
975 PushWord(code_buffer_, p->operands[2]);
976 PushWord(code_buffer_, p->operands[3]);
977 }
978 }
979
Mark Mendell55d0eac2014-02-06 11:02:52 -0800980 // Handle the fixups for methods.
981 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
982 LIR* p = method_address_insns_.Get(i);
983 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -0700984 uint32_t target_method_idx = p->operands[2];
985 const DexFile* target_dex_file =
986 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800987
988 // The offset to patch is the last 4 bytes of the instruction.
989 int patch_offset = p->offset + p->flags.size - 4;
990 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
991 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700992 target_method_idx, target_dex_file,
993 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800994 patch_offset);
995 }
996
997 // Handle the fixups for class types.
998 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
999 LIR* p = class_type_address_insns_.Get(i);
1000 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001001 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001002
1003 // The offset to patch is the last 4 bytes of the instruction.
1004 int patch_offset = p->offset + p->flags.size - 4;
1005 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001006 cu_->method_idx, target_method_idx, patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001007 }
1008
1009 // And now the PC-relative calls to methods.
1010 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
1011 LIR* p = call_method_insns_.Get(i);
1012 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001013 uint32_t target_method_idx = p->operands[1];
1014 const DexFile* target_dex_file =
1015 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001016
1017 // The offset to patch is the last 4 bytes of the instruction.
1018 int patch_offset = p->offset + p->flags.size - 4;
1019 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -07001020 cu_->method_idx, cu_->invoke_type,
1021 target_method_idx, target_dex_file,
1022 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001023 patch_offset, -4 /* offset */);
1024 }
1025
1026 // And do the normal processing.
1027 Mir2Lir::InstallLiteralPools();
1028}
1029
Mark Mendell4028a6c2014-02-19 20:06:20 -08001030/*
1031 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1032 * otherwise bails to standard library code.
1033 */
1034bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
1035 ClobberCallerSave();
1036 LockCallTemps(); // Using fixed registers
1037
1038 // EAX: 16 bit character being searched.
1039 // ECX: count: number of words to be searched.
1040 // EDI: String being searched.
1041 // EDX: temporary during execution.
1042 // EBX: temporary during execution.
1043
1044 RegLocation rl_obj = info->args[0];
1045 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001046 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001047
1048 uint32_t char_value =
1049 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1050
1051 if (char_value > 0xFFFF) {
1052 // We have to punt to the real String.indexOf.
1053 return false;
1054 }
1055
1056 // Okay, we are commited to inlining this.
buzbeea0cd2d72014-06-01 09:33:49 -07001057 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001058 RegLocation rl_dest = InlineTarget(info);
1059
1060 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001061 LoadValueDirectFixed(rl_obj, rs_rDX);
1062 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001063 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001064
1065 // Does the character fit in 16 bits?
Mingyao Yang3a74d152014-04-21 15:39:44 -07001066 LIR* slowpath_branch = nullptr;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001067 if (rl_char.is_const) {
1068 // We need the value in EAX.
buzbee2700f7e2014-03-07 09:46:20 -08001069 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001070 } else {
1071 // Character is not a constant; compare at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001072 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001073 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001074 }
1075
1076 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001077 // Location of reference to data array within the String object.
1078 int value_offset = mirror::String::ValueOffset().Int32Value();
1079 // Location of count within the String object.
1080 int count_offset = mirror::String::CountOffset().Int32Value();
1081 // Starting offset within data array.
1082 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1083 // Start of char data with array_.
1084 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001085
1086 // Character is in EAX.
1087 // Object pointer is in EDX.
1088
1089 // We need to preserve EDI, but have no spare registers, so push it on the stack.
1090 // We have to remember that all stack addresses after this are offset by sizeof(EDI).
buzbee091cc402014-03-31 10:14:40 -07001091 NewLIR1(kX86Push32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001092
1093 // Compute the number of words to search in to rCX.
buzbee695d13a2014-04-19 13:32:20 -07001094 Load32Disp(rs_rDX, count_offset, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001095 LIR *length_compare = nullptr;
1096 int start_value = 0;
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001097 bool is_index_on_stack = false;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001098 if (zero_based) {
1099 // We have to handle an empty string. Use special instruction JECXZ.
1100 length_compare = NewLIR0(kX86Jecxz8);
1101 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001102 rl_start = info->args[2];
Mark Mendell4028a6c2014-02-19 20:06:20 -08001103 // We have to offset by the start index.
1104 if (rl_start.is_const) {
1105 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1106 start_value = std::max(start_value, 0);
1107
1108 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001109 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001110
1111 if (start_value != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001112 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001113 }
1114 } else {
1115 // Runtime start index.
buzbee30adc732014-05-09 15:10:18 -07001116 rl_start = UpdateLocTyped(rl_start, kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001117 if (rl_start.location == kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001118 // Handle "start index < 0" case.
1119 OpRegReg(kOpXor, rs_rBX, rs_rBX);
1120 OpRegReg(kOpCmp, rl_start.reg, rs_rBX);
1121 OpCondRegReg(kOpCmov, kCondLt, rl_start.reg, rs_rBX);
1122
1123 // The length of the string should be greater than the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001124 length_compare = OpCmpBranch(kCondLe, rs_rCX, rl_start.reg, nullptr);
1125 OpRegReg(kOpSub, rs_rCX, rl_start.reg);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001126 if (rl_start.reg == rs_rDI) {
1127 // The special case. We will use EDI further, so lets put start index to stack.
buzbee091cc402014-03-31 10:14:40 -07001128 NewLIR1(kX86Push32R, rs_rDI.GetReg());
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001129 is_index_on_stack = true;
1130 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001131 } else {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001132 // Load the start index from stack, remembering that we pushed EDI.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001133 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001134 {
1135 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1136 Load32Disp(rs_rX86_SP, displacement, rs_rBX);
1137 }
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001138 OpRegReg(kOpXor, rs_rDI, rs_rDI);
1139 OpRegReg(kOpCmp, rs_rBX, rs_rDI);
1140 OpCondRegReg(kOpCmov, kCondLt, rs_rBX, rs_rDI);
1141
1142 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rBX, nullptr);
1143 OpRegReg(kOpSub, rs_rCX, rs_rBX);
1144 // Put the start index to stack.
buzbee091cc402014-03-31 10:14:40 -07001145 NewLIR1(kX86Push32R, rs_rBX.GetReg());
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001146 is_index_on_stack = true;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001147 }
1148 }
1149 }
1150 DCHECK(length_compare != nullptr);
1151
1152 // ECX now contains the count in words to be searched.
1153
1154 // Load the address of the string into EBX.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001155 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
buzbee695d13a2014-04-19 13:32:20 -07001156 Load32Disp(rs_rDX, value_offset, rs_rDI);
1157 Load32Disp(rs_rDX, offset_offset, rs_rBX);
buzbee2700f7e2014-03-07 09:46:20 -08001158 OpLea(rs_rBX, rs_rDI, rs_rBX, 1, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001159
1160 // Now compute into EDI where the search will start.
1161 if (zero_based || rl_start.is_const) {
1162 if (start_value == 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001163 OpRegCopy(rs_rDI, rs_rBX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001164 } else {
buzbee091cc402014-03-31 10:14:40 -07001165 NewLIR3(kX86Lea32RM, rs_rDI.GetReg(), rs_rBX.GetReg(), 2 * start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001166 }
1167 } else {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001168 if (is_index_on_stack == true) {
1169 // Load the start index from stack.
buzbee091cc402014-03-31 10:14:40 -07001170 NewLIR1(kX86Pop32R, rs_rDX.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -08001171 OpLea(rs_rDI, rs_rBX, rs_rDX, 1, 0);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001172 } else {
1173 OpLea(rs_rDI, rs_rBX, rl_start.reg, 1, 0);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001174 }
1175 }
1176
1177 // EDI now contains the start of the string to be searched.
1178 // We are all prepared to do the search for the character.
1179 NewLIR0(kX86RepneScasw);
1180
1181 // Did we find a match?
1182 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1183
1184 // yes, we matched. Compute the index of the result.
1185 // index = ((curr_ptr - orig_ptr) / 2) - 1.
buzbee2700f7e2014-03-07 09:46:20 -08001186 OpRegReg(kOpSub, rs_rDI, rs_rBX);
1187 OpRegImm(kOpAsr, rs_rDI, 1);
buzbee091cc402014-03-31 10:14:40 -07001188 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_rDI.GetReg(), -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001189 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1190
1191 // Failed to match; return -1.
1192 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1193 length_compare->target = not_found;
1194 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001195 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001196
1197 // And join up at the end.
1198 all_done->target = NewLIR0(kPseudoTargetLabel);
1199 // Restore EDI from the stack.
buzbee091cc402014-03-31 10:14:40 -07001200 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001201
1202 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001203 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001204 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001205 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001206 }
1207
1208 StoreValue(rl_dest, rl_return);
1209 return true;
1210}
1211
Mark Mendellae9fd932014-02-10 16:14:35 -08001212/*
Mark Mendellae9fd932014-02-10 16:14:35 -08001213 * @brief Enter an 'advance LOC' into the FDE buffer
1214 * @param buf FDE buffer.
1215 * @param increment Amount by which to increase the current location.
1216 */
1217static void AdvanceLoc(std::vector<uint8_t>&buf, uint32_t increment) {
1218 if (increment < 64) {
1219 // Encoding in opcode.
1220 buf.push_back(0x1 << 6 | increment);
1221 } else if (increment < 256) {
1222 // Single byte delta.
1223 buf.push_back(0x02);
1224 buf.push_back(increment);
1225 } else if (increment < 256 * 256) {
1226 // Two byte delta.
1227 buf.push_back(0x03);
1228 buf.push_back(increment & 0xff);
1229 buf.push_back((increment >> 8) & 0xff);
1230 } else {
1231 // Four byte delta.
1232 buf.push_back(0x04);
1233 PushWord(buf, increment);
1234 }
1235}
1236
1237
1238std::vector<uint8_t>* X86CFIInitialization() {
1239 return X86Mir2Lir::ReturnCommonCallFrameInformation();
1240}
1241
1242std::vector<uint8_t>* X86Mir2Lir::ReturnCommonCallFrameInformation() {
1243 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
1244
1245 // Length of the CIE (except for this field).
1246 PushWord(*cfi_info, 16);
1247
1248 // CIE id.
1249 PushWord(*cfi_info, 0xFFFFFFFFU);
1250
1251 // Version: 3.
1252 cfi_info->push_back(0x03);
1253
1254 // Augmentation: empty string.
1255 cfi_info->push_back(0x0);
1256
1257 // Code alignment: 1.
1258 cfi_info->push_back(0x01);
1259
1260 // Data alignment: -4.
1261 cfi_info->push_back(0x7C);
1262
1263 // Return address register (R8).
1264 cfi_info->push_back(0x08);
1265
1266 // Initial return PC is 4(ESP): DW_CFA_def_cfa R4 4.
1267 cfi_info->push_back(0x0C);
1268 cfi_info->push_back(0x04);
1269 cfi_info->push_back(0x04);
1270
1271 // Return address location: 0(SP): DW_CFA_offset R8 1 (* -4);.
1272 cfi_info->push_back(0x2 << 6 | 0x08);
1273 cfi_info->push_back(0x01);
1274
1275 // And 2 Noops to align to 4 byte boundary.
1276 cfi_info->push_back(0x0);
1277 cfi_info->push_back(0x0);
1278
1279 DCHECK_EQ(cfi_info->size() & 3, 0U);
1280 return cfi_info;
1281}
1282
1283static void EncodeUnsignedLeb128(std::vector<uint8_t>& buf, uint32_t value) {
1284 uint8_t buffer[12];
1285 uint8_t *ptr = EncodeUnsignedLeb128(buffer, value);
1286 for (uint8_t *p = buffer; p < ptr; p++) {
1287 buf.push_back(*p);
1288 }
1289}
1290
1291std::vector<uint8_t>* X86Mir2Lir::ReturnCallFrameInformation() {
1292 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
1293
1294 // Generate the FDE for the method.
1295 DCHECK_NE(data_offset_, 0U);
1296
1297 // Length (will be filled in later in this routine).
1298 PushWord(*cfi_info, 0);
1299
1300 // CIE_pointer (can be filled in by linker); might be left at 0 if there is only
1301 // one CIE for the whole debug_frame section.
1302 PushWord(*cfi_info, 0);
1303
1304 // 'initial_location' (filled in by linker).
1305 PushWord(*cfi_info, 0);
1306
1307 // 'address_range' (number of bytes in the method).
1308 PushWord(*cfi_info, data_offset_);
1309
1310 // The instructions in the FDE.
1311 if (stack_decrement_ != nullptr) {
1312 // Advance LOC to just past the stack decrement.
1313 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
1314 AdvanceLoc(*cfi_info, pc);
1315
1316 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
1317 cfi_info->push_back(0x0e);
1318 EncodeUnsignedLeb128(*cfi_info, frame_size_);
1319
1320 // We continue with that stack until the epilogue.
1321 if (stack_increment_ != nullptr) {
1322 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
1323 AdvanceLoc(*cfi_info, new_pc - pc);
1324
1325 // We probably have code snippets after the epilogue, so save the
1326 // current state: DW_CFA_remember_state.
1327 cfi_info->push_back(0x0a);
1328
1329 // We have now popped the stack: DW_CFA_def_cfa_offset 4. There is only the return
1330 // PC on the stack now.
1331 cfi_info->push_back(0x0e);
1332 EncodeUnsignedLeb128(*cfi_info, 4);
1333
1334 // Everything after that is the same as before the epilogue.
1335 // Stack bump was followed by RET instruction.
1336 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1337 if (post_ret_insn != nullptr) {
1338 pc = new_pc;
1339 new_pc = post_ret_insn->offset;
1340 AdvanceLoc(*cfi_info, new_pc - pc);
1341 // Restore the state: DW_CFA_restore_state.
1342 cfi_info->push_back(0x0b);
1343 }
1344 }
1345 }
1346
1347 // Padding to a multiple of 4
1348 while ((cfi_info->size() & 3) != 0) {
1349 // DW_CFA_nop is encoded as 0.
1350 cfi_info->push_back(0);
1351 }
1352
1353 // Set the length of the FDE inside the generated bytes.
1354 uint32_t length = cfi_info->size() - 4;
1355 (*cfi_info)[0] = length;
1356 (*cfi_info)[1] = length >> 8;
1357 (*cfi_info)[2] = length >> 16;
1358 (*cfi_info)[3] = length >> 24;
1359 return cfi_info;
1360}
1361
Mark Mendelld65c51a2014-04-29 16:55:20 -04001362void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1363 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1364 case kMirOpConstVector:
1365 GenConst128(bb, mir);
1366 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001367 case kMirOpMoveVector:
1368 GenMoveVector(bb, mir);
1369 break;
1370 case kMirOpPackedMultiply:
1371 GenMultiplyVector(bb, mir);
1372 break;
1373 case kMirOpPackedAddition:
1374 GenAddVector(bb, mir);
1375 break;
1376 case kMirOpPackedSubtract:
1377 GenSubtractVector(bb, mir);
1378 break;
1379 case kMirOpPackedShiftLeft:
1380 GenShiftLeftVector(bb, mir);
1381 break;
1382 case kMirOpPackedSignedShiftRight:
1383 GenSignedShiftRightVector(bb, mir);
1384 break;
1385 case kMirOpPackedUnsignedShiftRight:
1386 GenUnsignedShiftRightVector(bb, mir);
1387 break;
1388 case kMirOpPackedAnd:
1389 GenAndVector(bb, mir);
1390 break;
1391 case kMirOpPackedOr:
1392 GenOrVector(bb, mir);
1393 break;
1394 case kMirOpPackedXor:
1395 GenXorVector(bb, mir);
1396 break;
1397 case kMirOpPackedAddReduce:
1398 GenAddReduceVector(bb, mir);
1399 break;
1400 case kMirOpPackedReduce:
1401 GenReduceVector(bb, mir);
1402 break;
1403 case kMirOpPackedSet:
1404 GenSetVector(bb, mir);
1405 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001406 default:
1407 break;
1408 }
1409}
1410
1411void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
1412 int type_size = mir->dalvikInsn.vA;
1413 // We support 128 bit vectors.
1414 DCHECK_EQ(type_size & 0xFFFF, 128);
Mark Mendellfe945782014-05-22 09:52:36 -04001415 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001416 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001417 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001418 // Check for all 0 case.
1419 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1420 NewLIR2(kX86XorpsRR, reg, reg);
1421 return;
1422 }
1423 // Okay, load it from the constant vector area.
1424 LIR *data_target = ScanVectorLiteral(mir);
1425 if (data_target == nullptr) {
1426 data_target = AddVectorLiteral(mir);
1427 }
1428
1429 // Address the start of the method.
1430 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001431 if (rl_method.wide) {
1432 rl_method = LoadValueWide(rl_method, kCoreReg);
1433 } else {
1434 rl_method = LoadValue(rl_method, kCoreReg);
1435 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001436
1437 // Load the proper value from the literal area.
1438 // We don't know the proper offset for the value, so pick one that will force
1439 // 4 byte offset. We will fix this up in the assembler later to have the right
1440 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001441 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001442 LIR *load = NewLIR3(kX86Mova128RM, reg, rl_method.reg.GetReg(), 256 /* bogus */);
1443 load->flags.fixup = kFixupLoad;
1444 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001445}
1446
Mark Mendellfe945782014-05-22 09:52:36 -04001447void X86Mir2Lir::GenMoveVector(BasicBlock *bb, MIR *mir) {
1448 // We only support 128 bit registers.
1449 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1450 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vB);
1451 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vC);
1452 NewLIR2(kX86Mova128RR, rs_dest.GetReg(), rs_src.GetReg());
1453}
1454
1455void X86Mir2Lir::GenMultiplyVector(BasicBlock *bb, MIR *mir) {
1456 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1457 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1458 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1459 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1460 int opcode = 0;
1461 switch (opsize) {
1462 case k32:
1463 opcode = kX86PmulldRR;
1464 break;
1465 case kSignedHalf:
1466 opcode = kX86PmullwRR;
1467 break;
1468 case kSingle:
1469 opcode = kX86MulpsRR;
1470 break;
1471 case kDouble:
1472 opcode = kX86MulpdRR;
1473 break;
1474 default:
1475 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1476 break;
1477 }
1478 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1479}
1480
1481void X86Mir2Lir::GenAddVector(BasicBlock *bb, MIR *mir) {
1482 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1483 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1484 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1485 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1486 int opcode = 0;
1487 switch (opsize) {
1488 case k32:
1489 opcode = kX86PadddRR;
1490 break;
1491 case kSignedHalf:
1492 case kUnsignedHalf:
1493 opcode = kX86PaddwRR;
1494 break;
1495 case kUnsignedByte:
1496 case kSignedByte:
1497 opcode = kX86PaddbRR;
1498 break;
1499 case kSingle:
1500 opcode = kX86AddpsRR;
1501 break;
1502 case kDouble:
1503 opcode = kX86AddpdRR;
1504 break;
1505 default:
1506 LOG(FATAL) << "Unsupported vector addition " << opsize;
1507 break;
1508 }
1509 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1510}
1511
1512void X86Mir2Lir::GenSubtractVector(BasicBlock *bb, MIR *mir) {
1513 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1514 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1515 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1516 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1517 int opcode = 0;
1518 switch (opsize) {
1519 case k32:
1520 opcode = kX86PsubdRR;
1521 break;
1522 case kSignedHalf:
1523 case kUnsignedHalf:
1524 opcode = kX86PsubwRR;
1525 break;
1526 case kUnsignedByte:
1527 case kSignedByte:
1528 opcode = kX86PsubbRR;
1529 break;
1530 case kSingle:
1531 opcode = kX86SubpsRR;
1532 break;
1533 case kDouble:
1534 opcode = kX86SubpdRR;
1535 break;
1536 default:
1537 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1538 break;
1539 }
1540 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1541}
1542
1543void X86Mir2Lir::GenShiftLeftVector(BasicBlock *bb, MIR *mir) {
1544 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1545 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1546 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1547 int imm = mir->dalvikInsn.vC;
1548 int opcode = 0;
1549 switch (opsize) {
1550 case k32:
1551 opcode = kX86PslldRI;
1552 break;
1553 case k64:
1554 opcode = kX86PsllqRI;
1555 break;
1556 case kSignedHalf:
1557 case kUnsignedHalf:
1558 opcode = kX86PsllwRI;
1559 break;
1560 default:
1561 LOG(FATAL) << "Unsupported vector shift left " << opsize;
1562 break;
1563 }
1564 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1565}
1566
1567void X86Mir2Lir::GenSignedShiftRightVector(BasicBlock *bb, MIR *mir) {
1568 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1569 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1570 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1571 int imm = mir->dalvikInsn.vC;
1572 int opcode = 0;
1573 switch (opsize) {
1574 case k32:
1575 opcode = kX86PsradRI;
1576 break;
1577 case kSignedHalf:
1578 case kUnsignedHalf:
1579 opcode = kX86PsrawRI;
1580 break;
1581 default:
1582 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
1583 break;
1584 }
1585 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1586}
1587
1588void X86Mir2Lir::GenUnsignedShiftRightVector(BasicBlock *bb, MIR *mir) {
1589 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1590 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1591 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1592 int imm = mir->dalvikInsn.vC;
1593 int opcode = 0;
1594 switch (opsize) {
1595 case k32:
1596 opcode = kX86PsrldRI;
1597 break;
1598 case k64:
1599 opcode = kX86PsrlqRI;
1600 break;
1601 case kSignedHalf:
1602 case kUnsignedHalf:
1603 opcode = kX86PsrlwRI;
1604 break;
1605 default:
1606 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1607 break;
1608 }
1609 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1610}
1611
1612void X86Mir2Lir::GenAndVector(BasicBlock *bb, MIR *mir) {
1613 // We only support 128 bit registers.
1614 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1615 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1616 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1617 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1618}
1619
1620void X86Mir2Lir::GenOrVector(BasicBlock *bb, MIR *mir) {
1621 // We only support 128 bit registers.
1622 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1623 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1624 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1625 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1626}
1627
1628void X86Mir2Lir::GenXorVector(BasicBlock *bb, MIR *mir) {
1629 // We only support 128 bit registers.
1630 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1631 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1632 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vC);
1633 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1634}
1635
1636void X86Mir2Lir::GenAddReduceVector(BasicBlock *bb, MIR *mir) {
1637 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1638 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1639 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vB);
1640 int imm = mir->dalvikInsn.vC;
1641 int opcode = 0;
1642 switch (opsize) {
1643 case k32:
1644 opcode = kX86PhadddRR;
1645 break;
1646 case kSignedHalf:
1647 case kUnsignedHalf:
1648 opcode = kX86PhaddwRR;
1649 break;
1650 default:
1651 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
1652 break;
1653 }
1654 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1655}
1656
1657void X86Mir2Lir::GenReduceVector(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_src = RegStorage::Solo128(mir->dalvikInsn.vB);
1661 int index = mir->dalvikInsn.arg[0];
1662 int opcode = 0;
1663 switch (opsize) {
1664 case k32:
1665 opcode = kX86PextrdRRI;
1666 break;
1667 case kSignedHalf:
1668 case kUnsignedHalf:
1669 opcode = kX86PextrwRRI;
1670 break;
1671 case kUnsignedByte:
1672 case kSignedByte:
1673 opcode = kX86PextrbRRI;
1674 break;
1675 default:
1676 LOG(FATAL) << "Unsupported vector reduce " << opsize;
1677 break;
1678 }
1679 // We need to extract to a GPR.
1680 RegStorage temp = AllocTemp();
1681 NewLIR3(opcode, temp.GetReg(), rs_src.GetReg(), index);
1682
1683 // Assume that the destination VR is in the def for the mir.
1684 RegLocation rl_dest = mir_graph_->GetDest(mir);
1685 RegLocation rl_temp =
1686 {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, temp, INVALID_SREG, INVALID_SREG};
1687 StoreValue(rl_dest, rl_temp);
1688}
1689
1690void X86Mir2Lir::GenSetVector(BasicBlock *bb, MIR *mir) {
1691 DCHECK_EQ(mir->dalvikInsn.vA & 0xFFFF, 128U);
1692 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vA >> 16);
1693 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vB);
1694 int op_low = 0, op_high = 0;
1695 switch (opsize) {
1696 case k32:
1697 op_low = kX86PshufdRRI;
1698 break;
1699 case kSignedHalf:
1700 case kUnsignedHalf:
1701 // Handles low quadword.
1702 op_low = kX86PshuflwRRI;
1703 // Handles upper quadword.
1704 op_high = kX86PshufdRRI;
1705 break;
1706 default:
1707 LOG(FATAL) << "Unsupported vector set " << opsize;
1708 break;
1709 }
1710
1711 // Load the value from the VR into a GPR.
1712 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
1713 rl_src = LoadValue(rl_src, kCoreReg);
1714
1715 // Load the value into the XMM register.
1716 NewLIR2(kX86MovdxrRR, rs_dest.GetReg(), rl_src.reg.GetReg());
1717
1718 // Now shuffle the value across the destination.
1719 NewLIR3(op_low, rs_dest.GetReg(), rs_dest.GetReg(), 0);
1720
1721 // And then repeat as needed.
1722 if (op_high != 0) {
1723 NewLIR3(op_high, rs_dest.GetReg(), rs_dest.GetReg(), 0);
1724 }
1725}
1726
1727
Mark Mendelld65c51a2014-04-29 16:55:20 -04001728LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
1729 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
1730 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1731 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
1732 args[2] == p->operands[2] && args[3] == p->operands[3]) {
1733 return p;
1734 }
1735 }
1736 return nullptr;
1737}
1738
1739LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
1740 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
1741 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
1742 new_value->operands[0] = args[0];
1743 new_value->operands[1] = args[1];
1744 new_value->operands[2] = args[2];
1745 new_value->operands[3] = args[3];
1746 new_value->next = const_vectors_;
1747 if (const_vectors_ == nullptr) {
1748 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
1749 }
1750 estimated_native_code_size_ += 16; // Space for one vector.
1751 const_vectors_ = new_value;
1752 return new_value;
1753}
1754
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001755// ------------ ABI support: mapping of args to physical registers -------------
1756RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide) {
1757 const RegStorage coreArgMappingToPhysicalReg[] = {rs_rX86_ARG1, rs_rX86_ARG2, rs_rX86_ARG3, rs_rX86_ARG4, rs_rX86_ARG5};
1758 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) / sizeof(RegStorage);
1759 const RegStorage fpArgMappingToPhysicalReg[] = {rs_rX86_FARG0, rs_rX86_FARG1, rs_rX86_FARG2, rs_rX86_FARG3,
1760 rs_rX86_FARG4, rs_rX86_FARG5, rs_rX86_FARG6, rs_rX86_FARG7};
1761 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) / sizeof(RegStorage);
1762
1763 RegStorage result = RegStorage::InvalidReg();
1764 if (is_double_or_float) {
1765 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
1766 result = fpArgMappingToPhysicalReg[cur_fp_reg_++];
1767 if (result.Valid()) {
1768 result = is_wide ? RegStorage::FloatSolo64(result.GetReg()) : RegStorage::FloatSolo32(result.GetReg());
1769 }
1770 }
1771 } else {
1772 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
1773 result = coreArgMappingToPhysicalReg[cur_core_reg_++];
1774 if (result.Valid()) {
1775 result = is_wide ? RegStorage::Solo64(result.GetReg()) : RegStorage::Solo32(result.GetReg());
1776 }
1777 }
1778 }
1779 return result;
1780}
1781
1782RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
1783 DCHECK(IsInitialized());
1784 auto res = mapping_.find(in_position);
1785 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
1786}
1787
1788void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count, InToRegStorageMapper* mapper) {
1789 DCHECK(mapper != nullptr);
1790 max_mapped_in_ = -1;
1791 is_there_stack_mapped_ = false;
1792 for (int in_position = 0; in_position < count; in_position++) {
1793 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp, arg_locs[in_position].wide);
1794 if (reg.Valid()) {
1795 mapping_[in_position] = reg;
1796 max_mapped_in_ = std::max(max_mapped_in_, in_position);
1797 if (reg.Is64BitSolo()) {
1798 // We covered 2 args, so skip the next one
1799 in_position++;
1800 }
1801 } else {
1802 is_there_stack_mapped_ = true;
1803 }
1804 }
1805 initialized_ = true;
1806}
1807
1808RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001809 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001810 return GetCoreArgMappingToPhysicalReg(arg_num);
1811 }
1812
1813 if (!in_to_reg_storage_mapping_.IsInitialized()) {
1814 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
1815 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
1816
1817 InToRegStorageX86_64Mapper mapper;
1818 in_to_reg_storage_mapping_.Initialize(arg_locs, cu_->num_ins, &mapper);
1819 }
1820 return in_to_reg_storage_mapping_.Get(arg_num);
1821}
1822
1823RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) {
1824 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
1825 // Not used for 64-bit, TODO: Move X86_32 to the same framework
1826 switch (core_arg_num) {
1827 case 0:
1828 return rs_rX86_ARG1;
1829 case 1:
1830 return rs_rX86_ARG2;
1831 case 2:
1832 return rs_rX86_ARG3;
1833 default:
1834 return RegStorage::InvalidReg();
1835 }
1836}
1837
1838// ---------End of ABI support: mapping of args to physical registers -------------
1839
1840/*
1841 * If there are any ins passed in registers that have not been promoted
1842 * to a callee-save register, flush them to the frame. Perform initial
1843 * assignment of promoted arguments.
1844 *
1845 * ArgLocs is an array of location records describing the incoming arguments
1846 * with one location record per word of argument.
1847 */
1848void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001849 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001850 /*
1851 * Dummy up a RegLocation for the incoming Method*
1852 * It will attempt to keep kArg0 live (or copy it to home location
1853 * if promoted).
1854 */
1855
1856 RegLocation rl_src = rl_method;
1857 rl_src.location = kLocPhysReg;
1858 rl_src.reg = TargetReg(kArg0);
1859 rl_src.home = false;
1860 MarkLive(rl_src);
1861 StoreValue(rl_method, rl_src);
1862 // If Method* has been promoted, explicitly flush
1863 if (rl_method.location == kLocPhysReg) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001864 StoreRefDisp(TargetReg(kSp), 0, TargetReg(kArg0), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001865 }
1866
1867 if (cu_->num_ins == 0) {
1868 return;
1869 }
1870
1871 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
1872 /*
1873 * Copy incoming arguments to their proper home locations.
1874 * NOTE: an older version of dx had an issue in which
1875 * it would reuse static method argument registers.
1876 * This could result in the same Dalvik virtual register
1877 * being promoted to both core and fp regs. To account for this,
1878 * we only copy to the corresponding promoted physical register
1879 * if it matches the type of the SSA name for the incoming
1880 * argument. It is also possible that long and double arguments
1881 * end up half-promoted. In those cases, we must flush the promoted
1882 * half to memory as well.
1883 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001884 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001885 for (int i = 0; i < cu_->num_ins; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001886 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001887 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001888
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001889 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001890 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001891 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001892
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001893 // We have already updated the arg location with promoted info
1894 // so we can be based on it.
1895 if (t_loc->location == kLocPhysReg) {
1896 // Just copy it.
1897 OpRegCopy(t_loc->reg, reg);
1898 } else {
1899 // Needs flush.
1900 if (t_loc->ref) {
1901 StoreRefDisp(TargetReg(kSp), SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001902 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001903 StoreBaseDisp(TargetReg(kSp), SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
1904 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001905 }
1906 }
1907 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001908 // If arriving in frame & promoted.
1909 if (t_loc->location == kLocPhysReg) {
1910 if (t_loc->ref) {
1911 LoadRefDisp(TargetReg(kSp), SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
1912 } else {
1913 LoadBaseDisp(TargetReg(kSp), SRegOffset(start_vreg + i), t_loc->reg,
1914 t_loc->wide ? k64 : k32, kNotVolatile);
1915 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001916 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07001917 }
1918 if (t_loc->wide) {
1919 // Increment i to skip the next one.
1920 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001921 }
1922 }
1923}
1924
1925/*
1926 * Load up to 5 arguments, the first three of which will be in
1927 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
1928 * and as part of the load sequence, it must be replaced with
1929 * the target method pointer. Note, this may also be called
1930 * for "range" variants if the number of arguments is 5 or fewer.
1931 */
1932int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
1933 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
1934 const MethodReference& target_method,
1935 uint32_t vtable_idx, uintptr_t direct_code,
1936 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001937 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001938 return Mir2Lir::GenDalvikArgsNoRange(info,
1939 call_state, pcrLabel, next_call_insn,
1940 target_method,
1941 vtable_idx, direct_code,
1942 direct_method, type, skip_this);
1943 }
1944 return GenDalvikArgsRange(info,
1945 call_state, pcrLabel, next_call_insn,
1946 target_method,
1947 vtable_idx, direct_code,
1948 direct_method, type, skip_this);
1949}
1950
1951/*
1952 * May have 0+ arguments (also used for jumbo). Note that
1953 * source virtual registers may be in physical registers, so may
1954 * need to be flushed to home location before copying. This
1955 * applies to arg3 and above (see below).
1956 *
1957 * Two general strategies:
1958 * If < 20 arguments
1959 * Pass args 3-18 using vldm/vstm block copy
1960 * Pass arg0, arg1 & arg2 in kArg1-kArg3
1961 * If 20+ arguments
1962 * Pass args arg19+ using memcpy block copy
1963 * Pass arg0, arg1 & arg2 in kArg1-kArg3
1964 *
1965 */
1966int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
1967 LIR** pcrLabel, NextCallInsn next_call_insn,
1968 const MethodReference& target_method,
1969 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
1970 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07001971 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07001972 return Mir2Lir::GenDalvikArgsRange(info, call_state,
1973 pcrLabel, next_call_insn,
1974 target_method,
1975 vtable_idx, direct_code, direct_method,
1976 type, skip_this);
1977 }
1978
1979 /* If no arguments, just return */
1980 if (info->num_arg_words == 0)
1981 return call_state;
1982
1983 const int start_index = skip_this ? 1 : 0;
1984
1985 InToRegStorageX86_64Mapper mapper;
1986 InToRegStorageMapping in_to_reg_storage_mapping;
1987 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
1988 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
1989 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
1990 in_to_reg_storage_mapping.Get(last_mapped_in).Is64BitSolo() ? 2 : 1;
1991 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
1992
1993 // Fisrt of all, check whether it make sense to use bulk copying
1994 // Optimization is aplicable only for range case
1995 // TODO: make a constant instead of 2
1996 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
1997 // Scan the rest of the args - if in phys_reg flush to memory
1998 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
1999 RegLocation loc = info->args[next_arg];
2000 if (loc.wide) {
2001 loc = UpdateLocWide(loc);
2002 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002003 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00002004 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002005 }
2006 next_arg += 2;
2007 } else {
2008 loc = UpdateLoc(loc);
2009 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002010 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00002011 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002012 }
2013 next_arg++;
2014 }
2015 }
2016
2017 // Logic below assumes that Method pointer is at offset zero from SP.
2018 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
2019
2020 // The rest can be copied together
2021 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
2022 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped, cu_->instruction_set);
2023
2024 int current_src_offset = start_offset;
2025 int current_dest_offset = outs_offset;
2026
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002027 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2028 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002029 while (regs_left_to_pass_via_stack > 0) {
2030 // This is based on the knowledge that the stack itself is 16-byte aligned.
2031 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2032 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2033 size_t bytes_to_move;
2034
2035 /*
2036 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2037 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2038 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2039 * We do this because we could potentially do a smaller move to align.
2040 */
2041 if (regs_left_to_pass_via_stack == 4 ||
2042 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2043 // Moving 128-bits via xmm register.
2044 bytes_to_move = sizeof(uint32_t) * 4;
2045
2046 // Allocate a free xmm temp. Since we are working through the calling sequence,
2047 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2048 // there are no free registers.
2049 RegStorage temp = AllocTempDouble();
2050
2051 LIR* ld1 = nullptr;
2052 LIR* ld2 = nullptr;
2053 LIR* st1 = nullptr;
2054 LIR* st2 = nullptr;
2055
2056 /*
2057 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2058 * do an aligned move. If we have 8-byte alignment, then do the move in two
2059 * parts. This approach prevents possible cache line splits. Finally, fall back
2060 * to doing an unaligned move. In most cases we likely won't split the cache
2061 * line but we cannot prove it and thus take a conservative approach.
2062 */
2063 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2064 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2065
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002066 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002067 if (src_is_16b_aligned) {
2068 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
2069 } else if (src_is_8b_aligned) {
2070 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
2071 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
2072 kMovHi128FP);
2073 } else {
2074 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
2075 }
2076
2077 if (dest_is_16b_aligned) {
2078 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
2079 } else if (dest_is_8b_aligned) {
2080 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
2081 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
2082 temp, kMovHi128FP);
2083 } else {
2084 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
2085 }
2086
2087 // TODO If we could keep track of aliasing information for memory accesses that are wider
2088 // than 64-bit, we wouldn't need to set up a barrier.
2089 if (ld1 != nullptr) {
2090 if (ld2 != nullptr) {
2091 // For 64-bit load we can actually set up the aliasing information.
2092 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2093 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2094 } else {
2095 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002096 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002097 }
2098 }
2099 if (st1 != nullptr) {
2100 if (st2 != nullptr) {
2101 // For 64-bit store we can actually set up the aliasing information.
2102 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2103 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2104 } else {
2105 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002106 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002107 }
2108 }
2109
2110 // Free the temporary used for the data movement.
2111 FreeTemp(temp);
2112 } else {
2113 // Moving 32-bits via general purpose register.
2114 bytes_to_move = sizeof(uint32_t);
2115
2116 // Instead of allocating a new temp, simply reuse one of the registers being used
2117 // for argument passing.
2118 RegStorage temp = TargetReg(kArg3);
2119
2120 // Now load the argument VR and store to the outs.
2121 Load32Disp(TargetReg(kSp), current_src_offset, temp);
2122 Store32Disp(TargetReg(kSp), current_dest_offset, temp);
2123 }
2124
2125 current_src_offset += bytes_to_move;
2126 current_dest_offset += bytes_to_move;
2127 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2128 }
2129 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2130 }
2131
2132 // Now handle rest not registers if they are
2133 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
2134 RegStorage regSingle = TargetReg(kArg2);
2135 RegStorage regWide = RegStorage::Solo64(TargetReg(kArg3).GetReg());
Chao-ying Fub6564c12014-06-24 13:24:36 -07002136 for (int i = start_index;
2137 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002138 RegLocation rl_arg = info->args[i];
2139 rl_arg = UpdateRawLoc(rl_arg);
2140 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2141 if (!reg.Valid()) {
2142 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2143
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002144 {
2145 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2146 if (rl_arg.wide) {
2147 if (rl_arg.location == kLocPhysReg) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00002148 StoreBaseDisp(TargetReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002149 } else {
2150 LoadValueDirectWideFixed(rl_arg, regWide);
Andreas Gampe3c12c512014-06-24 18:46:29 +00002151 StoreBaseDisp(TargetReg(kSp), out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002152 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002153 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002154 if (rl_arg.location == kLocPhysReg) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00002155 StoreBaseDisp(TargetReg(kSp), out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002156 } else {
2157 LoadValueDirectFixed(rl_arg, regSingle);
Andreas Gampe3c12c512014-06-24 18:46:29 +00002158 StoreBaseDisp(TargetReg(kSp), out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002159 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002160 }
2161 }
2162 call_state = next_call_insn(cu_, info, call_state, target_method,
2163 vtable_idx, direct_code, direct_method, type);
2164 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002165 if (rl_arg.wide) {
2166 i++;
2167 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002168 }
2169 }
2170
2171 // Finish with mapped registers
2172 for (int i = start_index; i <= last_mapped_in; i++) {
2173 RegLocation rl_arg = info->args[i];
2174 rl_arg = UpdateRawLoc(rl_arg);
2175 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2176 if (reg.Valid()) {
2177 if (rl_arg.wide) {
2178 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002179 } else {
2180 LoadValueDirectFixed(rl_arg, reg);
2181 }
2182 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2183 direct_code, direct_method, type);
2184 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002185 if (rl_arg.wide) {
2186 i++;
2187 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002188 }
2189
2190 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2191 direct_code, direct_method, type);
2192 if (pcrLabel) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002193 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002194 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
2195 } else {
2196 *pcrLabel = nullptr;
2197 // In lieu of generating a check for kArg1 being null, we need to
2198 // perform a load when doing implicit checks.
2199 RegStorage tmp = AllocTemp();
2200 Load32Disp(TargetReg(kArg1), 0, tmp);
2201 MarkPossibleNullPointerException(info->opt_flags);
2202 FreeTemp(tmp);
2203 }
2204 }
2205 return call_state;
2206}
2207
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002208} // namespace art
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002209