blob: d82756801284210bb76b5ce92dd14c40e447cf72 [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
Vladimir Marko3bc86152014-03-13 14:11:28 +000037void Mir2Lir::AddIntrinsicLaunchpad(CallInfo* info, LIR* branch, LIR* resume) {
38 class IntrinsicLaunchpadPath : public Mir2Lir::LIRSlowPath {
39 public:
40 IntrinsicLaunchpadPath(Mir2Lir* m2l, CallInfo* info, LIR* branch, LIR* resume = nullptr)
41 : 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
60 AddSlowPath(new (arena_) IntrinsicLaunchpadPath(this, info, branch, resume));
61}
62
Brian Carlstrom7940e442013-07-12 13:46:57 -070063/*
64 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allison081f73e2014-04-07 18:58:07 +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 Allison081f73e2014-04-07 18:58:07 +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 Allison081f73e2014-04-07 18:58:07 +000076 LIR* call_inst;
Brian Carlstrom60d7a652014-03-13 18:10:08 -070077 OpKind op = use_link ? kOpBlx : kOpBx;
Dave Allison081f73e2014-04-07 18:58:07 +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
Ian Rogersdd7624d2014-03-14 17:43:00 -0700258void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800259 RegStorage arg1, bool safepoint_pc) {
260 RegStorage r_tgt = CallHelperSetup(helper_offset);
261 DCHECK_NE(TargetReg(kArg0).GetReg(), arg1.GetReg()); // check copy into arg0 won't clobber arg1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 OpRegCopy(TargetReg(kArg0), arg0);
263 OpRegCopy(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000264 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 CallHelper(r_tgt, helper_offset, safepoint_pc);
266}
267
Ian Rogersdd7624d2014-03-14 17:43:00 -0700268void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800269 RegStorage arg1, int arg2, bool safepoint_pc) {
270 RegStorage r_tgt = CallHelperSetup(helper_offset);
271 DCHECK_NE(TargetReg(kArg0).GetReg(), arg1.GetReg()); // check copy into arg0 won't clobber arg1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 OpRegCopy(TargetReg(kArg0), arg0);
273 OpRegCopy(TargetReg(kArg1), arg1);
274 LoadConstant(TargetReg(kArg2), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000275 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 CallHelper(r_tgt, helper_offset, safepoint_pc);
277}
278
Ian Rogersdd7624d2014-03-14 17:43:00 -0700279void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 int arg0, RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800281 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 LoadValueDirectFixed(arg2, TargetReg(kArg2));
283 LoadCurrMethodDirect(TargetReg(kArg1));
284 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000285 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 CallHelper(r_tgt, helper_offset, safepoint_pc);
287}
288
Ian Rogersdd7624d2014-03-14 17:43:00 -0700289void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 int arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800291 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 LoadCurrMethodDirect(TargetReg(kArg1));
293 LoadConstant(TargetReg(kArg2), arg2);
294 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000295 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 CallHelper(r_tgt, helper_offset, safepoint_pc);
297}
298
Ian Rogersdd7624d2014-03-14 17:43:00 -0700299void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 int arg0, RegLocation arg1,
301 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800302 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700303 DCHECK_EQ(arg1.wide, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 LoadValueDirectFixed(arg1, TargetReg(kArg1));
305 if (arg2.wide == 0) {
306 LoadValueDirectFixed(arg2, TargetReg(kArg2));
307 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800308 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
309 LoadValueDirectWideFixed(arg2, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 }
311 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000312 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 CallHelper(r_tgt, helper_offset, safepoint_pc);
314}
315
Ian Rogersdd7624d2014-03-14 17:43:00 -0700316void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset<4> helper_offset,
Ian Rogersa9a82542013-10-04 11:17:26 -0700317 RegLocation arg0, RegLocation arg1,
318 RegLocation arg2,
319 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800320 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700321 DCHECK_EQ(arg0.wide, 0U);
322 LoadValueDirectFixed(arg0, TargetReg(kArg0));
323 DCHECK_EQ(arg1.wide, 0U);
324 LoadValueDirectFixed(arg1, TargetReg(kArg1));
325 DCHECK_EQ(arg1.wide, 0U);
326 LoadValueDirectFixed(arg2, TargetReg(kArg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000327 ClobberCallerSave();
Ian Rogersa9a82542013-10-04 11:17:26 -0700328 CallHelper(r_tgt, helper_offset, safepoint_pc);
329}
330
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331/*
332 * If there are any ins passed in registers that have not been promoted
333 * to a callee-save register, flush them to the frame. Perform intial
334 * assignment of promoted arguments.
335 *
336 * ArgLocs is an array of location records describing the incoming arguments
337 * with one location record per word of argument.
338 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700339void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 /*
341 * Dummy up a RegLocation for the incoming Method*
342 * It will attempt to keep kArg0 live (or copy it to home location
343 * if promoted).
344 */
345 RegLocation rl_src = rl_method;
346 rl_src.location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800347 rl_src.reg = TargetReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 rl_src.home = false;
buzbee2700f7e2014-03-07 09:46:20 -0800349 MarkLive(rl_src.reg, rl_src.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 StoreValue(rl_method, rl_src);
351 // If Method* has been promoted, explicitly flush
352 if (rl_method.location == kLocPhysReg) {
353 StoreWordDisp(TargetReg(kSp), 0, TargetReg(kArg0));
354 }
355
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800356 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800358 }
359
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
361 /*
362 * Copy incoming arguments to their proper home locations.
363 * NOTE: an older version of dx had an issue in which
364 * it would reuse static method argument registers.
365 * This could result in the same Dalvik virtual register
366 * being promoted to both core and fp regs. To account for this,
367 * we only copy to the corresponding promoted physical register
368 * if it matches the type of the SSA name for the incoming
369 * argument. It is also possible that long and double arguments
370 * end up half-promoted. In those cases, we must flush the promoted
371 * half to memory as well.
372 */
373 for (int i = 0; i < cu_->num_ins; i++) {
374 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800375 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800376
buzbee2700f7e2014-03-07 09:46:20 -0800377 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 // If arriving in register
379 bool need_flush = true;
380 RegLocation* t_loc = &ArgLocs[i];
381 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800382 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 need_flush = false;
384 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800385 OpRegCopy(RegStorage::Solo32(v_map->FpReg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 need_flush = false;
387 } else {
388 need_flush = true;
389 }
390
buzbeed0a03b82013-09-14 08:21:05 -0700391 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 if (t_loc->wide) {
393 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700394 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 need_flush |= (p_map->core_location != v_map->core_location) ||
396 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700397 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
398 /*
399 * In Arm, a double is represented as a pair of consecutive single float
400 * registers starting at an even number. It's possible that both Dalvik vRegs
401 * representing the incoming double were independently promoted as singles - but
402 * not in a form usable as a double. If so, we need to flush - even though the
403 * incoming arg appears fully in register. At this point in the code, both
404 * halves of the double are promoted. Make sure they are in a usable form.
405 */
406 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
407 int low_reg = promotion_map_[lowreg_index].FpReg;
408 int high_reg = promotion_map_[lowreg_index + 1].FpReg;
409 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
410 need_flush = true;
411 }
412 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 }
414 if (need_flush) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800415 StoreBaseDisp(TargetReg(kSp), SRegOffset(start_vreg + i), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 }
417 } else {
418 // If arriving in frame & promoted
419 if (v_map->core_location == kLocPhysReg) {
420 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i),
buzbee2700f7e2014-03-07 09:46:20 -0800421 RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 }
423 if (v_map->fp_location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800424 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->FpReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 }
426 }
427 }
428}
429
430/*
431 * Bit of a hack here - in the absence of a real scheduling pass,
432 * emit the next instruction in static & direct invoke sequences.
433 */
434static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
435 int state, const MethodReference& target_method,
436 uint32_t unused,
437 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700438 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 if (direct_code != 0 && direct_method != 0) {
441 switch (state) {
442 case 0: // Get the current Method* [sets kArg0]
443 if (direct_code != static_cast<unsigned int>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700444 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700445 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
446 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700447 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700448 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 }
450 if (direct_method != static_cast<unsigned int>(-1)) {
451 cg->LoadConstant(cg->TargetReg(kArg0), direct_method);
452 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700453 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 }
455 break;
456 default:
457 return -1;
458 }
459 } else {
460 switch (state) {
461 case 0: // Get the current Method* [sets kArg0]
462 // TUNING: we can save a reg copy if Method* has been promoted.
463 cg->LoadCurrMethodDirect(cg->TargetReg(kArg0));
464 break;
465 case 1: // Get method->dex_cache_resolved_methods_
466 cg->LoadWordDisp(cg->TargetReg(kArg0),
buzbee2700f7e2014-03-07 09:46:20 -0800467 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
468 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 // Set up direct code if known.
470 if (direct_code != 0) {
471 if (direct_code != static_cast<unsigned int>(-1)) {
472 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700473 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700474 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700475 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 }
477 }
478 break;
479 case 2: // Grab target method*
480 CHECK_EQ(cu->dex_file, target_method.dex_file);
481 cg->LoadWordDisp(cg->TargetReg(kArg0),
482 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbee2700f7e2014-03-07 09:46:20 -0800483 (target_method.dex_method_index * 4), cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 break;
485 case 3: // Grab the code from the method*
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700486 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 if (direct_code == 0) {
488 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800489 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 cg->TargetReg(kInvokeTgt));
491 }
492 break;
493 }
494 // Intentional fallthrough for x86
495 default:
496 return -1;
497 }
498 }
499 return state + 1;
500}
501
502/*
503 * Bit of a hack here - in the absence of a real scheduling pass,
504 * emit the next instruction in a virtual invoke sequence.
505 * We can use kLr as a temp prior to target address loading
506 * Note also that we'll load the first argument ("this") into
507 * kArg1 here rather than the standard LoadArgRegs.
508 */
509static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
510 int state, const MethodReference& target_method,
511 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700512 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
514 /*
515 * This is the fast path in which the target virtual method is
516 * fully resolved at compile time.
517 */
518 switch (state) {
519 case 0: { // Get "this" [set kArg1]
520 RegLocation rl_arg = info->args[0];
521 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
522 break;
523 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700524 case 1: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800525 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 // get this->klass_ [use kArg1, set kInvokeTgt]
527 cg->LoadWordDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
528 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800529 cg->MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700531 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
533 cg->TargetReg(kInvokeTgt));
534 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700535 case 3: // Get target method [use kInvokeTgt, set kArg0]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), (method_idx * 4) +
537 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
538 cg->TargetReg(kArg0));
539 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700540 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700541 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800543 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 cg->TargetReg(kInvokeTgt));
545 break;
546 }
547 // Intentional fallthrough for X86
548 default:
549 return -1;
550 }
551 return state + 1;
552}
553
554/*
Jeff Hao88474b42013-10-23 16:24:40 -0700555 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
556 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
557 * more than one interface method map to the same index. Note also that we'll load the first
558 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 */
560static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
561 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700562 uint32_t method_idx, uintptr_t unused,
563 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565
Jeff Hao88474b42013-10-23 16:24:40 -0700566 switch (state) {
567 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700568 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
569 cg->LoadConstant(cg->TargetReg(kHiddenArg), target_method.dex_method_index);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700570 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700571 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg), cg->TargetReg(kHiddenArg));
572 }
573 break;
574 case 1: { // Get "this" [set kArg1]
575 RegLocation rl_arg = info->args[0];
576 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
577 break;
578 }
579 case 2: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800580 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700581 // Get this->klass_ [use kArg1, set kInvokeTgt]
582 cg->LoadWordDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
583 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800584 cg->MarkPossibleNullPointerException(info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700585 break;
586 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
587 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
588 cg->TargetReg(kInvokeTgt));
589 break;
590 case 4: // Get target method [use kInvokeTgt, set kArg0]
591 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), ((method_idx % ClassLinker::kImtSize) * 4) +
592 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 cg->TargetReg(kArg0));
594 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700595 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700596 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700597 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800598 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Jeff Hao88474b42013-10-23 16:24:40 -0700599 cg->TargetReg(kInvokeTgt));
600 break;
601 }
602 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 default:
604 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 }
606 return state + 1;
607}
608
Ian Rogersdd7624d2014-03-14 17:43:00 -0700609static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset<4> trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700611 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
613 /*
614 * This handles the case in which the base method is not fully
615 * resolved at compile time, we bail to a runtime helper.
616 */
617 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700618 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 // Load trampoline target
Ian Rogers848871b2013-08-05 10:56:33 -0700620 cg->LoadWordDisp(cg->TargetReg(kSelf), trampoline.Int32Value(), cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 }
622 // Load kArg0 with method index
623 CHECK_EQ(cu->dex_file, target_method.dex_file);
624 cg->LoadConstant(cg->TargetReg(kArg0), target_method.dex_method_index);
625 return 1;
626 }
627 return -1;
628}
629
630static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
631 int state,
632 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000633 uint32_t unused, uintptr_t unused2,
634 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700635 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
637}
638
639static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
640 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000641 uint32_t unused, uintptr_t unused2,
642 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700643 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
645}
646
647static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
648 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000649 uint32_t unused, uintptr_t unused2,
650 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700651 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
653}
654
655static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
656 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000657 uint32_t unused, uintptr_t unused2,
658 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700659 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
661}
662
663static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
664 CallInfo* info, int state,
665 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000666 uint32_t unused, uintptr_t unused2,
667 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700668 ThreadOffset<4> trampoline =
669 QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
671}
672
673int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
674 NextCallInsn next_call_insn,
675 const MethodReference& target_method,
676 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700677 uintptr_t direct_method, InvokeType type, bool skip_this) {
buzbee2700f7e2014-03-07 09:46:20 -0800678 int last_arg_reg = TargetReg(kArg3).GetReg();
679 int next_reg = TargetReg(kArg1).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 int next_arg = 0;
681 if (skip_this) {
682 next_reg++;
683 next_arg++;
684 }
685 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
686 RegLocation rl_arg = info->args[next_arg++];
687 rl_arg = UpdateRawLoc(rl_arg);
buzbee2700f7e2014-03-07 09:46:20 -0800688 if (rl_arg.wide && (next_reg <= TargetReg(kArg2).GetReg())) {
689 RegStorage r_tmp(RegStorage::k64BitPair, next_reg, next_reg + 1);
690 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 next_reg++;
692 next_arg++;
693 } else {
694 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800695 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 rl_arg.is_const = false;
697 }
buzbee2700f7e2014-03-07 09:46:20 -0800698 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(next_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 }
700 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
701 direct_code, direct_method, type);
702 }
703 return call_state;
704}
705
706/*
707 * Load up to 5 arguments, the first three of which will be in
708 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
709 * and as part of the load sequence, it must be replaced with
710 * the target method pointer. Note, this may also be called
711 * for "range" variants if the number of arguments is 5 or fewer.
712 */
713int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
714 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
715 const MethodReference& target_method,
716 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700717 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 RegLocation rl_arg;
719
720 /* If no arguments, just return */
721 if (info->num_arg_words == 0)
722 return call_state;
723
724 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
725 direct_code, direct_method, type);
726
727 DCHECK_LE(info->num_arg_words, 5);
728 if (info->num_arg_words > 3) {
729 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700730 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 RegLocation rl_use0 = info->args[0];
732 RegLocation rl_use1 = info->args[1];
733 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800734 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
735 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 // Wide spans, we need the 2nd half of uses[2].
737 rl_arg = UpdateLocWide(rl_use2);
738 if (rl_arg.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800739 reg = rl_arg.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 } else {
741 // kArg2 & rArg3 can safely be used here
742 reg = TargetReg(kArg3);
743 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
744 call_state = next_call_insn(cu_, info, call_state, target_method,
745 vtable_idx, direct_code, direct_method, type);
746 }
747 StoreBaseDisp(TargetReg(kSp), (next_use + 1) * 4, reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
749 direct_code, direct_method, type);
750 next_use++;
751 }
752 // Loop through the rest
753 while (next_use < info->num_arg_words) {
buzbee2700f7e2014-03-07 09:46:20 -0800754 RegStorage low_reg;
755 RegStorage high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700756 rl_arg = info->args[next_use];
757 rl_arg = UpdateRawLoc(rl_arg);
758 if (rl_arg.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000759 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800760 low_reg = rl_arg.reg.GetLow();
761 high_reg = rl_arg.reg.GetHigh();
762 } else {
763 low_reg = rl_arg.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000764 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 } else {
766 low_reg = TargetReg(kArg2);
767 if (rl_arg.wide) {
768 high_reg = TargetReg(kArg3);
buzbee2700f7e2014-03-07 09:46:20 -0800769 LoadValueDirectWideFixed(rl_arg, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 } else {
771 LoadValueDirectFixed(rl_arg, low_reg);
772 }
773 call_state = next_call_insn(cu_, info, call_state, target_method,
774 vtable_idx, direct_code, direct_method, type);
775 }
776 int outs_offset = (next_use + 1) * 4;
777 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800778 StoreBaseDispWide(TargetReg(kSp), outs_offset, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 next_use += 2;
780 } else {
781 StoreWordDisp(TargetReg(kSp), outs_offset, low_reg);
782 next_use++;
783 }
784 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
785 direct_code, direct_method, type);
786 }
787 }
788
789 call_state = LoadArgRegs(info, call_state, next_call_insn,
790 target_method, vtable_idx, direct_code, direct_method,
791 type, skip_this);
792
793 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700794 if (Runtime::Current()->ExplicitNullChecks()) {
795 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
796 } else {
797 *pcrLabel = nullptr;
798 // In lieu of generating a check for kArg1 being null, we need to
799 // perform a load when doing implicit checks.
800 RegStorage tmp = AllocTemp();
801 LoadWordDisp(TargetReg(kArg1), 0, tmp);
802 MarkPossibleNullPointerException(info->opt_flags);
803 FreeTemp(tmp);
804 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 }
806 return call_state;
807}
808
809/*
810 * May have 0+ arguments (also used for jumbo). Note that
811 * source virtual registers may be in physical registers, so may
812 * need to be flushed to home location before copying. This
813 * applies to arg3 and above (see below).
814 *
815 * Two general strategies:
816 * If < 20 arguments
817 * Pass args 3-18 using vldm/vstm block copy
818 * Pass arg0, arg1 & arg2 in kArg1-kArg3
819 * If 20+ arguments
820 * Pass args arg19+ using memcpy block copy
821 * Pass arg0, arg1 & arg2 in kArg1-kArg3
822 *
823 */
824int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
825 LIR** pcrLabel, NextCallInsn next_call_insn,
826 const MethodReference& target_method,
827 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700828 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 // If we can treat it as non-range (Jumbo ops will use range form)
830 if (info->num_arg_words <= 5)
831 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
832 next_call_insn, target_method, vtable_idx,
833 direct_code, direct_method, type, skip_this);
834 /*
835 * First load the non-register arguments. Both forms expect all
836 * of the source arguments to be in their home frame location, so
837 * scan the s_reg names and flush any that have been promoted to
838 * frame backing storage.
839 */
840 // Scan the rest of the args - if in phys_reg flush to memory
841 for (int next_arg = 0; next_arg < info->num_arg_words;) {
842 RegLocation loc = info->args[next_arg];
843 if (loc.wide) {
844 loc = UpdateLocWide(loc);
845 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800846 StoreBaseDispWide(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 }
848 next_arg += 2;
849 } else {
850 loc = UpdateLoc(loc);
851 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800852 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 }
854 next_arg++;
855 }
856 }
857
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800858 // Logic below assumes that Method pointer is at offset zero from SP.
859 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
860
861 // The first 3 arguments are passed via registers.
862 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
863 // get size of uintptr_t or size of object reference according to model being used.
864 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800866 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
867 DCHECK_GT(regs_left_to_pass_via_stack, 0);
868
869 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
870 // Use vldm/vstm pair using kArg3 as a temp
871 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
872 direct_code, direct_method, type);
873 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
874 LIR* ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
875 // TUNING: loosen barrier
876 ld->u.m.def_mask = ENCODE_ALL;
877 SetMemRefType(ld, true /* is_load */, kDalvikReg);
878 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
879 direct_code, direct_method, type);
880 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
881 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
882 direct_code, direct_method, type);
883 LIR* st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
884 SetMemRefType(st, false /* is_load */, kDalvikReg);
885 st->u.m.def_mask = ENCODE_ALL;
886 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
887 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700888 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800889 int current_src_offset = start_offset;
890 int current_dest_offset = outs_offset;
891
892 while (regs_left_to_pass_via_stack > 0) {
893 // This is based on the knowledge that the stack itself is 16-byte aligned.
894 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
895 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
896 size_t bytes_to_move;
897
898 /*
899 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
900 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
901 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
902 * We do this because we could potentially do a smaller move to align.
903 */
904 if (regs_left_to_pass_via_stack == 4 ||
905 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
906 // Moving 128-bits via xmm register.
907 bytes_to_move = sizeof(uint32_t) * 4;
908
909 // Allocate a free xmm temp. Since we are working through the calling sequence,
910 // we expect to have an xmm temporary available.
buzbee2700f7e2014-03-07 09:46:20 -0800911 RegStorage temp = AllocTempDouble();
912 CHECK_GT(temp.GetLowReg(), 0);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800913
914 LIR* ld1 = nullptr;
915 LIR* ld2 = nullptr;
916 LIR* st1 = nullptr;
917 LIR* st2 = nullptr;
918
919 /*
920 * The logic is similar for both loads and stores. If we have 16-byte alignment,
921 * do an aligned move. If we have 8-byte alignment, then do the move in two
922 * parts. This approach prevents possible cache line splits. Finally, fall back
923 * to doing an unaligned move. In most cases we likely won't split the cache
924 * line but we cannot prove it and thus take a conservative approach.
925 */
926 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
927 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
928
929 if (src_is_16b_aligned) {
930 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
931 } else if (src_is_8b_aligned) {
932 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800933 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
934 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800935 } else {
936 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
937 }
938
939 if (dest_is_16b_aligned) {
940 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
941 } else if (dest_is_8b_aligned) {
942 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800943 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
944 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800945 } else {
946 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
947 }
948
949 // TODO If we could keep track of aliasing information for memory accesses that are wider
950 // than 64-bit, we wouldn't need to set up a barrier.
951 if (ld1 != nullptr) {
952 if (ld2 != nullptr) {
953 // For 64-bit load we can actually set up the aliasing information.
954 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
955 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
956 } else {
957 // Set barrier for 128-bit load.
958 SetMemRefType(ld1, true /* is_load */, kDalvikReg);
959 ld1->u.m.def_mask = ENCODE_ALL;
960 }
961 }
962 if (st1 != nullptr) {
963 if (st2 != nullptr) {
964 // For 64-bit store we can actually set up the aliasing information.
965 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
966 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
967 } else {
968 // Set barrier for 128-bit store.
969 SetMemRefType(st1, false /* is_load */, kDalvikReg);
970 st1->u.m.def_mask = ENCODE_ALL;
971 }
972 }
973
974 // Free the temporary used for the data movement.
buzbee2700f7e2014-03-07 09:46:20 -0800975 // CLEANUP: temp is currently a bogus pair, elmiminate extra free when updated.
976 FreeTemp(temp.GetLow());
977 FreeTemp(temp.GetHigh());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800978 } else {
979 // Moving 32-bits via general purpose register.
980 bytes_to_move = sizeof(uint32_t);
981
982 // Instead of allocating a new temp, simply reuse one of the registers being used
983 // for argument passing.
buzbee2700f7e2014-03-07 09:46:20 -0800984 RegStorage temp = TargetReg(kArg3);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800985
986 // Now load the argument VR and store to the outs.
987 LoadWordDisp(TargetReg(kSp), current_src_offset, temp);
988 StoreWordDisp(TargetReg(kSp), current_dest_offset, temp);
989 }
990
991 current_src_offset += bytes_to_move;
992 current_dest_offset += bytes_to_move;
993 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
994 }
995 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 // Generate memcpy
997 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
998 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700999 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(4, pMemcpy), TargetReg(kArg0),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 }
1002
1003 call_state = LoadArgRegs(info, call_state, next_call_insn,
1004 target_method, vtable_idx, direct_code, direct_method,
1005 type, skip_this);
1006
1007 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1008 direct_code, direct_method, type);
1009 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -07001010 if (Runtime::Current()->ExplicitNullChecks()) {
1011 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
1012 } else {
1013 *pcrLabel = nullptr;
1014 // In lieu of generating a check for kArg1 being null, we need to
1015 // perform a load when doing implicit checks.
1016 RegStorage tmp = AllocTemp();
1017 LoadWordDisp(TargetReg(kArg1), 0, tmp);
1018 MarkPossibleNullPointerException(info->opt_flags);
1019 FreeTemp(tmp);
1020 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 }
1022 return call_state;
1023}
1024
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001025RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 RegLocation res;
1027 if (info->result.location == kLocInvalid) {
1028 res = GetReturn(false);
1029 } else {
1030 res = info->result;
1031 }
1032 return res;
1033}
1034
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001035RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 RegLocation res;
1037 if (info->result.location == kLocInvalid) {
1038 res = GetReturnWide(false);
1039 } else {
1040 res = info->result;
1041 }
1042 return res;
1043}
1044
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001045bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 if (cu_->instruction_set == kMips) {
1047 // TODO - add Mips implementation
1048 return false;
1049 }
1050 // Location of reference to data array
1051 int value_offset = mirror::String::ValueOffset().Int32Value();
1052 // Location of count
1053 int count_offset = mirror::String::CountOffset().Int32Value();
1054 // Starting offset within data array
1055 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1056 // Start of char data with array_
1057 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1058
1059 RegLocation rl_obj = info->args[0];
1060 RegLocation rl_idx = info->args[1];
1061 rl_obj = LoadValue(rl_obj, kCoreReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001062 // X86 wants to avoid putting a constant index into a register.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001063 if (!((cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64)&& rl_idx.is_const)) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001064 rl_idx = LoadValue(rl_idx, kCoreReg);
1065 }
buzbee2700f7e2014-03-07 09:46:20 -08001066 RegStorage reg_max;
1067 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001069 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001070 RegStorage reg_off;
1071 RegStorage reg_ptr;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001072 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 reg_off = AllocTemp();
1074 reg_ptr = AllocTemp();
1075 if (range_check) {
1076 reg_max = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001077 LoadWordDisp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001078 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
buzbee2700f7e2014-03-07 09:46:20 -08001080 LoadWordDisp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001081 MarkPossibleNullPointerException(info->opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -08001082 LoadWordDisp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 if (range_check) {
1084 // Set up a launch pad to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001085 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001087 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001088 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001089 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 } else {
1091 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001092 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001094 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001095 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001096 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Vladimir Marko3bc86152014-03-13 14:11:28 +00001097 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001098 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001099 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001100 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001101 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 }
1103 reg_off = AllocTemp();
1104 reg_ptr = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001105 LoadWordDisp(rl_obj.reg, offset_offset, reg_off);
1106 LoadWordDisp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001108 if (rl_idx.is_const) {
1109 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1110 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001111 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001112 }
buzbee2700f7e2014-03-07 09:46:20 -08001113 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001114 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001115 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001116 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 RegLocation rl_dest = InlineTarget(info);
1118 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001119 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee2700f7e2014-03-07 09:46:20 -08001120 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001121 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001122 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg,
1123 RegStorage::InvalidReg(), kUnsignedHalf, INVALID_SREG);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001124 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 FreeTemp(reg_off);
1126 FreeTemp(reg_ptr);
1127 StoreValue(rl_dest, rl_result);
1128 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001129 DCHECK(range_check_branch != nullptr);
1130 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
1131 AddIntrinsicLaunchpad(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 return true;
1134}
1135
1136// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001137bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 if (cu_->instruction_set == kMips) {
1139 // TODO - add Mips implementation
1140 return false;
1141 }
1142 // dst = src.length();
1143 RegLocation rl_obj = info->args[0];
1144 rl_obj = LoadValue(rl_obj, kCoreReg);
1145 RegLocation rl_dest = InlineTarget(info);
1146 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001147 GenNullCheck(rl_obj.reg, info->opt_flags);
1148 LoadWordDisp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001149 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 if (is_empty) {
1151 // dst = (dst == 0);
1152 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001153 RegStorage t_reg = AllocTemp();
1154 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1155 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001157 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001158 OpRegImm(kOpSub, rl_result.reg, 1);
1159 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 }
1161 }
1162 StoreValue(rl_dest, rl_result);
1163 return true;
1164}
1165
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001166bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1167 if (cu_->instruction_set == kMips) {
1168 // TODO - add Mips implementation
1169 return false;
1170 }
1171 RegLocation rl_src_i = info->args[0];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001172 RegLocation rl_dest = (size == kLong) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001173 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1174 if (size == kLong) {
1175 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001176 RegStorage r_i_low = rl_i.reg.GetLow();
1177 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001178 // 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 +00001179 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001180 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001181 }
buzbee2700f7e2014-03-07 09:46:20 -08001182 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1183 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1184 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001185 FreeTemp(r_i_low);
1186 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001187 StoreValueWide(rl_dest, rl_result);
1188 } else {
1189 DCHECK(size == kWord || size == kSignedHalf);
1190 OpKind op = (size == kWord) ? kOpRev : kOpRevsh;
1191 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001192 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001193 StoreValue(rl_dest, rl_result);
1194 }
1195 return true;
1196}
1197
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001198bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199 if (cu_->instruction_set == kMips) {
1200 // TODO - add Mips implementation
1201 return false;
1202 }
1203 RegLocation rl_src = info->args[0];
1204 rl_src = LoadValue(rl_src, kCoreReg);
1205 RegLocation rl_dest = InlineTarget(info);
1206 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001207 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001209 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1210 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1211 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 StoreValue(rl_dest, rl_result);
1213 return true;
1214}
1215
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001216bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 if (cu_->instruction_set == kMips) {
1218 // TODO - add Mips implementation
1219 return false;
1220 }
Vladimir Markob9823312014-03-20 17:38:43 +00001221 RegLocation rl_src = info->args[0];
1222 rl_src = LoadValueWide(rl_src, kCoreReg);
1223 RegLocation rl_dest = InlineTargetWide(info);
1224 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1225
1226 // If on x86 or if we would clobber a register needed later, just copy the source first.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001227 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 || rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
buzbee2700f7e2014-03-07 09:46:20 -08001228 OpRegCopyWide(rl_result.reg, rl_src.reg);
1229 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1230 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1231 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001232 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1233 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001234 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001235 }
1236 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 }
Vladimir Markob9823312014-03-20 17:38:43 +00001238
1239 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001240 RegStorage sign_reg = AllocTemp();
1241 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1242 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1243 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1244 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1245 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001246 StoreValueWide(rl_dest, rl_result);
1247 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248}
1249
Yixin Shoudbb17e32014-02-07 05:09:30 -08001250bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1251 if (cu_->instruction_set == kMips) {
1252 // TODO - add Mips implementation
1253 return false;
1254 }
1255 RegLocation rl_src = info->args[0];
1256 rl_src = LoadValue(rl_src, kCoreReg);
1257 RegLocation rl_dest = InlineTarget(info);
1258 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001259 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001260 StoreValue(rl_dest, rl_result);
1261 return true;
1262}
1263
1264bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1265 if (cu_->instruction_set == kMips) {
1266 // TODO - add Mips implementation
1267 return false;
1268 }
1269 RegLocation rl_src = info->args[0];
1270 rl_src = LoadValueWide(rl_src, kCoreReg);
1271 RegLocation rl_dest = InlineTargetWide(info);
1272 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001273 OpRegCopyWide(rl_result.reg, rl_src.reg);
1274 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001275 StoreValueWide(rl_dest, rl_result);
1276 return true;
1277}
1278
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001279bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 if (cu_->instruction_set == kMips) {
1281 // TODO - add Mips implementation
1282 return false;
1283 }
1284 RegLocation rl_src = info->args[0];
1285 RegLocation rl_dest = InlineTarget(info);
1286 StoreValue(rl_dest, rl_src);
1287 return true;
1288}
1289
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001290bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 if (cu_->instruction_set == kMips) {
1292 // TODO - add Mips implementation
1293 return false;
1294 }
1295 RegLocation rl_src = info->args[0];
1296 RegLocation rl_dest = InlineTargetWide(info);
1297 StoreValueWide(rl_dest, rl_src);
1298 return true;
1299}
1300
1301/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001302 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 * otherwise bails to standard library code.
1304 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001305bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 if (cu_->instruction_set == kMips) {
1307 // TODO - add Mips implementation
1308 return false;
1309 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001310 RegLocation rl_obj = info->args[0];
1311 RegLocation rl_char = info->args[1];
1312 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1313 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1314 return false;
1315 }
1316
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001317 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001319 RegStorage reg_ptr = TargetReg(kArg0);
1320 RegStorage reg_char = TargetReg(kArg1);
1321 RegStorage reg_start = TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 LoadValueDirectFixed(rl_obj, reg_ptr);
1324 LoadValueDirectFixed(rl_char, reg_char);
1325 if (zero_based) {
1326 LoadConstant(reg_start, 0);
1327 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001328 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329 LoadValueDirectFixed(rl_start, reg_start);
1330 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001331 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001332 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001333 LIR* high_code_point_branch =
1334 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001336 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001337 if (!rl_char.is_const) {
1338 // Add the slow path for code points beyond 0xFFFF.
1339 DCHECK(high_code_point_branch != nullptr);
1340 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1341 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
1342 AddIntrinsicLaunchpad(info, high_code_point_branch, resume_tgt);
1343 } else {
1344 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1345 DCHECK(high_code_point_branch == nullptr);
1346 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 RegLocation rl_return = GetReturn(false);
1348 RegLocation rl_dest = InlineTarget(info);
1349 StoreValue(rl_dest, rl_return);
1350 return true;
1351}
1352
1353/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001354bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 if (cu_->instruction_set == kMips) {
1356 // TODO - add Mips implementation
1357 return false;
1358 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001359 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001361 RegStorage reg_this = TargetReg(kArg0);
1362 RegStorage reg_cmp = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363
1364 RegLocation rl_this = info->args[0];
1365 RegLocation rl_cmp = info->args[1];
1366 LoadValueDirectFixed(rl_this, reg_this);
1367 LoadValueDirectFixed(rl_cmp, reg_cmp);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001368 RegStorage r_tgt = (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) ?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001369 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo)) : RegStorage::InvalidReg();
Dave Allisonf9439142014-03-27 15:10:22 -07001370 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001371 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001372 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001373 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
1374 AddIntrinsicLaunchpad(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 // NOTE: not a safepoint
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001376 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 OpReg(kOpBlx, r_tgt);
1378 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001379 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001380 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 RegLocation rl_return = GetReturn(false);
1382 RegLocation rl_dest = InlineTarget(info);
1383 StoreValue(rl_dest, rl_return);
1384 return true;
1385}
1386
1387bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1388 RegLocation rl_dest = InlineTarget(info);
1389 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001390 ThreadOffset<4> offset = Thread::PeerOffset<4>();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 if (cu_->instruction_set == kThumb2 || cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -08001392 LoadWordDisp(TargetReg(kSelf), offset.Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001394 CHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001395 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg.GetReg(), offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001396 }
1397 StoreValue(rl_dest, rl_result);
1398 return true;
1399}
1400
1401bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1402 bool is_long, bool is_volatile) {
1403 if (cu_->instruction_set == kMips) {
1404 // TODO - add Mips implementation
1405 return false;
1406 }
1407 // Unused - RegLocation rl_src_unsafe = info->args[0];
1408 RegLocation rl_src_obj = info->args[1]; // Object
1409 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001410 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001411 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001412
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1414 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1415 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1416 if (is_long) {
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001417 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001418 LoadBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_result.reg.GetLow(),
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001419 rl_result.reg.GetHigh(), kLong, INVALID_SREG);
1420 } else {
1421 RegStorage rl_temp_offset = AllocTemp();
1422 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1423 LoadBaseDispWide(rl_temp_offset, 0, rl_result.reg, INVALID_SREG);
1424 FreeTemp(rl_temp_offset.GetReg());
1425 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001427 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, kWord);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001428 }
1429
1430 if (is_volatile) {
1431 // Without context sensitive analysis, we must issue the most conservative barriers.
1432 // In this case, either a load or store may follow so we issue both barriers.
1433 GenMemBarrier(kLoadLoad);
1434 GenMemBarrier(kLoadStore);
1435 }
1436
1437 if (is_long) {
1438 StoreValueWide(rl_dest, rl_result);
1439 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 StoreValue(rl_dest, rl_result);
1441 }
1442 return true;
1443}
1444
1445bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1446 bool is_object, bool is_volatile, bool is_ordered) {
1447 if (cu_->instruction_set == kMips) {
1448 // TODO - add Mips implementation
1449 return false;
1450 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 // Unused - RegLocation rl_src_unsafe = info->args[0];
1452 RegLocation rl_src_obj = info->args[1]; // Object
1453 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001454 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001455 RegLocation rl_src_value = info->args[4]; // value to store
1456 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001457 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 GenMemBarrier(kStoreStore);
1459 }
1460 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1461 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1462 RegLocation rl_value;
1463 if (is_long) {
1464 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001465 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001466 StoreBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_value.reg.GetLow(),
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001467 rl_value.reg.GetHigh(), kLong, INVALID_SREG);
1468 } else {
1469 RegStorage rl_temp_offset = AllocTemp();
1470 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1471 StoreBaseDispWide(rl_temp_offset, 0, rl_value.reg);
1472 FreeTemp(rl_temp_offset.GetReg());
1473 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 } else {
1475 rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001476 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001478
1479 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001480 FreeTemp(rl_offset.reg.GetReg());
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001481
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001483 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484 GenMemBarrier(kStoreLoad);
1485 }
1486 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001487 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001488 }
1489 return true;
1490}
1491
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001492void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001493 if ((info->opt_flags & MIR_INLINED) != 0) {
1494 // Already inlined but we may still need the null check.
1495 if (info->type != kStatic &&
1496 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1497 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
1498 RegLocation rl_obj = LoadValue(info->args[0], kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001499 GenImmedCheck(kCondEq, rl_obj.reg, 0, kThrowNullPointer);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001500 }
1501 return;
1502 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001503 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1504 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1505 ->GenIntrinsic(this, info)) {
1506 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001508 GenInvokeNoInline(info);
1509}
1510
1511void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512 int call_state = 0;
1513 LIR* null_ck;
1514 LIR** p_null_ck = NULL;
1515 NextCallInsn next_call_insn;
1516 FlushAllRegs(); /* Everything to home location */
1517 // Explicit register usage
1518 LockCallTemps();
1519
Vladimir Markof096aad2014-01-23 15:51:58 +00001520 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1521 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1522 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1523 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1524 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001528 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 } else if (info->type == kDirect) {
1530 if (fast_path) {
1531 p_null_ck = &null_ck;
1532 }
1533 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1534 skip_this = false;
1535 } else if (info->type == kStatic) {
1536 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1537 skip_this = false;
1538 } else if (info->type == kSuper) {
1539 DCHECK(!fast_path); // Fast path is a direct call.
1540 next_call_insn = NextSuperCallInsnSP;
1541 skip_this = false;
1542 } else {
1543 DCHECK_EQ(info->type, kVirtual);
1544 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1545 skip_this = fast_path;
1546 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001547 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 if (!info->is_range) {
1549 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001550 next_call_insn, target_method, method_info.VTableIndex(),
1551 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001552 original_type, skip_this);
1553 } else {
1554 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001555 next_call_insn, target_method, method_info.VTableIndex(),
1556 method_info.DirectCode(), method_info.DirectMethod(),
1557 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001558 }
1559 // Finish up any of the call sequence not interleaved in arg loading
1560 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001561 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1562 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001563 }
1564 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001565 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1567 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001568 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001569 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001570 // We can have the linker fixup a call relative.
1571 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001572 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001573 } else {
1574 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1575 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1576 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001578 ThreadOffset<4> trampoline(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 switch (info->type) {
1580 case kInterface:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001581 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 break;
1583 case kDirect:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001584 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 break;
1586 case kStatic:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001587 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588 break;
1589 case kSuper:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001590 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001591 break;
1592 case kVirtual:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001593 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001594 break;
1595 default:
1596 LOG(FATAL) << "Unexpected invoke type";
1597 }
1598 call_inst = OpThreadMem(kOpBlx, trampoline);
1599 }
1600 }
1601 MarkSafepointPC(call_inst);
1602
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001603 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 if (info->result.location != kLocInvalid) {
1605 // We have a following MOVE_RESULT - do it now.
1606 if (info->result.wide) {
1607 RegLocation ret_loc = GetReturnWide(info->result.fp);
1608 StoreValueWide(info->result, ret_loc);
1609 } else {
1610 RegLocation ret_loc = GetReturn(info->result.fp);
1611 StoreValue(info->result, ret_loc);
1612 }
1613 }
1614}
1615
1616} // namespace art