blob: db2f2724364141292184de71bea333cc8ceef9c1 [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
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +070017#include <cstdarg>
Nicolas Geoffrayf3e2cc42014-02-18 18:37:26 +000018#include <inttypes.h>
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +070019#include <string>
Nicolas Geoffrayf3e2cc42014-02-18 18:37:26 +000020
Andreas Gampe53c913b2014-08-12 23:19:23 -070021#include "backend_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "codegen_x86.h"
23#include "dex/compiler_internals.h"
24#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070025#include "dex/reg_storage_eq.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010027#include "mirror/art_method.h"
Mark Mendelle19c91f2014-02-25 08:19:08 -080028#include "mirror/string.h"
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -070029#include "oat.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "x86_lir.h"
Tong Shen547cdfd2014-08-05 01:54:19 -070031#include "utils/dwarf_cfi.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
Brian Carlstrom7940e442013-07-12 13:46:57 -070033namespace art {
34
Vladimir Marko089142c2014-06-05 10:57:05 +010035static constexpr RegStorage core_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070036 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
37};
Vladimir Marko089142c2014-06-05 10:57:05 +010038static constexpr RegStorage core_regs_arr_64[] = {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070039 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
buzbee091cc402014-03-31 10:14:40 -070040 rs_r8, rs_r9, rs_r10, rs_r11, rs_r12, rs_r13, rs_r14, rs_r15
Brian Carlstrom7940e442013-07-12 13:46:57 -070041};
Vladimir Marko089142c2014-06-05 10:57:05 +010042static constexpr RegStorage core_regs_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070043 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 +070044 rs_r8q, rs_r9q, rs_r10q, rs_r11q, rs_r12q, rs_r13q, rs_r14q, rs_r15q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070045};
Vladimir Marko089142c2014-06-05 10:57:05 +010046static constexpr RegStorage sp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070047 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
48};
Vladimir Marko089142c2014-06-05 10:57:05 +010049static constexpr RegStorage sp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070050 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
buzbee091cc402014-03-31 10:14:40 -070051 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070052};
Vladimir Marko089142c2014-06-05 10:57:05 +010053static constexpr RegStorage dp_regs_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070054 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
55};
Vladimir Marko089142c2014-06-05 10:57:05 +010056static constexpr RegStorage dp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070057 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
buzbee091cc402014-03-31 10:14:40 -070058 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070059};
Serguei Katkovc3801912014-07-08 17:21:53 +070060static constexpr RegStorage xp_regs_arr_32[] = {
61 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
62};
63static constexpr RegStorage xp_regs_arr_64[] = {
64 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
65 rs_xr8, rs_xr9, rs_xr10, rs_xr11, rs_xr12, rs_xr13, rs_xr14, rs_xr15
66};
Vladimir Marko089142c2014-06-05 10:57:05 +010067static constexpr RegStorage reserved_regs_arr_32[] = {rs_rX86_SP_32};
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +070068static constexpr RegStorage reserved_regs_arr_64[] = {rs_rX86_SP_32};
Vladimir Marko089142c2014-06-05 10:57:05 +010069static constexpr RegStorage reserved_regs_arr_64q[] = {rs_rX86_SP_64};
70static constexpr RegStorage core_temps_arr_32[] = {rs_rAX, rs_rCX, rs_rDX, rs_rBX};
71static constexpr RegStorage core_temps_arr_64[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070072 rs_rAX, rs_rCX, rs_rDX, rs_rSI, rs_rDI,
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070073 rs_r8, rs_r9, rs_r10, rs_r11
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070074};
Serguei Katkovc3801912014-07-08 17:21:53 +070075
76// How to add register to be available for promotion:
77// 1) Remove register from array defining temp
78// 2) Update ClobberCallerSave
79// 3) Update JNI compiler ABI:
80// 3.1) add reg in JniCallingConvention method
81// 3.2) update CoreSpillMask/FpSpillMask
82// 4) Update entrypoints
83// 4.1) Update constants in asm_support_x86_64.h for new frame size
84// 4.2) Remove entry in SmashCallerSaves
85// 4.3) Update jni_entrypoints to spill/unspill new callee save reg
86// 4.4) Update quick_entrypoints to spill/unspill new callee save reg
87// 5) Update runtime ABI
88// 5.1) Update quick_method_frame_info with new required spills
89// 5.2) Update QuickArgumentVisitor with new offsets to gprs and xmms
90// Note that you cannot use register corresponding to incoming args
91// according to ABI and QCG needs one additional XMM temp for
92// bulk copy in preparation to call.
Vladimir Marko089142c2014-06-05 10:57:05 +010093static constexpr RegStorage core_temps_arr_64q[] = {
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070094 rs_r0q, rs_r1q, rs_r2q, rs_r6q, rs_r7q,
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070095 rs_r8q, rs_r9q, rs_r10q, rs_r11q
Dmitry Petrochenko0999a6f2014-05-22 12:26:50 +070096};
Vladimir Marko089142c2014-06-05 10:57:05 +010097static constexpr RegStorage sp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070098 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
99};
Vladimir Marko089142c2014-06-05 10:57:05 +0100100static constexpr RegStorage sp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -0700101 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700102 rs_fr8, rs_fr9, rs_fr10, rs_fr11
buzbee091cc402014-03-31 10:14:40 -0700103};
Vladimir Marko089142c2014-06-05 10:57:05 +0100104static constexpr RegStorage dp_temps_arr_32[] = {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700105 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
106};
Vladimir Marko089142c2014-06-05 10:57:05 +0100107static constexpr RegStorage dp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -0700108 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700109 rs_dr8, rs_dr9, rs_dr10, rs_dr11
buzbee091cc402014-03-31 10:14:40 -0700110};
111
Vladimir Marko089142c2014-06-05 10:57:05 +0100112static constexpr RegStorage xp_temps_arr_32[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400113 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
114};
Vladimir Marko089142c2014-06-05 10:57:05 +0100115static constexpr RegStorage xp_temps_arr_64[] = {
Mark Mendellfe945782014-05-22 09:52:36 -0400116 rs_xr0, rs_xr1, rs_xr2, rs_xr3, rs_xr4, rs_xr5, rs_xr6, rs_xr7,
Serguei Katkovc3801912014-07-08 17:21:53 +0700117 rs_xr8, rs_xr9, rs_xr10, rs_xr11
Mark Mendellfe945782014-05-22 09:52:36 -0400118};
119
Vladimir Marko089142c2014-06-05 10:57:05 +0100120static constexpr ArrayRef<const RegStorage> empty_pool;
121static constexpr ArrayRef<const RegStorage> core_regs_32(core_regs_arr_32);
122static constexpr ArrayRef<const RegStorage> core_regs_64(core_regs_arr_64);
123static constexpr ArrayRef<const RegStorage> core_regs_64q(core_regs_arr_64q);
124static constexpr ArrayRef<const RegStorage> sp_regs_32(sp_regs_arr_32);
125static constexpr ArrayRef<const RegStorage> sp_regs_64(sp_regs_arr_64);
126static constexpr ArrayRef<const RegStorage> dp_regs_32(dp_regs_arr_32);
127static constexpr ArrayRef<const RegStorage> dp_regs_64(dp_regs_arr_64);
Serguei Katkovc3801912014-07-08 17:21:53 +0700128static constexpr ArrayRef<const RegStorage> xp_regs_32(xp_regs_arr_32);
129static constexpr ArrayRef<const RegStorage> xp_regs_64(xp_regs_arr_64);
Vladimir Marko089142c2014-06-05 10:57:05 +0100130static constexpr ArrayRef<const RegStorage> reserved_regs_32(reserved_regs_arr_32);
131static constexpr ArrayRef<const RegStorage> reserved_regs_64(reserved_regs_arr_64);
132static constexpr ArrayRef<const RegStorage> reserved_regs_64q(reserved_regs_arr_64q);
133static constexpr ArrayRef<const RegStorage> core_temps_32(core_temps_arr_32);
134static constexpr ArrayRef<const RegStorage> core_temps_64(core_temps_arr_64);
135static constexpr ArrayRef<const RegStorage> core_temps_64q(core_temps_arr_64q);
136static constexpr ArrayRef<const RegStorage> sp_temps_32(sp_temps_arr_32);
137static constexpr ArrayRef<const RegStorage> sp_temps_64(sp_temps_arr_64);
138static constexpr ArrayRef<const RegStorage> dp_temps_32(dp_temps_arr_32);
139static constexpr ArrayRef<const RegStorage> dp_temps_64(dp_temps_arr_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700140
Vladimir Marko089142c2014-06-05 10:57:05 +0100141static constexpr ArrayRef<const RegStorage> xp_temps_32(xp_temps_arr_32);
142static constexpr ArrayRef<const RegStorage> xp_temps_64(xp_temps_arr_64);
Mark Mendellfe945782014-05-22 09:52:36 -0400143
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700144RegLocation X86Mir2Lir::LocCReturn() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000145 return x86_loc_c_return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146}
147
buzbeea0cd2d72014-06-01 09:33:49 -0700148RegLocation X86Mir2Lir::LocCReturnRef() {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700149 return cu_->target64 ? x86_64_loc_c_return_ref : x86_loc_c_return_ref;
buzbeea0cd2d72014-06-01 09:33:49 -0700150}
151
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700152RegLocation X86Mir2Lir::LocCReturnWide() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700153 return cu_->target64 ? x86_64_loc_c_return_wide : x86_loc_c_return_wide;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154}
155
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700156RegLocation X86Mir2Lir::LocCReturnFloat() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000157 return x86_loc_c_return_float;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158}
159
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700160RegLocation X86Mir2Lir::LocCReturnDouble() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000161 return x86_loc_c_return_double;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162}
163
Ian Rogersb28c1c02014-11-08 11:21:21 -0800164// 32-bit reg storage locations for 32-bit targets.
165static const RegStorage RegStorage32FromSpecialTargetRegister_Target32[] {
166 RegStorage::InvalidReg(), // kSelf - Thread pointer.
167 RegStorage::InvalidReg(), // kSuspend - Used to reduce suspend checks for some targets.
168 RegStorage::InvalidReg(), // kLr - no register as the return address is pushed on entry.
169 RegStorage::InvalidReg(), // kPc - not exposed on X86 see kX86StartOfMethod.
170 rs_rX86_SP_32, // kSp
171 rs_rAX, // kArg0
172 rs_rCX, // kArg1
173 rs_rDX, // kArg2
174 rs_rBX, // kArg3
175 RegStorage::InvalidReg(), // kArg4
176 RegStorage::InvalidReg(), // kArg5
177 RegStorage::InvalidReg(), // kArg6
178 RegStorage::InvalidReg(), // kArg7
179 rs_rAX, // kFArg0
180 rs_rCX, // kFArg1
181 rs_rDX, // kFArg2
182 rs_rBX, // kFArg3
183 RegStorage::InvalidReg(), // kFArg4
184 RegStorage::InvalidReg(), // kFArg5
185 RegStorage::InvalidReg(), // kFArg6
186 RegStorage::InvalidReg(), // kFArg7
187 RegStorage::InvalidReg(), // kFArg8
188 RegStorage::InvalidReg(), // kFArg9
189 RegStorage::InvalidReg(), // kFArg10
190 RegStorage::InvalidReg(), // kFArg11
191 RegStorage::InvalidReg(), // kFArg12
192 RegStorage::InvalidReg(), // kFArg13
193 RegStorage::InvalidReg(), // kFArg14
194 RegStorage::InvalidReg(), // kFArg15
195 rs_rAX, // kRet0
196 rs_rDX, // kRet1
197 rs_rAX, // kInvokeTgt
198 rs_rAX, // kHiddenArg - used to hold the method index before copying to fr0.
199 rs_fr0, // kHiddenFpArg
200 rs_rCX, // kCount
201};
202
203// 32-bit reg storage locations for 64-bit targets.
204static const RegStorage RegStorage32FromSpecialTargetRegister_Target64[] {
205 RegStorage::InvalidReg(), // kSelf - Thread pointer.
206 RegStorage::InvalidReg(), // kSuspend - Used to reduce suspend checks for some targets.
207 RegStorage::InvalidReg(), // kLr - no register as the return address is pushed on entry.
208 RegStorage::InvalidReg(), // kPc - TODO: RIP based addressing.
209 rs_rX86_SP_32, // kSp
210 rs_rDI, // kArg0
211 rs_rSI, // kArg1
212 rs_rDX, // kArg2
213 rs_rCX, // kArg3
214 rs_r8, // kArg4
215 rs_r9, // kArg5
216 RegStorage::InvalidReg(), // kArg6
217 RegStorage::InvalidReg(), // kArg7
218 rs_fr0, // kFArg0
219 rs_fr1, // kFArg1
220 rs_fr2, // kFArg2
221 rs_fr3, // kFArg3
222 rs_fr4, // kFArg4
223 rs_fr5, // kFArg5
224 rs_fr6, // kFArg6
225 rs_fr7, // kFArg7
226 RegStorage::InvalidReg(), // kFArg8
227 RegStorage::InvalidReg(), // kFArg9
228 RegStorage::InvalidReg(), // kFArg10
229 RegStorage::InvalidReg(), // kFArg11
230 RegStorage::InvalidReg(), // kFArg12
231 RegStorage::InvalidReg(), // kFArg13
232 RegStorage::InvalidReg(), // kFArg14
233 RegStorage::InvalidReg(), // kFArg15
234 rs_rAX, // kRet0
235 rs_rDX, // kRet1
236 rs_rAX, // kInvokeTgt
237 rs_rAX, // kHiddenArg
238 RegStorage::InvalidReg(), // kHiddenFpArg
239 rs_rCX, // kCount
240};
241static_assert(arraysize(RegStorage32FromSpecialTargetRegister_Target32) ==
242 arraysize(RegStorage32FromSpecialTargetRegister_Target64),
243 "Mismatch in RegStorage array sizes");
244
Chao-ying Fua77ee512014-07-01 17:43:41 -0700245// Return a target-dependent special register for 32-bit.
Ian Rogersb28c1c02014-11-08 11:21:21 -0800246RegStorage X86Mir2Lir::TargetReg32(SpecialTargetRegister reg) const {
247 DCHECK_EQ(RegStorage32FromSpecialTargetRegister_Target32[kCount], rs_rCX);
248 DCHECK_EQ(RegStorage32FromSpecialTargetRegister_Target64[kCount], rs_rCX);
249 DCHECK_LT(reg, arraysize(RegStorage32FromSpecialTargetRegister_Target32));
250 return cu_->target64 ? RegStorage32FromSpecialTargetRegister_Target64[reg]
251 : RegStorage32FromSpecialTargetRegister_Target32[reg];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252}
253
Chao-ying Fua77ee512014-07-01 17:43:41 -0700254RegStorage X86Mir2Lir::TargetReg(SpecialTargetRegister reg) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700255 UNUSED(reg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700256 LOG(FATAL) << "Do not use this function!!!";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700257 UNREACHABLE();
Chao-ying Fua77ee512014-07-01 17:43:41 -0700258}
259
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260/*
261 * Decode the register id.
262 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100263ResourceMask X86Mir2Lir::GetRegMaskCommon(const RegStorage& reg) const {
264 /* Double registers in x86 are just a single FP register. This is always just a single bit. */
265 return ResourceMask::Bit(
266 /* FP register starts at bit position 16 */
267 ((reg.IsFloat() || reg.StorageSize() > 8) ? kX86FPReg0 : 0) + reg.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268}
269
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100270ResourceMask X86Mir2Lir::GetPCUseDefEncoding() const {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100271 return kEncodeNone;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272}
273
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100274void X86Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags,
275 ResourceMask* use_mask, ResourceMask* def_mask) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700276 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbeeb48819d2013-09-14 16:15:25 -0700277 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278
279 // X86-specific resource map setup here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 if (flags & REG_USE_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100281 use_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 }
283
284 if (flags & REG_DEF_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100285 def_mask->SetBit(kX86RegSP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 }
287
288 if (flags & REG_DEFA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100289 SetupRegMask(def_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 }
291
292 if (flags & REG_DEFD) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100293 SetupRegMask(def_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 }
295 if (flags & REG_USEA) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100296 SetupRegMask(use_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 }
298
299 if (flags & REG_USEC) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100300 SetupRegMask(use_mask, rs_rCX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 }
302
303 if (flags & REG_USED) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100304 SetupRegMask(use_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 }
Vladimir Marko70b797d2013-12-03 15:25:24 +0000306
307 if (flags & REG_USEB) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100308 SetupRegMask(use_mask, rs_rBX.GetReg());
Vladimir Marko70b797d2013-12-03 15:25:24 +0000309 }
Mark Mendell4028a6c2014-02-19 20:06:20 -0800310
311 // Fixup hard to describe instruction: Uses rAX, rCX, rDI; sets rDI.
312 if (lir->opcode == kX86RepneScasw) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100313 SetupRegMask(use_mask, rs_rAX.GetReg());
314 SetupRegMask(use_mask, rs_rCX.GetReg());
315 SetupRegMask(use_mask, rs_rDI.GetReg());
316 SetupRegMask(def_mask, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800317 }
Serguei Katkove90501d2014-03-12 15:56:54 +0700318
319 if (flags & USE_FP_STACK) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100320 use_mask->SetBit(kX86FPStack);
321 def_mask->SetBit(kX86FPStack);
Serguei Katkove90501d2014-03-12 15:56:54 +0700322 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323}
324
325/* For dumping instructions */
326static const char* x86RegName[] = {
327 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
328 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
329};
330
331static const char* x86CondName[] = {
332 "O",
333 "NO",
334 "B/NAE/C",
335 "NB/AE/NC",
336 "Z/EQ",
337 "NZ/NE",
338 "BE/NA",
339 "NBE/A",
340 "S",
341 "NS",
342 "P/PE",
343 "NP/PO",
344 "L/NGE",
345 "NL/GE",
346 "LE/NG",
347 "NLE/G"
348};
349
350/*
351 * Interpret a format string and build a string no longer than size
352 * See format key in Assemble.cc.
353 */
354std::string X86Mir2Lir::BuildInsnString(const char *fmt, LIR *lir, unsigned char* base_addr) {
355 std::string buf;
356 size_t i = 0;
357 size_t fmt_len = strlen(fmt);
358 while (i < fmt_len) {
359 if (fmt[i] != '!') {
360 buf += fmt[i];
361 i++;
362 } else {
363 i++;
364 DCHECK_LT(i, fmt_len);
365 char operand_number_ch = fmt[i];
366 i++;
367 if (operand_number_ch == '!') {
368 buf += "!";
369 } else {
370 int operand_number = operand_number_ch - '0';
371 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
372 DCHECK_LT(i, fmt_len);
373 int operand = lir->operands[operand_number];
374 switch (fmt[i]) {
375 case 'c':
376 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
377 buf += x86CondName[operand];
378 break;
379 case 'd':
380 buf += StringPrintf("%d", operand);
381 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400382 case 'q': {
383 int64_t value = static_cast<int64_t>(static_cast<int64_t>(operand) << 32 |
384 static_cast<uint32_t>(lir->operands[operand_number+1]));
385 buf +=StringPrintf("%" PRId64, value);
Haitao Fenge70f1792014-08-09 08:31:02 +0800386 break;
Yixin Shou5192cbb2014-07-01 13:48:17 -0400387 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 case 'p': {
buzbee0d829482013-10-11 15:24:55 -0700389 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(operand));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 buf += StringPrintf("0x%08x", tab_rec->offset);
391 break;
392 }
393 case 'r':
buzbee091cc402014-03-31 10:14:40 -0700394 if (RegStorage::IsFloat(operand)) {
395 int fp_reg = RegStorage::RegNum(operand);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 buf += StringPrintf("xmm%d", fp_reg);
397 } else {
buzbee091cc402014-03-31 10:14:40 -0700398 int reg_num = RegStorage::RegNum(operand);
399 DCHECK_LT(static_cast<size_t>(reg_num), sizeof(x86RegName));
400 buf += x86RegName[reg_num];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 }
402 break;
403 case 't':
Ian Rogers107c31e2014-01-23 20:55:29 -0800404 buf += StringPrintf("0x%08" PRIxPTR " (L%p)",
405 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + operand,
406 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 break;
408 default:
409 buf += StringPrintf("DecodeError '%c'", fmt[i]);
410 break;
411 }
412 i++;
413 }
414 }
415 }
416 return buf;
417}
418
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100419void X86Mir2Lir::DumpResourceMask(LIR *x86LIR, const ResourceMask& mask, const char *prefix) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 char buf[256];
421 buf[0] = 0;
422
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100423 if (mask.Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 strcpy(buf, "all");
425 } else {
426 char num[8];
427 int i;
428
429 for (i = 0; i < kX86RegEnd; i++) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100430 if (mask.HasBit(i)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800431 snprintf(num, arraysize(num), "%d ", i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 strcat(buf, num);
433 }
434 }
435
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100436 if (mask.HasBit(ResourceMask::kCCode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 strcat(buf, "cc ");
438 }
439 /* Memory bits */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100440 if (x86LIR && (mask.HasBit(ResourceMask::kDalvikReg))) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800441 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
442 DECODE_ALIAS_INFO_REG(x86LIR->flags.alias_info),
443 (DECODE_ALIAS_INFO_WIDE(x86LIR->flags.alias_info)) ? "(+1)" : "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100445 if (mask.HasBit(ResourceMask::kLiteral)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 strcat(buf, "lit ");
447 }
448
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100449 if (mask.HasBit(ResourceMask::kHeapRef)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 strcat(buf, "heap ");
451 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100452 if (mask.HasBit(ResourceMask::kMustNotAlias)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 strcat(buf, "noalias ");
454 }
455 }
456 if (buf[0]) {
457 LOG(INFO) << prefix << ": " << buf;
458 }
459}
460
461void X86Mir2Lir::AdjustSpillMask() {
462 // Adjustment for LR spilling, x86 has no LR so nothing to do here
buzbee091cc402014-03-31 10:14:40 -0700463 core_spill_mask_ |= (1 << rs_rRET.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 num_core_spills_++;
465}
466
Mark Mendelle87f9b52014-04-30 14:13:18 -0400467RegStorage X86Mir2Lir::AllocateByteRegister() {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700468 RegStorage reg = AllocTypedTemp(false, kCoreReg);
Elena Sayapinadd644502014-07-01 18:39:52 +0700469 if (!cu_->target64) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800470 DCHECK_LT(reg.GetRegNum(), rs_rX86_SP_32.GetRegNum());
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700471 }
472 return reg;
473}
474
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700475RegStorage X86Mir2Lir::Get128BitRegister(RegStorage reg) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700476 return GetRegInfo(reg)->Master()->GetReg();
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700477}
478
Ian Rogersb28c1c02014-11-08 11:21:21 -0800479bool X86Mir2Lir::IsByteRegister(RegStorage reg) const {
480 return cu_->target64 || reg.GetRegNum() < rs_rX86_SP_32.GetRegNum();
Mark Mendelle87f9b52014-04-30 14:13:18 -0400481}
482
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483/* Clobber all regs that might be used by an external C call */
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000484void X86Mir2Lir::ClobberCallerSave() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700485 if (cu_->target64) {
Serguei Katkovc3801912014-07-08 17:21:53 +0700486 Clobber(rs_rAX);
487 Clobber(rs_rCX);
488 Clobber(rs_rDX);
489 Clobber(rs_rSI);
490 Clobber(rs_rDI);
491
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700492 Clobber(rs_r8);
493 Clobber(rs_r9);
494 Clobber(rs_r10);
495 Clobber(rs_r11);
496
497 Clobber(rs_fr8);
498 Clobber(rs_fr9);
499 Clobber(rs_fr10);
500 Clobber(rs_fr11);
Serguei Katkovc3801912014-07-08 17:21:53 +0700501 } else {
502 Clobber(rs_rAX);
503 Clobber(rs_rCX);
504 Clobber(rs_rDX);
505 Clobber(rs_rBX);
Chao-ying Fu35ec2b52014-06-16 16:40:31 -0700506 }
Serguei Katkovc3801912014-07-08 17:21:53 +0700507
508 Clobber(rs_fr0);
509 Clobber(rs_fr1);
510 Clobber(rs_fr2);
511 Clobber(rs_fr3);
512 Clobber(rs_fr4);
513 Clobber(rs_fr5);
514 Clobber(rs_fr6);
515 Clobber(rs_fr7);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516}
517
518RegLocation X86Mir2Lir::GetReturnWideAlt() {
519 RegLocation res = LocCReturnWide();
Ian Rogersb28c1c02014-11-08 11:21:21 -0800520 DCHECK_EQ(res.reg.GetLowReg(), rs_rAX.GetReg());
521 DCHECK_EQ(res.reg.GetHighReg(), rs_rDX.GetReg());
buzbee091cc402014-03-31 10:14:40 -0700522 Clobber(rs_rAX);
523 Clobber(rs_rDX);
524 MarkInUse(rs_rAX);
525 MarkInUse(rs_rDX);
526 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 return res;
528}
529
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700530RegLocation X86Mir2Lir::GetReturnAlt() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 RegLocation res = LocCReturn();
buzbee091cc402014-03-31 10:14:40 -0700532 res.reg.SetReg(rs_rDX.GetReg());
533 Clobber(rs_rDX);
534 MarkInUse(rs_rDX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 return res;
536}
537
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700539void X86Mir2Lir::LockCallTemps() {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800540 LockTemp(TargetReg32(kArg0));
541 LockTemp(TargetReg32(kArg1));
542 LockTemp(TargetReg32(kArg2));
543 LockTemp(TargetReg32(kArg3));
Elena Sayapinadd644502014-07-01 18:39:52 +0700544 if (cu_->target64) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800545 LockTemp(TargetReg32(kArg4));
546 LockTemp(TargetReg32(kArg5));
547 LockTemp(TargetReg32(kFArg0));
548 LockTemp(TargetReg32(kFArg1));
549 LockTemp(TargetReg32(kFArg2));
550 LockTemp(TargetReg32(kFArg3));
551 LockTemp(TargetReg32(kFArg4));
552 LockTemp(TargetReg32(kFArg5));
553 LockTemp(TargetReg32(kFArg6));
554 LockTemp(TargetReg32(kFArg7));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700555 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556}
557
558/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700559void X86Mir2Lir::FreeCallTemps() {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800560 FreeTemp(TargetReg32(kArg0));
561 FreeTemp(TargetReg32(kArg1));
562 FreeTemp(TargetReg32(kArg2));
563 FreeTemp(TargetReg32(kArg3));
Elena Sayapinadd644502014-07-01 18:39:52 +0700564 if (cu_->target64) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800565 FreeTemp(TargetReg32(kArg4));
566 FreeTemp(TargetReg32(kArg5));
567 FreeTemp(TargetReg32(kFArg0));
568 FreeTemp(TargetReg32(kFArg1));
569 FreeTemp(TargetReg32(kFArg2));
570 FreeTemp(TargetReg32(kFArg3));
571 FreeTemp(TargetReg32(kFArg4));
572 FreeTemp(TargetReg32(kFArg5));
573 FreeTemp(TargetReg32(kFArg6));
574 FreeTemp(TargetReg32(kFArg7));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700575 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576}
577
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800578bool X86Mir2Lir::ProvidesFullMemoryBarrier(X86OpCode opcode) {
579 switch (opcode) {
580 case kX86LockCmpxchgMR:
581 case kX86LockCmpxchgAR:
Ian Rogers0f9b9c52014-06-09 01:32:12 -0700582 case kX86LockCmpxchg64M:
583 case kX86LockCmpxchg64A:
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800584 case kX86XchgMR:
585 case kX86Mfence:
586 // Atomic memory instructions provide full barrier.
587 return true;
588 default:
589 break;
590 }
591
592 // Conservative if cannot prove it provides full barrier.
593 return false;
594}
595
Andreas Gampeb14329f2014-05-15 11:16:06 -0700596bool X86Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800598 // Start off with using the last LIR as the barrier. If it is not enough, then we will update it.
599 LIR* mem_barrier = last_lir_insn_;
600
Andreas Gampeb14329f2014-05-15 11:16:06 -0700601 bool ret = false;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800602 /*
Hans Boehm48f5c472014-06-27 14:50:10 -0700603 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
604 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
605 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800606 */
Hans Boehm48f5c472014-06-27 14:50:10 -0700607 if (barrier_kind == kAnyAny) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800608 // If no LIR exists already that can be used a barrier, then generate an mfence.
609 if (mem_barrier == nullptr) {
610 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700611 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800612 }
613
614 // If last instruction does not provide full barrier, then insert an mfence.
615 if (ProvidesFullMemoryBarrier(static_cast<X86OpCode>(mem_barrier->opcode)) == false) {
616 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700617 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800618 }
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700619 } else if (barrier_kind == kNTStoreStore) {
620 mem_barrier = NewLIR0(kX86Sfence);
621 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800622 }
623
624 // Now ensure that a scheduling barrier is in place.
625 if (mem_barrier == nullptr) {
626 GenBarrier();
627 } else {
628 // Mark as a scheduling barrier.
629 DCHECK(!mem_barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100630 mem_barrier->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800631 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700632 return ret;
633#else
634 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635#endif
636}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000637
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638void X86Mir2Lir::CompilerInitializeRegAlloc() {
Elena Sayapinadd644502014-07-01 18:39:52 +0700639 if (cu_->target64) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100640 reg_pool_.reset(new (arena_) RegisterPool(this, arena_, core_regs_64, core_regs_64q, sp_regs_64,
641 dp_regs_64, reserved_regs_64, reserved_regs_64q,
642 core_temps_64, core_temps_64q,
643 sp_temps_64, dp_temps_64));
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700644 } else {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100645 reg_pool_.reset(new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
646 dp_regs_32, reserved_regs_32, empty_pool,
647 core_temps_32, empty_pool,
648 sp_temps_32, dp_temps_32));
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700649 }
buzbee091cc402014-03-31 10:14:40 -0700650
651 // Target-specific adjustments.
652
Mark Mendellfe945782014-05-22 09:52:36 -0400653 // Add in XMM registers.
Serguei Katkovc3801912014-07-08 17:21:53 +0700654 const ArrayRef<const RegStorage> *xp_regs = cu_->target64 ? &xp_regs_64 : &xp_regs_32;
655 for (RegStorage reg : *xp_regs) {
Mark Mendellfe945782014-05-22 09:52:36 -0400656 RegisterInfo* info = new (arena_) RegisterInfo(reg, GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100657 reginfo_map_[reg.GetReg()] = info;
Serguei Katkovc3801912014-07-08 17:21:53 +0700658 }
659 const ArrayRef<const RegStorage> *xp_temps = cu_->target64 ? &xp_temps_64 : &xp_temps_32;
660 for (RegStorage reg : *xp_temps) {
661 RegisterInfo* xp_reg_info = GetRegInfo(reg);
662 xp_reg_info->SetIsTemp(true);
Mark Mendellfe945782014-05-22 09:52:36 -0400663 }
664
buzbee091cc402014-03-31 10:14:40 -0700665 // Alias single precision xmm to double xmms.
666 // TODO: as needed, add larger vector sizes - alias all to the largest.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100667 for (RegisterInfo* info : reg_pool_->sp_regs_) {
buzbee091cc402014-03-31 10:14:40 -0700668 int sp_reg_num = info->GetReg().GetRegNum();
Mark Mendellfe945782014-05-22 09:52:36 -0400669 RegStorage xp_reg = RegStorage::Solo128(sp_reg_num);
670 RegisterInfo* xp_reg_info = GetRegInfo(xp_reg);
671 // 128-bit xmm vector register's master storage should refer to itself.
672 DCHECK_EQ(xp_reg_info, xp_reg_info->Master());
673
674 // Redirect 32-bit vector's master storage to 128-bit vector.
675 info->SetMaster(xp_reg_info);
676
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700677 RegStorage dp_reg = RegStorage::FloatSolo64(sp_reg_num);
buzbee091cc402014-03-31 10:14:40 -0700678 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
Mark Mendellfe945782014-05-22 09:52:36 -0400679 // Redirect 64-bit vector's master storage to 128-bit vector.
680 dp_reg_info->SetMaster(xp_reg_info);
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700681 // Singles should show a single 32-bit mask bit, at first referring to the low half.
682 DCHECK_EQ(info->StorageMask(), 0x1U);
683 }
684
Elena Sayapinadd644502014-07-01 18:39:52 +0700685 if (cu_->target64) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700686 // Alias 32bit W registers to corresponding 64bit X registers.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100687 for (RegisterInfo* info : reg_pool_->core_regs_) {
Dmitry Petrochenko76af0d32014-06-05 21:15:08 +0700688 int x_reg_num = info->GetReg().GetRegNum();
689 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
690 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
691 // 64bit X register's master storage should refer to itself.
692 DCHECK_EQ(x_reg_info, x_reg_info->Master());
693 // Redirect 32bit W master storage to 64bit X.
694 info->SetMaster(x_reg_info);
695 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
696 DCHECK_EQ(info->StorageMask(), 0x1U);
697 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 }
buzbee091cc402014-03-31 10:14:40 -0700699
700 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
701 // TODO: adjust for x86/hard float calling convention.
702 reg_pool_->next_core_reg_ = 2;
703 reg_pool_->next_sp_reg_ = 2;
704 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705}
706
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700707int X86Mir2Lir::VectorRegisterSize() {
708 return 128;
709}
710
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -0700711int X86Mir2Lir::NumReservableVectorRegisters(bool long_or_fp) {
712 int num_vector_temps = cu_->target64 ? xp_temps_64.size() : xp_temps_32.size();
713
714 // Leave a few temps for use by backend as scratch.
715 return long_or_fp ? num_vector_temps - 2 : num_vector_temps - 1;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700716}
717
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718void X86Mir2Lir::SpillCoreRegs() {
719 if (num_core_spills_ == 0) {
720 return;
721 }
722 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700723 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Ian Rogersb28c1c02014-11-08 11:21:21 -0800724 int offset =
725 frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700726 OpSize size = cu_->target64 ? k64 : k32;
Ian Rogersb28c1c02014-11-08 11:21:21 -0800727 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 for (int reg = 0; mask; mask >>= 1, reg++) {
729 if (mask & 0x1) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800730 StoreBaseDisp(rs_rSP, offset,
731 cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
Serguei Katkovc3801912014-07-08 17:21:53 +0700732 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700733 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 }
735 }
736}
737
738void X86Mir2Lir::UnSpillCoreRegs() {
739 if (num_core_spills_ == 0) {
740 return;
741 }
742 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700743 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700744 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Serguei Katkovc3801912014-07-08 17:21:53 +0700745 OpSize size = cu_->target64 ? k64 : k32;
Ian Rogersb28c1c02014-11-08 11:21:21 -0800746 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 for (int reg = 0; mask; mask >>= 1, reg++) {
748 if (mask & 0x1) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800749 LoadBaseDisp(rs_rSP, offset, cu_->target64 ? RegStorage::Solo64(reg) : RegStorage::Solo32(reg),
Serguei Katkovc3801912014-07-08 17:21:53 +0700750 size, kNotVolatile);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700751 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 }
753 }
754}
755
Serguei Katkovc3801912014-07-08 17:21:53 +0700756void X86Mir2Lir::SpillFPRegs() {
757 if (num_fp_spills_ == 0) {
758 return;
759 }
760 uint32_t mask = fp_spill_mask_;
Ian Rogersb28c1c02014-11-08 11:21:21 -0800761 int offset = frame_size_ -
762 (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
763 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Serguei Katkovc3801912014-07-08 17:21:53 +0700764 for (int reg = 0; mask; mask >>= 1, reg++) {
765 if (mask & 0x1) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800766 StoreBaseDisp(rs_rSP, offset, RegStorage::FloatSolo64(reg), k64, kNotVolatile);
Serguei Katkovc3801912014-07-08 17:21:53 +0700767 offset += sizeof(double);
768 }
769 }
770}
771void X86Mir2Lir::UnSpillFPRegs() {
772 if (num_fp_spills_ == 0) {
773 return;
774 }
775 uint32_t mask = fp_spill_mask_;
Ian Rogersb28c1c02014-11-08 11:21:21 -0800776 int offset = frame_size_ -
777 (GetInstructionSetPointerSize(cu_->instruction_set) * (num_fp_spills_ + num_core_spills_));
778 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
Serguei Katkovc3801912014-07-08 17:21:53 +0700779 for (int reg = 0; mask; mask >>= 1, reg++) {
780 if (mask & 0x1) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800781 LoadBaseDisp(rs_rSP, offset, RegStorage::FloatSolo64(reg),
Serguei Katkovc3801912014-07-08 17:21:53 +0700782 k64, kNotVolatile);
783 offset += sizeof(double);
784 }
785 }
786}
787
788
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700789bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
791}
792
Vladimir Marko674744e2014-04-24 15:18:26 +0100793RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700794 // X86_64 can handle any size.
Elena Sayapinadd644502014-07-01 18:39:52 +0700795 if (cu_->target64) {
Chao-ying Fu06839f82014-08-14 15:59:17 -0700796 return RegClassBySize(size);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -0700797 }
798
Vladimir Marko674744e2014-04-24 15:18:26 +0100799 if (UNLIKELY(is_volatile)) {
800 // On x86, atomic 64-bit load/store requires an fp register.
801 // Smaller aligned load/store is atomic for both core and fp registers.
802 if (size == k64 || size == kDouble) {
803 return kFPReg;
804 }
805 }
806 return RegClassBySize(size);
807}
808
Elena Sayapinadd644502014-07-01 18:39:52 +0700809X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800810 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700811 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100812 method_address_insns_(arena->Adapter()),
813 class_type_address_insns_(arena->Adapter()),
814 call_method_insns_(arena->Adapter()),
Elena Sayapinadd644502014-07-01 18:39:52 +0700815 stack_decrement_(nullptr), stack_increment_(nullptr),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400816 const_vectors_(nullptr) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100817 method_address_insns_.reserve(100);
818 class_type_address_insns_.reserve(100);
819 call_method_insns_.reserve(100);
Mark Mendelld65c51a2014-04-29 16:55:20 -0400820 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700821 for (int i = 0; i < kX86Last; i++) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700822 DCHECK_EQ(X86Mir2Lir::EncodingMap[i].opcode, i)
823 << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
824 << " is wrong: expecting " << i << ", seeing "
825 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 }
827}
828
829Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
830 ArenaAllocator* const arena) {
Elena Sayapinadd644502014-07-01 18:39:52 +0700831 return new X86Mir2Lir(cu, mir_graph, arena);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832}
833
Andreas Gampe98430592014-07-27 19:44:50 -0700834// Not used in x86(-64)
835RegStorage X86Mir2Lir::LoadHelper(QuickEntrypointEnum trampoline) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700836 UNUSED(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700837 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700838 UNREACHABLE();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700839}
840
Dave Allisonb373e092014-02-20 16:06:36 -0800841LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
Dave Allison69dfe512014-07-11 17:11:58 +0000842 // First load the pointer in fs:[suspend-trigger] into eax
843 // Then use a test instruction to indirect via that address.
Dave Allisondfd3b472014-07-16 16:04:32 -0700844 if (cu_->target64) {
845 NewLIR2(kX86Mov64RT, rs_rAX.GetReg(),
846 Thread::ThreadSuspendTriggerOffset<8>().Int32Value());
847 } else {
848 NewLIR2(kX86Mov32RT, rs_rAX.GetReg(),
849 Thread::ThreadSuspendTriggerOffset<4>().Int32Value());
850 }
Dave Allison69dfe512014-07-11 17:11:58 +0000851 return NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rAX.GetReg(), 0);
Dave Allisonb373e092014-02-20 16:06:36 -0800852}
853
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700854uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700855 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 return X86Mir2Lir::EncodingMap[opcode].flags;
857}
858
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700859const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700860 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 return X86Mir2Lir::EncodingMap[opcode].name;
862}
863
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700864const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700865 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 return X86Mir2Lir::EncodingMap[opcode].fmt;
867}
868
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000869void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
870 // Can we do this directly to memory?
871 rl_dest = UpdateLocWide(rl_dest);
872 if ((rl_dest.location == kLocDalvikFrame) ||
873 (rl_dest.location == kLocCompilerTemp)) {
874 int32_t val_lo = Low32Bits(value);
875 int32_t val_hi = High32Bits(value);
Ian Rogersb28c1c02014-11-08 11:21:21 -0800876 int r_base = rs_rX86_SP_32.GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000877 int displacement = SRegOffset(rl_dest.s_reg_low);
878
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100879 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee2700f7e2014-03-07 09:46:20 -0800880 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000881 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
882 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800883 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000884 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
885 false /* is_load */, true /* is64bit */);
886 return;
887 }
888
889 // Just use the standard code to do the generation.
890 Mir2Lir::GenConstWide(rl_dest, value);
891}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800892
893// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
894void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
895 LOG(INFO) << "location: " << loc.location << ','
896 << (loc.wide ? " w" : " ")
897 << (loc.defined ? " D" : " ")
898 << (loc.is_const ? " c" : " ")
899 << (loc.fp ? " F" : " ")
900 << (loc.core ? " C" : " ")
901 << (loc.ref ? " r" : " ")
902 << (loc.high_word ? " h" : " ")
903 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800904 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000905 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800906 << ", s_reg: " << loc.s_reg_low
907 << ", orig: " << loc.orig_sreg;
908}
909
Mark Mendell67c39c42014-01-31 17:28:00 -0800910void X86Mir2Lir::Materialize() {
911 // A good place to put the analysis before starting.
912 AnalyzeMIR();
913
914 // Now continue with regular code generation.
915 Mir2Lir::Materialize();
916}
917
Jeff Hao49161ce2014-03-12 11:05:25 -0700918void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800919 SpecialTargetRegister symbolic_reg) {
920 /*
921 * For x86, just generate a 32 bit move immediate instruction, that will be filled
922 * in at 'link time'. For now, put a unique value based on target to ensure that
923 * code deduplication works.
924 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700925 int target_method_idx = target_method.dex_method_index;
926 const DexFile* target_dex_file = target_method.dex_file;
927 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
928 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800929
Jeff Hao49161ce2014-03-12 11:05:25 -0700930 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700931 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
932 TargetReg(symbolic_reg, kNotWide).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700933 static_cast<int>(target_method_id_ptr), target_method_idx,
934 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800935 AppendLIR(move);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100936 method_address_insns_.push_back(move);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800937}
938
Fred Shihe7f82e22014-08-06 10:46:37 -0700939void X86Mir2Lir::LoadClassType(const DexFile& dex_file, uint32_t type_idx,
940 SpecialTargetRegister symbolic_reg) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800941 /*
942 * For x86, just generate a 32 bit move immediate instruction, that will be filled
943 * in at 'link time'. For now, put a unique value based on target to ensure that
944 * code deduplication works.
945 */
Fred Shihe7f82e22014-08-06 10:46:37 -0700946 const DexFile::TypeId& id = dex_file.GetTypeId(type_idx);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800947 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
948
949 // Generate the move instruction with the unique pointer and save index and type.
Andreas Gampeccc60262014-07-04 18:02:38 -0700950 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI,
951 TargetReg(symbolic_reg, kNotWide).GetReg(),
Fred Shihe7f82e22014-08-06 10:46:37 -0700952 static_cast<int>(ptr), type_idx,
953 WrapPointer(const_cast<DexFile*>(&dex_file)));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800954 AppendLIR(move);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100955 class_type_address_insns_.push_back(move);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800956}
957
Vladimir Markof4da6752014-08-01 19:04:18 +0100958LIR* X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800959 /*
960 * For x86, just generate a 32 bit call relative instruction, that will be filled
Vladimir Markof4da6752014-08-01 19:04:18 +0100961 * in at 'link time'.
Mark Mendell55d0eac2014-02-06 11:02:52 -0800962 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700963 int target_method_idx = target_method.dex_method_index;
964 const DexFile* target_dex_file = target_method.dex_file;
Mark Mendell55d0eac2014-02-06 11:02:52 -0800965
Jeff Hao49161ce2014-03-12 11:05:25 -0700966 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
Vladimir Markof4da6752014-08-01 19:04:18 +0100967 // NOTE: Method deduplication takes linker patches into account, so we can just pass 0
968 // as a placeholder for the offset.
969 LIR* call = RawLIR(current_dalvik_offset_, kX86CallI, 0,
Jeff Hao49161ce2014-03-12 11:05:25 -0700970 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800971 AppendLIR(call);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100972 call_method_insns_.push_back(call);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800973 return call;
974}
975
Vladimir Markof4da6752014-08-01 19:04:18 +0100976static LIR* GenInvokeNoInlineCall(Mir2Lir* mir_to_lir, InvokeType type) {
977 QuickEntrypointEnum trampoline;
978 switch (type) {
979 case kInterface:
980 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
981 break;
982 case kDirect:
983 trampoline = kQuickInvokeDirectTrampolineWithAccessCheck;
984 break;
985 case kStatic:
986 trampoline = kQuickInvokeStaticTrampolineWithAccessCheck;
987 break;
988 case kSuper:
989 trampoline = kQuickInvokeSuperTrampolineWithAccessCheck;
990 break;
991 case kVirtual:
992 trampoline = kQuickInvokeVirtualTrampolineWithAccessCheck;
993 break;
994 default:
995 LOG(FATAL) << "Unexpected invoke type";
996 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
997 }
998 return mir_to_lir->InvokeTrampoline(kOpBlx, RegStorage::InvalidReg(), trampoline);
999}
1000
1001LIR* X86Mir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) {
1002 LIR* call_insn;
1003 if (method_info.FastPath()) {
1004 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
1005 // We can have the linker fixup a call relative.
1006 call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType());
1007 } else {
1008 call_insn = OpMem(kOpBlx, TargetReg(kArg0, kRef),
1009 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1010 }
1011 } else {
1012 call_insn = GenInvokeNoInlineCall(this, method_info.GetSharpType());
1013 }
1014 return call_insn;
1015}
1016
Mark Mendell55d0eac2014-02-06 11:02:52 -08001017void X86Mir2Lir::InstallLiteralPools() {
1018 // These are handled differently for x86.
1019 DCHECK(code_literal_list_ == nullptr);
1020 DCHECK(method_literal_list_ == nullptr);
1021 DCHECK(class_literal_list_ == nullptr);
1022
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001023
Mark Mendelld65c51a2014-04-29 16:55:20 -04001024 if (const_vectors_ != nullptr) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001025 // Vector literals must be 16-byte aligned. The header that is placed
1026 // in the code section causes misalignment so we take it into account.
1027 // Otherwise, we are sure that for x86 method is aligned to 16.
1028 DCHECK_EQ(GetInstructionSetAlignment(cu_->instruction_set), 16u);
1029 uint32_t bytes_to_fill = (0x10 - ((code_buffer_.size() + sizeof(OatQuickMethodHeader)) & 0xF)) & 0xF;
1030 while (bytes_to_fill > 0) {
1031 code_buffer_.push_back(0);
1032 bytes_to_fill--;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001033 }
1034
Mark Mendelld65c51a2014-04-29 16:55:20 -04001035 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001036 PushWord(&code_buffer_, p->operands[0]);
1037 PushWord(&code_buffer_, p->operands[1]);
1038 PushWord(&code_buffer_, p->operands[2]);
1039 PushWord(&code_buffer_, p->operands[3]);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001040 }
1041 }
1042
Mark Mendell55d0eac2014-02-06 11:02:52 -08001043 // Handle the fixups for methods.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001044 for (LIR* p : method_address_insns_) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001045 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001046 uint32_t target_method_idx = p->operands[2];
1047 const DexFile* target_dex_file =
1048 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001049
1050 // The offset to patch is the last 4 bytes of the instruction.
1051 int patch_offset = p->offset + p->flags.size - 4;
Vladimir Markof4da6752014-08-01 19:04:18 +01001052 patches_.push_back(LinkerPatch::MethodPatch(patch_offset,
1053 target_dex_file, target_method_idx));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001054 }
1055
1056 // Handle the fixups for class types.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001057 for (LIR* p : class_type_address_insns_) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001058 DCHECK_EQ(p->opcode, kX86Mov32RI);
Fred Shihe7f82e22014-08-06 10:46:37 -07001059
1060 const DexFile* class_dex_file =
1061 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Vladimir Markof4da6752014-08-01 19:04:18 +01001062 uint32_t target_type_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001063
1064 // The offset to patch is the last 4 bytes of the instruction.
1065 int patch_offset = p->offset + p->flags.size - 4;
Vladimir Markof4da6752014-08-01 19:04:18 +01001066 patches_.push_back(LinkerPatch::TypePatch(patch_offset,
1067 class_dex_file, target_type_idx));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001068 }
1069
1070 // And now the PC-relative calls to methods.
Vladimir Markof4da6752014-08-01 19:04:18 +01001071 patches_.reserve(call_method_insns_.size());
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001072 for (LIR* p : call_method_insns_) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001073 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -07001074 uint32_t target_method_idx = p->operands[1];
1075 const DexFile* target_dex_file =
1076 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001077
1078 // The offset to patch is the last 4 bytes of the instruction.
1079 int patch_offset = p->offset + p->flags.size - 4;
Vladimir Markof4da6752014-08-01 19:04:18 +01001080 patches_.push_back(LinkerPatch::RelativeCodePatch(patch_offset,
1081 target_dex_file, target_method_idx));
Mark Mendell55d0eac2014-02-06 11:02:52 -08001082 }
1083
1084 // And do the normal processing.
1085 Mir2Lir::InstallLiteralPools();
1086}
1087
DaniilSokolov70c4f062014-06-24 17:34:00 -07001088bool X86Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
DaniilSokolov70c4f062014-06-24 17:34:00 -07001089 RegLocation rl_src = info->args[0];
1090 RegLocation rl_srcPos = info->args[1];
1091 RegLocation rl_dst = info->args[2];
1092 RegLocation rl_dstPos = info->args[3];
1093 RegLocation rl_length = info->args[4];
1094 if (rl_srcPos.is_const && (mir_graph_->ConstantValue(rl_srcPos) < 0)) {
1095 return false;
1096 }
1097 if (rl_dstPos.is_const && (mir_graph_->ConstantValue(rl_dstPos) < 0)) {
1098 return false;
1099 }
1100 ClobberCallerSave();
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001101 LockCallTemps(); // Using fixed registers.
1102 RegStorage tmp_reg = cu_->target64 ? rs_r11 : rs_rBX;
1103 LoadValueDirectFixed(rl_src, rs_rAX);
1104 LoadValueDirectFixed(rl_dst, rs_rCX);
1105 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_rAX, rs_rCX, nullptr);
1106 LIR* src_null_branch = OpCmpImmBranch(kCondEq, rs_rAX, 0, nullptr);
1107 LIR* dst_null_branch = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1108 LoadValueDirectFixed(rl_length, rs_rDX);
1109 // If the length of the copy is > 128 characters (256 bytes) or negative then go slow path.
1110 LIR* len_too_big = OpCmpImmBranch(kCondHi, rs_rDX, 128, nullptr);
1111 LoadValueDirectFixed(rl_src, rs_rAX);
1112 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001113 LIR* src_bad_len = nullptr;
avignatef9f0ed42014-09-17 22:35:07 +07001114 LIR* src_bad_off = nullptr;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001115 LIR* srcPos_negative = nullptr;
1116 if (!rl_srcPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001117 LoadValueDirectFixed(rl_srcPos, tmp_reg);
1118 srcPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
avignatef9f0ed42014-09-17 22:35:07 +07001119 // src_pos < src_len
1120 src_bad_off = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
1121 // src_len - src_pos < copy_len
1122 OpRegRegReg(kOpSub, tmp_reg, rs_rAX, tmp_reg);
1123 src_bad_len = OpCmpBranch(kCondLt, tmp_reg, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001124 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001125 int32_t pos_val = mir_graph_->ConstantValue(rl_srcPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001126 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001127 src_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001128 } else {
avignatef9f0ed42014-09-17 22:35:07 +07001129 // src_pos < src_len
1130 src_bad_off = OpCmpImmBranch(kCondLt, rs_rAX, pos_val, nullptr);
1131 // src_len - src_pos < copy_len
1132 OpRegRegImm(kOpSub, tmp_reg, rs_rAX, pos_val);
1133 src_bad_len = OpCmpBranch(kCondLt, tmp_reg, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001134 }
1135 }
1136 LIR* dstPos_negative = nullptr;
1137 LIR* dst_bad_len = nullptr;
avignatef9f0ed42014-09-17 22:35:07 +07001138 LIR* dst_bad_off = nullptr;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001139 LoadValueDirectFixed(rl_dst, rs_rAX);
1140 LoadWordDisp(rs_rAX, mirror::Array::LengthOffset().Int32Value(), rs_rAX);
1141 if (!rl_dstPos.is_const) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001142 LoadValueDirectFixed(rl_dstPos, tmp_reg);
1143 dstPos_negative = OpCmpImmBranch(kCondLt, tmp_reg, 0, nullptr);
avignatef9f0ed42014-09-17 22:35:07 +07001144 // dst_pos < dst_len
1145 dst_bad_off = OpCmpBranch(kCondLt, rs_rAX, tmp_reg, nullptr);
1146 // dst_len - dst_pos < copy_len
1147 OpRegRegReg(kOpSub, tmp_reg, rs_rAX, tmp_reg);
1148 dst_bad_len = OpCmpBranch(kCondLt, tmp_reg, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001149 } else {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001150 int32_t pos_val = mir_graph_->ConstantValue(rl_dstPos.orig_sreg);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001151 if (pos_val == 0) {
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001152 dst_bad_len = OpCmpBranch(kCondLt, rs_rAX, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001153 } else {
avignatef9f0ed42014-09-17 22:35:07 +07001154 // dst_pos < dst_len
1155 dst_bad_off = OpCmpImmBranch(kCondLt, rs_rAX, pos_val, nullptr);
1156 // dst_len - dst_pos < copy_len
1157 OpRegRegImm(kOpSub, tmp_reg, rs_rAX, pos_val);
1158 dst_bad_len = OpCmpBranch(kCondLt, tmp_reg, rs_rDX, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001159 }
1160 }
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001161 // Everything is checked now.
1162 LoadValueDirectFixed(rl_src, rs_rAX);
1163 LoadValueDirectFixed(rl_dst, tmp_reg);
1164 LoadValueDirectFixed(rl_srcPos, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001165 NewLIR5(kX86Lea32RA, rs_rAX.GetReg(), rs_rAX.GetReg(),
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001166 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value());
1167 // RAX now holds the address of the first src element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001168
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001169 LoadValueDirectFixed(rl_dstPos, rs_rCX);
1170 NewLIR5(kX86Lea32RA, tmp_reg.GetReg(), tmp_reg.GetReg(),
1171 rs_rCX.GetReg(), 1, mirror::Array::DataOffset(2).Int32Value() );
1172 // RBX now holds the address of the first dst element to be copied.
DaniilSokolov70c4f062014-06-24 17:34:00 -07001173
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001174 // Check if the number of elements to be copied is odd or even. If odd
DaniilSokolov70c4f062014-06-24 17:34:00 -07001175 // then copy the first element (so that the remaining number of elements
1176 // is even).
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001177 LoadValueDirectFixed(rl_length, rs_rCX);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001178 OpRegImm(kOpAnd, rs_rCX, 1);
1179 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_rCX, 0, nullptr);
1180 OpRegImm(kOpSub, rs_rDX, 1);
1181 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001182 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSignedHalf);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001183
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001184 // Since the remaining number of elements is even, we will copy by
DaniilSokolov70c4f062014-06-24 17:34:00 -07001185 // two elements at a time.
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001186 LIR* beginLoop = NewLIR0(kPseudoTargetLabel);
1187 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_rDX, 0, nullptr);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001188 OpRegImm(kOpSub, rs_rDX, 2);
1189 LoadBaseIndexedDisp(rs_rAX, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov5a5e8562014-07-17 18:58:15 -07001190 StoreBaseIndexedDisp(tmp_reg, rs_rDX, 1, 0, rs_rCX, kSingle);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001191 OpUnconditionalBranch(beginLoop);
1192 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1193 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1194 LIR *return_point = NewLIR0(kPseudoTargetLabel);
1195 jmp_to_ret->target = return_point;
1196 jmp_to_begin_loop->target = beginLoop;
1197 src_dst_same->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001198 len_too_big->target = check_failed;
1199 src_null_branch->target = check_failed;
1200 if (srcPos_negative != nullptr)
1201 srcPos_negative ->target = check_failed;
avignatef9f0ed42014-09-17 22:35:07 +07001202 if (src_bad_off != nullptr)
1203 src_bad_off->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001204 if (src_bad_len != nullptr)
1205 src_bad_len->target = check_failed;
1206 dst_null_branch->target = check_failed;
1207 if (dstPos_negative != nullptr)
1208 dstPos_negative->target = check_failed;
avignatef9f0ed42014-09-17 22:35:07 +07001209 if (dst_bad_off != nullptr)
1210 dst_bad_off->target = check_failed;
DaniilSokolov70c4f062014-06-24 17:34:00 -07001211 if (dst_bad_len != nullptr)
1212 dst_bad_len->target = check_failed;
1213 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001214 ClobberCallerSave(); // We must clobber everything because slow path will return here
DaniilSokolov70c4f062014-06-24 17:34:00 -07001215 return true;
1216}
1217
1218
Mark Mendell4028a6c2014-02-19 20:06:20 -08001219/*
1220 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
1221 * otherwise bails to standard library code.
1222 */
1223bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001224 RegLocation rl_obj = info->args[0];
1225 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -08001226 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001227 // RBX is promotable in 64-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001228 RegStorage rs_tmp = cu_->target64 ? rs_r11 : rs_rBX;
1229 int start_value = -1;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001230
1231 uint32_t char_value =
1232 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
1233
1234 if (char_value > 0xFFFF) {
1235 // We have to punt to the real String.indexOf.
1236 return false;
1237 }
1238
1239 // Okay, we are commited to inlining this.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001240 // EAX: 16 bit character being searched.
1241 // ECX: count: number of words to be searched.
1242 // EDI: String being searched.
1243 // EDX: temporary during execution.
1244 // EBX or R11: temporary during execution (depending on mode).
1245 // REP SCASW: search instruction.
1246
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001247 FlushAllRegs();
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001248
buzbeea0cd2d72014-06-01 09:33:49 -07001249 RegLocation rl_return = GetReturn(kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001250 RegLocation rl_dest = InlineTarget(info);
1251
1252 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -08001253 LoadValueDirectFixed(rl_obj, rs_rDX);
1254 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001255 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001256
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001257 LIR *slowpath_branch = nullptr, *length_compare = nullptr;
1258
1259 // We need the value in EAX.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001260 if (rl_char.is_const) {
buzbee2700f7e2014-03-07 09:46:20 -08001261 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001262 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001263 // Does the character fit in 16 bits? Compare it at runtime.
buzbee2700f7e2014-03-07 09:46:20 -08001264 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001265 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001266 }
1267
1268 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001269 // Location of reference to data array within the String object.
1270 int value_offset = mirror::String::ValueOffset().Int32Value();
1271 // Location of count within the String object.
1272 int count_offset = mirror::String::CountOffset().Int32Value();
1273 // Starting offset within data array.
1274 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1275 // Start of char data with array_.
1276 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -08001277
Dave Allison69dfe512014-07-11 17:11:58 +00001278 // Compute the number of words to search in to rCX.
1279 Load32Disp(rs_rDX, count_offset, rs_rCX);
1280
Dave Allisondfd3b472014-07-16 16:04:32 -07001281 // Possible signal here due to null pointer dereference.
1282 // Note that the signal handler will expect the top word of
1283 // the stack to be the ArtMethod*. If the PUSH edi instruction
1284 // below is ahead of the load above then this will not be true
1285 // and the signal handler will not work.
1286 MarkPossibleNullPointerException(0);
Dave Allison69dfe512014-07-11 17:11:58 +00001287
Dave Allisondfd3b472014-07-16 16:04:32 -07001288 if (!cu_->target64) {
nikolay serdjuk8bd698f2014-08-01 09:24:06 +07001289 // EDI is promotable in 32-bit mode.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001290 NewLIR1(kX86Push32R, rs_rDI.GetReg());
1291 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001292
Mark Mendell4028a6c2014-02-19 20:06:20 -08001293 if (zero_based) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001294 // Start index is not present.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001295 // We have to handle an empty string. Use special instruction JECXZ.
1296 length_compare = NewLIR0(kX86Jecxz8);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001297
1298 // Copy the number of words to search in a temporary register.
1299 // We will use the register at the end to calculate result.
1300 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001301 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001302 // Start index is present.
buzbeea44d4f52014-03-05 11:26:39 -08001303 rl_start = info->args[2];
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001304
Mark Mendell4028a6c2014-02-19 20:06:20 -08001305 // We have to offset by the start index.
1306 if (rl_start.is_const) {
1307 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
1308 start_value = std::max(start_value, 0);
1309
1310 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -08001311 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001312 OpRegImm(kOpMov, rs_rDI, start_value);
1313
1314 // Copy the number of words to search in a temporary register.
1315 // We will use the register at the end to calculate result.
1316 OpRegReg(kOpMov, rs_tmp, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001317
1318 if (start_value != 0) {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001319 // Decrease the number of words to search by the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001320 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001321 }
1322 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001323 // Handle "start index < 0" case.
1324 if (!cu_->target64 && rl_start.location != kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001325 // Load the start index from stack, remembering that we pushed EDI.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001326 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
Vladimir Marko74de63b2014-08-19 15:00:34 +01001327 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08001328 Load32Disp(rs_rX86_SP_32, displacement, rs_rDI);
Vladimir Marko74de63b2014-08-19 15:00:34 +01001329 // Dalvik register annotation in LoadBaseIndexedDisp() used wrong offset. Fix it.
1330 DCHECK(!DECODE_ALIAS_INFO_WIDE(last_lir_insn_->flags.alias_info));
1331 int reg_id = DECODE_ALIAS_INFO_REG(last_lir_insn_->flags.alias_info) - 1;
1332 AnnotateDalvikRegAccess(last_lir_insn_, reg_id, true, false);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001333 } else {
1334 LoadValueDirectFixed(rl_start, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001335 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001336 OpRegReg(kOpXor, rs_tmp, rs_tmp);
1337 OpRegReg(kOpCmp, rs_rDI, rs_tmp);
1338 OpCondRegReg(kOpCmov, kCondLt, rs_rDI, rs_tmp);
1339
1340 // The length of the string should be greater than the start index.
1341 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rDI, nullptr);
1342
1343 // Copy the number of words to search in a temporary register.
1344 // We will use the register at the end to calculate result.
1345 OpRegReg(kOpMov, rs_tmp, rs_rCX);
1346
1347 // Decrease the number of words to search by the start index.
1348 OpRegReg(kOpSub, rs_rCX, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001349 }
1350 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001351
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001352 // Load the address of the string into EDI.
1353 // In case of start index we have to add the address to existing value in EDI.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001354 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001355 if (zero_based || (!zero_based && rl_start.is_const && start_value == 0)) {
1356 Load32Disp(rs_rDX, offset_offset, rs_rDI);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001357 } else {
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001358 OpRegMem(kOpAdd, rs_rDI, rs_rDX, offset_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001359 }
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001360 OpRegImm(kOpLsl, rs_rDI, 1);
1361 OpRegMem(kOpAdd, rs_rDI, rs_rDX, value_offset);
1362 OpRegImm(kOpAdd, rs_rDI, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001363
1364 // EDI now contains the start of the string to be searched.
1365 // We are all prepared to do the search for the character.
1366 NewLIR0(kX86RepneScasw);
1367
1368 // Did we find a match?
1369 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1370
1371 // yes, we matched. Compute the index of the result.
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001372 OpRegReg(kOpSub, rs_tmp, rs_rCX);
1373 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_tmp.GetReg(), -1);
1374
Mark Mendell4028a6c2014-02-19 20:06:20 -08001375 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1376
1377 // Failed to match; return -1.
1378 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1379 length_compare->target = not_found;
1380 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001381 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001382
1383 // And join up at the end.
1384 all_done->target = NewLIR0(kPseudoTargetLabel);
nikolay serdjukc3561ae2014-07-18 12:35:46 +07001385
1386 if (!cu_->target64)
1387 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001388
1389 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001390 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001391 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001392 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001393 ClobberCallerSave(); // We must clobber everything because slow path will return here
Mark Mendell4028a6c2014-02-19 20:06:20 -08001394 }
1395
1396 StoreValue(rl_dest, rl_return);
1397 return true;
1398}
1399
Tong Shen35e1e6a2014-07-30 09:31:22 -07001400static bool ARTRegIDToDWARFRegID(bool is_x86_64, int art_reg_id, int* dwarf_reg_id) {
1401 if (is_x86_64) {
1402 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001403 case 3 : *dwarf_reg_id = 3; return true; // %rbx
Tong Shen35e1e6a2014-07-30 09:31:22 -07001404 // This is the only discrepancy between ART & DWARF register numbering.
Andreas Gampebda27222014-07-30 23:21:36 -07001405 case 5 : *dwarf_reg_id = 6; return true; // %rbp
1406 case 12: *dwarf_reg_id = 12; return true; // %r12
1407 case 13: *dwarf_reg_id = 13; return true; // %r13
1408 case 14: *dwarf_reg_id = 14; return true; // %r14
1409 case 15: *dwarf_reg_id = 15; return true; // %r15
1410 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001411 }
1412 } else {
1413 switch (art_reg_id) {
Andreas Gampebda27222014-07-30 23:21:36 -07001414 case 5: *dwarf_reg_id = 5; return true; // %ebp
1415 case 6: *dwarf_reg_id = 6; return true; // %esi
1416 case 7: *dwarf_reg_id = 7; return true; // %edi
1417 default: return false; // Should not get here
Tong Shen35e1e6a2014-07-30 09:31:22 -07001418 }
1419 }
1420}
1421
Tong Shen547cdfd2014-08-05 01:54:19 -07001422std::vector<uint8_t>* X86Mir2Lir::ReturnFrameDescriptionEntry() {
1423 std::vector<uint8_t>* cfi_info = new std::vector<uint8_t>;
Mark Mendellae9fd932014-02-10 16:14:35 -08001424
1425 // Generate the FDE for the method.
1426 DCHECK_NE(data_offset_, 0U);
1427
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001428 WriteFDEHeader(cfi_info, cu_->target64);
1429 WriteFDEAddressRange(cfi_info, data_offset_, cu_->target64);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001430
Mark Mendellae9fd932014-02-10 16:14:35 -08001431 // The instructions in the FDE.
1432 if (stack_decrement_ != nullptr) {
1433 // Advance LOC to just past the stack decrement.
1434 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001435 DW_CFA_advance_loc(cfi_info, pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001436
1437 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
Tong Shen547cdfd2014-08-05 01:54:19 -07001438 DW_CFA_def_cfa_offset(cfi_info, frame_size_);
Mark Mendellae9fd932014-02-10 16:14:35 -08001439
Tong Shen35e1e6a2014-07-30 09:31:22 -07001440 // Handle register spills
1441 const uint32_t kSpillInstLen = (cu_->target64) ? 5 : 4;
1442 const int kDataAlignmentFactor = (cu_->target64) ? -8 : -4;
1443 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
1444 int offset = -(GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
1445 for (int reg = 0; mask; mask >>= 1, reg++) {
1446 if (mask & 0x1) {
1447 pc += kSpillInstLen;
1448
1449 // Advance LOC to pass this instruction
Tong Shen547cdfd2014-08-05 01:54:19 -07001450 DW_CFA_advance_loc(cfi_info, kSpillInstLen);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001451
1452 int dwarf_reg_id;
1453 if (ARTRegIDToDWARFRegID(cu_->target64, reg, &dwarf_reg_id)) {
Tong Shen547cdfd2014-08-05 01:54:19 -07001454 // DW_CFA_offset_extended_sf reg offset
1455 DW_CFA_offset_extended_sf(cfi_info, dwarf_reg_id, offset / kDataAlignmentFactor);
Tong Shen35e1e6a2014-07-30 09:31:22 -07001456 }
1457
1458 offset += GetInstructionSetPointerSize(cu_->instruction_set);
1459 }
1460 }
1461
Mark Mendellae9fd932014-02-10 16:14:35 -08001462 // We continue with that stack until the epilogue.
1463 if (stack_increment_ != nullptr) {
1464 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001465 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001466
1467 // We probably have code snippets after the epilogue, so save the
1468 // current state: DW_CFA_remember_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001469 DW_CFA_remember_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001470
Tong Shen35e1e6a2014-07-30 09:31:22 -07001471 // We have now popped the stack: DW_CFA_def_cfa_offset 4/8.
1472 // There is only the return PC on the stack now.
Tong Shen547cdfd2014-08-05 01:54:19 -07001473 DW_CFA_def_cfa_offset(cfi_info, GetInstructionSetPointerSize(cu_->instruction_set));
Mark Mendellae9fd932014-02-10 16:14:35 -08001474
1475 // Everything after that is the same as before the epilogue.
1476 // Stack bump was followed by RET instruction.
1477 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1478 if (post_ret_insn != nullptr) {
1479 pc = new_pc;
1480 new_pc = post_ret_insn->offset;
Tong Shen547cdfd2014-08-05 01:54:19 -07001481 DW_CFA_advance_loc(cfi_info, new_pc - pc);
Mark Mendellae9fd932014-02-10 16:14:35 -08001482 // Restore the state: DW_CFA_restore_state.
Tong Shen547cdfd2014-08-05 01:54:19 -07001483 DW_CFA_restore_state(cfi_info);
Mark Mendellae9fd932014-02-10 16:14:35 -08001484 }
1485 }
1486 }
1487
Tong Shen547cdfd2014-08-05 01:54:19 -07001488 PadCFI(cfi_info);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001489 WriteCFILength(cfi_info, cu_->target64);
Mark Mendellae9fd932014-02-10 16:14:35 -08001490
Mark Mendellae9fd932014-02-10 16:14:35 -08001491 return cfi_info;
1492}
1493
Mark Mendelld65c51a2014-04-29 16:55:20 -04001494void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1495 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001496 case kMirOpReserveVectorRegisters:
1497 ReserveVectorRegisters(mir);
1498 break;
1499 case kMirOpReturnVectorRegisters:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001500 ReturnVectorRegisters(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001501 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001502 case kMirOpConstVector:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001503 GenConst128(mir);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001504 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001505 case kMirOpMoveVector:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001506 GenMoveVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001507 break;
1508 case kMirOpPackedMultiply:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001509 GenMultiplyVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001510 break;
1511 case kMirOpPackedAddition:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001512 GenAddVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001513 break;
1514 case kMirOpPackedSubtract:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001515 GenSubtractVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001516 break;
1517 case kMirOpPackedShiftLeft:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001518 GenShiftLeftVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001519 break;
1520 case kMirOpPackedSignedShiftRight:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001521 GenSignedShiftRightVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001522 break;
1523 case kMirOpPackedUnsignedShiftRight:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001524 GenUnsignedShiftRightVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001525 break;
1526 case kMirOpPackedAnd:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001527 GenAndVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001528 break;
1529 case kMirOpPackedOr:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001530 GenOrVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001531 break;
1532 case kMirOpPackedXor:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001533 GenXorVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001534 break;
1535 case kMirOpPackedAddReduce:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001536 GenAddReduceVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001537 break;
1538 case kMirOpPackedReduce:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001539 GenReduceVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001540 break;
1541 case kMirOpPackedSet:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001542 GenSetVector(mir);
Mark Mendellfe945782014-05-22 09:52:36 -04001543 break;
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -07001544 case kMirOpMemBarrier:
1545 GenMemBarrier(static_cast<MemBarrierKind>(mir->dalvikInsn.vA));
1546 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001547 case kMirOpPackedArrayGet:
1548 GenPackedArrayGet(bb, mir);
1549 break;
1550 case kMirOpPackedArrayPut:
1551 GenPackedArrayPut(bb, mir);
1552 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001553 default:
1554 break;
1555 }
1556}
1557
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001558void X86Mir2Lir::ReserveVectorRegisters(MIR* mir) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001559 for (uint32_t i = mir->dalvikInsn.vA; i <= mir->dalvikInsn.vB; i++) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001560 RegStorage xp_reg = RegStorage::Solo128(i);
1561 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1562 Clobber(xp_reg);
1563
1564 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1565 info != nullptr;
1566 info = info->GetAliasChain()) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001567 ArenaVector<RegisterInfo*>* regs =
1568 info->GetReg().IsSingle() ? &reg_pool_->sp_regs_ : &reg_pool_->dp_regs_;
1569 auto it = std::find(regs->begin(), regs->end(), info);
1570 DCHECK(it != regs->end());
1571 regs->erase(it);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001572 }
1573 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001574}
1575
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001576void X86Mir2Lir::ReturnVectorRegisters(MIR* mir) {
1577 for (uint32_t i = mir->dalvikInsn.vA; i <= mir->dalvikInsn.vB; i++) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001578 RegStorage xp_reg = RegStorage::Solo128(i);
1579 RegisterInfo *xp_reg_info = GetRegInfo(xp_reg);
1580
1581 for (RegisterInfo *info = xp_reg_info->GetAliasChain();
1582 info != nullptr;
1583 info = info->GetAliasChain()) {
1584 if (info->GetReg().IsSingle()) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001585 reg_pool_->sp_regs_.push_back(info);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001586 } else {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001587 reg_pool_->dp_regs_.push_back(info);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001588 }
1589 }
1590 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001591}
1592
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001593void X86Mir2Lir::GenConst128(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001594 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001595 Clobber(rs_dest);
1596
Mark Mendelld65c51a2014-04-29 16:55:20 -04001597 uint32_t *args = mir->dalvikInsn.arg;
Mark Mendellfe945782014-05-22 09:52:36 -04001598 int reg = rs_dest.GetReg();
Mark Mendelld65c51a2014-04-29 16:55:20 -04001599 // Check for all 0 case.
1600 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1601 NewLIR2(kX86XorpsRR, reg, reg);
1602 return;
1603 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001604
1605 // Append the mov const vector to reg opcode.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001606 AppendOpcodeWithConst(kX86MovdqaRM, reg, mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001607}
1608
1609void X86Mir2Lir::AppendOpcodeWithConst(X86OpCode opcode, int reg, MIR* mir) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001610 // The literal pool needs position independent logic.
1611 store_method_addr_used_ = true;
1612
1613 // To deal with correct memory ordering, reverse order of constants.
1614 int32_t constants[4];
1615 constants[3] = mir->dalvikInsn.arg[0];
1616 constants[2] = mir->dalvikInsn.arg[1];
1617 constants[1] = mir->dalvikInsn.arg[2];
1618 constants[0] = mir->dalvikInsn.arg[3];
1619
1620 // Search if there is already a constant in pool with this value.
1621 LIR *data_target = ScanVectorLiteral(constants);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001622 if (data_target == nullptr) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001623 data_target = AddVectorLiteral(constants);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001624 }
1625
1626 // Address the start of the method.
1627 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
Chao-ying Fue0ccdc02014-06-06 17:32:37 -07001628 if (rl_method.wide) {
1629 rl_method = LoadValueWide(rl_method, kCoreReg);
1630 } else {
1631 rl_method = LoadValue(rl_method, kCoreReg);
1632 }
Mark Mendelld65c51a2014-04-29 16:55:20 -04001633
1634 // Load the proper value from the literal area.
1635 // We don't know the proper offset for the value, so pick one that will force
1636 // 4 byte offset. We will fix this up in the assembler later to have the right
1637 // value.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001638 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001639 LIR *load = NewLIR3(opcode, reg, rl_method.reg.GetReg(), 256 /* bogus */);
Mark Mendelld65c51a2014-04-29 16:55:20 -04001640 load->flags.fixup = kFixupLoad;
1641 load->target = data_target;
Mark Mendelld65c51a2014-04-29 16:55:20 -04001642}
1643
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001644void X86Mir2Lir::GenMoveVector(MIR* mir) {
Mark Mendellfe945782014-05-22 09:52:36 -04001645 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001646 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1647 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001648 Clobber(rs_dest);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001649 RegStorage rs_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001650 NewLIR2(kX86MovdqaRR, rs_dest.GetReg(), rs_src.GetReg());
Mark Mendellfe945782014-05-22 09:52:36 -04001651}
1652
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001653void X86Mir2Lir::GenMultiplyVectorSignedByte(RegStorage rs_dest_src1, RegStorage rs_src2) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001654 /*
1655 * Emulate the behavior of a kSignedByte by separating out the 16 values in the two XMM
1656 * and multiplying 8 at a time before recombining back into one XMM register.
1657 *
1658 * let xmm1, xmm2 be real srcs (keep low bits of 16bit lanes)
1659 * xmm3 is tmp (operate on high bits of 16bit lanes)
1660 *
1661 * xmm3 = xmm1
1662 * xmm1 = xmm1 .* xmm2
1663 * xmm1 = xmm1 & 0x00ff00ff00ff00ff00ff00ff00ff00ff // xmm1 now has low bits
1664 * xmm3 = xmm3 .>> 8
1665 * xmm2 = xmm2 & 0xff00ff00ff00ff00ff00ff00ff00ff00
1666 * xmm2 = xmm2 .* xmm3 // xmm2 now has high bits
1667 * xmm1 = xmm1 | xmm2 // combine results
1668 */
1669
1670 // Copy xmm1.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001671 RegStorage rs_src1_high_tmp = Get128BitRegister(AllocTempDouble());
1672 RegStorage rs_dest_high_tmp = Get128BitRegister(AllocTempDouble());
1673 NewLIR2(kX86MovdqaRR, rs_src1_high_tmp.GetReg(), rs_src2.GetReg());
1674 NewLIR2(kX86MovdqaRR, rs_dest_high_tmp.GetReg(), rs_dest_src1.GetReg());
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001675
1676 // Multiply low bits.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001677 // x7 *= x3
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001678 NewLIR2(kX86PmullwRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1679
1680 // xmm1 now has low bits.
1681 AndMaskVectorRegister(rs_dest_src1, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF, 0x00FF00FF);
1682
1683 // Prepare high bits for multiplication.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001684 NewLIR2(kX86PsrlwRI, rs_src1_high_tmp.GetReg(), 0x8);
1685 AndMaskVectorRegister(rs_dest_high_tmp, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00, 0xFF00FF00);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001686
1687 // Multiply high bits and xmm2 now has high bits.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001688 NewLIR2(kX86PmullwRR, rs_src1_high_tmp.GetReg(), rs_dest_high_tmp.GetReg());
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001689
1690 // Combine back into dest XMM register.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001691 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src1_high_tmp.GetReg());
1692}
1693
1694void X86Mir2Lir::GenMultiplyVectorLong(RegStorage rs_dest_src1, RegStorage rs_src2) {
1695 /*
1696 * We need to emulate the packed long multiply.
1697 * For kMirOpPackedMultiply xmm1, xmm0:
1698 * - xmm1 is src/dest
1699 * - xmm0 is src
1700 * - Get xmm2 and xmm3 as temp
1701 * - Idea is to multiply the lower 32 of each operand with the higher 32 of the other.
1702 * - Then add the two results.
1703 * - Move it to the upper 32 of the destination
1704 * - Then multiply the lower 32-bits of the operands and add the result to the destination.
1705 *
1706 * (op dest src )
1707 * movdqa %xmm2, %xmm1
1708 * movdqa %xmm3, %xmm0
1709 * psrlq %xmm3, $0x20
1710 * pmuludq %xmm3, %xmm2
1711 * psrlq %xmm1, $0x20
1712 * pmuludq %xmm1, %xmm0
1713 * paddq %xmm1, %xmm3
1714 * psllq %xmm1, $0x20
1715 * pmuludq %xmm2, %xmm0
1716 * paddq %xmm1, %xmm2
1717 *
1718 * When both the operands are the same, then we need to calculate the lower-32 * higher-32
1719 * calculation only once. Thus we don't need the xmm3 temp above. That sequence becomes:
1720 *
1721 * (op dest src )
1722 * movdqa %xmm2, %xmm1
1723 * psrlq %xmm1, $0x20
1724 * pmuludq %xmm1, %xmm0
1725 * paddq %xmm1, %xmm1
1726 * psllq %xmm1, $0x20
1727 * pmuludq %xmm2, %xmm0
1728 * paddq %xmm1, %xmm2
1729 *
1730 */
1731
1732 bool both_operands_same = (rs_dest_src1.GetReg() == rs_src2.GetReg());
1733
1734 RegStorage rs_tmp_vector_1;
1735 RegStorage rs_tmp_vector_2;
1736 rs_tmp_vector_1 = Get128BitRegister(AllocTempDouble());
1737 NewLIR2(kX86MovdqaRR, rs_tmp_vector_1.GetReg(), rs_dest_src1.GetReg());
1738
1739 if (both_operands_same == false) {
1740 rs_tmp_vector_2 = Get128BitRegister(AllocTempDouble());
1741 NewLIR2(kX86MovdqaRR, rs_tmp_vector_2.GetReg(), rs_src2.GetReg());
1742 NewLIR2(kX86PsrlqRI, rs_tmp_vector_2.GetReg(), 0x20);
1743 NewLIR2(kX86PmuludqRR, rs_tmp_vector_2.GetReg(), rs_tmp_vector_1.GetReg());
1744 }
1745
1746 NewLIR2(kX86PsrlqRI, rs_dest_src1.GetReg(), 0x20);
1747 NewLIR2(kX86PmuludqRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
1748
1749 if (both_operands_same == false) {
1750 NewLIR2(kX86PaddqRR, rs_dest_src1.GetReg(), rs_tmp_vector_2.GetReg());
1751 } else {
1752 NewLIR2(kX86PaddqRR, rs_dest_src1.GetReg(), rs_dest_src1.GetReg());
1753 }
1754
1755 NewLIR2(kX86PsllqRI, rs_dest_src1.GetReg(), 0x20);
1756 NewLIR2(kX86PmuludqRR, rs_tmp_vector_1.GetReg(), rs_src2.GetReg());
1757 NewLIR2(kX86PaddqRR, rs_dest_src1.GetReg(), rs_tmp_vector_1.GetReg());
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001758}
1759
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001760void X86Mir2Lir::GenMultiplyVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001761 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1762 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1763 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001764 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001765 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001766 int opcode = 0;
1767 switch (opsize) {
1768 case k32:
1769 opcode = kX86PmulldRR;
1770 break;
1771 case kSignedHalf:
1772 opcode = kX86PmullwRR;
1773 break;
1774 case kSingle:
1775 opcode = kX86MulpsRR;
1776 break;
1777 case kDouble:
1778 opcode = kX86MulpdRR;
1779 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001780 case kSignedByte:
1781 // HW doesn't support 16x16 byte multiplication so emulate it.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001782 GenMultiplyVectorSignedByte(rs_dest_src1, rs_src2);
1783 return;
1784 case k64:
1785 GenMultiplyVectorLong(rs_dest_src1, rs_src2);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001786 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001787 default:
1788 LOG(FATAL) << "Unsupported vector multiply " << opsize;
1789 break;
1790 }
1791 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1792}
1793
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001794void X86Mir2Lir::GenAddVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001795 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1796 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1797 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001798 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001799 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001800 int opcode = 0;
1801 switch (opsize) {
1802 case k32:
1803 opcode = kX86PadddRR;
1804 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001805 case k64:
1806 opcode = kX86PaddqRR;
1807 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001808 case kSignedHalf:
1809 case kUnsignedHalf:
1810 opcode = kX86PaddwRR;
1811 break;
1812 case kUnsignedByte:
1813 case kSignedByte:
1814 opcode = kX86PaddbRR;
1815 break;
1816 case kSingle:
1817 opcode = kX86AddpsRR;
1818 break;
1819 case kDouble:
1820 opcode = kX86AddpdRR;
1821 break;
1822 default:
1823 LOG(FATAL) << "Unsupported vector addition " << opsize;
1824 break;
1825 }
1826 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1827}
1828
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001829void X86Mir2Lir::GenSubtractVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001830 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1831 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1832 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001833 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001834 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04001835 int opcode = 0;
1836 switch (opsize) {
1837 case k32:
1838 opcode = kX86PsubdRR;
1839 break;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001840 case k64:
1841 opcode = kX86PsubqRR;
1842 break;
Mark Mendellfe945782014-05-22 09:52:36 -04001843 case kSignedHalf:
1844 case kUnsignedHalf:
1845 opcode = kX86PsubwRR;
1846 break;
1847 case kUnsignedByte:
1848 case kSignedByte:
1849 opcode = kX86PsubbRR;
1850 break;
1851 case kSingle:
1852 opcode = kX86SubpsRR;
1853 break;
1854 case kDouble:
1855 opcode = kX86SubpdRR;
1856 break;
1857 default:
1858 LOG(FATAL) << "Unsupported vector subtraction " << opsize;
1859 break;
1860 }
1861 NewLIR2(opcode, rs_dest_src1.GetReg(), rs_src2.GetReg());
1862}
1863
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001864void X86Mir2Lir::GenShiftByteVector(MIR* mir) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001865 // Destination does not need clobbered because it has already been as part
1866 // of the general packed shift handler (caller of this method).
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001867 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001868
1869 int opcode = 0;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001870 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1871 case kMirOpPackedShiftLeft:
1872 opcode = kX86PsllwRI;
1873 break;
1874 case kMirOpPackedSignedShiftRight:
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001875 case kMirOpPackedUnsignedShiftRight:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001876 // TODO Add support for emulated byte shifts.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001877 default:
1878 LOG(FATAL) << "Unsupported shift operation on byte vector " << opcode;
1879 break;
1880 }
1881
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001882 // Clear xmm register and return if shift more than byte length.
1883 int imm = mir->dalvikInsn.vB;
1884 if (imm >= 8) {
1885 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_dest_src1.GetReg());
1886 return;
1887 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001888
1889 // Shift lower values.
1890 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1891
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001892 /*
1893 * The above shift will shift the whole word, but that means
1894 * both the bytes will shift as well. To emulate a byte level
1895 * shift, we can just throw away the lower (8 - N) bits of the
1896 * upper byte, and we are done.
1897 */
1898 uint8_t byte_mask = 0xFF << imm;
1899 uint32_t int_mask = byte_mask;
1900 int_mask = int_mask << 8 | byte_mask;
1901 int_mask = int_mask << 8 | byte_mask;
1902 int_mask = int_mask << 8 | byte_mask;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001903
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001904 // And the destination with the mask
1905 AndMaskVectorRegister(rs_dest_src1, int_mask, int_mask, int_mask, int_mask);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001906}
1907
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001908void X86Mir2Lir::GenShiftLeftVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001909 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1910 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1911 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001912 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001913 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001914 int opcode = 0;
1915 switch (opsize) {
1916 case k32:
1917 opcode = kX86PslldRI;
1918 break;
1919 case k64:
1920 opcode = kX86PsllqRI;
1921 break;
1922 case kSignedHalf:
1923 case kUnsignedHalf:
1924 opcode = kX86PsllwRI;
1925 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001926 case kSignedByte:
1927 case kUnsignedByte:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001928 GenShiftByteVector(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001929 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001930 default:
1931 LOG(FATAL) << "Unsupported vector shift left " << opsize;
1932 break;
1933 }
1934 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1935}
1936
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001937void X86Mir2Lir::GenSignedShiftRightVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001938 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1939 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1940 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001941 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001942 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001943 int opcode = 0;
1944 switch (opsize) {
1945 case k32:
1946 opcode = kX86PsradRI;
1947 break;
1948 case kSignedHalf:
1949 case kUnsignedHalf:
1950 opcode = kX86PsrawRI;
1951 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001952 case kSignedByte:
1953 case kUnsignedByte:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001954 GenShiftByteVector(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001955 return;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001956 case k64:
1957 // TODO Implement emulated shift algorithm.
Mark Mendellfe945782014-05-22 09:52:36 -04001958 default:
1959 LOG(FATAL) << "Unsupported vector signed shift right " << opsize;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001960 UNREACHABLE();
Mark Mendellfe945782014-05-22 09:52:36 -04001961 }
1962 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1963}
1964
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001965void X86Mir2Lir::GenUnsignedShiftRightVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001966 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1967 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
1968 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001969 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001970 int imm = mir->dalvikInsn.vB;
Mark Mendellfe945782014-05-22 09:52:36 -04001971 int opcode = 0;
1972 switch (opsize) {
1973 case k32:
1974 opcode = kX86PsrldRI;
1975 break;
1976 case k64:
1977 opcode = kX86PsrlqRI;
1978 break;
1979 case kSignedHalf:
1980 case kUnsignedHalf:
1981 opcode = kX86PsrlwRI;
1982 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001983 case kSignedByte:
1984 case kUnsignedByte:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001985 GenShiftByteVector(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001986 return;
Mark Mendellfe945782014-05-22 09:52:36 -04001987 default:
1988 LOG(FATAL) << "Unsupported vector unsigned shift right " << opsize;
1989 break;
1990 }
1991 NewLIR2(opcode, rs_dest_src1.GetReg(), imm);
1992}
1993
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001994void X86Mir2Lir::GenAndVector(MIR* mir) {
Mark Mendellfe945782014-05-22 09:52:36 -04001995 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001996 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
1997 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001998 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07001999 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04002000 NewLIR2(kX86PandRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
2001}
2002
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002003void X86Mir2Lir::GenOrVector(MIR* mir) {
Mark Mendellfe945782014-05-22 09:52:36 -04002004 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002005 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2006 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002007 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002008 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04002009 NewLIR2(kX86PorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
2010}
2011
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002012void X86Mir2Lir::GenXorVector(MIR* mir) {
Mark Mendellfe945782014-05-22 09:52:36 -04002013 // We only support 128 bit registers.
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002014 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2015 RegStorage rs_dest_src1 = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002016 Clobber(rs_dest_src1);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002017 RegStorage rs_src2 = RegStorage::Solo128(mir->dalvikInsn.vB);
Mark Mendellfe945782014-05-22 09:52:36 -04002018 NewLIR2(kX86PxorRR, rs_dest_src1.GetReg(), rs_src2.GetReg());
2019}
2020
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002021void X86Mir2Lir::AndMaskVectorRegister(RegStorage rs_src1, uint32_t m1, uint32_t m2, uint32_t m3, uint32_t m4) {
2022 MaskVectorRegister(kX86PandRM, rs_src1, m1, m2, m3, m4);
2023}
2024
2025void X86Mir2Lir::MaskVectorRegister(X86OpCode opcode, RegStorage rs_src1, uint32_t m0, uint32_t m1, uint32_t m2, uint32_t m3) {
2026 // Create temporary MIR as container for 128-bit binary mask.
2027 MIR const_mir;
2028 MIR* const_mirp = &const_mir;
2029 const_mirp->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpConstVector);
2030 const_mirp->dalvikInsn.arg[0] = m0;
2031 const_mirp->dalvikInsn.arg[1] = m1;
2032 const_mirp->dalvikInsn.arg[2] = m2;
2033 const_mirp->dalvikInsn.arg[3] = m3;
2034
2035 // Mask vector with const from literal pool.
2036 AppendOpcodeWithConst(opcode, rs_src1.GetReg(), const_mirp);
2037}
2038
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002039void X86Mir2Lir::GenAddReduceVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002040 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002041 RegStorage vector_src = RegStorage::Solo128(mir->dalvikInsn.vB);
2042 bool is_wide = opsize == k64 || opsize == kDouble;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002043
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002044 // Get the location of the virtual register. Since this bytecode is overloaded
2045 // for different types (and sizes), we need different logic for each path.
2046 // The design of bytecode uses same VR for source and destination.
2047 RegLocation rl_src, rl_dest, rl_result;
2048 if (is_wide) {
2049 rl_src = mir_graph_->GetSrcWide(mir, 0);
2050 rl_dest = mir_graph_->GetDestWide(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002051 } else {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002052 rl_src = mir_graph_->GetSrc(mir, 0);
2053 rl_dest = mir_graph_->GetDest(mir);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002054 }
2055
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002056 // We need a temp for byte and short values
2057 RegStorage temp;
2058
2059 // There is a different path depending on type and size.
2060 if (opsize == kSingle) {
2061 // Handle float case.
2062 // TODO Add support for fast math (not value safe) and do horizontal add in that case.
2063
2064 rl_src = LoadValue(rl_src, kFPReg);
2065 rl_result = EvalLoc(rl_dest, kFPReg, true);
2066
2067 // Since we are doing an add-reduce, we move the reg holding the VR
2068 // into the result so we include it in result.
2069 OpRegCopy(rl_result.reg, rl_src.reg);
2070 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), vector_src.GetReg());
2071
2072 // Since FP must keep order of operation for value safety, we shift to low
2073 // 32-bits and add to result.
2074 for (int i = 0; i < 3; i++) {
2075 NewLIR3(kX86ShufpsRRI, vector_src.GetReg(), vector_src.GetReg(), 0x39);
2076 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), vector_src.GetReg());
2077 }
2078
2079 StoreValue(rl_dest, rl_result);
2080 } else if (opsize == kDouble) {
2081 // Handle double case.
2082 rl_src = LoadValueWide(rl_src, kFPReg);
2083 rl_result = EvalLocWide(rl_dest, kFPReg, true);
2084 LOG(FATAL) << "Unsupported vector add reduce for double.";
2085 } else if (opsize == k64) {
2086 /*
2087 * Handle long case:
2088 * 1) Reduce the vector register to lower half (with addition).
2089 * 1-1) Get an xmm temp and fill it with vector register.
2090 * 1-2) Shift the xmm temp by 8-bytes.
2091 * 1-3) Add the xmm temp to vector register that is being reduced.
2092 * 2) Allocate temp GP / GP pair.
2093 * 2-1) In 64-bit case, use movq to move result to a 64-bit GP.
2094 * 2-2) In 32-bit case, use movd twice to move to 32-bit GP pair.
2095 * 3) Finish the add reduction by doing what add-long/2addr does,
2096 * but instead of having a VR as one of the sources, we have our temp GP.
2097 */
2098 RegStorage rs_tmp_vector = Get128BitRegister(AllocTempDouble());
2099 NewLIR2(kX86MovdqaRR, rs_tmp_vector.GetReg(), vector_src.GetReg());
2100 NewLIR2(kX86PsrldqRI, rs_tmp_vector.GetReg(), 8);
2101 NewLIR2(kX86PaddqRR, vector_src.GetReg(), rs_tmp_vector.GetReg());
2102 FreeTemp(rs_tmp_vector);
2103
2104 // We would like to be able to reuse the add-long implementation, so set up a fake
2105 // register location to pass it.
2106 RegLocation temp_loc = mir_graph_->GetBadLoc();
2107 temp_loc.core = 1;
2108 temp_loc.wide = 1;
2109 temp_loc.location = kLocPhysReg;
2110 temp_loc.reg = AllocTempWide();
2111
2112 if (cu_->target64) {
2113 DCHECK(!temp_loc.reg.IsPair());
2114 NewLIR2(kX86MovqrxRR, temp_loc.reg.GetReg(), vector_src.GetReg());
2115 } else {
2116 NewLIR2(kX86MovdrxRR, temp_loc.reg.GetLowReg(), vector_src.GetReg());
2117 NewLIR2(kX86PsrlqRI, vector_src.GetReg(), 0x20);
2118 NewLIR2(kX86MovdrxRR, temp_loc.reg.GetHighReg(), vector_src.GetReg());
2119 }
2120
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002121 GenArithOpLong(Instruction::ADD_LONG_2ADDR, rl_dest, temp_loc, temp_loc, mir->optimization_flags);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002122 } else if (opsize == kSignedByte || opsize == kUnsignedByte) {
2123 RegStorage rs_tmp = Get128BitRegister(AllocTempDouble());
2124 NewLIR2(kX86PxorRR, rs_tmp.GetReg(), rs_tmp.GetReg());
2125 NewLIR2(kX86PsadbwRR, vector_src.GetReg(), rs_tmp.GetReg());
2126 NewLIR3(kX86PshufdRRI, rs_tmp.GetReg(), vector_src.GetReg(), 0x4e);
2127 NewLIR2(kX86PaddbRR, vector_src.GetReg(), rs_tmp.GetReg());
2128 // Move to a GPR
2129 temp = AllocTemp();
2130 NewLIR2(kX86MovdrxRR, temp.GetReg(), vector_src.GetReg());
2131 } else {
2132 // Handle and the int and short cases together
2133
2134 // Initialize as if we were handling int case. Below we update
2135 // the opcode if handling byte or short.
2136 int vec_bytes = (mir->dalvikInsn.vC & 0xFFFF) / 8;
2137 int vec_unit_size;
2138 int horizontal_add_opcode;
2139 int extract_opcode;
2140
2141 if (opsize == kSignedHalf || opsize == kUnsignedHalf) {
2142 extract_opcode = kX86PextrwRRI;
2143 horizontal_add_opcode = kX86PhaddwRR;
2144 vec_unit_size = 2;
2145 } else if (opsize == k32) {
2146 vec_unit_size = 4;
2147 horizontal_add_opcode = kX86PhadddRR;
2148 extract_opcode = kX86PextrdRRI;
2149 } else {
2150 LOG(FATAL) << "Unsupported vector add reduce " << opsize;
2151 return;
2152 }
2153
2154 int elems = vec_bytes / vec_unit_size;
2155
2156 while (elems > 1) {
2157 NewLIR2(horizontal_add_opcode, vector_src.GetReg(), vector_src.GetReg());
2158 elems >>= 1;
2159 }
2160
2161 // Handle this as arithmetic unary case.
2162 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2163
2164 // Extract to a GP register because this is integral typed.
2165 temp = AllocTemp();
2166 NewLIR3(extract_opcode, temp.GetReg(), vector_src.GetReg(), 0);
2167 }
2168
2169 if (opsize != k64 && opsize != kSingle && opsize != kDouble) {
2170 // The logic below looks very similar to the handling of ADD_INT_2ADDR
2171 // except the rhs is not a VR but a physical register allocated above.
2172 // No load of source VR is done because it assumes that rl_result will
2173 // share physical register / memory location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002174 rl_result = UpdateLocTyped(rl_dest);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002175 if (rl_result.location == kLocPhysReg) {
2176 // Ensure res is in a core reg.
2177 rl_result = EvalLoc(rl_dest, kCoreReg, true);
2178 OpRegReg(kOpAdd, rl_result.reg, temp);
2179 StoreFinalValue(rl_dest, rl_result);
2180 } else {
2181 // Do the addition directly to memory.
2182 OpMemReg(kOpAdd, rl_result, temp.GetReg());
2183 }
2184 }
Mark Mendellfe945782014-05-22 09:52:36 -04002185}
2186
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002187void X86Mir2Lir::GenReduceVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002188 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2189 RegLocation rl_dest = mir_graph_->GetDest(mir);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002190 RegStorage vector_src = RegStorage::Solo128(mir->dalvikInsn.vB);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002191 RegLocation rl_result;
2192 bool is_wide = false;
2193
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002194 // There is a different path depending on type and size.
2195 if (opsize == kSingle) {
2196 // Handle float case.
2197 // TODO Add support for fast math (not value safe) and do horizontal add in that case.
Mark Mendellfe945782014-05-22 09:52:36 -04002198
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002199 rl_result = EvalLoc(rl_dest, kFPReg, true);
2200 NewLIR2(kX86PxorRR, rl_result.reg.GetReg(), rl_result.reg.GetReg());
2201 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), vector_src.GetReg());
2202
2203 // Since FP must keep order of operation for value safety, we shift to low
2204 // 32-bits and add to result.
2205 for (int i = 0; i < 3; i++) {
2206 NewLIR3(kX86ShufpsRRI, vector_src.GetReg(), vector_src.GetReg(), 0x39);
2207 NewLIR2(kX86AddssRR, rl_result.reg.GetReg(), vector_src.GetReg());
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002208 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002209
2210 StoreValue(rl_dest, rl_result);
2211 } else if (opsize == kDouble) {
2212 // TODO Handle double case.
2213 LOG(FATAL) << "Unsupported add reduce for double.";
2214 } else if (opsize == k64) {
2215 /*
2216 * Handle long case:
2217 * 1) Reduce the vector register to lower half (with addition).
2218 * 1-1) Get an xmm temp and fill it with vector register.
2219 * 1-2) Shift the xmm temp by 8-bytes.
2220 * 1-3) Add the xmm temp to vector register that is being reduced.
2221 * 2) Evaluate destination to a GP / GP pair.
2222 * 2-1) In 64-bit case, use movq to move result to a 64-bit GP.
2223 * 2-2) In 32-bit case, use movd twice to move to 32-bit GP pair.
2224 * 3) Store the result to the final destination.
2225 */
Udayan Banerji53cec002014-09-26 10:41:47 -07002226 NewLIR2(kX86PsrldqRI, vector_src.GetReg(), 8);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002227 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
2228 if (cu_->target64) {
2229 DCHECK(!rl_result.reg.IsPair());
2230 NewLIR2(kX86MovqrxRR, rl_result.reg.GetReg(), vector_src.GetReg());
2231 } else {
2232 NewLIR2(kX86MovdrxRR, rl_result.reg.GetLowReg(), vector_src.GetReg());
2233 NewLIR2(kX86PsrlqRI, vector_src.GetReg(), 0x20);
2234 NewLIR2(kX86MovdrxRR, rl_result.reg.GetHighReg(), vector_src.GetReg());
2235 }
2236
2237 StoreValueWide(rl_dest, rl_result);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002238 } else {
Udayan Banerji53cec002014-09-26 10:41:47 -07002239 int extract_index = mir->dalvikInsn.arg[0];
2240 int extr_opcode = 0;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002241 rl_result = UpdateLocTyped(rl_dest);
Udayan Banerji53cec002014-09-26 10:41:47 -07002242
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002243 // Handle the rest of integral types now.
2244 switch (opsize) {
2245 case k32:
Udayan Banerji53cec002014-09-26 10:41:47 -07002246 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrdRRI : kX86PextrdMRI;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002247 break;
2248 case kSignedHalf:
2249 case kUnsignedHalf:
Udayan Banerji53cec002014-09-26 10:41:47 -07002250 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrwRRI : kX86PextrwMRI;
2251 break;
2252 case kSignedByte:
2253 extr_opcode = (rl_result.location == kLocPhysReg) ? kX86PextrbRRI : kX86PextrbMRI;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002254 break;
2255 default:
2256 LOG(FATAL) << "Unsupported vector reduce " << opsize;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002257 UNREACHABLE();
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002258 }
2259
2260 if (rl_result.location == kLocPhysReg) {
2261 NewLIR3(extr_opcode, rl_result.reg.GetReg(), vector_src.GetReg(), extract_index);
Udayan Banerji53cec002014-09-26 10:41:47 -07002262 StoreFinalValue(rl_dest, rl_result);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002263 } else {
2264 int displacement = SRegOffset(rl_result.s_reg_low);
Ian Rogersb28c1c02014-11-08 11:21:21 -08002265 LIR *l = NewLIR3(extr_opcode, rs_rX86_SP_32.GetReg(), displacement, vector_src.GetReg());
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002266 AnnotateDalvikRegAccess(l, displacement >> 2, true /* is_load */, is_wide /* is_64bit */);
2267 AnnotateDalvikRegAccess(l, displacement >> 2, false /* is_load */, is_wide /* is_64bit */);
2268 }
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002269 }
Mark Mendellfe945782014-05-22 09:52:36 -04002270}
2271
Mark Mendell0a1174e2014-09-11 14:51:02 -04002272void X86Mir2Lir::LoadVectorRegister(RegStorage rs_dest, RegStorage rs_src,
2273 OpSize opsize, int op_mov) {
2274 if (!cu_->target64 && opsize == k64) {
2275 // Logic assumes that longs are loaded in GP register pairs.
2276 NewLIR2(kX86MovdxrRR, rs_dest.GetReg(), rs_src.GetLowReg());
2277 RegStorage r_tmp = AllocTempDouble();
2278 NewLIR2(kX86MovdxrRR, r_tmp.GetReg(), rs_src.GetHighReg());
2279 NewLIR2(kX86PunpckldqRR, rs_dest.GetReg(), r_tmp.GetReg());
2280 FreeTemp(r_tmp);
2281 } else {
2282 NewLIR2(op_mov, rs_dest.GetReg(), rs_src.GetReg());
2283 }
2284}
2285
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002286void X86Mir2Lir::GenSetVector(MIR* mir) {
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002287 DCHECK_EQ(mir->dalvikInsn.vC & 0xFFFF, 128U);
2288 OpSize opsize = static_cast<OpSize>(mir->dalvikInsn.vC >> 16);
2289 RegStorage rs_dest = RegStorage::Solo128(mir->dalvikInsn.vA);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002290 Clobber(rs_dest);
2291 int op_shuffle = 0, op_shuffle_high = 0, op_mov = kX86MovdxrRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002292 RegisterClass reg_type = kCoreReg;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002293 bool is_wide = false;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002294
Mark Mendellfe945782014-05-22 09:52:36 -04002295 switch (opsize) {
2296 case k32:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002297 op_shuffle = kX86PshufdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002298 break;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002299 case kSingle:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002300 op_shuffle = kX86PshufdRRI;
2301 op_mov = kX86MovdqaRR;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002302 reg_type = kFPReg;
2303 break;
2304 case k64:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002305 op_shuffle = kX86PunpcklqdqRR;
Udayan Banerji53cec002014-09-26 10:41:47 -07002306 op_mov = kX86MovqxrRR;
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002307 is_wide = true;
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002308 break;
2309 case kSignedByte:
2310 case kUnsignedByte:
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002311 // We will have the source loaded up in a
2312 // double-word before we use this shuffle
2313 op_shuffle = kX86PshufdRRI;
2314 break;
Mark Mendellfe945782014-05-22 09:52:36 -04002315 case kSignedHalf:
2316 case kUnsignedHalf:
2317 // Handles low quadword.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002318 op_shuffle = kX86PshuflwRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002319 // Handles upper quadword.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002320 op_shuffle_high = kX86PshufdRRI;
Mark Mendellfe945782014-05-22 09:52:36 -04002321 break;
2322 default:
2323 LOG(FATAL) << "Unsupported vector set " << opsize;
2324 break;
2325 }
2326
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002327 // Load the value from the VR into a physical register.
2328 RegLocation rl_src;
2329 if (!is_wide) {
2330 rl_src = mir_graph_->GetSrc(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002331 rl_src = LoadValue(rl_src, reg_type);
2332 } else {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002333 rl_src = mir_graph_->GetSrcWide(mir, 0);
Udayan Banerji60bfe7b2014-07-08 19:59:43 -07002334 rl_src = LoadValueWide(rl_src, reg_type);
2335 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002336 RegStorage reg_to_shuffle = rl_src.reg;
Mark Mendellfe945782014-05-22 09:52:36 -04002337
2338 // Load the value into the XMM register.
Mark Mendell0a1174e2014-09-11 14:51:02 -04002339 LoadVectorRegister(rs_dest, reg_to_shuffle, opsize, op_mov);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002340
2341 if (opsize == kSignedByte || opsize == kUnsignedByte) {
2342 // In the byte case, first duplicate it to be a word
2343 // Then duplicate it to be a double-word
2344 NewLIR2(kX86PunpcklbwRR, rs_dest.GetReg(), rs_dest.GetReg());
2345 NewLIR2(kX86PunpcklwdRR, rs_dest.GetReg(), rs_dest.GetReg());
2346 }
Mark Mendellfe945782014-05-22 09:52:36 -04002347
2348 // Now shuffle the value across the destination.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002349 if (op_shuffle == kX86PunpcklqdqRR) {
2350 NewLIR2(op_shuffle, rs_dest.GetReg(), rs_dest.GetReg());
2351 } else {
2352 NewLIR3(op_shuffle, rs_dest.GetReg(), rs_dest.GetReg(), 0);
2353 }
Mark Mendellfe945782014-05-22 09:52:36 -04002354
2355 // And then repeat as needed.
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002356 if (op_shuffle_high != 0) {
2357 NewLIR3(op_shuffle_high, rs_dest.GetReg(), rs_dest.GetReg(), 0);
Mark Mendellfe945782014-05-22 09:52:36 -04002358 }
2359}
2360
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002361void X86Mir2Lir::GenPackedArrayGet(BasicBlock* bb, MIR* mir) {
2362 UNUSED(bb, mir);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002363 UNIMPLEMENTED(FATAL) << "Extended opcode kMirOpPackedArrayGet not supported.";
2364}
2365
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002366void X86Mir2Lir::GenPackedArrayPut(BasicBlock* bb, MIR* mir) {
2367 UNUSED(bb, mir);
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002368 UNIMPLEMENTED(FATAL) << "Extended opcode kMirOpPackedArrayPut not supported.";
2369}
2370
2371LIR* X86Mir2Lir::ScanVectorLiteral(int32_t* constants) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04002372 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002373 if (constants[0] == p->operands[0] && constants[1] == p->operands[1] &&
2374 constants[2] == p->operands[2] && constants[3] == p->operands[3]) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04002375 return p;
2376 }
2377 }
2378 return nullptr;
2379}
2380
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002381LIR* X86Mir2Lir::AddVectorLiteral(int32_t* constants) {
Mark Mendelld65c51a2014-04-29 16:55:20 -04002382 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002383 new_value->operands[0] = constants[0];
2384 new_value->operands[1] = constants[1];
2385 new_value->operands[2] = constants[2];
2386 new_value->operands[3] = constants[3];
Mark Mendelld65c51a2014-04-29 16:55:20 -04002387 new_value->next = const_vectors_;
2388 if (const_vectors_ == nullptr) {
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07002389 estimated_native_code_size_ += 12; // Maximum needed to align to 16 byte boundary.
Mark Mendelld65c51a2014-04-29 16:55:20 -04002390 }
2391 estimated_native_code_size_ += 16; // Space for one vector.
2392 const_vectors_ = new_value;
2393 return new_value;
2394}
2395
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002396// ------------ ABI support: mapping of args to physical registers -------------
Andreas Gampeccc60262014-07-04 18:02:38 -07002397RegStorage X86Mir2Lir::InToRegStorageX86_64Mapper::GetNextReg(bool is_double_or_float, bool is_wide,
2398 bool is_ref) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002399 const SpecialTargetRegister coreArgMappingToPhysicalReg[] = {kArg1, kArg2, kArg3, kArg4, kArg5};
Andreas Gampeccc60262014-07-04 18:02:38 -07002400 const int coreArgMappingToPhysicalRegSize = sizeof(coreArgMappingToPhysicalReg) /
2401 sizeof(SpecialTargetRegister);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002402 const SpecialTargetRegister fpArgMappingToPhysicalReg[] = {kFArg0, kFArg1, kFArg2, kFArg3,
Andreas Gampeccc60262014-07-04 18:02:38 -07002403 kFArg4, kFArg5, kFArg6, kFArg7};
2404 const int fpArgMappingToPhysicalRegSize = sizeof(fpArgMappingToPhysicalReg) /
2405 sizeof(SpecialTargetRegister);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002406
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002407 if (is_double_or_float) {
2408 if (cur_fp_reg_ < fpArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002409 return ml_->TargetReg(fpArgMappingToPhysicalReg[cur_fp_reg_++], is_wide ? kWide : kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002410 }
2411 } else {
2412 if (cur_core_reg_ < coreArgMappingToPhysicalRegSize) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002413 return ml_->TargetReg(coreArgMappingToPhysicalReg[cur_core_reg_++],
2414 is_ref ? kRef : (is_wide ? kWide : kNotWide));
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002415 }
2416 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07002417 return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002418}
2419
2420RegStorage X86Mir2Lir::InToRegStorageMapping::Get(int in_position) {
2421 DCHECK(IsInitialized());
2422 auto res = mapping_.find(in_position);
2423 return res != mapping_.end() ? res->second : RegStorage::InvalidReg();
2424}
2425
Andreas Gampeccc60262014-07-04 18:02:38 -07002426void X86Mir2Lir::InToRegStorageMapping::Initialize(RegLocation* arg_locs, int count,
2427 InToRegStorageMapper* mapper) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002428 DCHECK(mapper != nullptr);
2429 max_mapped_in_ = -1;
2430 is_there_stack_mapped_ = false;
2431 for (int in_position = 0; in_position < count; in_position++) {
Serguei Katkov407a9d22014-07-05 03:09:32 +07002432 RegStorage reg = mapper->GetNextReg(arg_locs[in_position].fp,
2433 arg_locs[in_position].wide, arg_locs[in_position].ref);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002434 if (reg.Valid()) {
2435 mapping_[in_position] = reg;
2436 max_mapped_in_ = std::max(max_mapped_in_, in_position);
Serguei Katkov407a9d22014-07-05 03:09:32 +07002437 if (arg_locs[in_position].wide) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002438 // We covered 2 args, so skip the next one
2439 in_position++;
2440 }
2441 } else {
2442 is_there_stack_mapped_ = true;
2443 }
2444 }
2445 initialized_ = true;
2446}
2447
2448RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002449 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002450 return GetCoreArgMappingToPhysicalReg(arg_num);
2451 }
2452
2453 if (!in_to_reg_storage_mapping_.IsInitialized()) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002454 int start_vreg = cu_->mir_graph->GetFirstInVR();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002455 RegLocation* arg_locs = &mir_graph_->reg_location_[start_vreg];
2456
Chao-ying Fua77ee512014-07-01 17:43:41 -07002457 InToRegStorageX86_64Mapper mapper(this);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002458 in_to_reg_storage_mapping_.Initialize(arg_locs, mir_graph_->GetNumOfInVRs(), &mapper);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002459 }
2460 return in_to_reg_storage_mapping_.Get(arg_num);
2461}
2462
Ian Rogersb28c1c02014-11-08 11:21:21 -08002463RegStorage X86Mir2Lir::GetCoreArgMappingToPhysicalReg(int core_arg_num) const {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002464 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
2465 // Not used for 64-bit, TODO: Move X86_32 to the same framework
2466 switch (core_arg_num) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002467 case 0: return TargetReg32(kArg1);
2468 case 1: return TargetReg32(kArg2);
2469 case 2: return TargetReg32(kArg3);
2470 default: return RegStorage::InvalidReg();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002471 }
2472}
2473
2474// ---------End of ABI support: mapping of args to physical registers -------------
2475
2476/*
2477 * If there are any ins passed in registers that have not been promoted
2478 * to a callee-save register, flush them to the frame. Perform initial
2479 * assignment of promoted arguments.
2480 *
2481 * ArgLocs is an array of location records describing the incoming arguments
2482 * with one location record per word of argument.
2483 */
2484void X86Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002485 if (!cu_->target64) return Mir2Lir::FlushIns(ArgLocs, rl_method);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002486 /*
2487 * Dummy up a RegLocation for the incoming Method*
2488 * It will attempt to keep kArg0 live (or copy it to home location
2489 * if promoted).
2490 */
2491
2492 RegLocation rl_src = rl_method;
2493 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -07002494 rl_src.reg = TargetReg(kArg0, kRef);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002495 rl_src.home = false;
2496 MarkLive(rl_src);
2497 StoreValue(rl_method, rl_src);
2498 // If Method* has been promoted, explicitly flush
2499 if (rl_method.location == kLocPhysReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002500 const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32;
2501 StoreRefDisp(rs_rSP, 0, As32BitReg(TargetReg(kArg0, kRef)), kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002502 }
2503
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002504 if (mir_graph_->GetNumOfInVRs() == 0) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002505 return;
2506 }
2507
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002508 int start_vreg = cu_->mir_graph->GetFirstInVR();
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002509 /*
2510 * Copy incoming arguments to their proper home locations.
2511 * NOTE: an older version of dx had an issue in which
2512 * it would reuse static method argument registers.
2513 * This could result in the same Dalvik virtual register
2514 * being promoted to both core and fp regs. To account for this,
2515 * we only copy to the corresponding promoted physical register
2516 * if it matches the type of the SSA name for the incoming
2517 * argument. It is also possible that long and double arguments
2518 * end up half-promoted. In those cases, we must flush the promoted
2519 * half to memory as well.
2520 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002521 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002522 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002523 // get reg corresponding to input
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002524 RegStorage reg = GetArgMappingToPhysicalReg(i);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002525
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002526 RegLocation* t_loc = &ArgLocs[i];
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002527 if (reg.Valid()) {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002528 // If arriving in register.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002529
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002530 // We have already updated the arg location with promoted info
2531 // so we can be based on it.
2532 if (t_loc->location == kLocPhysReg) {
2533 // Just copy it.
2534 OpRegCopy(t_loc->reg, reg);
2535 } else {
2536 // Needs flush.
2537 if (t_loc->ref) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002538 StoreRefDisp(rs_rX86_SP_64, SRegOffset(start_vreg + i), reg, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002539 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002540 StoreBaseDisp(rs_rX86_SP_64, SRegOffset(start_vreg + i), reg, t_loc->wide ? k64 : k32,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002541 kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002542 }
2543 }
2544 } else {
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002545 // If arriving in frame & promoted.
2546 if (t_loc->location == kLocPhysReg) {
2547 if (t_loc->ref) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002548 LoadRefDisp(rs_rX86_SP_64, SRegOffset(start_vreg + i), t_loc->reg, kNotVolatile);
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002549 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002550 LoadBaseDisp(rs_rX86_SP_64, SRegOffset(start_vreg + i), t_loc->reg,
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002551 t_loc->wide ? k64 : k32, kNotVolatile);
2552 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002553 }
Dmitry Petrochenko4d5d7942014-06-27 12:25:01 +07002554 }
2555 if (t_loc->wide) {
2556 // Increment i to skip the next one.
2557 i++;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002558 }
2559 }
2560}
2561
2562/*
2563 * Load up to 5 arguments, the first three of which will be in
2564 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
2565 * and as part of the load sequence, it must be replaced with
2566 * the target method pointer. Note, this may also be called
2567 * for "range" variants if the number of arguments is 5 or fewer.
2568 */
2569int X86Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
2570 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
2571 const MethodReference& target_method,
2572 uint32_t vtable_idx, uintptr_t direct_code,
2573 uintptr_t direct_method, InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002574 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002575 return Mir2Lir::GenDalvikArgsNoRange(info,
Ian Rogersb28c1c02014-11-08 11:21:21 -08002576 call_state, pcrLabel, next_call_insn,
2577 target_method,
2578 vtable_idx, direct_code,
2579 direct_method, type, skip_this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002580 }
2581 return GenDalvikArgsRange(info,
Ian Rogersb28c1c02014-11-08 11:21:21 -08002582 call_state, pcrLabel, next_call_insn,
2583 target_method,
2584 vtable_idx, direct_code,
2585 direct_method, type, skip_this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002586}
2587
2588/*
2589 * May have 0+ arguments (also used for jumbo). Note that
2590 * source virtual registers may be in physical registers, so may
2591 * need to be flushed to home location before copying. This
2592 * applies to arg3 and above (see below).
2593 *
2594 * Two general strategies:
2595 * If < 20 arguments
2596 * Pass args 3-18 using vldm/vstm block copy
2597 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2598 * If 20+ arguments
2599 * Pass args arg19+ using memcpy block copy
2600 * Pass arg0, arg1 & arg2 in kArg1-kArg3
2601 *
2602 */
2603int X86Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
2604 LIR** pcrLabel, NextCallInsn next_call_insn,
2605 const MethodReference& target_method,
2606 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
2607 InvokeType type, bool skip_this) {
Elena Sayapinadd644502014-07-01 18:39:52 +07002608 if (!cu_->target64) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002609 return Mir2Lir::GenDalvikArgsRange(info, call_state,
2610 pcrLabel, next_call_insn,
2611 target_method,
2612 vtable_idx, direct_code, direct_method,
2613 type, skip_this);
2614 }
2615
2616 /* If no arguments, just return */
2617 if (info->num_arg_words == 0)
2618 return call_state;
2619
2620 const int start_index = skip_this ? 1 : 0;
2621
Chao-ying Fua77ee512014-07-01 17:43:41 -07002622 InToRegStorageX86_64Mapper mapper(this);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002623 InToRegStorageMapping in_to_reg_storage_mapping;
2624 in_to_reg_storage_mapping.Initialize(info->args, info->num_arg_words, &mapper);
2625 const int last_mapped_in = in_to_reg_storage_mapping.GetMaxMappedIn();
2626 const int size_of_the_last_mapped = last_mapped_in == -1 ? 1 :
Serguei Katkov8e3acdd2014-07-15 12:01:00 +07002627 info->args[last_mapped_in].wide ? 2 : 1;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002628 int regs_left_to_pass_via_stack = info->num_arg_words - (last_mapped_in + size_of_the_last_mapped);
2629
2630 // Fisrt of all, check whether it make sense to use bulk copying
2631 // Optimization is aplicable only for range case
2632 // TODO: make a constant instead of 2
2633 if (info->is_range && regs_left_to_pass_via_stack >= 2) {
2634 // Scan the rest of the args - if in phys_reg flush to memory
2635 for (int next_arg = last_mapped_in + size_of_the_last_mapped; next_arg < info->num_arg_words;) {
2636 RegLocation loc = info->args[next_arg];
2637 if (loc.wide) {
2638 loc = UpdateLocWide(loc);
2639 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002640 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08002641 StoreBaseDisp(rs_rX86_SP_64, SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002642 }
2643 next_arg += 2;
2644 } else {
2645 loc = UpdateLoc(loc);
2646 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002647 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Ian Rogersb28c1c02014-11-08 11:21:21 -08002648 StoreBaseDisp(rs_rX86_SP_64, SRegOffset(loc.s_reg_low), loc.reg, k32, kNotVolatile);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002649 }
2650 next_arg++;
2651 }
2652 }
2653
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002654 // The rest can be copied together
2655 int start_offset = SRegOffset(info->args[last_mapped_in + size_of_the_last_mapped].s_reg_low);
Andreas Gampeccc60262014-07-04 18:02:38 -07002656 int outs_offset = StackVisitor::GetOutVROffset(last_mapped_in + size_of_the_last_mapped,
2657 cu_->instruction_set);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002658
2659 int current_src_offset = start_offset;
2660 int current_dest_offset = outs_offset;
2661
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002662 // Only davik regs are accessed in this loop; no next_call_insn() calls.
2663 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002664 while (regs_left_to_pass_via_stack > 0) {
2665 // This is based on the knowledge that the stack itself is 16-byte aligned.
2666 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
2667 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
2668 size_t bytes_to_move;
2669
2670 /*
2671 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
2672 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
2673 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
2674 * We do this because we could potentially do a smaller move to align.
2675 */
2676 if (regs_left_to_pass_via_stack == 4 ||
2677 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
2678 // Moving 128-bits via xmm register.
2679 bytes_to_move = sizeof(uint32_t) * 4;
2680
2681 // Allocate a free xmm temp. Since we are working through the calling sequence,
2682 // we expect to have an xmm temporary available. AllocTempDouble will abort if
2683 // there are no free registers.
2684 RegStorage temp = AllocTempDouble();
2685
2686 LIR* ld1 = nullptr;
2687 LIR* ld2 = nullptr;
2688 LIR* st1 = nullptr;
2689 LIR* st2 = nullptr;
2690
2691 /*
2692 * The logic is similar for both loads and stores. If we have 16-byte alignment,
2693 * do an aligned move. If we have 8-byte alignment, then do the move in two
2694 * parts. This approach prevents possible cache line splits. Finally, fall back
2695 * to doing an unaligned move. In most cases we likely won't split the cache
2696 * line but we cannot prove it and thus take a conservative approach.
2697 */
2698 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
2699 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
2700
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002701 ScopedMemRefType mem_ref_type2(this, ResourceMask::kDalvikReg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002702 if (src_is_16b_aligned) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002703 ld1 = OpMovRegMem(temp, rs_rX86_SP_64, current_src_offset, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002704 } else if (src_is_8b_aligned) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002705 ld1 = OpMovRegMem(temp, rs_rX86_SP_64, current_src_offset, kMovLo128FP);
2706 ld2 = OpMovRegMem(temp, rs_rX86_SP_64, current_src_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002707 kMovHi128FP);
2708 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002709 ld1 = OpMovRegMem(temp, rs_rX86_SP_64, current_src_offset, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002710 }
2711
2712 if (dest_is_16b_aligned) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002713 st1 = OpMovMemReg(rs_rX86_SP_64, current_dest_offset, temp, kMovA128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002714 } else if (dest_is_8b_aligned) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002715 st1 = OpMovMemReg(rs_rX86_SP_64, current_dest_offset, temp, kMovLo128FP);
2716 st2 = OpMovMemReg(rs_rX86_SP_64, current_dest_offset + (bytes_to_move >> 1),
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002717 temp, kMovHi128FP);
2718 } else {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002719 st1 = OpMovMemReg(rs_rX86_SP_64, current_dest_offset, temp, kMovU128FP);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002720 }
2721
2722 // TODO If we could keep track of aliasing information for memory accesses that are wider
2723 // than 64-bit, we wouldn't need to set up a barrier.
2724 if (ld1 != nullptr) {
2725 if (ld2 != nullptr) {
2726 // For 64-bit load we can actually set up the aliasing information.
2727 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
2728 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
2729 } else {
2730 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002731 ld1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002732 }
2733 }
2734 if (st1 != nullptr) {
2735 if (st2 != nullptr) {
2736 // For 64-bit store we can actually set up the aliasing information.
2737 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
2738 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
2739 } else {
2740 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002741 st1->u.m.def_mask = &kEncodeAll;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002742 }
2743 }
2744
2745 // Free the temporary used for the data movement.
2746 FreeTemp(temp);
2747 } else {
2748 // Moving 32-bits via general purpose register.
2749 bytes_to_move = sizeof(uint32_t);
2750
2751 // Instead of allocating a new temp, simply reuse one of the registers being used
2752 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07002753 RegStorage temp = TargetReg(kArg3, kNotWide);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002754
2755 // Now load the argument VR and store to the outs.
Ian Rogersb28c1c02014-11-08 11:21:21 -08002756 Load32Disp(rs_rX86_SP_64, current_src_offset, temp);
2757 Store32Disp(rs_rX86_SP_64, current_dest_offset, temp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002758 }
2759
2760 current_src_offset += bytes_to_move;
2761 current_dest_offset += bytes_to_move;
2762 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
2763 }
2764 DCHECK_EQ(regs_left_to_pass_via_stack, 0);
2765 }
2766
2767 // Now handle rest not registers if they are
2768 if (in_to_reg_storage_mapping.IsThereStackMapped()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002769 RegStorage regSingle = TargetReg(kArg2, kNotWide);
2770 RegStorage regWide = TargetReg(kArg3, kWide);
Chao-ying Fub6564c12014-06-24 13:24:36 -07002771 for (int i = start_index;
2772 i < last_mapped_in + size_of_the_last_mapped + regs_left_to_pass_via_stack; i++) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002773 RegLocation rl_arg = info->args[i];
2774 rl_arg = UpdateRawLoc(rl_arg);
2775 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2776 if (!reg.Valid()) {
2777 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
2778
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002779 {
2780 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
2781 if (rl_arg.wide) {
2782 if (rl_arg.location == kLocPhysReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002783 StoreBaseDisp(rs_rX86_SP_64, out_offset, rl_arg.reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002784 } else {
2785 LoadValueDirectWideFixed(rl_arg, regWide);
Ian Rogersb28c1c02014-11-08 11:21:21 -08002786 StoreBaseDisp(rs_rX86_SP_64, out_offset, regWide, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002787 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002788 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002789 if (rl_arg.location == kLocPhysReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08002790 StoreBaseDisp(rs_rX86_SP_64, out_offset, rl_arg.reg, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002791 } else {
2792 LoadValueDirectFixed(rl_arg, regSingle);
Ian Rogersb28c1c02014-11-08 11:21:21 -08002793 StoreBaseDisp(rs_rX86_SP_64, out_offset, regSingle, k32, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01002794 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002795 }
2796 }
2797 call_state = next_call_insn(cu_, info, call_state, target_method,
2798 vtable_idx, direct_code, direct_method, type);
2799 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002800 if (rl_arg.wide) {
2801 i++;
2802 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002803 }
2804 }
2805
2806 // Finish with mapped registers
2807 for (int i = start_index; i <= last_mapped_in; i++) {
2808 RegLocation rl_arg = info->args[i];
2809 rl_arg = UpdateRawLoc(rl_arg);
2810 RegStorage reg = in_to_reg_storage_mapping.Get(i);
2811 if (reg.Valid()) {
2812 if (rl_arg.wide) {
2813 LoadValueDirectWideFixed(rl_arg, reg);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002814 } else {
2815 LoadValueDirectFixed(rl_arg, reg);
2816 }
2817 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2818 direct_code, direct_method, type);
2819 }
Chao-ying Fub6564c12014-06-24 13:24:36 -07002820 if (rl_arg.wide) {
2821 i++;
2822 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002823 }
2824
2825 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
2826 direct_code, direct_method, type);
2827 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00002828 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002829 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002830 } else {
2831 *pcrLabel = nullptr;
2832 // In lieu of generating a check for kArg1 being null, we need to
2833 // perform a load when doing implicit checks.
2834 RegStorage tmp = AllocTemp();
Andreas Gampeccc60262014-07-04 18:02:38 -07002835 Load32Disp(TargetReg(kArg1, kRef), 0, tmp);
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +07002836 MarkPossibleNullPointerException(info->opt_flags);
2837 FreeTemp(tmp);
2838 }
2839 }
2840 return call_state;
2841}
2842
Andreas Gampe98430592014-07-27 19:44:50 -07002843bool X86Mir2Lir::GenInlinedCharAt(CallInfo* info) {
2844 // Location of reference to data array
2845 int value_offset = mirror::String::ValueOffset().Int32Value();
2846 // Location of count
2847 int count_offset = mirror::String::CountOffset().Int32Value();
2848 // Starting offset within data array
2849 int offset_offset = mirror::String::OffsetOffset().Int32Value();
2850 // Start of char data with array_
2851 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
2852
2853 RegLocation rl_obj = info->args[0];
2854 RegLocation rl_idx = info->args[1];
2855 rl_obj = LoadValue(rl_obj, kRefReg);
2856 // X86 wants to avoid putting a constant index into a register.
2857 if (!rl_idx.is_const) {
2858 rl_idx = LoadValue(rl_idx, kCoreReg);
2859 }
2860 RegStorage reg_max;
2861 GenNullCheck(rl_obj.reg, info->opt_flags);
2862 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
2863 LIR* range_check_branch = nullptr;
2864 RegStorage reg_off;
2865 RegStorage reg_ptr;
2866 if (range_check) {
2867 // On x86, we can compare to memory directly
2868 // Set up a launch pad to allow retry in case of bounds violation */
2869 if (rl_idx.is_const) {
2870 LIR* comparison;
2871 range_check_branch = OpCmpMemImmBranch(
2872 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
2873 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr, &comparison);
2874 MarkPossibleNullPointerExceptionAfter(0, comparison);
2875 } else {
2876 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
2877 MarkPossibleNullPointerException(0);
2878 range_check_branch = OpCondBranch(kCondUge, nullptr);
2879 }
2880 }
2881 reg_off = AllocTemp();
2882 reg_ptr = AllocTempRef();
2883 Load32Disp(rl_obj.reg, offset_offset, reg_off);
2884 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
2885 if (rl_idx.is_const) {
2886 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
2887 } else {
2888 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
2889 }
2890 FreeTemp(rl_obj.reg);
2891 if (rl_idx.location == kLocPhysReg) {
2892 FreeTemp(rl_idx.reg);
2893 }
2894 RegLocation rl_dest = InlineTarget(info);
2895 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
2896 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
2897 FreeTemp(reg_off);
2898 FreeTemp(reg_ptr);
2899 StoreValue(rl_dest, rl_result);
2900 if (range_check) {
2901 DCHECK(range_check_branch != nullptr);
2902 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
2903 AddIntrinsicSlowPath(info, range_check_branch);
2904 }
2905 return true;
2906}
2907
Alexei Zavjalov6bbf0962014-07-15 02:19:41 +07002908bool X86Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
2909 RegLocation rl_dest = InlineTarget(info);
2910
2911 // Early exit if the result is unused.
2912 if (rl_dest.orig_sreg < 0) {
2913 return true;
2914 }
2915
2916 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
2917
2918 if (cu_->target64) {
2919 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<8>());
2920 } else {
2921 OpRegThreadMem(kOpMov, rl_result.reg, Thread::PeerOffset<4>());
2922 }
2923
2924 StoreValue(rl_dest, rl_result);
2925 return true;
2926}
2927
Maxim Kazantsev6dccdc22014-08-18 18:43:55 +07002928/**
2929 * Lock temp registers for explicit usage. Registers will be freed in destructor.
2930 */
2931X86Mir2Lir::ExplicitTempRegisterLock::ExplicitTempRegisterLock(X86Mir2Lir* mir_to_lir,
2932 int n_regs, ...) :
2933 temp_regs_(n_regs),
2934 mir_to_lir_(mir_to_lir) {
2935 va_list regs;
2936 va_start(regs, n_regs);
2937 for (int i = 0; i < n_regs; i++) {
2938 RegStorage reg = *(va_arg(regs, RegStorage*));
2939 RegisterInfo* info = mir_to_lir_->GetRegInfo(reg);
2940
2941 // Make sure we don't have promoted register here.
2942 DCHECK(info->IsTemp());
2943
2944 temp_regs_.push_back(reg);
2945 mir_to_lir_->FlushReg(reg);
2946
2947 if (reg.IsPair()) {
2948 RegStorage partner = info->Partner();
2949 temp_regs_.push_back(partner);
2950 mir_to_lir_->FlushReg(partner);
2951 }
2952
2953 mir_to_lir_->Clobber(reg);
2954 mir_to_lir_->LockTemp(reg);
2955 }
2956
2957 va_end(regs);
2958}
2959
2960/*
2961 * Free all locked registers.
2962 */
2963X86Mir2Lir::ExplicitTempRegisterLock::~ExplicitTempRegisterLock() {
2964 // Free all locked temps.
2965 for (auto it : temp_regs_) {
2966 mir_to_lir_->FreeTemp(it);
2967 }
2968}
2969
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002970} // namespace art