blob: 53b6ed420eb517bc05d444a9e02914eb63112c95 [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
17#include "dex/compiler_ir.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000018#include "dex/frontend.h"
19#include "dex/quick/dex_file_method_inliner.h"
20#include "dex/quick/dex_file_to_method_inliner_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "dex_file-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070022#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "invoke_type.h"
24#include "mirror/array.h"
25#include "mirror/string.h"
26#include "mir_to_lir-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "x86/codegen_x86.h"
28
29namespace art {
30
31/*
32 * This source files contains "gen" codegen routines that should
33 * be applicable to most targets. Only mid-level support utilities
34 * and "op" calls may be used here.
35 */
36
Mingyao Yang3a74d152014-04-21 15:39:44 -070037void Mir2Lir::AddIntrinsicSlowPath(CallInfo* info, LIR* branch, LIR* resume) {
38 class IntrinsicSlowPathPath : public Mir2Lir::LIRSlowPath {
Vladimir Marko3bc86152014-03-13 14:11:28 +000039 public:
Mingyao Yang3a74d152014-04-21 15:39:44 -070040 IntrinsicSlowPathPath(Mir2Lir* m2l, CallInfo* info, LIR* branch, LIR* resume = nullptr)
Vladimir Marko3bc86152014-03-13 14:11:28 +000041 : LIRSlowPath(m2l, info->offset, branch, resume), info_(info) {
42 }
43
44 void Compile() {
45 m2l_->ResetRegPool();
46 m2l_->ResetDefTracking();
47 LIR* label = GenerateTargetLabel();
48 label->opcode = kPseudoIntrinsicRetry;
49 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
50 m2l_->GenInvokeNoInline(info_);
51 if (cont_ != nullptr) {
52 m2l_->OpUnconditionalBranch(cont_);
53 }
54 }
55
56 private:
57 CallInfo* const info_;
58 };
59
Mingyao Yang3a74d152014-04-21 15:39:44 -070060 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000061}
62
Brian Carlstrom7940e442013-07-12 13:46:57 -070063/*
64 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000065 * the helper target address, and the actual call to the helper. Because x86
66 * has a memory call operation, part 1 is a NOP for x86. For other targets,
67 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070068 */
Ian Rogersdd7624d2014-03-14 17:43:00 -070069RegStorage Mir2Lir::CallHelperSetup(ThreadOffset<4> helper_offset) {
Dave Allisond6ed6422014-04-09 23:36:15 +000070 return (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) ? RegStorage::InvalidReg() : LoadHelper(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070071}
72
73/* NOTE: if r_tgt is a temp, it will be freed following use */
Ian Rogersdd7624d2014-03-14 17:43:00 -070074LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<4> helper_offset, bool safepoint_pc,
buzbee2700f7e2014-03-07 09:46:20 -080075 bool use_link) {
Dave Allisond6ed6422014-04-09 23:36:15 +000076 LIR* call_inst;
Brian Carlstrom60d7a652014-03-13 18:10:08 -070077 OpKind op = use_link ? kOpBlx : kOpBx;
Dave Allisond6ed6422014-04-09 23:36:15 +000078 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
79 call_inst = OpThreadMem(op, helper_offset);
80 } else {
81 call_inst = OpReg(op, r_tgt);
82 FreeTemp(r_tgt);
83 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 if (safepoint_pc) {
85 MarkSafepointPC(call_inst);
86 }
87 return call_inst;
88}
89
Mingyao Yang42894562014-04-07 12:42:16 -070090void Mir2Lir::CallRuntimeHelper(ThreadOffset<4> helper_offset, bool safepoint_pc) {
91 RegStorage r_tgt = CallHelperSetup(helper_offset);
92 ClobberCallerSave();
93 CallHelper(r_tgt, helper_offset, safepoint_pc);
94}
95
Ian Rogersdd7624d2014-03-14 17:43:00 -070096void Mir2Lir::CallRuntimeHelperImm(ThreadOffset<4> helper_offset, int arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -080097 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +000099 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100 CallHelper(r_tgt, helper_offset, safepoint_pc);
101}
102
Ian Rogersdd7624d2014-03-14 17:43:00 -0700103void Mir2Lir::CallRuntimeHelperReg(ThreadOffset<4> helper_offset, RegStorage arg0,
104 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800105 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 OpRegCopy(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000107 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 CallHelper(r_tgt, helper_offset, safepoint_pc);
109}
110
Ian Rogersdd7624d2014-03-14 17:43:00 -0700111void Mir2Lir::CallRuntimeHelperRegLocation(ThreadOffset<4> helper_offset, RegLocation arg0,
Ian Rogers848871b2013-08-05 10:56:33 -0700112 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800113 RegStorage r_tgt = CallHelperSetup(helper_offset);
114 if (arg0.wide == 0) {
115 LoadValueDirectFixed(arg0, TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800117 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
118 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000120 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 CallHelper(r_tgt, helper_offset, safepoint_pc);
122}
123
Ian Rogersdd7624d2014-03-14 17:43:00 -0700124void Mir2Lir::CallRuntimeHelperImmImm(ThreadOffset<4> helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800126 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 LoadConstant(TargetReg(kArg0), arg0);
128 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000129 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 CallHelper(r_tgt, helper_offset, safepoint_pc);
131}
132
Ian Rogersdd7624d2014-03-14 17:43:00 -0700133void Mir2Lir::CallRuntimeHelperImmRegLocation(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 RegLocation arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800135 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 if (arg1.wide == 0) {
137 LoadValueDirectFixed(arg1, TargetReg(kArg1));
138 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800139 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
140 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 }
142 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000143 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 CallHelper(r_tgt, helper_offset, safepoint_pc);
145}
146
Ian Rogersdd7624d2014-03-14 17:43:00 -0700147void Mir2Lir::CallRuntimeHelperRegLocationImm(ThreadOffset<4> helper_offset, RegLocation arg0,
148 int arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800149 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 LoadValueDirectFixed(arg0, TargetReg(kArg0));
151 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000152 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 CallHelper(r_tgt, helper_offset, safepoint_pc);
154}
155
Ian Rogersdd7624d2014-03-14 17:43:00 -0700156void Mir2Lir::CallRuntimeHelperImmReg(ThreadOffset<4> helper_offset, int arg0, RegStorage arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800158 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 OpRegCopy(TargetReg(kArg1), arg1);
160 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000161 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 CallHelper(r_tgt, helper_offset, safepoint_pc);
163}
164
Ian Rogersdd7624d2014-03-14 17:43:00 -0700165void Mir2Lir::CallRuntimeHelperRegImm(ThreadOffset<4> helper_offset, RegStorage arg0, int arg1,
Ian Rogers848871b2013-08-05 10:56:33 -0700166 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800167 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 OpRegCopy(TargetReg(kArg0), arg0);
169 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000170 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 CallHelper(r_tgt, helper_offset, safepoint_pc);
172}
173
Ian Rogersdd7624d2014-03-14 17:43:00 -0700174void Mir2Lir::CallRuntimeHelperImmMethod(ThreadOffset<4> helper_offset, int arg0,
175 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800176 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 LoadCurrMethodDirect(TargetReg(kArg1));
178 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000179 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 CallHelper(r_tgt, helper_offset, safepoint_pc);
181}
182
Ian Rogersdd7624d2014-03-14 17:43:00 -0700183void Mir2Lir::CallRuntimeHelperRegMethod(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800184 bool safepoint_pc) {
185 RegStorage r_tgt = CallHelperSetup(helper_offset);
186 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800187 if (TargetReg(kArg0) != arg0) {
188 OpRegCopy(TargetReg(kArg0), arg0);
189 }
190 LoadCurrMethodDirect(TargetReg(kArg1));
191 ClobberCallerSave();
192 CallHelper(r_tgt, helper_offset, safepoint_pc);
193}
194
Ian Rogersdd7624d2014-03-14 17:43:00 -0700195void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(ThreadOffset<4> helper_offset, RegStorage arg0,
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800196 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800197 RegStorage r_tgt = CallHelperSetup(helper_offset);
198 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800199 if (TargetReg(kArg0) != arg0) {
200 OpRegCopy(TargetReg(kArg0), arg0);
201 }
202 LoadCurrMethodDirect(TargetReg(kArg1));
203 LoadValueDirectFixed(arg2, TargetReg(kArg2));
204 ClobberCallerSave();
205 CallHelper(r_tgt, helper_offset, safepoint_pc);
206}
207
Ian Rogersdd7624d2014-03-14 17:43:00 -0700208void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(ThreadOffset<4> helper_offset,
209 RegLocation arg0, RegLocation arg1,
210 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800211 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 if (arg0.wide == 0) {
213 LoadValueDirectFixed(arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
214 if (arg1.wide == 0) {
215 if (cu_->instruction_set == kMips) {
216 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1));
217 } else {
218 LoadValueDirectFixed(arg1, TargetReg(kArg1));
219 }
220 } else {
221 if (cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -0800222 RegStorage r_tmp;
223 if (arg1.fp) {
224 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
225 } else {
226 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
227 }
228 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800230 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
231 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 }
233 }
234 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800235 RegStorage r_tmp;
236 if (arg0.fp) {
237 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg0), TargetReg(kFArg1));
238 } else {
239 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
240 }
241 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 if (arg1.wide == 0) {
243 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2));
244 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800245 RegStorage r_tmp;
246 if (arg1.fp) {
247 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
248 } else {
249 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
250 }
251 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 }
253 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000254 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 CallHelper(r_tgt, helper_offset, safepoint_pc);
256}
257
Mingyao Yang80365d92014-04-18 12:10:58 -0700258void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
259 if (arg1.GetReg() == TargetReg(kArg0).GetReg()) {
260 if (arg0.GetReg() == TargetReg(kArg1).GetReg()) {
261 // Swap kArg0 and kArg1 with kArg2 as temp.
262 OpRegCopy(TargetReg(kArg2), arg1);
263 OpRegCopy(TargetReg(kArg0), arg0);
264 OpRegCopy(TargetReg(kArg1), TargetReg(kArg2));
265 } else {
266 OpRegCopy(TargetReg(kArg1), arg1);
267 OpRegCopy(TargetReg(kArg0), arg0);
268 }
269 } else {
270 OpRegCopy(TargetReg(kArg0), arg0);
271 OpRegCopy(TargetReg(kArg1), arg1);
272 }
273}
274
Ian Rogersdd7624d2014-03-14 17:43:00 -0700275void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800276 RegStorage arg1, bool safepoint_pc) {
277 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700278 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000279 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 CallHelper(r_tgt, helper_offset, safepoint_pc);
281}
282
Ian Rogersdd7624d2014-03-14 17:43:00 -0700283void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800284 RegStorage arg1, int arg2, bool safepoint_pc) {
285 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700286 CopyToArgumentRegs(arg0, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 LoadConstant(TargetReg(kArg2), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000288 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 CallHelper(r_tgt, helper_offset, safepoint_pc);
290}
291
Ian Rogersdd7624d2014-03-14 17:43:00 -0700292void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 int arg0, RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800294 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 LoadValueDirectFixed(arg2, TargetReg(kArg2));
296 LoadCurrMethodDirect(TargetReg(kArg1));
297 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000298 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 CallHelper(r_tgt, helper_offset, safepoint_pc);
300}
301
Ian Rogersdd7624d2014-03-14 17:43:00 -0700302void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 int arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800304 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 LoadCurrMethodDirect(TargetReg(kArg1));
306 LoadConstant(TargetReg(kArg2), arg2);
307 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000308 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 CallHelper(r_tgt, helper_offset, safepoint_pc);
310}
311
Ian Rogersdd7624d2014-03-14 17:43:00 -0700312void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 int arg0, RegLocation arg1,
314 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800315 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700316 DCHECK_EQ(arg1.wide, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 LoadValueDirectFixed(arg1, TargetReg(kArg1));
318 if (arg2.wide == 0) {
319 LoadValueDirectFixed(arg2, TargetReg(kArg2));
320 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800321 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
322 LoadValueDirectWideFixed(arg2, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 }
324 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000325 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 CallHelper(r_tgt, helper_offset, safepoint_pc);
327}
328
Ian Rogersdd7624d2014-03-14 17:43:00 -0700329void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset<4> helper_offset,
Ian Rogersa9a82542013-10-04 11:17:26 -0700330 RegLocation arg0, RegLocation arg1,
331 RegLocation arg2,
332 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800333 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700334 DCHECK_EQ(arg0.wide, 0U);
335 LoadValueDirectFixed(arg0, TargetReg(kArg0));
336 DCHECK_EQ(arg1.wide, 0U);
337 LoadValueDirectFixed(arg1, TargetReg(kArg1));
338 DCHECK_EQ(arg1.wide, 0U);
339 LoadValueDirectFixed(arg2, TargetReg(kArg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000340 ClobberCallerSave();
Ian Rogersa9a82542013-10-04 11:17:26 -0700341 CallHelper(r_tgt, helper_offset, safepoint_pc);
342}
343
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344/*
345 * If there are any ins passed in registers that have not been promoted
346 * to a callee-save register, flush them to the frame. Perform intial
347 * assignment of promoted arguments.
348 *
349 * ArgLocs is an array of location records describing the incoming arguments
350 * with one location record per word of argument.
351 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700352void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 /*
354 * Dummy up a RegLocation for the incoming Method*
355 * It will attempt to keep kArg0 live (or copy it to home location
356 * if promoted).
357 */
358 RegLocation rl_src = rl_method;
359 rl_src.location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800360 rl_src.reg = TargetReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 rl_src.home = false;
buzbee2700f7e2014-03-07 09:46:20 -0800362 MarkLive(rl_src.reg, rl_src.s_reg_low);
buzbee695d13a2014-04-19 13:32:20 -0700363 if (rl_method.wide) {
364 StoreValueWide(rl_method, rl_src);
365 } else {
366 StoreValue(rl_method, rl_src);
367 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 // If Method* has been promoted, explicitly flush
369 if (rl_method.location == kLocPhysReg) {
370 StoreWordDisp(TargetReg(kSp), 0, TargetReg(kArg0));
371 }
372
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800373 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800375 }
376
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
378 /*
379 * Copy incoming arguments to their proper home locations.
380 * NOTE: an older version of dx had an issue in which
381 * it would reuse static method argument registers.
382 * This could result in the same Dalvik virtual register
383 * being promoted to both core and fp regs. To account for this,
384 * we only copy to the corresponding promoted physical register
385 * if it matches the type of the SSA name for the incoming
386 * argument. It is also possible that long and double arguments
387 * end up half-promoted. In those cases, we must flush the promoted
388 * half to memory as well.
389 */
390 for (int i = 0; i < cu_->num_ins; i++) {
391 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800392 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800393
buzbee2700f7e2014-03-07 09:46:20 -0800394 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 // If arriving in register
396 bool need_flush = true;
397 RegLocation* t_loc = &ArgLocs[i];
398 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800399 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 need_flush = false;
401 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800402 OpRegCopy(RegStorage::Solo32(v_map->FpReg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 need_flush = false;
404 } else {
405 need_flush = true;
406 }
407
buzbeed0a03b82013-09-14 08:21:05 -0700408 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 if (t_loc->wide) {
410 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700411 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 need_flush |= (p_map->core_location != v_map->core_location) ||
413 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700414 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
415 /*
416 * In Arm, a double is represented as a pair of consecutive single float
417 * registers starting at an even number. It's possible that both Dalvik vRegs
418 * representing the incoming double were independently promoted as singles - but
419 * not in a form usable as a double. If so, we need to flush - even though the
420 * incoming arg appears fully in register. At this point in the code, both
421 * halves of the double are promoted. Make sure they are in a usable form.
422 */
423 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
424 int low_reg = promotion_map_[lowreg_index].FpReg;
425 int high_reg = promotion_map_[lowreg_index + 1].FpReg;
426 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
427 need_flush = true;
428 }
429 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 }
431 if (need_flush) {
buzbee695d13a2014-04-19 13:32:20 -0700432 Store32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 }
434 } else {
435 // If arriving in frame & promoted
436 if (v_map->core_location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700437 Load32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 }
439 if (v_map->fp_location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700440 Load32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->FpReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 }
442 }
443 }
444}
445
446/*
447 * Bit of a hack here - in the absence of a real scheduling pass,
448 * emit the next instruction in static & direct invoke sequences.
449 */
450static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
451 int state, const MethodReference& target_method,
452 uint32_t unused,
453 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700454 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 if (direct_code != 0 && direct_method != 0) {
457 switch (state) {
458 case 0: // Get the current Method* [sets kArg0]
459 if (direct_code != static_cast<unsigned int>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700460 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700461 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
462 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700463 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700464 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 }
466 if (direct_method != static_cast<unsigned int>(-1)) {
467 cg->LoadConstant(cg->TargetReg(kArg0), direct_method);
468 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700469 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 }
471 break;
472 default:
473 return -1;
474 }
475 } else {
476 switch (state) {
477 case 0: // Get the current Method* [sets kArg0]
478 // TUNING: we can save a reg copy if Method* has been promoted.
479 cg->LoadCurrMethodDirect(cg->TargetReg(kArg0));
480 break;
481 case 1: // Get method->dex_cache_resolved_methods_
buzbee695d13a2014-04-19 13:32:20 -0700482 cg->LoadRefDisp(cg->TargetReg(kArg0),
483 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
484 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 // Set up direct code if known.
486 if (direct_code != 0) {
487 if (direct_code != static_cast<unsigned int>(-1)) {
488 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700489 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700490 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700491 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 }
493 }
494 break;
495 case 2: // Grab target method*
496 CHECK_EQ(cu->dex_file, target_method.dex_file);
buzbee695d13a2014-04-19 13:32:20 -0700497 cg->LoadRefDisp(cg->TargetReg(kArg0),
498 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
499 (target_method.dex_method_index * 4), cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 break;
501 case 3: // Grab the code from the method*
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700502 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 if (direct_code == 0) {
504 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800505 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 cg->TargetReg(kInvokeTgt));
507 }
508 break;
509 }
510 // Intentional fallthrough for x86
511 default:
512 return -1;
513 }
514 }
515 return state + 1;
516}
517
518/*
519 * Bit of a hack here - in the absence of a real scheduling pass,
520 * emit the next instruction in a virtual invoke sequence.
521 * We can use kLr as a temp prior to target address loading
522 * Note also that we'll load the first argument ("this") into
523 * kArg1 here rather than the standard LoadArgRegs.
524 */
525static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
526 int state, const MethodReference& target_method,
527 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700528 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
530 /*
531 * This is the fast path in which the target virtual method is
532 * fully resolved at compile time.
533 */
534 switch (state) {
535 case 0: { // Get "this" [set kArg1]
536 RegLocation rl_arg = info->args[0];
537 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
538 break;
539 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700540 case 1: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800541 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 // get this->klass_ [use kArg1, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700543 cg->LoadRefDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
544 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800545 cg->MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700547 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700548 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
549 cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700551 case 3: // Get target method [use kInvokeTgt, set kArg0]
buzbee695d13a2014-04-19 13:32:20 -0700552 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), (method_idx * 4) +
553 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
554 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700556 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700557 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800559 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 cg->TargetReg(kInvokeTgt));
561 break;
562 }
563 // Intentional fallthrough for X86
564 default:
565 return -1;
566 }
567 return state + 1;
568}
569
570/*
Jeff Hao88474b42013-10-23 16:24:40 -0700571 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
572 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
573 * more than one interface method map to the same index. Note also that we'll load the first
574 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 */
576static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
577 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700578 uint32_t method_idx, uintptr_t unused,
579 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581
Jeff Hao88474b42013-10-23 16:24:40 -0700582 switch (state) {
583 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700584 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
585 cg->LoadConstant(cg->TargetReg(kHiddenArg), target_method.dex_method_index);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700586 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700587 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg), cg->TargetReg(kHiddenArg));
588 }
589 break;
590 case 1: { // Get "this" [set kArg1]
591 RegLocation rl_arg = info->args[0];
592 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
593 break;
594 }
595 case 2: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800596 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700597 // Get this->klass_ [use kArg1, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700598 cg->LoadRefDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
599 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800600 cg->MarkPossibleNullPointerException(info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700601 break;
602 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700603 // NOTE: native pointer.
604 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
605 cg->TargetReg(kInvokeTgt));
Jeff Hao88474b42013-10-23 16:24:40 -0700606 break;
607 case 4: // Get target method [use kInvokeTgt, set kArg0]
buzbee695d13a2014-04-19 13:32:20 -0700608 // NOTE: native pointer.
Jeff Hao88474b42013-10-23 16:24:40 -0700609 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), ((method_idx % ClassLinker::kImtSize) * 4) +
610 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 cg->TargetReg(kArg0));
612 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700613 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700614 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700615 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800616 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Jeff Hao88474b42013-10-23 16:24:40 -0700617 cg->TargetReg(kInvokeTgt));
618 break;
619 }
620 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 default:
622 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 }
624 return state + 1;
625}
626
Ian Rogersdd7624d2014-03-14 17:43:00 -0700627static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset<4> trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700629 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
631 /*
632 * This handles the case in which the base method is not fully
633 * resolved at compile time, we bail to a runtime helper.
634 */
635 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700636 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 // Load trampoline target
Ian Rogers848871b2013-08-05 10:56:33 -0700638 cg->LoadWordDisp(cg->TargetReg(kSelf), trampoline.Int32Value(), cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
640 // Load kArg0 with method index
641 CHECK_EQ(cu->dex_file, target_method.dex_file);
642 cg->LoadConstant(cg->TargetReg(kArg0), target_method.dex_method_index);
643 return 1;
644 }
645 return -1;
646}
647
648static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
649 int state,
650 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000651 uint32_t unused, uintptr_t unused2,
652 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700653 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
655}
656
657static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
658 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000659 uint32_t unused, uintptr_t unused2,
660 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700661 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
663}
664
665static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
666 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000667 uint32_t unused, uintptr_t unused2,
668 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700669 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
671}
672
673static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
674 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000675 uint32_t unused, uintptr_t unused2,
676 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700677 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
679}
680
681static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
682 CallInfo* info, int state,
683 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000684 uint32_t unused, uintptr_t unused2,
685 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700686 ThreadOffset<4> trampoline =
687 QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
689}
690
691int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
692 NextCallInsn next_call_insn,
693 const MethodReference& target_method,
694 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700695 uintptr_t direct_method, InvokeType type, bool skip_this) {
buzbee2700f7e2014-03-07 09:46:20 -0800696 int last_arg_reg = TargetReg(kArg3).GetReg();
697 int next_reg = TargetReg(kArg1).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 int next_arg = 0;
699 if (skip_this) {
700 next_reg++;
701 next_arg++;
702 }
703 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
704 RegLocation rl_arg = info->args[next_arg++];
705 rl_arg = UpdateRawLoc(rl_arg);
buzbee2700f7e2014-03-07 09:46:20 -0800706 if (rl_arg.wide && (next_reg <= TargetReg(kArg2).GetReg())) {
707 RegStorage r_tmp(RegStorage::k64BitPair, next_reg, next_reg + 1);
708 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 next_reg++;
710 next_arg++;
711 } else {
712 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800713 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 rl_arg.is_const = false;
715 }
buzbee2700f7e2014-03-07 09:46:20 -0800716 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(next_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 }
718 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
719 direct_code, direct_method, type);
720 }
721 return call_state;
722}
723
724/*
725 * Load up to 5 arguments, the first three of which will be in
726 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
727 * and as part of the load sequence, it must be replaced with
728 * the target method pointer. Note, this may also be called
729 * for "range" variants if the number of arguments is 5 or fewer.
730 */
731int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
732 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
733 const MethodReference& target_method,
734 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700735 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 RegLocation rl_arg;
737
738 /* If no arguments, just return */
739 if (info->num_arg_words == 0)
740 return call_state;
741
742 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
743 direct_code, direct_method, type);
744
745 DCHECK_LE(info->num_arg_words, 5);
746 if (info->num_arg_words > 3) {
747 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700748 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 RegLocation rl_use0 = info->args[0];
750 RegLocation rl_use1 = info->args[1];
751 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800752 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
753 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 // Wide spans, we need the 2nd half of uses[2].
755 rl_arg = UpdateLocWide(rl_use2);
756 if (rl_arg.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800757 reg = rl_arg.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 } else {
759 // kArg2 & rArg3 can safely be used here
760 reg = TargetReg(kArg3);
buzbee695d13a2014-04-19 13:32:20 -0700761 Load32Disp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 call_state = next_call_insn(cu_, info, call_state, target_method,
763 vtable_idx, direct_code, direct_method, type);
764 }
buzbee695d13a2014-04-19 13:32:20 -0700765 Store32Disp(TargetReg(kSp), (next_use + 1) * 4, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
767 direct_code, direct_method, type);
768 next_use++;
769 }
770 // Loop through the rest
771 while (next_use < info->num_arg_words) {
buzbee2700f7e2014-03-07 09:46:20 -0800772 RegStorage low_reg;
773 RegStorage high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774 rl_arg = info->args[next_use];
775 rl_arg = UpdateRawLoc(rl_arg);
776 if (rl_arg.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000777 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800778 low_reg = rl_arg.reg.GetLow();
779 high_reg = rl_arg.reg.GetHigh();
780 } else {
781 low_reg = rl_arg.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000782 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 } else {
784 low_reg = TargetReg(kArg2);
785 if (rl_arg.wide) {
786 high_reg = TargetReg(kArg3);
buzbee2700f7e2014-03-07 09:46:20 -0800787 LoadValueDirectWideFixed(rl_arg, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 } else {
789 LoadValueDirectFixed(rl_arg, low_reg);
790 }
791 call_state = next_call_insn(cu_, info, call_state, target_method,
792 vtable_idx, direct_code, direct_method, type);
793 }
794 int outs_offset = (next_use + 1) * 4;
795 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800796 StoreBaseDispWide(TargetReg(kSp), outs_offset, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 next_use += 2;
798 } else {
buzbee695d13a2014-04-19 13:32:20 -0700799 Store32Disp(TargetReg(kSp), outs_offset, low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 next_use++;
801 }
802 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
803 direct_code, direct_method, type);
804 }
805 }
806
807 call_state = LoadArgRegs(info, call_state, next_call_insn,
808 target_method, vtable_idx, direct_code, direct_method,
809 type, skip_this);
810
811 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700812 if (Runtime::Current()->ExplicitNullChecks()) {
813 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
814 } else {
815 *pcrLabel = nullptr;
816 // In lieu of generating a check for kArg1 being null, we need to
817 // perform a load when doing implicit checks.
818 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700819 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -0700820 MarkPossibleNullPointerException(info->opt_flags);
821 FreeTemp(tmp);
822 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 }
824 return call_state;
825}
826
827/*
828 * May have 0+ arguments (also used for jumbo). Note that
829 * source virtual registers may be in physical registers, so may
830 * need to be flushed to home location before copying. This
831 * applies to arg3 and above (see below).
832 *
833 * Two general strategies:
834 * If < 20 arguments
835 * Pass args 3-18 using vldm/vstm block copy
836 * Pass arg0, arg1 & arg2 in kArg1-kArg3
837 * If 20+ arguments
838 * Pass args arg19+ using memcpy block copy
839 * Pass arg0, arg1 & arg2 in kArg1-kArg3
840 *
841 */
842int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
843 LIR** pcrLabel, NextCallInsn next_call_insn,
844 const MethodReference& target_method,
845 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700846 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 // If we can treat it as non-range (Jumbo ops will use range form)
848 if (info->num_arg_words <= 5)
849 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
850 next_call_insn, target_method, vtable_idx,
851 direct_code, direct_method, type, skip_this);
852 /*
853 * First load the non-register arguments. Both forms expect all
854 * of the source arguments to be in their home frame location, so
855 * scan the s_reg names and flush any that have been promoted to
856 * frame backing storage.
857 */
858 // Scan the rest of the args - if in phys_reg flush to memory
859 for (int next_arg = 0; next_arg < info->num_arg_words;) {
860 RegLocation loc = info->args[next_arg];
861 if (loc.wide) {
862 loc = UpdateLocWide(loc);
863 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800864 StoreBaseDispWide(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 }
866 next_arg += 2;
867 } else {
868 loc = UpdateLoc(loc);
869 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
buzbee695d13a2014-04-19 13:32:20 -0700870 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871 }
872 next_arg++;
873 }
874 }
875
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800876 // Logic below assumes that Method pointer is at offset zero from SP.
877 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
878
879 // The first 3 arguments are passed via registers.
880 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
881 // get size of uintptr_t or size of object reference according to model being used.
882 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800884 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
885 DCHECK_GT(regs_left_to_pass_via_stack, 0);
886
887 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
888 // Use vldm/vstm pair using kArg3 as a temp
889 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
890 direct_code, direct_method, type);
891 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
892 LIR* ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
893 // TUNING: loosen barrier
894 ld->u.m.def_mask = ENCODE_ALL;
895 SetMemRefType(ld, true /* is_load */, kDalvikReg);
896 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
897 direct_code, direct_method, type);
898 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
899 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
900 direct_code, direct_method, type);
901 LIR* st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
902 SetMemRefType(st, false /* is_load */, kDalvikReg);
903 st->u.m.def_mask = ENCODE_ALL;
904 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
905 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700906 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800907 int current_src_offset = start_offset;
908 int current_dest_offset = outs_offset;
909
910 while (regs_left_to_pass_via_stack > 0) {
911 // This is based on the knowledge that the stack itself is 16-byte aligned.
912 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
913 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
914 size_t bytes_to_move;
915
916 /*
917 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
918 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
919 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
920 * We do this because we could potentially do a smaller move to align.
921 */
922 if (regs_left_to_pass_via_stack == 4 ||
923 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
924 // Moving 128-bits via xmm register.
925 bytes_to_move = sizeof(uint32_t) * 4;
926
927 // Allocate a free xmm temp. Since we are working through the calling sequence,
928 // we expect to have an xmm temporary available.
buzbee2700f7e2014-03-07 09:46:20 -0800929 RegStorage temp = AllocTempDouble();
930 CHECK_GT(temp.GetLowReg(), 0);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800931
932 LIR* ld1 = nullptr;
933 LIR* ld2 = nullptr;
934 LIR* st1 = nullptr;
935 LIR* st2 = nullptr;
936
937 /*
938 * The logic is similar for both loads and stores. If we have 16-byte alignment,
939 * do an aligned move. If we have 8-byte alignment, then do the move in two
940 * parts. This approach prevents possible cache line splits. Finally, fall back
941 * to doing an unaligned move. In most cases we likely won't split the cache
942 * line but we cannot prove it and thus take a conservative approach.
943 */
944 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
945 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
946
947 if (src_is_16b_aligned) {
948 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
949 } else if (src_is_8b_aligned) {
950 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800951 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
952 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800953 } else {
954 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
955 }
956
957 if (dest_is_16b_aligned) {
958 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
959 } else if (dest_is_8b_aligned) {
960 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800961 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
962 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800963 } else {
964 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
965 }
966
967 // TODO If we could keep track of aliasing information for memory accesses that are wider
968 // than 64-bit, we wouldn't need to set up a barrier.
969 if (ld1 != nullptr) {
970 if (ld2 != nullptr) {
971 // For 64-bit load we can actually set up the aliasing information.
972 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
973 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
974 } else {
975 // Set barrier for 128-bit load.
976 SetMemRefType(ld1, true /* is_load */, kDalvikReg);
977 ld1->u.m.def_mask = ENCODE_ALL;
978 }
979 }
980 if (st1 != nullptr) {
981 if (st2 != nullptr) {
982 // For 64-bit store we can actually set up the aliasing information.
983 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
984 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
985 } else {
986 // Set barrier for 128-bit store.
987 SetMemRefType(st1, false /* is_load */, kDalvikReg);
988 st1->u.m.def_mask = ENCODE_ALL;
989 }
990 }
991
992 // Free the temporary used for the data movement.
buzbee2700f7e2014-03-07 09:46:20 -0800993 // CLEANUP: temp is currently a bogus pair, elmiminate extra free when updated.
994 FreeTemp(temp.GetLow());
995 FreeTemp(temp.GetHigh());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800996 } else {
997 // Moving 32-bits via general purpose register.
998 bytes_to_move = sizeof(uint32_t);
999
1000 // Instead of allocating a new temp, simply reuse one of the registers being used
1001 // for argument passing.
buzbee2700f7e2014-03-07 09:46:20 -08001002 RegStorage temp = TargetReg(kArg3);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001003
1004 // Now load the argument VR and store to the outs.
buzbee695d13a2014-04-19 13:32:20 -07001005 Load32Disp(TargetReg(kSp), current_src_offset, temp);
1006 Store32Disp(TargetReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001007 }
1008
1009 current_src_offset += bytes_to_move;
1010 current_dest_offset += bytes_to_move;
1011 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1012 }
1013 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 // Generate memcpy
1015 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
1016 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001017 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(4, pMemcpy), TargetReg(kArg0),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 }
1020
1021 call_state = LoadArgRegs(info, call_state, next_call_insn,
1022 target_method, vtable_idx, direct_code, direct_method,
1023 type, skip_this);
1024
1025 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1026 direct_code, direct_method, type);
1027 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -07001028 if (Runtime::Current()->ExplicitNullChecks()) {
1029 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
1030 } else {
1031 *pcrLabel = nullptr;
1032 // In lieu of generating a check for kArg1 being null, we need to
1033 // perform a load when doing implicit checks.
1034 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001035 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -07001036 MarkPossibleNullPointerException(info->opt_flags);
1037 FreeTemp(tmp);
1038 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 }
1040 return call_state;
1041}
1042
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001043RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 RegLocation res;
1045 if (info->result.location == kLocInvalid) {
1046 res = GetReturn(false);
1047 } else {
1048 res = info->result;
1049 }
1050 return res;
1051}
1052
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001053RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 RegLocation res;
1055 if (info->result.location == kLocInvalid) {
1056 res = GetReturnWide(false);
1057 } else {
1058 res = info->result;
1059 }
1060 return res;
1061}
1062
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001063bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001064 if (cu_->instruction_set == kMips) {
1065 // TODO - add Mips implementation
1066 return false;
1067 }
1068 // Location of reference to data array
1069 int value_offset = mirror::String::ValueOffset().Int32Value();
1070 // Location of count
1071 int count_offset = mirror::String::CountOffset().Int32Value();
1072 // Starting offset within data array
1073 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1074 // Start of char data with array_
1075 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1076
1077 RegLocation rl_obj = info->args[0];
1078 RegLocation rl_idx = info->args[1];
1079 rl_obj = LoadValue(rl_obj, kCoreReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001080 // X86 wants to avoid putting a constant index into a register.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001081 if (!((cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64)&& rl_idx.is_const)) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001082 rl_idx = LoadValue(rl_idx, kCoreReg);
1083 }
buzbee2700f7e2014-03-07 09:46:20 -08001084 RegStorage reg_max;
1085 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001087 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001088 RegStorage reg_off;
1089 RegStorage reg_ptr;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001090 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 reg_off = AllocTemp();
1092 reg_ptr = AllocTemp();
1093 if (range_check) {
1094 reg_max = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001095 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001096 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 }
buzbee695d13a2014-04-19 13:32:20 -07001098 Load32Disp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001099 MarkPossibleNullPointerException(info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001100 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 if (range_check) {
Mingyao Yang3a74d152014-04-21 15:39:44 -07001102 // Set up a slow path to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001103 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001105 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001106 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001107 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 } else {
1109 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001110 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001112 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001113 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001114 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Vladimir Marko3bc86152014-03-13 14:11:28 +00001115 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001116 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001117 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001118 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001119 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 }
1121 reg_off = AllocTemp();
1122 reg_ptr = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001123 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1124 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001126 if (rl_idx.is_const) {
1127 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1128 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001129 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001130 }
buzbee2700f7e2014-03-07 09:46:20 -08001131 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001132 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001133 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001134 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 RegLocation rl_dest = InlineTarget(info);
1136 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001137 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee2700f7e2014-03-07 09:46:20 -08001138 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001139 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001140 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg,
1141 RegStorage::InvalidReg(), kUnsignedHalf, INVALID_SREG);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001142 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 FreeTemp(reg_off);
1144 FreeTemp(reg_ptr);
1145 StoreValue(rl_dest, rl_result);
1146 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001147 DCHECK(range_check_branch != nullptr);
1148 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001149 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 return true;
1152}
1153
1154// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001155bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 if (cu_->instruction_set == kMips) {
1157 // TODO - add Mips implementation
1158 return false;
1159 }
1160 // dst = src.length();
1161 RegLocation rl_obj = info->args[0];
1162 rl_obj = LoadValue(rl_obj, kCoreReg);
1163 RegLocation rl_dest = InlineTarget(info);
1164 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001165 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001166 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001167 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 if (is_empty) {
1169 // dst = (dst == 0);
1170 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001171 RegStorage t_reg = AllocTemp();
1172 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1173 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001175 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001176 OpRegImm(kOpSub, rl_result.reg, 1);
1177 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 }
1179 }
1180 StoreValue(rl_dest, rl_result);
1181 return true;
1182}
1183
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001184bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1185 if (cu_->instruction_set == kMips) {
1186 // TODO - add Mips implementation
1187 return false;
1188 }
1189 RegLocation rl_src_i = info->args[0];
buzbee695d13a2014-04-19 13:32:20 -07001190 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001191 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001192 if (size == k64) {
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001193 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001194 RegStorage r_i_low = rl_i.reg.GetLow();
1195 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001196 // First REV shall clobber rl_result.reg.GetReg(), save the value in a temp for the second REV.
Vladimir Markof246af22013-11-27 12:30:15 +00001197 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001198 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001199 }
buzbee2700f7e2014-03-07 09:46:20 -08001200 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1201 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1202 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001203 FreeTemp(r_i_low);
1204 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001205 StoreValueWide(rl_dest, rl_result);
1206 } else {
buzbee695d13a2014-04-19 13:32:20 -07001207 DCHECK(size == k32 || size == kSignedHalf);
1208 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001209 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001210 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001211 StoreValue(rl_dest, rl_result);
1212 }
1213 return true;
1214}
1215
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001216bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 if (cu_->instruction_set == kMips) {
1218 // TODO - add Mips implementation
1219 return false;
1220 }
1221 RegLocation rl_src = info->args[0];
1222 rl_src = LoadValue(rl_src, kCoreReg);
1223 RegLocation rl_dest = InlineTarget(info);
1224 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001225 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001227 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1228 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1229 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 StoreValue(rl_dest, rl_result);
1231 return true;
1232}
1233
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001234bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 if (cu_->instruction_set == kMips) {
1236 // TODO - add Mips implementation
1237 return false;
1238 }
Vladimir Markob9823312014-03-20 17:38:43 +00001239 RegLocation rl_src = info->args[0];
1240 rl_src = LoadValueWide(rl_src, kCoreReg);
1241 RegLocation rl_dest = InlineTargetWide(info);
1242 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1243
1244 // If on x86 or if we would clobber a register needed later, just copy the source first.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001245 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 || rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
buzbee2700f7e2014-03-07 09:46:20 -08001246 OpRegCopyWide(rl_result.reg, rl_src.reg);
1247 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1248 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1249 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001250 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1251 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001252 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001253 }
1254 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 }
Vladimir Markob9823312014-03-20 17:38:43 +00001256
1257 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001258 RegStorage sign_reg = AllocTemp();
1259 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1260 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1261 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1262 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1263 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001264 StoreValueWide(rl_dest, rl_result);
1265 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266}
1267
Yixin Shoudbb17e32014-02-07 05:09:30 -08001268bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1269 if (cu_->instruction_set == kMips) {
1270 // TODO - add Mips implementation
1271 return false;
1272 }
1273 RegLocation rl_src = info->args[0];
1274 rl_src = LoadValue(rl_src, kCoreReg);
1275 RegLocation rl_dest = InlineTarget(info);
1276 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001277 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001278 StoreValue(rl_dest, rl_result);
1279 return true;
1280}
1281
1282bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1283 if (cu_->instruction_set == kMips) {
1284 // TODO - add Mips implementation
1285 return false;
1286 }
1287 RegLocation rl_src = info->args[0];
1288 rl_src = LoadValueWide(rl_src, kCoreReg);
1289 RegLocation rl_dest = InlineTargetWide(info);
1290 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001291 OpRegCopyWide(rl_result.reg, rl_src.reg);
1292 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001293 StoreValueWide(rl_dest, rl_result);
1294 return true;
1295}
1296
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001297bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 if (cu_->instruction_set == kMips) {
1299 // TODO - add Mips implementation
1300 return false;
1301 }
1302 RegLocation rl_src = info->args[0];
1303 RegLocation rl_dest = InlineTarget(info);
1304 StoreValue(rl_dest, rl_src);
1305 return true;
1306}
1307
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001308bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001309 if (cu_->instruction_set == kMips) {
1310 // TODO - add Mips implementation
1311 return false;
1312 }
1313 RegLocation rl_src = info->args[0];
1314 RegLocation rl_dest = InlineTargetWide(info);
1315 StoreValueWide(rl_dest, rl_src);
1316 return true;
1317}
1318
1319/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001320 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001321 * otherwise bails to standard library code.
1322 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001323bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 if (cu_->instruction_set == kMips) {
1325 // TODO - add Mips implementation
1326 return false;
1327 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001328 RegLocation rl_obj = info->args[0];
1329 RegLocation rl_char = info->args[1];
1330 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1331 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1332 return false;
1333 }
1334
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001335 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001337 RegStorage reg_ptr = TargetReg(kArg0);
1338 RegStorage reg_char = TargetReg(kArg1);
1339 RegStorage reg_start = TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 LoadValueDirectFixed(rl_obj, reg_ptr);
1342 LoadValueDirectFixed(rl_char, reg_char);
1343 if (zero_based) {
1344 LoadConstant(reg_start, 0);
1345 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001346 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 LoadValueDirectFixed(rl_start, reg_start);
1348 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001349 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001350 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001351 LIR* high_code_point_branch =
1352 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001354 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001355 if (!rl_char.is_const) {
1356 // Add the slow path for code points beyond 0xFFFF.
1357 DCHECK(high_code_point_branch != nullptr);
1358 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1359 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001360 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001361 } else {
1362 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1363 DCHECK(high_code_point_branch == nullptr);
1364 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 RegLocation rl_return = GetReturn(false);
1366 RegLocation rl_dest = InlineTarget(info);
1367 StoreValue(rl_dest, rl_return);
1368 return true;
1369}
1370
1371/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001372bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 if (cu_->instruction_set == kMips) {
1374 // TODO - add Mips implementation
1375 return false;
1376 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001377 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001379 RegStorage reg_this = TargetReg(kArg0);
1380 RegStorage reg_cmp = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381
1382 RegLocation rl_this = info->args[0];
1383 RegLocation rl_cmp = info->args[1];
1384 LoadValueDirectFixed(rl_this, reg_this);
1385 LoadValueDirectFixed(rl_cmp, reg_cmp);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001386 RegStorage r_tgt = (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) ?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001387 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo)) : RegStorage::InvalidReg();
Dave Allisonf9439142014-03-27 15:10:22 -07001388 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001389 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001390 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001391 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001392 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393 // NOTE: not a safepoint
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001394 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 OpReg(kOpBlx, r_tgt);
1396 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001397 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 RegLocation rl_return = GetReturn(false);
1400 RegLocation rl_dest = InlineTarget(info);
1401 StoreValue(rl_dest, rl_return);
1402 return true;
1403}
1404
1405bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1406 RegLocation rl_dest = InlineTarget(info);
1407 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001408 ThreadOffset<4> offset = Thread::PeerOffset<4>();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 if (cu_->instruction_set == kThumb2 || cu_->instruction_set == kMips) {
buzbee695d13a2014-04-19 13:32:20 -07001410 Load32Disp(TargetReg(kSelf), offset.Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001412 CHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001413 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg.GetReg(), offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001414 }
1415 StoreValue(rl_dest, rl_result);
1416 return true;
1417}
1418
1419bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1420 bool is_long, bool is_volatile) {
1421 if (cu_->instruction_set == kMips) {
1422 // TODO - add Mips implementation
1423 return false;
1424 }
1425 // Unused - RegLocation rl_src_unsafe = info->args[0];
1426 RegLocation rl_src_obj = info->args[1]; // Object
1427 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001428 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001429 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001430
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1432 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1433 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1434 if (is_long) {
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001435 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001436 LoadBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_result.reg.GetLow(),
buzbee695d13a2014-04-19 13:32:20 -07001437 rl_result.reg.GetHigh(), k64, INVALID_SREG);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001438 } else {
1439 RegStorage rl_temp_offset = AllocTemp();
1440 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1441 LoadBaseDispWide(rl_temp_offset, 0, rl_result.reg, INVALID_SREG);
1442 FreeTemp(rl_temp_offset.GetReg());
1443 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 } else {
buzbee695d13a2014-04-19 13:32:20 -07001445 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001446 }
1447
1448 if (is_volatile) {
1449 // Without context sensitive analysis, we must issue the most conservative barriers.
1450 // In this case, either a load or store may follow so we issue both barriers.
1451 GenMemBarrier(kLoadLoad);
1452 GenMemBarrier(kLoadStore);
1453 }
1454
1455 if (is_long) {
1456 StoreValueWide(rl_dest, rl_result);
1457 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 StoreValue(rl_dest, rl_result);
1459 }
1460 return true;
1461}
1462
1463bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1464 bool is_object, bool is_volatile, bool is_ordered) {
1465 if (cu_->instruction_set == kMips) {
1466 // TODO - add Mips implementation
1467 return false;
1468 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 // Unused - RegLocation rl_src_unsafe = info->args[0];
1470 RegLocation rl_src_obj = info->args[1]; // Object
1471 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001472 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 RegLocation rl_src_value = info->args[4]; // value to store
1474 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001475 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001476 GenMemBarrier(kStoreStore);
1477 }
1478 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1479 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1480 RegLocation rl_value;
1481 if (is_long) {
1482 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001483 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001484 StoreBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_value.reg.GetLow(),
buzbee695d13a2014-04-19 13:32:20 -07001485 rl_value.reg.GetHigh(), k64, INVALID_SREG);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001486 } else {
1487 RegStorage rl_temp_offset = AllocTemp();
1488 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1489 StoreBaseDispWide(rl_temp_offset, 0, rl_value.reg);
1490 FreeTemp(rl_temp_offset.GetReg());
1491 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 } else {
1493 rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -07001494 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001496
1497 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001498 FreeTemp(rl_offset.reg.GetReg());
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001499
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001501 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 GenMemBarrier(kStoreLoad);
1503 }
1504 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001505 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 }
1507 return true;
1508}
1509
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001510void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001511 if ((info->opt_flags & MIR_INLINED) != 0) {
1512 // Already inlined but we may still need the null check.
1513 if (info->type != kStatic &&
1514 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1515 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
1516 RegLocation rl_obj = LoadValue(info->args[0], kCoreReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001517 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001518 }
1519 return;
1520 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001521 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1522 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1523 ->GenIntrinsic(this, info)) {
1524 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001526 GenInvokeNoInline(info);
1527}
1528
1529void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 int call_state = 0;
1531 LIR* null_ck;
1532 LIR** p_null_ck = NULL;
1533 NextCallInsn next_call_insn;
1534 FlushAllRegs(); /* Everything to home location */
1535 // Explicit register usage
1536 LockCallTemps();
1537
Vladimir Markof096aad2014-01-23 15:51:58 +00001538 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1539 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1540 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1541 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1542 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001544 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001546 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001547 } else if (info->type == kDirect) {
1548 if (fast_path) {
1549 p_null_ck = &null_ck;
1550 }
1551 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1552 skip_this = false;
1553 } else if (info->type == kStatic) {
1554 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1555 skip_this = false;
1556 } else if (info->type == kSuper) {
1557 DCHECK(!fast_path); // Fast path is a direct call.
1558 next_call_insn = NextSuperCallInsnSP;
1559 skip_this = false;
1560 } else {
1561 DCHECK_EQ(info->type, kVirtual);
1562 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1563 skip_this = fast_path;
1564 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001565 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 if (!info->is_range) {
1567 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001568 next_call_insn, target_method, method_info.VTableIndex(),
1569 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 original_type, skip_this);
1571 } else {
1572 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001573 next_call_insn, target_method, method_info.VTableIndex(),
1574 method_info.DirectCode(), method_info.DirectMethod(),
1575 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 }
1577 // Finish up any of the call sequence not interleaved in arg loading
1578 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001579 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1580 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 }
1582 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001583 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1585 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001586 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001587 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001588 // We can have the linker fixup a call relative.
1589 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001590 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001591 } else {
1592 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1593 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1594 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001596 ThreadOffset<4> trampoline(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001597 switch (info->type) {
1598 case kInterface:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001599 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 break;
1601 case kDirect:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001602 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 break;
1604 case kStatic:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001605 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001606 break;
1607 case kSuper:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001608 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 break;
1610 case kVirtual:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001611 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 break;
1613 default:
1614 LOG(FATAL) << "Unexpected invoke type";
1615 }
1616 call_inst = OpThreadMem(kOpBlx, trampoline);
1617 }
1618 }
1619 MarkSafepointPC(call_inst);
1620
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001621 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 if (info->result.location != kLocInvalid) {
1623 // We have a following MOVE_RESULT - do it now.
1624 if (info->result.wide) {
1625 RegLocation ret_loc = GetReturnWide(info->result.fp);
1626 StoreValueWide(info->result, ret_loc);
1627 } else {
1628 RegLocation ret_loc = GetReturn(info->result.fp);
1629 StoreValue(info->result, ret_loc);
1630 }
1631 }
1632}
1633
1634} // namespace art