blob: 93a23a6a6ecf03dc365f8da1fa5404e71102baac [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();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070047 GenerateTargetLabel(kPseudoIntrinsicRetry);
Vladimir Marko3bc86152014-03-13 14:11:28 +000048 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
49 m2l_->GenInvokeNoInline(info_);
50 if (cont_ != nullptr) {
51 m2l_->OpUnconditionalBranch(cont_);
52 }
53 }
54
55 private:
56 CallInfo* const info_;
57 };
58
Mingyao Yang3a74d152014-04-21 15:39:44 -070059 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000060}
61
Brian Carlstrom7940e442013-07-12 13:46:57 -070062/*
63 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000064 * the helper target address, and the actual call to the helper. Because x86
65 * has a memory call operation, part 1 is a NOP for x86. For other targets,
66 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 */
Ian Rogersdd7624d2014-03-14 17:43:00 -070068RegStorage Mir2Lir::CallHelperSetup(ThreadOffset<4> helper_offset) {
Dave Allisond6ed6422014-04-09 23:36:15 +000069 return (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) ? RegStorage::InvalidReg() : LoadHelper(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070070}
71
72/* NOTE: if r_tgt is a temp, it will be freed following use */
Ian Rogersdd7624d2014-03-14 17:43:00 -070073LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<4> helper_offset, bool safepoint_pc,
buzbee2700f7e2014-03-07 09:46:20 -080074 bool use_link) {
Dave Allisond6ed6422014-04-09 23:36:15 +000075 LIR* call_inst;
Brian Carlstrom60d7a652014-03-13 18:10:08 -070076 OpKind op = use_link ? kOpBlx : kOpBx;
Dave Allisond6ed6422014-04-09 23:36:15 +000077 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
78 call_inst = OpThreadMem(op, helper_offset);
79 } else {
80 call_inst = OpReg(op, r_tgt);
81 FreeTemp(r_tgt);
82 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 if (safepoint_pc) {
84 MarkSafepointPC(call_inst);
85 }
86 return call_inst;
87}
88
Mingyao Yang42894562014-04-07 12:42:16 -070089void Mir2Lir::CallRuntimeHelper(ThreadOffset<4> helper_offset, bool safepoint_pc) {
90 RegStorage r_tgt = CallHelperSetup(helper_offset);
91 ClobberCallerSave();
92 CallHelper(r_tgt, helper_offset, safepoint_pc);
93}
94
Ian Rogersdd7624d2014-03-14 17:43:00 -070095void Mir2Lir::CallRuntimeHelperImm(ThreadOffset<4> helper_offset, int arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -080096 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +000098 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 CallHelper(r_tgt, helper_offset, safepoint_pc);
100}
101
Ian Rogersdd7624d2014-03-14 17:43:00 -0700102void Mir2Lir::CallRuntimeHelperReg(ThreadOffset<4> helper_offset, RegStorage arg0,
103 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800104 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 OpRegCopy(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000106 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 CallHelper(r_tgt, helper_offset, safepoint_pc);
108}
109
Ian Rogersdd7624d2014-03-14 17:43:00 -0700110void Mir2Lir::CallRuntimeHelperRegLocation(ThreadOffset<4> helper_offset, RegLocation arg0,
Ian Rogers848871b2013-08-05 10:56:33 -0700111 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800112 RegStorage r_tgt = CallHelperSetup(helper_offset);
113 if (arg0.wide == 0) {
114 LoadValueDirectFixed(arg0, TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800116 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
117 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000119 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 CallHelper(r_tgt, helper_offset, safepoint_pc);
121}
122
Ian Rogersdd7624d2014-03-14 17:43:00 -0700123void Mir2Lir::CallRuntimeHelperImmImm(ThreadOffset<4> helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800125 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 LoadConstant(TargetReg(kArg0), arg0);
127 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000128 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 CallHelper(r_tgt, helper_offset, safepoint_pc);
130}
131
Ian Rogersdd7624d2014-03-14 17:43:00 -0700132void Mir2Lir::CallRuntimeHelperImmRegLocation(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 RegLocation arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800134 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 if (arg1.wide == 0) {
136 LoadValueDirectFixed(arg1, TargetReg(kArg1));
137 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800138 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
139 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 }
141 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000142 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 CallHelper(r_tgt, helper_offset, safepoint_pc);
144}
145
Ian Rogersdd7624d2014-03-14 17:43:00 -0700146void Mir2Lir::CallRuntimeHelperRegLocationImm(ThreadOffset<4> helper_offset, RegLocation arg0,
147 int arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800148 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 LoadValueDirectFixed(arg0, TargetReg(kArg0));
150 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000151 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 CallHelper(r_tgt, helper_offset, safepoint_pc);
153}
154
Ian Rogersdd7624d2014-03-14 17:43:00 -0700155void Mir2Lir::CallRuntimeHelperImmReg(ThreadOffset<4> helper_offset, int arg0, RegStorage arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800157 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 OpRegCopy(TargetReg(kArg1), arg1);
159 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000160 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 CallHelper(r_tgt, helper_offset, safepoint_pc);
162}
163
Ian Rogersdd7624d2014-03-14 17:43:00 -0700164void Mir2Lir::CallRuntimeHelperRegImm(ThreadOffset<4> helper_offset, RegStorage arg0, int arg1,
Ian Rogers848871b2013-08-05 10:56:33 -0700165 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800166 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 OpRegCopy(TargetReg(kArg0), arg0);
168 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000169 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 CallHelper(r_tgt, helper_offset, safepoint_pc);
171}
172
Ian Rogersdd7624d2014-03-14 17:43:00 -0700173void Mir2Lir::CallRuntimeHelperImmMethod(ThreadOffset<4> helper_offset, int arg0,
174 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800175 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 LoadCurrMethodDirect(TargetReg(kArg1));
177 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000178 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 CallHelper(r_tgt, helper_offset, safepoint_pc);
180}
181
Ian Rogersdd7624d2014-03-14 17:43:00 -0700182void Mir2Lir::CallRuntimeHelperRegMethod(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800183 bool safepoint_pc) {
184 RegStorage r_tgt = CallHelperSetup(helper_offset);
185 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800186 if (TargetReg(kArg0) != arg0) {
187 OpRegCopy(TargetReg(kArg0), arg0);
188 }
189 LoadCurrMethodDirect(TargetReg(kArg1));
190 ClobberCallerSave();
191 CallHelper(r_tgt, helper_offset, safepoint_pc);
192}
193
Ian Rogersdd7624d2014-03-14 17:43:00 -0700194void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(ThreadOffset<4> helper_offset, RegStorage arg0,
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800195 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800196 RegStorage r_tgt = CallHelperSetup(helper_offset);
197 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800198 if (TargetReg(kArg0) != arg0) {
199 OpRegCopy(TargetReg(kArg0), arg0);
200 }
201 LoadCurrMethodDirect(TargetReg(kArg1));
202 LoadValueDirectFixed(arg2, TargetReg(kArg2));
203 ClobberCallerSave();
204 CallHelper(r_tgt, helper_offset, safepoint_pc);
205}
206
Ian Rogersdd7624d2014-03-14 17:43:00 -0700207void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(ThreadOffset<4> helper_offset,
208 RegLocation arg0, RegLocation arg1,
209 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800210 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 if (arg0.wide == 0) {
212 LoadValueDirectFixed(arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
213 if (arg1.wide == 0) {
214 if (cu_->instruction_set == kMips) {
215 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1));
216 } else {
217 LoadValueDirectFixed(arg1, TargetReg(kArg1));
218 }
219 } else {
220 if (cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -0800221 RegStorage r_tmp;
222 if (arg1.fp) {
223 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
224 } else {
225 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
226 }
227 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800229 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
230 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 }
232 }
233 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800234 RegStorage r_tmp;
235 if (arg0.fp) {
236 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg0), TargetReg(kFArg1));
237 } else {
238 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
239 }
240 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 if (arg1.wide == 0) {
242 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2));
243 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800244 RegStorage r_tmp;
245 if (arg1.fp) {
246 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
247 } else {
248 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
249 }
250 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 }
252 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000253 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 CallHelper(r_tgt, helper_offset, safepoint_pc);
255}
256
Mingyao Yang80365d92014-04-18 12:10:58 -0700257void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
258 if (arg1.GetReg() == TargetReg(kArg0).GetReg()) {
259 if (arg0.GetReg() == TargetReg(kArg1).GetReg()) {
260 // Swap kArg0 and kArg1 with kArg2 as temp.
261 OpRegCopy(TargetReg(kArg2), arg1);
262 OpRegCopy(TargetReg(kArg0), arg0);
263 OpRegCopy(TargetReg(kArg1), TargetReg(kArg2));
264 } else {
265 OpRegCopy(TargetReg(kArg1), arg1);
266 OpRegCopy(TargetReg(kArg0), arg0);
267 }
268 } else {
269 OpRegCopy(TargetReg(kArg0), arg0);
270 OpRegCopy(TargetReg(kArg1), arg1);
271 }
272}
273
Ian Rogersdd7624d2014-03-14 17:43:00 -0700274void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800275 RegStorage arg1, bool safepoint_pc) {
276 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700277 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000278 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 CallHelper(r_tgt, helper_offset, safepoint_pc);
280}
281
Ian Rogersdd7624d2014-03-14 17:43:00 -0700282void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800283 RegStorage arg1, int arg2, bool safepoint_pc) {
284 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700285 CopyToArgumentRegs(arg0, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 LoadConstant(TargetReg(kArg2), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000287 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 CallHelper(r_tgt, helper_offset, safepoint_pc);
289}
290
Ian Rogersdd7624d2014-03-14 17:43:00 -0700291void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 int arg0, RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800293 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 LoadValueDirectFixed(arg2, TargetReg(kArg2));
295 LoadCurrMethodDirect(TargetReg(kArg1));
296 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000297 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 CallHelper(r_tgt, helper_offset, safepoint_pc);
299}
300
Ian Rogersdd7624d2014-03-14 17:43:00 -0700301void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 int arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800303 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 LoadCurrMethodDirect(TargetReg(kArg1));
305 LoadConstant(TargetReg(kArg2), arg2);
306 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000307 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 CallHelper(r_tgt, helper_offset, safepoint_pc);
309}
310
Ian Rogersdd7624d2014-03-14 17:43:00 -0700311void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 int arg0, RegLocation arg1,
313 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800314 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700315 DCHECK_EQ(arg1.wide, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 LoadValueDirectFixed(arg1, TargetReg(kArg1));
317 if (arg2.wide == 0) {
318 LoadValueDirectFixed(arg2, TargetReg(kArg2));
319 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800320 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
321 LoadValueDirectWideFixed(arg2, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 }
323 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000324 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 CallHelper(r_tgt, helper_offset, safepoint_pc);
326}
327
Ian Rogersdd7624d2014-03-14 17:43:00 -0700328void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset<4> helper_offset,
Ian Rogersa9a82542013-10-04 11:17:26 -0700329 RegLocation arg0, RegLocation arg1,
330 RegLocation arg2,
331 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800332 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700333 DCHECK_EQ(arg0.wide, 0U);
334 LoadValueDirectFixed(arg0, TargetReg(kArg0));
335 DCHECK_EQ(arg1.wide, 0U);
336 LoadValueDirectFixed(arg1, TargetReg(kArg1));
337 DCHECK_EQ(arg1.wide, 0U);
338 LoadValueDirectFixed(arg2, TargetReg(kArg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000339 ClobberCallerSave();
Ian Rogersa9a82542013-10-04 11:17:26 -0700340 CallHelper(r_tgt, helper_offset, safepoint_pc);
341}
342
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343/*
344 * If there are any ins passed in registers that have not been promoted
345 * to a callee-save register, flush them to the frame. Perform intial
346 * assignment of promoted arguments.
347 *
348 * ArgLocs is an array of location records describing the incoming arguments
349 * with one location record per word of argument.
350 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700351void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 /*
353 * Dummy up a RegLocation for the incoming Method*
354 * It will attempt to keep kArg0 live (or copy it to home location
355 * if promoted).
356 */
357 RegLocation rl_src = rl_method;
358 rl_src.location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800359 rl_src.reg = TargetReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 rl_src.home = false;
buzbee2700f7e2014-03-07 09:46:20 -0800361 MarkLive(rl_src.reg, rl_src.s_reg_low);
buzbee695d13a2014-04-19 13:32:20 -0700362 if (rl_method.wide) {
363 StoreValueWide(rl_method, rl_src);
364 } else {
365 StoreValue(rl_method, rl_src);
366 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 // If Method* has been promoted, explicitly flush
368 if (rl_method.location == kLocPhysReg) {
369 StoreWordDisp(TargetReg(kSp), 0, TargetReg(kArg0));
370 }
371
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800372 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800374 }
375
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
377 /*
378 * Copy incoming arguments to their proper home locations.
379 * NOTE: an older version of dx had an issue in which
380 * it would reuse static method argument registers.
381 * This could result in the same Dalvik virtual register
382 * being promoted to both core and fp regs. To account for this,
383 * we only copy to the corresponding promoted physical register
384 * if it matches the type of the SSA name for the incoming
385 * argument. It is also possible that long and double arguments
386 * end up half-promoted. In those cases, we must flush the promoted
387 * half to memory as well.
388 */
389 for (int i = 0; i < cu_->num_ins; i++) {
390 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800391 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800392
buzbee2700f7e2014-03-07 09:46:20 -0800393 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 // If arriving in register
395 bool need_flush = true;
396 RegLocation* t_loc = &ArgLocs[i];
397 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800398 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 need_flush = false;
400 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800401 OpRegCopy(RegStorage::Solo32(v_map->FpReg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 need_flush = false;
403 } else {
404 need_flush = true;
405 }
406
buzbeed0a03b82013-09-14 08:21:05 -0700407 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 if (t_loc->wide) {
409 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700410 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 need_flush |= (p_map->core_location != v_map->core_location) ||
412 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700413 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
414 /*
415 * In Arm, a double is represented as a pair of consecutive single float
416 * registers starting at an even number. It's possible that both Dalvik vRegs
417 * representing the incoming double were independently promoted as singles - but
418 * not in a form usable as a double. If so, we need to flush - even though the
419 * incoming arg appears fully in register. At this point in the code, both
420 * halves of the double are promoted. Make sure they are in a usable form.
421 */
422 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
423 int low_reg = promotion_map_[lowreg_index].FpReg;
424 int high_reg = promotion_map_[lowreg_index + 1].FpReg;
425 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
426 need_flush = true;
427 }
428 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 }
430 if (need_flush) {
buzbee695d13a2014-04-19 13:32:20 -0700431 Store32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 }
433 } else {
434 // If arriving in frame & promoted
435 if (v_map->core_location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700436 Load32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 }
438 if (v_map->fp_location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700439 Load32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->FpReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 }
441 }
442 }
443}
444
445/*
446 * Bit of a hack here - in the absence of a real scheduling pass,
447 * emit the next instruction in static & direct invoke sequences.
448 */
449static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
450 int state, const MethodReference& target_method,
451 uint32_t unused,
452 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700453 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 if (direct_code != 0 && direct_method != 0) {
456 switch (state) {
457 case 0: // Get the current Method* [sets kArg0]
Ian Rogersff093b32014-04-30 19:04:27 -0700458 if (direct_code != static_cast<uintptr_t>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700459 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700460 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
461 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700462 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700463 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 }
Ian Rogersff093b32014-04-30 19:04:27 -0700465 if (direct_method != static_cast<uintptr_t>(-1)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 cg->LoadConstant(cg->TargetReg(kArg0), direct_method);
467 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700468 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 }
470 break;
471 default:
472 return -1;
473 }
474 } else {
475 switch (state) {
476 case 0: // Get the current Method* [sets kArg0]
477 // TUNING: we can save a reg copy if Method* has been promoted.
478 cg->LoadCurrMethodDirect(cg->TargetReg(kArg0));
479 break;
480 case 1: // Get method->dex_cache_resolved_methods_
buzbee695d13a2014-04-19 13:32:20 -0700481 cg->LoadRefDisp(cg->TargetReg(kArg0),
482 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
483 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 // Set up direct code if known.
485 if (direct_code != 0) {
Ian Rogersff093b32014-04-30 19:04:27 -0700486 if (direct_code != static_cast<uintptr_t>(-1)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700488 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700489 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700490 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 }
492 }
493 break;
494 case 2: // Grab target method*
495 CHECK_EQ(cu->dex_file, target_method.dex_file);
buzbee695d13a2014-04-19 13:32:20 -0700496 cg->LoadRefDisp(cg->TargetReg(kArg0),
497 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
498 (target_method.dex_method_index * 4), cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 break;
500 case 3: // Grab the code from the method*
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700501 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 if (direct_code == 0) {
503 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800504 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 cg->TargetReg(kInvokeTgt));
506 }
507 break;
508 }
509 // Intentional fallthrough for x86
510 default:
511 return -1;
512 }
513 }
514 return state + 1;
515}
516
517/*
518 * Bit of a hack here - in the absence of a real scheduling pass,
519 * emit the next instruction in a virtual invoke sequence.
520 * We can use kLr as a temp prior to target address loading
521 * Note also that we'll load the first argument ("this") into
522 * kArg1 here rather than the standard LoadArgRegs.
523 */
524static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
525 int state, const MethodReference& target_method,
526 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700527 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
529 /*
530 * This is the fast path in which the target virtual method is
531 * fully resolved at compile time.
532 */
533 switch (state) {
534 case 0: { // Get "this" [set kArg1]
535 RegLocation rl_arg = info->args[0];
536 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
537 break;
538 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700539 case 1: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800540 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 // get this->klass_ [use kArg1, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700542 cg->LoadRefDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
543 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800544 cg->MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700546 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700547 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
548 cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700550 case 3: // Get target method [use kInvokeTgt, set kArg0]
buzbee695d13a2014-04-19 13:32:20 -0700551 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), (method_idx * 4) +
552 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
553 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700555 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700556 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800558 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 cg->TargetReg(kInvokeTgt));
560 break;
561 }
562 // Intentional fallthrough for X86
563 default:
564 return -1;
565 }
566 return state + 1;
567}
568
569/*
Jeff Hao88474b42013-10-23 16:24:40 -0700570 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
571 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
572 * more than one interface method map to the same index. Note also that we'll load the first
573 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 */
575static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
576 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700577 uint32_t method_idx, uintptr_t unused,
578 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580
Jeff Hao88474b42013-10-23 16:24:40 -0700581 switch (state) {
582 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700583 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
584 cg->LoadConstant(cg->TargetReg(kHiddenArg), target_method.dex_method_index);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700585 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700586 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg), cg->TargetReg(kHiddenArg));
587 }
588 break;
589 case 1: { // Get "this" [set kArg1]
590 RegLocation rl_arg = info->args[0];
591 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
592 break;
593 }
594 case 2: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800595 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700596 // Get this->klass_ [use kArg1, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700597 cg->LoadRefDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
598 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800599 cg->MarkPossibleNullPointerException(info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700600 break;
601 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700602 // NOTE: native pointer.
603 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
604 cg->TargetReg(kInvokeTgt));
Jeff Hao88474b42013-10-23 16:24:40 -0700605 break;
606 case 4: // Get target method [use kInvokeTgt, set kArg0]
buzbee695d13a2014-04-19 13:32:20 -0700607 // NOTE: native pointer.
Jeff Hao88474b42013-10-23 16:24:40 -0700608 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), ((method_idx % ClassLinker::kImtSize) * 4) +
609 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 cg->TargetReg(kArg0));
611 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700612 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700613 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700614 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800615 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Jeff Hao88474b42013-10-23 16:24:40 -0700616 cg->TargetReg(kInvokeTgt));
617 break;
618 }
619 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 default:
621 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 }
623 return state + 1;
624}
625
Ian Rogersdd7624d2014-03-14 17:43:00 -0700626static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset<4> trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700628 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
630 /*
631 * This handles the case in which the base method is not fully
632 * resolved at compile time, we bail to a runtime helper.
633 */
634 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700635 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 // Load trampoline target
Ian Rogers848871b2013-08-05 10:56:33 -0700637 cg->LoadWordDisp(cg->TargetReg(kSelf), trampoline.Int32Value(), cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 }
639 // Load kArg0 with method index
640 CHECK_EQ(cu->dex_file, target_method.dex_file);
641 cg->LoadConstant(cg->TargetReg(kArg0), target_method.dex_method_index);
642 return 1;
643 }
644 return -1;
645}
646
647static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
648 int state,
649 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000650 uint32_t unused, uintptr_t unused2,
651 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700652 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
654}
655
656static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
657 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000658 uint32_t unused, uintptr_t unused2,
659 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700660 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
662}
663
664static int NextSuperCallInsnSP(CompilationUnit* cu, 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 = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
670}
671
672static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
673 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000674 uint32_t unused, uintptr_t unused2,
675 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700676 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
678}
679
680static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
681 CallInfo* info, int state,
682 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000683 uint32_t unused, uintptr_t unused2,
684 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700685 ThreadOffset<4> trampoline =
686 QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
688}
689
690int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
691 NextCallInsn next_call_insn,
692 const MethodReference& target_method,
693 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700694 uintptr_t direct_method, InvokeType type, bool skip_this) {
buzbee2700f7e2014-03-07 09:46:20 -0800695 int last_arg_reg = TargetReg(kArg3).GetReg();
696 int next_reg = TargetReg(kArg1).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 int next_arg = 0;
698 if (skip_this) {
699 next_reg++;
700 next_arg++;
701 }
702 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
703 RegLocation rl_arg = info->args[next_arg++];
704 rl_arg = UpdateRawLoc(rl_arg);
buzbee2700f7e2014-03-07 09:46:20 -0800705 if (rl_arg.wide && (next_reg <= TargetReg(kArg2).GetReg())) {
706 RegStorage r_tmp(RegStorage::k64BitPair, next_reg, next_reg + 1);
707 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 next_reg++;
709 next_arg++;
710 } else {
711 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800712 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 rl_arg.is_const = false;
714 }
buzbee2700f7e2014-03-07 09:46:20 -0800715 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(next_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 }
717 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
718 direct_code, direct_method, type);
719 }
720 return call_state;
721}
722
723/*
724 * Load up to 5 arguments, the first three of which will be in
725 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
726 * and as part of the load sequence, it must be replaced with
727 * the target method pointer. Note, this may also be called
728 * for "range" variants if the number of arguments is 5 or fewer.
729 */
730int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
731 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
732 const MethodReference& target_method,
733 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700734 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 RegLocation rl_arg;
736
737 /* If no arguments, just return */
738 if (info->num_arg_words == 0)
739 return call_state;
740
741 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
742 direct_code, direct_method, type);
743
744 DCHECK_LE(info->num_arg_words, 5);
745 if (info->num_arg_words > 3) {
746 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700747 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 RegLocation rl_use0 = info->args[0];
749 RegLocation rl_use1 = info->args[1];
750 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800751 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
752 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 // Wide spans, we need the 2nd half of uses[2].
754 rl_arg = UpdateLocWide(rl_use2);
755 if (rl_arg.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800756 reg = rl_arg.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 } else {
758 // kArg2 & rArg3 can safely be used here
759 reg = TargetReg(kArg3);
buzbee695d13a2014-04-19 13:32:20 -0700760 Load32Disp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 call_state = next_call_insn(cu_, info, call_state, target_method,
762 vtable_idx, direct_code, direct_method, type);
763 }
buzbee695d13a2014-04-19 13:32:20 -0700764 Store32Disp(TargetReg(kSp), (next_use + 1) * 4, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
766 direct_code, direct_method, type);
767 next_use++;
768 }
769 // Loop through the rest
770 while (next_use < info->num_arg_words) {
buzbee2700f7e2014-03-07 09:46:20 -0800771 RegStorage low_reg;
772 RegStorage high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 rl_arg = info->args[next_use];
774 rl_arg = UpdateRawLoc(rl_arg);
775 if (rl_arg.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000776 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800777 low_reg = rl_arg.reg.GetLow();
778 high_reg = rl_arg.reg.GetHigh();
779 } else {
780 low_reg = rl_arg.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000781 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 } else {
783 low_reg = TargetReg(kArg2);
784 if (rl_arg.wide) {
785 high_reg = TargetReg(kArg3);
buzbee2700f7e2014-03-07 09:46:20 -0800786 LoadValueDirectWideFixed(rl_arg, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 } else {
788 LoadValueDirectFixed(rl_arg, low_reg);
789 }
790 call_state = next_call_insn(cu_, info, call_state, target_method,
791 vtable_idx, direct_code, direct_method, type);
792 }
793 int outs_offset = (next_use + 1) * 4;
794 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800795 StoreBaseDispWide(TargetReg(kSp), outs_offset, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 next_use += 2;
797 } else {
buzbee695d13a2014-04-19 13:32:20 -0700798 Store32Disp(TargetReg(kSp), outs_offset, low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 next_use++;
800 }
801 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
802 direct_code, direct_method, type);
803 }
804 }
805
806 call_state = LoadArgRegs(info, call_state, next_call_insn,
807 target_method, vtable_idx, direct_code, direct_method,
808 type, skip_this);
809
810 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700811 if (Runtime::Current()->ExplicitNullChecks()) {
812 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
813 } else {
814 *pcrLabel = nullptr;
815 // In lieu of generating a check for kArg1 being null, we need to
816 // perform a load when doing implicit checks.
817 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700818 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -0700819 MarkPossibleNullPointerException(info->opt_flags);
820 FreeTemp(tmp);
821 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 }
823 return call_state;
824}
825
826/*
827 * May have 0+ arguments (also used for jumbo). Note that
828 * source virtual registers may be in physical registers, so may
829 * need to be flushed to home location before copying. This
830 * applies to arg3 and above (see below).
831 *
832 * Two general strategies:
833 * If < 20 arguments
834 * Pass args 3-18 using vldm/vstm block copy
835 * Pass arg0, arg1 & arg2 in kArg1-kArg3
836 * If 20+ arguments
837 * Pass args arg19+ using memcpy block copy
838 * Pass arg0, arg1 & arg2 in kArg1-kArg3
839 *
840 */
841int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
842 LIR** pcrLabel, NextCallInsn next_call_insn,
843 const MethodReference& target_method,
844 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700845 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 // If we can treat it as non-range (Jumbo ops will use range form)
847 if (info->num_arg_words <= 5)
848 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
849 next_call_insn, target_method, vtable_idx,
850 direct_code, direct_method, type, skip_this);
851 /*
852 * First load the non-register arguments. Both forms expect all
853 * of the source arguments to be in their home frame location, so
854 * scan the s_reg names and flush any that have been promoted to
855 * frame backing storage.
856 */
857 // Scan the rest of the args - if in phys_reg flush to memory
858 for (int next_arg = 0; next_arg < info->num_arg_words;) {
859 RegLocation loc = info->args[next_arg];
860 if (loc.wide) {
861 loc = UpdateLocWide(loc);
862 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800863 StoreBaseDispWide(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 }
865 next_arg += 2;
866 } else {
867 loc = UpdateLoc(loc);
868 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
buzbee695d13a2014-04-19 13:32:20 -0700869 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 }
871 next_arg++;
872 }
873 }
874
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800875 // Logic below assumes that Method pointer is at offset zero from SP.
876 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
877
878 // The first 3 arguments are passed via registers.
879 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
880 // get size of uintptr_t or size of object reference according to model being used.
881 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800883 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
884 DCHECK_GT(regs_left_to_pass_via_stack, 0);
885
886 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
887 // Use vldm/vstm pair using kArg3 as a temp
888 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
889 direct_code, direct_method, type);
890 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
891 LIR* ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
892 // TUNING: loosen barrier
893 ld->u.m.def_mask = ENCODE_ALL;
894 SetMemRefType(ld, true /* is_load */, kDalvikReg);
895 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
896 direct_code, direct_method, type);
897 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
898 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
899 direct_code, direct_method, type);
900 LIR* st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
901 SetMemRefType(st, false /* is_load */, kDalvikReg);
902 st->u.m.def_mask = ENCODE_ALL;
903 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
904 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700905 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800906 int current_src_offset = start_offset;
907 int current_dest_offset = outs_offset;
908
909 while (regs_left_to_pass_via_stack > 0) {
910 // This is based on the knowledge that the stack itself is 16-byte aligned.
911 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
912 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
913 size_t bytes_to_move;
914
915 /*
916 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
917 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
918 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
919 * We do this because we could potentially do a smaller move to align.
920 */
921 if (regs_left_to_pass_via_stack == 4 ||
922 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
923 // Moving 128-bits via xmm register.
924 bytes_to_move = sizeof(uint32_t) * 4;
925
926 // Allocate a free xmm temp. Since we are working through the calling sequence,
927 // we expect to have an xmm temporary available.
buzbee2700f7e2014-03-07 09:46:20 -0800928 RegStorage temp = AllocTempDouble();
929 CHECK_GT(temp.GetLowReg(), 0);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800930
931 LIR* ld1 = nullptr;
932 LIR* ld2 = nullptr;
933 LIR* st1 = nullptr;
934 LIR* st2 = nullptr;
935
936 /*
937 * The logic is similar for both loads and stores. If we have 16-byte alignment,
938 * do an aligned move. If we have 8-byte alignment, then do the move in two
939 * parts. This approach prevents possible cache line splits. Finally, fall back
940 * to doing an unaligned move. In most cases we likely won't split the cache
941 * line but we cannot prove it and thus take a conservative approach.
942 */
943 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
944 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
945
946 if (src_is_16b_aligned) {
947 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
948 } else if (src_is_8b_aligned) {
949 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800950 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
951 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800952 } else {
953 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
954 }
955
956 if (dest_is_16b_aligned) {
957 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
958 } else if (dest_is_8b_aligned) {
959 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800960 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
961 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800962 } else {
963 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
964 }
965
966 // TODO If we could keep track of aliasing information for memory accesses that are wider
967 // than 64-bit, we wouldn't need to set up a barrier.
968 if (ld1 != nullptr) {
969 if (ld2 != nullptr) {
970 // For 64-bit load we can actually set up the aliasing information.
971 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
972 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
973 } else {
974 // Set barrier for 128-bit load.
975 SetMemRefType(ld1, true /* is_load */, kDalvikReg);
976 ld1->u.m.def_mask = ENCODE_ALL;
977 }
978 }
979 if (st1 != nullptr) {
980 if (st2 != nullptr) {
981 // For 64-bit store we can actually set up the aliasing information.
982 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
983 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
984 } else {
985 // Set barrier for 128-bit store.
986 SetMemRefType(st1, false /* is_load */, kDalvikReg);
987 st1->u.m.def_mask = ENCODE_ALL;
988 }
989 }
990
991 // Free the temporary used for the data movement.
buzbee2700f7e2014-03-07 09:46:20 -0800992 // CLEANUP: temp is currently a bogus pair, elmiminate extra free when updated.
993 FreeTemp(temp.GetLow());
994 FreeTemp(temp.GetHigh());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800995 } else {
996 // Moving 32-bits via general purpose register.
997 bytes_to_move = sizeof(uint32_t);
998
999 // Instead of allocating a new temp, simply reuse one of the registers being used
1000 // for argument passing.
buzbee2700f7e2014-03-07 09:46:20 -08001001 RegStorage temp = TargetReg(kArg3);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001002
1003 // Now load the argument VR and store to the outs.
buzbee695d13a2014-04-19 13:32:20 -07001004 Load32Disp(TargetReg(kSp), current_src_offset, temp);
1005 Store32Disp(TargetReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001006 }
1007
1008 current_src_offset += bytes_to_move;
1009 current_dest_offset += bytes_to_move;
1010 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1011 }
1012 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 // Generate memcpy
1014 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
1015 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001016 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(4, pMemcpy), TargetReg(kArg0),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 }
1019
1020 call_state = LoadArgRegs(info, call_state, next_call_insn,
1021 target_method, vtable_idx, direct_code, direct_method,
1022 type, skip_this);
1023
1024 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1025 direct_code, direct_method, type);
1026 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -07001027 if (Runtime::Current()->ExplicitNullChecks()) {
1028 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
1029 } else {
1030 *pcrLabel = nullptr;
1031 // In lieu of generating a check for kArg1 being null, we need to
1032 // perform a load when doing implicit checks.
1033 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001034 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -07001035 MarkPossibleNullPointerException(info->opt_flags);
1036 FreeTemp(tmp);
1037 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 }
1039 return call_state;
1040}
1041
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001042RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 RegLocation res;
1044 if (info->result.location == kLocInvalid) {
1045 res = GetReturn(false);
1046 } else {
1047 res = info->result;
1048 }
1049 return res;
1050}
1051
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001052RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 RegLocation res;
1054 if (info->result.location == kLocInvalid) {
1055 res = GetReturnWide(false);
1056 } else {
1057 res = info->result;
1058 }
1059 return res;
1060}
1061
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001062bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 if (cu_->instruction_set == kMips) {
1064 // TODO - add Mips implementation
1065 return false;
1066 }
1067 // Location of reference to data array
1068 int value_offset = mirror::String::ValueOffset().Int32Value();
1069 // Location of count
1070 int count_offset = mirror::String::CountOffset().Int32Value();
1071 // Starting offset within data array
1072 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1073 // Start of char data with array_
1074 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1075
1076 RegLocation rl_obj = info->args[0];
1077 RegLocation rl_idx = info->args[1];
1078 rl_obj = LoadValue(rl_obj, kCoreReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001079 // X86 wants to avoid putting a constant index into a register.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001080 if (!((cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64)&& rl_idx.is_const)) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001081 rl_idx = LoadValue(rl_idx, kCoreReg);
1082 }
buzbee2700f7e2014-03-07 09:46:20 -08001083 RegStorage reg_max;
1084 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001086 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001087 RegStorage reg_off;
1088 RegStorage reg_ptr;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001089 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 reg_off = AllocTemp();
1091 reg_ptr = AllocTemp();
1092 if (range_check) {
1093 reg_max = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001094 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001095 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 }
buzbee695d13a2014-04-19 13:32:20 -07001097 Load32Disp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001098 MarkPossibleNullPointerException(info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001099 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 if (range_check) {
Mingyao Yang3a74d152014-04-21 15:39:44 -07001101 // Set up a slow path to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001102 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001104 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001105 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001106 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 } else {
1108 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001109 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001111 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001112 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001113 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Vladimir Marko3bc86152014-03-13 14:11:28 +00001114 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001115 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001116 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001117 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001118 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 }
1120 reg_off = AllocTemp();
1121 reg_ptr = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001122 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1123 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001125 if (rl_idx.is_const) {
1126 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1127 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001128 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001129 }
buzbee2700f7e2014-03-07 09:46:20 -08001130 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001131 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001132 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001133 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 RegLocation rl_dest = InlineTarget(info);
1135 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001136 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee2700f7e2014-03-07 09:46:20 -08001137 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001138 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001139 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg,
1140 RegStorage::InvalidReg(), kUnsignedHalf, INVALID_SREG);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001141 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 FreeTemp(reg_off);
1143 FreeTemp(reg_ptr);
1144 StoreValue(rl_dest, rl_result);
1145 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001146 DCHECK(range_check_branch != nullptr);
1147 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001148 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 return true;
1151}
1152
1153// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001154bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 if (cu_->instruction_set == kMips) {
1156 // TODO - add Mips implementation
1157 return false;
1158 }
1159 // dst = src.length();
1160 RegLocation rl_obj = info->args[0];
1161 rl_obj = LoadValue(rl_obj, kCoreReg);
1162 RegLocation rl_dest = InlineTarget(info);
1163 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001164 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001165 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001166 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 if (is_empty) {
1168 // dst = (dst == 0);
1169 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001170 RegStorage t_reg = AllocTemp();
1171 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1172 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001174 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001175 OpRegImm(kOpSub, rl_result.reg, 1);
1176 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 }
1178 }
1179 StoreValue(rl_dest, rl_result);
1180 return true;
1181}
1182
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001183bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1184 if (cu_->instruction_set == kMips) {
1185 // TODO - add Mips implementation
1186 return false;
1187 }
1188 RegLocation rl_src_i = info->args[0];
buzbee695d13a2014-04-19 13:32:20 -07001189 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001190 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001191 if (size == k64) {
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001192 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001193 RegStorage r_i_low = rl_i.reg.GetLow();
1194 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001195 // 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 +00001196 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001197 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001198 }
buzbee2700f7e2014-03-07 09:46:20 -08001199 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1200 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1201 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001202 FreeTemp(r_i_low);
1203 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001204 StoreValueWide(rl_dest, rl_result);
1205 } else {
buzbee695d13a2014-04-19 13:32:20 -07001206 DCHECK(size == k32 || size == kSignedHalf);
1207 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001208 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001209 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001210 StoreValue(rl_dest, rl_result);
1211 }
1212 return true;
1213}
1214
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001215bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 if (cu_->instruction_set == kMips) {
1217 // TODO - add Mips implementation
1218 return false;
1219 }
1220 RegLocation rl_src = info->args[0];
1221 rl_src = LoadValue(rl_src, kCoreReg);
1222 RegLocation rl_dest = InlineTarget(info);
1223 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001224 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001226 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1227 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1228 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 StoreValue(rl_dest, rl_result);
1230 return true;
1231}
1232
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001233bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 if (cu_->instruction_set == kMips) {
1235 // TODO - add Mips implementation
1236 return false;
1237 }
Vladimir Markob9823312014-03-20 17:38:43 +00001238 RegLocation rl_src = info->args[0];
1239 rl_src = LoadValueWide(rl_src, kCoreReg);
1240 RegLocation rl_dest = InlineTargetWide(info);
1241 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1242
1243 // If on x86 or if we would clobber a register needed later, just copy the source first.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001244 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 || rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
buzbee2700f7e2014-03-07 09:46:20 -08001245 OpRegCopyWide(rl_result.reg, rl_src.reg);
1246 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1247 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1248 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001249 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1250 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001251 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001252 }
1253 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 }
Vladimir Markob9823312014-03-20 17:38:43 +00001255
1256 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001257 RegStorage sign_reg = AllocTemp();
1258 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1259 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1260 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1261 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1262 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001263 StoreValueWide(rl_dest, rl_result);
1264 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265}
1266
Yixin Shoudbb17e32014-02-07 05:09:30 -08001267bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1268 if (cu_->instruction_set == kMips) {
1269 // TODO - add Mips implementation
1270 return false;
1271 }
1272 RegLocation rl_src = info->args[0];
1273 rl_src = LoadValue(rl_src, kCoreReg);
1274 RegLocation rl_dest = InlineTarget(info);
1275 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001276 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001277 StoreValue(rl_dest, rl_result);
1278 return true;
1279}
1280
1281bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1282 if (cu_->instruction_set == kMips) {
1283 // TODO - add Mips implementation
1284 return false;
1285 }
1286 RegLocation rl_src = info->args[0];
1287 rl_src = LoadValueWide(rl_src, kCoreReg);
1288 RegLocation rl_dest = InlineTargetWide(info);
1289 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001290 OpRegCopyWide(rl_result.reg, rl_src.reg);
1291 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001292 StoreValueWide(rl_dest, rl_result);
1293 return true;
1294}
1295
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001296bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 if (cu_->instruction_set == kMips) {
1298 // TODO - add Mips implementation
1299 return false;
1300 }
1301 RegLocation rl_src = info->args[0];
1302 RegLocation rl_dest = InlineTarget(info);
1303 StoreValue(rl_dest, rl_src);
1304 return true;
1305}
1306
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001307bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 if (cu_->instruction_set == kMips) {
1309 // TODO - add Mips implementation
1310 return false;
1311 }
1312 RegLocation rl_src = info->args[0];
1313 RegLocation rl_dest = InlineTargetWide(info);
1314 StoreValueWide(rl_dest, rl_src);
1315 return true;
1316}
1317
1318/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001319 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 * otherwise bails to standard library code.
1321 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001322bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 if (cu_->instruction_set == kMips) {
1324 // TODO - add Mips implementation
1325 return false;
1326 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001327 RegLocation rl_obj = info->args[0];
1328 RegLocation rl_char = info->args[1];
1329 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1330 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1331 return false;
1332 }
1333
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001334 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001336 RegStorage reg_ptr = TargetReg(kArg0);
1337 RegStorage reg_char = TargetReg(kArg1);
1338 RegStorage reg_start = TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 LoadValueDirectFixed(rl_obj, reg_ptr);
1341 LoadValueDirectFixed(rl_char, reg_char);
1342 if (zero_based) {
1343 LoadConstant(reg_start, 0);
1344 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001345 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 LoadValueDirectFixed(rl_start, reg_start);
1347 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001348 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001349 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001350 LIR* high_code_point_branch =
1351 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001353 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001354 if (!rl_char.is_const) {
1355 // Add the slow path for code points beyond 0xFFFF.
1356 DCHECK(high_code_point_branch != nullptr);
1357 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1358 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001359 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001360 } else {
1361 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1362 DCHECK(high_code_point_branch == nullptr);
1363 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 RegLocation rl_return = GetReturn(false);
1365 RegLocation rl_dest = InlineTarget(info);
1366 StoreValue(rl_dest, rl_return);
1367 return true;
1368}
1369
1370/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001371bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 if (cu_->instruction_set == kMips) {
1373 // TODO - add Mips implementation
1374 return false;
1375 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001376 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001378 RegStorage reg_this = TargetReg(kArg0);
1379 RegStorage reg_cmp = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001380
1381 RegLocation rl_this = info->args[0];
1382 RegLocation rl_cmp = info->args[1];
1383 LoadValueDirectFixed(rl_this, reg_this);
1384 LoadValueDirectFixed(rl_cmp, reg_cmp);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001385 RegStorage r_tgt = (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) ?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001386 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo)) : RegStorage::InvalidReg();
Dave Allisonf9439142014-03-27 15:10:22 -07001387 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001388 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001389 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001390 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001391 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001392 // NOTE: not a safepoint
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001393 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 OpReg(kOpBlx, r_tgt);
1395 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001396 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 RegLocation rl_return = GetReturn(false);
1399 RegLocation rl_dest = InlineTarget(info);
1400 StoreValue(rl_dest, rl_return);
1401 return true;
1402}
1403
1404bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1405 RegLocation rl_dest = InlineTarget(info);
1406 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001407 ThreadOffset<4> offset = Thread::PeerOffset<4>();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408 if (cu_->instruction_set == kThumb2 || cu_->instruction_set == kMips) {
buzbee695d13a2014-04-19 13:32:20 -07001409 Load32Disp(TargetReg(kSelf), offset.Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001411 CHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001412 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg.GetReg(), offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 }
1414 StoreValue(rl_dest, rl_result);
1415 return true;
1416}
1417
1418bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1419 bool is_long, bool is_volatile) {
1420 if (cu_->instruction_set == kMips) {
1421 // TODO - add Mips implementation
1422 return false;
1423 }
1424 // Unused - RegLocation rl_src_unsafe = info->args[0];
1425 RegLocation rl_src_obj = info->args[1]; // Object
1426 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001427 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001428 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001429
Brian Carlstrom7940e442013-07-12 13:46:57 -07001430 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1431 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1432 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1433 if (is_long) {
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001434 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001435 LoadBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_result.reg.GetLow(),
buzbee695d13a2014-04-19 13:32:20 -07001436 rl_result.reg.GetHigh(), k64, INVALID_SREG);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001437 } else {
1438 RegStorage rl_temp_offset = AllocTemp();
1439 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1440 LoadBaseDispWide(rl_temp_offset, 0, rl_result.reg, INVALID_SREG);
1441 FreeTemp(rl_temp_offset.GetReg());
1442 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 } else {
buzbee695d13a2014-04-19 13:32:20 -07001444 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001445 }
1446
1447 if (is_volatile) {
1448 // Without context sensitive analysis, we must issue the most conservative barriers.
1449 // In this case, either a load or store may follow so we issue both barriers.
1450 GenMemBarrier(kLoadLoad);
1451 GenMemBarrier(kLoadStore);
1452 }
1453
1454 if (is_long) {
1455 StoreValueWide(rl_dest, rl_result);
1456 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001457 StoreValue(rl_dest, rl_result);
1458 }
1459 return true;
1460}
1461
1462bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1463 bool is_object, bool is_volatile, bool is_ordered) {
1464 if (cu_->instruction_set == kMips) {
1465 // TODO - add Mips implementation
1466 return false;
1467 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 // Unused - RegLocation rl_src_unsafe = info->args[0];
1469 RegLocation rl_src_obj = info->args[1]; // Object
1470 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001471 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 RegLocation rl_src_value = info->args[4]; // value to store
1473 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001474 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 GenMemBarrier(kStoreStore);
1476 }
1477 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1478 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1479 RegLocation rl_value;
1480 if (is_long) {
1481 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001482 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001483 StoreBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_value.reg.GetLow(),
buzbee695d13a2014-04-19 13:32:20 -07001484 rl_value.reg.GetHigh(), k64, INVALID_SREG);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001485 } else {
1486 RegStorage rl_temp_offset = AllocTemp();
1487 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1488 StoreBaseDispWide(rl_temp_offset, 0, rl_value.reg);
1489 FreeTemp(rl_temp_offset.GetReg());
1490 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 } else {
1492 rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -07001493 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001494 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001495
1496 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001497 FreeTemp(rl_offset.reg.GetReg());
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001498
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001500 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 GenMemBarrier(kStoreLoad);
1502 }
1503 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001504 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 }
1506 return true;
1507}
1508
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001509void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001510 if ((info->opt_flags & MIR_INLINED) != 0) {
1511 // Already inlined but we may still need the null check.
1512 if (info->type != kStatic &&
1513 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1514 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
1515 RegLocation rl_obj = LoadValue(info->args[0], kCoreReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001516 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001517 }
1518 return;
1519 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001520 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1521 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1522 ->GenIntrinsic(this, info)) {
1523 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001525 GenInvokeNoInline(info);
1526}
1527
1528void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 int call_state = 0;
1530 LIR* null_ck;
1531 LIR** p_null_ck = NULL;
1532 NextCallInsn next_call_insn;
1533 FlushAllRegs(); /* Everything to home location */
1534 // Explicit register usage
1535 LockCallTemps();
1536
Vladimir Markof096aad2014-01-23 15:51:58 +00001537 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1538 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1539 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1540 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1541 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001542 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001544 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001545 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546 } else if (info->type == kDirect) {
1547 if (fast_path) {
1548 p_null_ck = &null_ck;
1549 }
1550 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1551 skip_this = false;
1552 } else if (info->type == kStatic) {
1553 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1554 skip_this = false;
1555 } else if (info->type == kSuper) {
1556 DCHECK(!fast_path); // Fast path is a direct call.
1557 next_call_insn = NextSuperCallInsnSP;
1558 skip_this = false;
1559 } else {
1560 DCHECK_EQ(info->type, kVirtual);
1561 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1562 skip_this = fast_path;
1563 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001564 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001565 if (!info->is_range) {
1566 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001567 next_call_insn, target_method, method_info.VTableIndex(),
1568 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 original_type, skip_this);
1570 } else {
1571 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001572 next_call_insn, target_method, method_info.VTableIndex(),
1573 method_info.DirectCode(), method_info.DirectMethod(),
1574 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575 }
1576 // Finish up any of the call sequence not interleaved in arg loading
1577 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001578 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1579 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 }
1581 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001582 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001583 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1584 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001585 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001586 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001587 // We can have the linker fixup a call relative.
1588 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001589 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001590 } else {
1591 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1592 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1593 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001594 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001595 ThreadOffset<4> trampoline(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 switch (info->type) {
1597 case kInterface:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001598 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 break;
1600 case kDirect:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001601 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 break;
1603 case kStatic:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001604 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 break;
1606 case kSuper:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001607 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 break;
1609 case kVirtual:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001610 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 break;
1612 default:
1613 LOG(FATAL) << "Unexpected invoke type";
1614 }
1615 call_inst = OpThreadMem(kOpBlx, trampoline);
1616 }
1617 }
1618 MarkSafepointPC(call_inst);
1619
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001620 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 if (info->result.location != kLocInvalid) {
1622 // We have a following MOVE_RESULT - do it now.
1623 if (info->result.wide) {
1624 RegLocation ret_loc = GetReturnWide(info->result.fp);
1625 StoreValueWide(info->result, ret_loc);
1626 } else {
1627 RegLocation ret_loc = GetReturn(info->result.fp);
1628 StoreValue(info->result, ret_loc);
1629 }
1630 }
1631}
1632
1633} // namespace art