blob: 4ecfeb9ea5452d026a15be2d5959abcf167bc01b [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;
buzbee091cc402014-03-31 10:14:40 -0700361 MarkLive(rl_src);
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) {
buzbee091cc402014-03-31 10:14:40 -0700756 // NOTE: not correct for 64-bit core regs, but this needs rewriting for hard-float.
757 reg = rl_arg.reg.IsPair() ? rl_arg.reg.GetHigh() : rl_arg.reg.DoubleToHighSingle();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 } else {
759 // kArg2 & rArg3 can safely be used here
760 reg = TargetReg(kArg3);
buzbee695d13a2014-04-19 13:32:20 -0700761 Load32Disp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 call_state = next_call_insn(cu_, info, call_state, target_method,
763 vtable_idx, direct_code, direct_method, type);
764 }
buzbee695d13a2014-04-19 13:32:20 -0700765 Store32Disp(TargetReg(kSp), (next_use + 1) * 4, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
767 direct_code, direct_method, type);
768 next_use++;
769 }
770 // Loop through the rest
771 while (next_use < info->num_arg_words) {
buzbee091cc402014-03-31 10:14:40 -0700772 RegStorage arg_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) {
buzbee091cc402014-03-31 10:14:40 -0700776 arg_reg = rl_arg.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 } else {
buzbee091cc402014-03-31 10:14:40 -0700778 arg_reg = rl_arg.wide ? RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)) :
779 TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 if (rl_arg.wide) {
buzbee091cc402014-03-31 10:14:40 -0700781 LoadValueDirectWideFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 } else {
buzbee091cc402014-03-31 10:14:40 -0700783 LoadValueDirectFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 }
785 call_state = next_call_insn(cu_, info, call_state, target_method,
786 vtable_idx, direct_code, direct_method, type);
787 }
788 int outs_offset = (next_use + 1) * 4;
789 if (rl_arg.wide) {
buzbee091cc402014-03-31 10:14:40 -0700790 StoreBaseDispWide(TargetReg(kSp), outs_offset, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 next_use += 2;
792 } else {
buzbee091cc402014-03-31 10:14:40 -0700793 Store32Disp(TargetReg(kSp), outs_offset, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 next_use++;
795 }
796 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
797 direct_code, direct_method, type);
798 }
799 }
800
801 call_state = LoadArgRegs(info, call_state, next_call_insn,
802 target_method, vtable_idx, direct_code, direct_method,
803 type, skip_this);
804
805 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700806 if (Runtime::Current()->ExplicitNullChecks()) {
807 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
808 } else {
809 *pcrLabel = nullptr;
810 // In lieu of generating a check for kArg1 being null, we need to
811 // perform a load when doing implicit checks.
812 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700813 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -0700814 MarkPossibleNullPointerException(info->opt_flags);
815 FreeTemp(tmp);
816 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 }
818 return call_state;
819}
820
821/*
822 * May have 0+ arguments (also used for jumbo). Note that
823 * source virtual registers may be in physical registers, so may
824 * need to be flushed to home location before copying. This
825 * applies to arg3 and above (see below).
826 *
827 * Two general strategies:
828 * If < 20 arguments
829 * Pass args 3-18 using vldm/vstm block copy
830 * Pass arg0, arg1 & arg2 in kArg1-kArg3
831 * If 20+ arguments
832 * Pass args arg19+ using memcpy block copy
833 * Pass arg0, arg1 & arg2 in kArg1-kArg3
834 *
835 */
836int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
837 LIR** pcrLabel, NextCallInsn next_call_insn,
838 const MethodReference& target_method,
839 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700840 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 // If we can treat it as non-range (Jumbo ops will use range form)
842 if (info->num_arg_words <= 5)
843 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
844 next_call_insn, target_method, vtable_idx,
845 direct_code, direct_method, type, skip_this);
846 /*
847 * First load the non-register arguments. Both forms expect all
848 * of the source arguments to be in their home frame location, so
849 * scan the s_reg names and flush any that have been promoted to
850 * frame backing storage.
851 */
852 // Scan the rest of the args - if in phys_reg flush to memory
853 for (int next_arg = 0; next_arg < info->num_arg_words;) {
854 RegLocation loc = info->args[next_arg];
855 if (loc.wide) {
856 loc = UpdateLocWide(loc);
857 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800858 StoreBaseDispWide(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700859 }
860 next_arg += 2;
861 } else {
862 loc = UpdateLoc(loc);
863 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
buzbee695d13a2014-04-19 13:32:20 -0700864 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 }
866 next_arg++;
867 }
868 }
869
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800870 // Logic below assumes that Method pointer is at offset zero from SP.
871 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
872
873 // The first 3 arguments are passed via registers.
874 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
875 // get size of uintptr_t or size of object reference according to model being used.
876 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800878 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
879 DCHECK_GT(regs_left_to_pass_via_stack, 0);
880
881 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
882 // Use vldm/vstm pair using kArg3 as a temp
883 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
884 direct_code, direct_method, type);
885 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
886 LIR* ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
887 // TUNING: loosen barrier
888 ld->u.m.def_mask = ENCODE_ALL;
889 SetMemRefType(ld, true /* is_load */, kDalvikReg);
890 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
891 direct_code, direct_method, type);
892 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
893 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
894 direct_code, direct_method, type);
895 LIR* st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
896 SetMemRefType(st, false /* is_load */, kDalvikReg);
897 st->u.m.def_mask = ENCODE_ALL;
898 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
899 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700900 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800901 int current_src_offset = start_offset;
902 int current_dest_offset = outs_offset;
903
904 while (regs_left_to_pass_via_stack > 0) {
905 // This is based on the knowledge that the stack itself is 16-byte aligned.
906 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
907 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
908 size_t bytes_to_move;
909
910 /*
911 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
912 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
913 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
914 * We do this because we could potentially do a smaller move to align.
915 */
916 if (regs_left_to_pass_via_stack == 4 ||
917 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
918 // Moving 128-bits via xmm register.
919 bytes_to_move = sizeof(uint32_t) * 4;
920
921 // Allocate a free xmm temp. Since we are working through the calling sequence,
922 // we expect to have an xmm temporary available.
buzbee2700f7e2014-03-07 09:46:20 -0800923 RegStorage temp = AllocTempDouble();
buzbee091cc402014-03-31 10:14:40 -0700924 DCHECK(temp.Valid());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800925
926 LIR* ld1 = nullptr;
927 LIR* ld2 = nullptr;
928 LIR* st1 = nullptr;
929 LIR* st2 = nullptr;
930
931 /*
932 * The logic is similar for both loads and stores. If we have 16-byte alignment,
933 * do an aligned move. If we have 8-byte alignment, then do the move in two
934 * parts. This approach prevents possible cache line splits. Finally, fall back
935 * to doing an unaligned move. In most cases we likely won't split the cache
936 * line but we cannot prove it and thus take a conservative approach.
937 */
938 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
939 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
940
941 if (src_is_16b_aligned) {
942 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
943 } else if (src_is_8b_aligned) {
944 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800945 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
946 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800947 } else {
948 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
949 }
950
951 if (dest_is_16b_aligned) {
952 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
953 } else if (dest_is_8b_aligned) {
954 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800955 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
956 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800957 } else {
958 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
959 }
960
961 // TODO If we could keep track of aliasing information for memory accesses that are wider
962 // than 64-bit, we wouldn't need to set up a barrier.
963 if (ld1 != nullptr) {
964 if (ld2 != nullptr) {
965 // For 64-bit load we can actually set up the aliasing information.
966 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
967 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
968 } else {
969 // Set barrier for 128-bit load.
970 SetMemRefType(ld1, true /* is_load */, kDalvikReg);
971 ld1->u.m.def_mask = ENCODE_ALL;
972 }
973 }
974 if (st1 != nullptr) {
975 if (st2 != nullptr) {
976 // For 64-bit store we can actually set up the aliasing information.
977 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
978 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
979 } else {
980 // Set barrier for 128-bit store.
981 SetMemRefType(st1, false /* is_load */, kDalvikReg);
982 st1->u.m.def_mask = ENCODE_ALL;
983 }
984 }
985
986 // Free the temporary used for the data movement.
buzbee091cc402014-03-31 10:14:40 -0700987 FreeTemp(temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800988 } else {
989 // Moving 32-bits via general purpose register.
990 bytes_to_move = sizeof(uint32_t);
991
992 // Instead of allocating a new temp, simply reuse one of the registers being used
993 // for argument passing.
buzbee2700f7e2014-03-07 09:46:20 -0800994 RegStorage temp = TargetReg(kArg3);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800995
996 // Now load the argument VR and store to the outs.
buzbee695d13a2014-04-19 13:32:20 -0700997 Load32Disp(TargetReg(kSp), current_src_offset, temp);
998 Store32Disp(TargetReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800999 }
1000
1001 current_src_offset += bytes_to_move;
1002 current_dest_offset += bytes_to_move;
1003 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1004 }
1005 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 // Generate memcpy
1007 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
1008 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001009 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(4, pMemcpy), TargetReg(kArg0),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 }
1012
1013 call_state = LoadArgRegs(info, call_state, next_call_insn,
1014 target_method, vtable_idx, direct_code, direct_method,
1015 type, skip_this);
1016
1017 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1018 direct_code, direct_method, type);
1019 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -07001020 if (Runtime::Current()->ExplicitNullChecks()) {
1021 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
1022 } else {
1023 *pcrLabel = nullptr;
1024 // In lieu of generating a check for kArg1 being null, we need to
1025 // perform a load when doing implicit checks.
1026 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001027 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -07001028 MarkPossibleNullPointerException(info->opt_flags);
1029 FreeTemp(tmp);
1030 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 }
1032 return call_state;
1033}
1034
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001035RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 RegLocation res;
1037 if (info->result.location == kLocInvalid) {
1038 res = GetReturn(false);
1039 } else {
1040 res = info->result;
1041 }
1042 return res;
1043}
1044
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001045RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 RegLocation res;
1047 if (info->result.location == kLocInvalid) {
1048 res = GetReturnWide(false);
1049 } else {
1050 res = info->result;
1051 }
1052 return res;
1053}
1054
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001055bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 if (cu_->instruction_set == kMips) {
1057 // TODO - add Mips implementation
1058 return false;
1059 }
1060 // Location of reference to data array
1061 int value_offset = mirror::String::ValueOffset().Int32Value();
1062 // Location of count
1063 int count_offset = mirror::String::CountOffset().Int32Value();
1064 // Starting offset within data array
1065 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1066 // Start of char data with array_
1067 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1068
1069 RegLocation rl_obj = info->args[0];
1070 RegLocation rl_idx = info->args[1];
1071 rl_obj = LoadValue(rl_obj, kCoreReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001072 // X86 wants to avoid putting a constant index into a register.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001073 if (!((cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64)&& rl_idx.is_const)) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001074 rl_idx = LoadValue(rl_idx, kCoreReg);
1075 }
buzbee2700f7e2014-03-07 09:46:20 -08001076 RegStorage reg_max;
1077 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001079 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001080 RegStorage reg_off;
1081 RegStorage reg_ptr;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001082 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 reg_off = AllocTemp();
1084 reg_ptr = AllocTemp();
1085 if (range_check) {
1086 reg_max = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001087 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001088 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 }
buzbee695d13a2014-04-19 13:32:20 -07001090 Load32Disp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001091 MarkPossibleNullPointerException(info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001092 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 if (range_check) {
Mingyao Yang3a74d152014-04-21 15:39:44 -07001094 // Set up a slow path to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001095 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001097 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001098 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001099 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 } else {
1101 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001102 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001104 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001105 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001106 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Vladimir Marko3bc86152014-03-13 14:11:28 +00001107 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001108 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001109 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001110 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001111 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 }
1113 reg_off = AllocTemp();
1114 reg_ptr = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001115 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1116 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001118 if (rl_idx.is_const) {
1119 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1120 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001121 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001122 }
buzbee2700f7e2014-03-07 09:46:20 -08001123 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001124 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001125 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001126 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 RegLocation rl_dest = InlineTarget(info);
1128 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001129 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee2700f7e2014-03-07 09:46:20 -08001130 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001131 } else {
buzbee091cc402014-03-31 10:14:40 -07001132 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf,
1133 INVALID_SREG);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001134 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 FreeTemp(reg_off);
1136 FreeTemp(reg_ptr);
1137 StoreValue(rl_dest, rl_result);
1138 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001139 DCHECK(range_check_branch != nullptr);
1140 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001141 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 return true;
1144}
1145
1146// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001147bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 if (cu_->instruction_set == kMips) {
1149 // TODO - add Mips implementation
1150 return false;
1151 }
1152 // dst = src.length();
1153 RegLocation rl_obj = info->args[0];
1154 rl_obj = LoadValue(rl_obj, kCoreReg);
1155 RegLocation rl_dest = InlineTarget(info);
1156 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001157 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001158 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001159 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 if (is_empty) {
1161 // dst = (dst == 0);
1162 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001163 RegStorage t_reg = AllocTemp();
1164 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1165 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001167 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001168 OpRegImm(kOpSub, rl_result.reg, 1);
1169 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 }
1171 }
1172 StoreValue(rl_dest, rl_result);
1173 return true;
1174}
1175
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001176bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1177 if (cu_->instruction_set == kMips) {
1178 // TODO - add Mips implementation
1179 return false;
1180 }
1181 RegLocation rl_src_i = info->args[0];
buzbee695d13a2014-04-19 13:32:20 -07001182 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001183 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001184 if (size == k64) {
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001185 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001186 RegStorage r_i_low = rl_i.reg.GetLow();
1187 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001188 // 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 +00001189 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001190 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001191 }
buzbee2700f7e2014-03-07 09:46:20 -08001192 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1193 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1194 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001195 FreeTemp(r_i_low);
1196 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001197 StoreValueWide(rl_dest, rl_result);
1198 } else {
buzbee695d13a2014-04-19 13:32:20 -07001199 DCHECK(size == k32 || size == kSignedHalf);
1200 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001201 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001202 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001203 StoreValue(rl_dest, rl_result);
1204 }
1205 return true;
1206}
1207
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001208bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 if (cu_->instruction_set == kMips) {
1210 // TODO - add Mips implementation
1211 return false;
1212 }
1213 RegLocation rl_src = info->args[0];
1214 rl_src = LoadValue(rl_src, kCoreReg);
1215 RegLocation rl_dest = InlineTarget(info);
1216 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001217 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001219 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1220 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1221 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 StoreValue(rl_dest, rl_result);
1223 return true;
1224}
1225
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001226bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 if (cu_->instruction_set == kMips) {
1228 // TODO - add Mips implementation
1229 return false;
1230 }
Vladimir Markob9823312014-03-20 17:38:43 +00001231 RegLocation rl_src = info->args[0];
1232 rl_src = LoadValueWide(rl_src, kCoreReg);
1233 RegLocation rl_dest = InlineTargetWide(info);
1234 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1235
1236 // If on x86 or if we would clobber a register needed later, just copy the source first.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001237 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 || rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
buzbee2700f7e2014-03-07 09:46:20 -08001238 OpRegCopyWide(rl_result.reg, rl_src.reg);
1239 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1240 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1241 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001242 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1243 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001244 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001245 }
1246 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 }
Vladimir Markob9823312014-03-20 17:38:43 +00001248
1249 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001250 RegStorage sign_reg = AllocTemp();
1251 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1252 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1253 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1254 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1255 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001256 StoreValueWide(rl_dest, rl_result);
1257 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258}
1259
Yixin Shoudbb17e32014-02-07 05:09:30 -08001260bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1261 if (cu_->instruction_set == kMips) {
1262 // TODO - add Mips implementation
1263 return false;
1264 }
1265 RegLocation rl_src = info->args[0];
1266 rl_src = LoadValue(rl_src, kCoreReg);
1267 RegLocation rl_dest = InlineTarget(info);
1268 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001269 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001270 StoreValue(rl_dest, rl_result);
1271 return true;
1272}
1273
1274bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1275 if (cu_->instruction_set == kMips) {
1276 // TODO - add Mips implementation
1277 return false;
1278 }
1279 RegLocation rl_src = info->args[0];
1280 rl_src = LoadValueWide(rl_src, kCoreReg);
1281 RegLocation rl_dest = InlineTargetWide(info);
1282 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001283 OpRegCopyWide(rl_result.reg, rl_src.reg);
1284 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001285 StoreValueWide(rl_dest, rl_result);
1286 return true;
1287}
1288
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001289bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 if (cu_->instruction_set == kMips) {
1291 // TODO - add Mips implementation
1292 return false;
1293 }
1294 RegLocation rl_src = info->args[0];
1295 RegLocation rl_dest = InlineTarget(info);
1296 StoreValue(rl_dest, rl_src);
1297 return true;
1298}
1299
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001300bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 if (cu_->instruction_set == kMips) {
1302 // TODO - add Mips implementation
1303 return false;
1304 }
1305 RegLocation rl_src = info->args[0];
1306 RegLocation rl_dest = InlineTargetWide(info);
1307 StoreValueWide(rl_dest, rl_src);
1308 return true;
1309}
1310
1311/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001312 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 * otherwise bails to standard library code.
1314 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001315bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 if (cu_->instruction_set == kMips) {
1317 // TODO - add Mips implementation
1318 return false;
1319 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001320 RegLocation rl_obj = info->args[0];
1321 RegLocation rl_char = info->args[1];
1322 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1323 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1324 return false;
1325 }
1326
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001327 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001329 RegStorage reg_ptr = TargetReg(kArg0);
1330 RegStorage reg_char = TargetReg(kArg1);
1331 RegStorage reg_start = TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 LoadValueDirectFixed(rl_obj, reg_ptr);
1334 LoadValueDirectFixed(rl_char, reg_char);
1335 if (zero_based) {
1336 LoadConstant(reg_start, 0);
1337 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001338 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 LoadValueDirectFixed(rl_start, reg_start);
1340 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001341 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001342 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001343 LIR* high_code_point_branch =
1344 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001346 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001347 if (!rl_char.is_const) {
1348 // Add the slow path for code points beyond 0xFFFF.
1349 DCHECK(high_code_point_branch != nullptr);
1350 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1351 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001352 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001353 } else {
1354 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1355 DCHECK(high_code_point_branch == nullptr);
1356 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 RegLocation rl_return = GetReturn(false);
1358 RegLocation rl_dest = InlineTarget(info);
1359 StoreValue(rl_dest, rl_return);
1360 return true;
1361}
1362
1363/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001364bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 if (cu_->instruction_set == kMips) {
1366 // TODO - add Mips implementation
1367 return false;
1368 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001369 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001371 RegStorage reg_this = TargetReg(kArg0);
1372 RegStorage reg_cmp = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373
1374 RegLocation rl_this = info->args[0];
1375 RegLocation rl_cmp = info->args[1];
1376 LoadValueDirectFixed(rl_this, reg_this);
1377 LoadValueDirectFixed(rl_cmp, reg_cmp);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001378 RegStorage r_tgt = (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) ?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001379 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo)) : RegStorage::InvalidReg();
Dave Allisonf9439142014-03-27 15:10:22 -07001380 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001381 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001382 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001383 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001384 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 // NOTE: not a safepoint
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001386 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 OpReg(kOpBlx, r_tgt);
1388 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001389 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 RegLocation rl_return = GetReturn(false);
1392 RegLocation rl_dest = InlineTarget(info);
1393 StoreValue(rl_dest, rl_return);
1394 return true;
1395}
1396
1397bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1398 RegLocation rl_dest = InlineTarget(info);
1399 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001400 ThreadOffset<4> offset = Thread::PeerOffset<4>();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 if (cu_->instruction_set == kThumb2 || cu_->instruction_set == kMips) {
buzbee695d13a2014-04-19 13:32:20 -07001402 Load32Disp(TargetReg(kSelf), offset.Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001403 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001404 CHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee091cc402014-03-31 10:14:40 -07001405 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 }
1407 StoreValue(rl_dest, rl_result);
1408 return true;
1409}
1410
1411bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1412 bool is_long, bool is_volatile) {
1413 if (cu_->instruction_set == kMips) {
1414 // TODO - add Mips implementation
1415 return false;
1416 }
1417 // Unused - RegLocation rl_src_unsafe = info->args[0];
1418 RegLocation rl_src_obj = info->args[1]; // Object
1419 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001420 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001421 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001422
Brian Carlstrom7940e442013-07-12 13:46:57 -07001423 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1424 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1425 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1426 if (is_long) {
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001427 if (cu_->instruction_set == kX86) {
buzbee091cc402014-03-31 10:14:40 -07001428 LoadBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_result.reg, k64, INVALID_SREG);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001429 } else {
1430 RegStorage rl_temp_offset = AllocTemp();
1431 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1432 LoadBaseDispWide(rl_temp_offset, 0, rl_result.reg, INVALID_SREG);
buzbee091cc402014-03-31 10:14:40 -07001433 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001434 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001435 } else {
buzbee695d13a2014-04-19 13:32:20 -07001436 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001437 }
1438
1439 if (is_volatile) {
1440 // Without context sensitive analysis, we must issue the most conservative barriers.
1441 // In this case, either a load or store may follow so we issue both barriers.
1442 GenMemBarrier(kLoadLoad);
1443 GenMemBarrier(kLoadStore);
1444 }
1445
1446 if (is_long) {
1447 StoreValueWide(rl_dest, rl_result);
1448 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001449 StoreValue(rl_dest, rl_result);
1450 }
1451 return true;
1452}
1453
1454bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1455 bool is_object, bool is_volatile, bool is_ordered) {
1456 if (cu_->instruction_set == kMips) {
1457 // TODO - add Mips implementation
1458 return false;
1459 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 // Unused - RegLocation rl_src_unsafe = info->args[0];
1461 RegLocation rl_src_obj = info->args[1]; // Object
1462 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001463 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 RegLocation rl_src_value = info->args[4]; // value to store
1465 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001466 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 GenMemBarrier(kStoreStore);
1468 }
1469 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1470 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1471 RegLocation rl_value;
1472 if (is_long) {
1473 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001474 if (cu_->instruction_set == kX86) {
buzbee091cc402014-03-31 10:14:40 -07001475 StoreBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_value.reg, k64, INVALID_SREG);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001476 } else {
1477 RegStorage rl_temp_offset = AllocTemp();
1478 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1479 StoreBaseDispWide(rl_temp_offset, 0, rl_value.reg);
buzbee091cc402014-03-31 10:14:40 -07001480 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001481 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 } else {
1483 rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -07001484 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001485 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001486
1487 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001488 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001489
Brian Carlstrom7940e442013-07-12 13:46:57 -07001490 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001491 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 GenMemBarrier(kStoreLoad);
1493 }
1494 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001495 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 }
1497 return true;
1498}
1499
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001500void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001501 if ((info->opt_flags & MIR_INLINED) != 0) {
1502 // Already inlined but we may still need the null check.
1503 if (info->type != kStatic &&
1504 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1505 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
1506 RegLocation rl_obj = LoadValue(info->args[0], kCoreReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001507 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001508 }
1509 return;
1510 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001511 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1512 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1513 ->GenIntrinsic(this, info)) {
1514 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001516 GenInvokeNoInline(info);
1517}
1518
1519void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 int call_state = 0;
1521 LIR* null_ck;
1522 LIR** p_null_ck = NULL;
1523 NextCallInsn next_call_insn;
1524 FlushAllRegs(); /* Everything to home location */
1525 // Explicit register usage
1526 LockCallTemps();
1527
Vladimir Markof096aad2014-01-23 15:51:58 +00001528 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1529 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1530 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1531 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1532 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001534 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001536 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 } else if (info->type == kDirect) {
1538 if (fast_path) {
1539 p_null_ck = &null_ck;
1540 }
1541 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1542 skip_this = false;
1543 } else if (info->type == kStatic) {
1544 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1545 skip_this = false;
1546 } else if (info->type == kSuper) {
1547 DCHECK(!fast_path); // Fast path is a direct call.
1548 next_call_insn = NextSuperCallInsnSP;
1549 skip_this = false;
1550 } else {
1551 DCHECK_EQ(info->type, kVirtual);
1552 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1553 skip_this = fast_path;
1554 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001555 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556 if (!info->is_range) {
1557 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001558 next_call_insn, target_method, method_info.VTableIndex(),
1559 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560 original_type, skip_this);
1561 } else {
1562 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001563 next_call_insn, target_method, method_info.VTableIndex(),
1564 method_info.DirectCode(), method_info.DirectMethod(),
1565 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 }
1567 // Finish up any of the call sequence not interleaved in arg loading
1568 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001569 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1570 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 }
1572 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001573 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1575 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001576 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001577 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001578 // We can have the linker fixup a call relative.
1579 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001580 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001581 } else {
1582 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1583 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1584 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001586 ThreadOffset<4> trampoline(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 switch (info->type) {
1588 case kInterface:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001589 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 break;
1591 case kDirect:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001592 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 break;
1594 case kStatic:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001595 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 break;
1597 case kSuper:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001598 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 break;
1600 case kVirtual:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001601 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 break;
1603 default:
1604 LOG(FATAL) << "Unexpected invoke type";
1605 }
1606 call_inst = OpThreadMem(kOpBlx, trampoline);
1607 }
1608 }
1609 MarkSafepointPC(call_inst);
1610
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001611 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 if (info->result.location != kLocInvalid) {
1613 // We have a following MOVE_RESULT - do it now.
1614 if (info->result.wide) {
1615 RegLocation ret_loc = GetReturnWide(info->result.fp);
1616 StoreValueWide(info->result, ret_loc);
1617 } else {
1618 RegLocation ret_loc = GetReturn(info->result.fp);
1619 StoreValue(info->result, ret_loc);
1620 }
1621 }
1622}
1623
1624} // namespace art