blob: 7689b51f9667db1bcfbe6fd739b19242b83e4c0f [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "dex/compiler_ir.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000018#include "dex/frontend.h"
19#include "dex/quick/dex_file_method_inliner.h"
20#include "dex/quick/dex_file_to_method_inliner_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "dex_file-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070022#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "invoke_type.h"
24#include "mirror/array.h"
25#include "mirror/string.h"
26#include "mir_to_lir-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "x86/codegen_x86.h"
28
29namespace art {
30
31/*
32 * This source files contains "gen" codegen routines that should
33 * be applicable to most targets. Only mid-level support utilities
34 * and "op" calls may be used here.
35 */
36
Vladimir Marko3bc86152014-03-13 14:11:28 +000037void Mir2Lir::AddIntrinsicLaunchpad(CallInfo* info, LIR* branch, LIR* resume) {
38 class IntrinsicLaunchpadPath : public Mir2Lir::LIRSlowPath {
39 public:
40 IntrinsicLaunchpadPath(Mir2Lir* m2l, CallInfo* info, LIR* branch, LIR* resume = nullptr)
41 : LIRSlowPath(m2l, info->offset, branch, resume), info_(info) {
42 }
43
44 void Compile() {
45 m2l_->ResetRegPool();
46 m2l_->ResetDefTracking();
47 LIR* label = GenerateTargetLabel();
48 label->opcode = kPseudoIntrinsicRetry;
49 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
50 m2l_->GenInvokeNoInline(info_);
51 if (cont_ != nullptr) {
52 m2l_->OpUnconditionalBranch(cont_);
53 }
54 }
55
56 private:
57 CallInfo* const info_;
58 };
59
60 AddSlowPath(new (arena_) IntrinsicLaunchpadPath(this, info, branch, resume));
61}
62
Brian Carlstrom7940e442013-07-12 13:46:57 -070063/*
64 * To save scheduling time, helper calls are broken into two parts: generation of
Brian Carlstrom60d7a652014-03-13 18:10:08 -070065 * the helper target address, and the actual call to the helper. Because x86
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 * has a memory call operation, part 1 is a NOP for x86. For other targets,
67 * load arguments between the two parts.
68 */
buzbee2700f7e2014-03-07 09:46:20 -080069RegStorage Mir2Lir::CallHelperSetup(ThreadOffset helper_offset) {
70 return (cu_->instruction_set == kX86) ? RegStorage::InvalidReg() : LoadHelper(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070071}
72
73/* NOTE: if r_tgt is a temp, it will be freed following use */
buzbee2700f7e2014-03-07 09:46:20 -080074LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset helper_offset, bool safepoint_pc,
75 bool use_link) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 LIR* call_inst;
Brian Carlstrom60d7a652014-03-13 18:10:08 -070077 OpKind op = use_link ? kOpBlx : kOpBx;
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 if (cu_->instruction_set == kX86) {
Brian Carlstrom60d7a652014-03-13 18:10:08 -070079 call_inst = OpThreadMem(op, helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 } else {
Brian Carlstrom60d7a652014-03-13 18:10:08 -070081 call_inst = OpReg(op, r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 FreeTemp(r_tgt);
83 }
84 if (safepoint_pc) {
85 MarkSafepointPC(call_inst);
86 }
87 return call_inst;
88}
89
Ian Rogers848871b2013-08-05 10:56:33 -070090void Mir2Lir::CallRuntimeHelperImm(ThreadOffset helper_offset, int arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -080091 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +000093 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 CallHelper(r_tgt, helper_offset, safepoint_pc);
95}
96
buzbee2700f7e2014-03-07 09:46:20 -080097void Mir2Lir::CallRuntimeHelperReg(ThreadOffset helper_offset, RegStorage arg0, bool safepoint_pc) {
98 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 OpRegCopy(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000100 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 CallHelper(r_tgt, helper_offset, safepoint_pc);
102}
103
Ian Rogers848871b2013-08-05 10:56:33 -0700104void Mir2Lir::CallRuntimeHelperRegLocation(ThreadOffset helper_offset, RegLocation arg0,
105 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800106 RegStorage r_tgt = CallHelperSetup(helper_offset);
107 if (arg0.wide == 0) {
108 LoadValueDirectFixed(arg0, TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800110 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
111 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000113 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 CallHelper(r_tgt, helper_offset, safepoint_pc);
115}
116
Ian Rogers848871b2013-08-05 10:56:33 -0700117void Mir2Lir::CallRuntimeHelperImmImm(ThreadOffset helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800119 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 LoadConstant(TargetReg(kArg0), arg0);
121 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000122 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 CallHelper(r_tgt, helper_offset, safepoint_pc);
124}
125
Ian Rogers848871b2013-08-05 10:56:33 -0700126void Mir2Lir::CallRuntimeHelperImmRegLocation(ThreadOffset helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 RegLocation arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800128 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 if (arg1.wide == 0) {
130 LoadValueDirectFixed(arg1, TargetReg(kArg1));
131 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800132 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
133 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 }
135 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000136 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 CallHelper(r_tgt, helper_offset, safepoint_pc);
138}
139
Ian Rogers848871b2013-08-05 10:56:33 -0700140void Mir2Lir::CallRuntimeHelperRegLocationImm(ThreadOffset helper_offset, RegLocation arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800142 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 LoadValueDirectFixed(arg0, TargetReg(kArg0));
144 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000145 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 CallHelper(r_tgt, helper_offset, safepoint_pc);
147}
148
buzbee2700f7e2014-03-07 09:46:20 -0800149void Mir2Lir::CallRuntimeHelperImmReg(ThreadOffset helper_offset, int arg0, RegStorage arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800151 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 OpRegCopy(TargetReg(kArg1), arg1);
153 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000154 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 CallHelper(r_tgt, helper_offset, safepoint_pc);
156}
157
buzbee2700f7e2014-03-07 09:46:20 -0800158void Mir2Lir::CallRuntimeHelperRegImm(ThreadOffset helper_offset, RegStorage arg0, int arg1,
Ian Rogers848871b2013-08-05 10:56:33 -0700159 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800160 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 OpRegCopy(TargetReg(kArg0), arg0);
162 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000163 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 CallHelper(r_tgt, helper_offset, safepoint_pc);
165}
166
Ian Rogers848871b2013-08-05 10:56:33 -0700167void Mir2Lir::CallRuntimeHelperImmMethod(ThreadOffset helper_offset, int arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800168 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 LoadCurrMethodDirect(TargetReg(kArg1));
170 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000171 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 CallHelper(r_tgt, helper_offset, safepoint_pc);
173}
174
buzbee2700f7e2014-03-07 09:46:20 -0800175void Mir2Lir::CallRuntimeHelperRegMethod(ThreadOffset helper_offset, RegStorage arg0,
176 bool safepoint_pc) {
177 RegStorage r_tgt = CallHelperSetup(helper_offset);
178 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800179 if (TargetReg(kArg0) != arg0) {
180 OpRegCopy(TargetReg(kArg0), arg0);
181 }
182 LoadCurrMethodDirect(TargetReg(kArg1));
183 ClobberCallerSave();
184 CallHelper(r_tgt, helper_offset, safepoint_pc);
185}
186
buzbee2700f7e2014-03-07 09:46:20 -0800187void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(ThreadOffset helper_offset, RegStorage arg0,
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800188 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800189 RegStorage r_tgt = CallHelperSetup(helper_offset);
190 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800191 if (TargetReg(kArg0) != arg0) {
192 OpRegCopy(TargetReg(kArg0), arg0);
193 }
194 LoadCurrMethodDirect(TargetReg(kArg1));
195 LoadValueDirectFixed(arg2, TargetReg(kArg2));
196 ClobberCallerSave();
197 CallHelper(r_tgt, helper_offset, safepoint_pc);
198}
199
Ian Rogers848871b2013-08-05 10:56:33 -0700200void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(ThreadOffset helper_offset, RegLocation arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 RegLocation arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800202 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 if (arg0.wide == 0) {
204 LoadValueDirectFixed(arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
205 if (arg1.wide == 0) {
206 if (cu_->instruction_set == kMips) {
207 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1));
208 } else {
209 LoadValueDirectFixed(arg1, TargetReg(kArg1));
210 }
211 } else {
212 if (cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -0800213 RegStorage r_tmp;
214 if (arg1.fp) {
215 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
216 } else {
217 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
218 }
219 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800221 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
222 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 }
224 }
225 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800226 RegStorage r_tmp;
227 if (arg0.fp) {
228 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg0), TargetReg(kFArg1));
229 } else {
230 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
231 }
232 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 if (arg1.wide == 0) {
234 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2));
235 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800236 RegStorage r_tmp;
237 if (arg1.fp) {
238 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
239 } else {
240 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
241 }
242 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 }
244 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000245 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 CallHelper(r_tgt, helper_offset, safepoint_pc);
247}
248
buzbee2700f7e2014-03-07 09:46:20 -0800249void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset helper_offset, RegStorage arg0,
250 RegStorage arg1, bool safepoint_pc) {
251 RegStorage r_tgt = CallHelperSetup(helper_offset);
252 DCHECK_NE(TargetReg(kArg0).GetReg(), arg1.GetReg()); // check copy into arg0 won't clobber arg1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 OpRegCopy(TargetReg(kArg0), arg0);
254 OpRegCopy(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000255 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 CallHelper(r_tgt, helper_offset, safepoint_pc);
257}
258
buzbee2700f7e2014-03-07 09:46:20 -0800259void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset helper_offset, RegStorage arg0,
260 RegStorage arg1, int arg2, bool safepoint_pc) {
261 RegStorage r_tgt = CallHelperSetup(helper_offset);
262 DCHECK_NE(TargetReg(kArg0).GetReg(), arg1.GetReg()); // check copy into arg0 won't clobber arg1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 OpRegCopy(TargetReg(kArg0), arg0);
264 OpRegCopy(TargetReg(kArg1), arg1);
265 LoadConstant(TargetReg(kArg2), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000266 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 CallHelper(r_tgt, helper_offset, safepoint_pc);
268}
269
Ian Rogers848871b2013-08-05 10:56:33 -0700270void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 int arg0, RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800272 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 LoadValueDirectFixed(arg2, TargetReg(kArg2));
274 LoadCurrMethodDirect(TargetReg(kArg1));
275 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000276 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 CallHelper(r_tgt, helper_offset, safepoint_pc);
278}
279
Ian Rogers848871b2013-08-05 10:56:33 -0700280void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 int arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800282 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 LoadCurrMethodDirect(TargetReg(kArg1));
284 LoadConstant(TargetReg(kArg2), arg2);
285 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000286 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 CallHelper(r_tgt, helper_offset, safepoint_pc);
288}
289
Ian Rogers848871b2013-08-05 10:56:33 -0700290void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 int arg0, RegLocation arg1,
292 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800293 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700294 DCHECK_EQ(arg1.wide, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 LoadValueDirectFixed(arg1, TargetReg(kArg1));
296 if (arg2.wide == 0) {
297 LoadValueDirectFixed(arg2, TargetReg(kArg2));
298 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800299 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
300 LoadValueDirectWideFixed(arg2, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 }
302 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000303 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 CallHelper(r_tgt, helper_offset, safepoint_pc);
305}
306
Ian Rogersa9a82542013-10-04 11:17:26 -0700307void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset helper_offset,
308 RegLocation arg0, RegLocation arg1,
309 RegLocation arg2,
310 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800311 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700312 DCHECK_EQ(arg0.wide, 0U);
313 LoadValueDirectFixed(arg0, TargetReg(kArg0));
314 DCHECK_EQ(arg1.wide, 0U);
315 LoadValueDirectFixed(arg1, TargetReg(kArg1));
316 DCHECK_EQ(arg1.wide, 0U);
317 LoadValueDirectFixed(arg2, TargetReg(kArg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000318 ClobberCallerSave();
Ian Rogersa9a82542013-10-04 11:17:26 -0700319 CallHelper(r_tgt, helper_offset, safepoint_pc);
320}
321
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322/*
323 * If there are any ins passed in registers that have not been promoted
324 * to a callee-save register, flush them to the frame. Perform intial
325 * assignment of promoted arguments.
326 *
327 * ArgLocs is an array of location records describing the incoming arguments
328 * with one location record per word of argument.
329 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700330void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 /*
332 * Dummy up a RegLocation for the incoming Method*
333 * It will attempt to keep kArg0 live (or copy it to home location
334 * if promoted).
335 */
336 RegLocation rl_src = rl_method;
337 rl_src.location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800338 rl_src.reg = TargetReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 rl_src.home = false;
buzbee2700f7e2014-03-07 09:46:20 -0800340 MarkLive(rl_src.reg, rl_src.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 StoreValue(rl_method, rl_src);
342 // If Method* has been promoted, explicitly flush
343 if (rl_method.location == kLocPhysReg) {
344 StoreWordDisp(TargetReg(kSp), 0, TargetReg(kArg0));
345 }
346
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800347 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800349 }
350
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
352 /*
353 * Copy incoming arguments to their proper home locations.
354 * NOTE: an older version of dx had an issue in which
355 * it would reuse static method argument registers.
356 * This could result in the same Dalvik virtual register
357 * being promoted to both core and fp regs. To account for this,
358 * we only copy to the corresponding promoted physical register
359 * if it matches the type of the SSA name for the incoming
360 * argument. It is also possible that long and double arguments
361 * end up half-promoted. In those cases, we must flush the promoted
362 * half to memory as well.
363 */
364 for (int i = 0; i < cu_->num_ins; i++) {
365 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800366 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800367
buzbee2700f7e2014-03-07 09:46:20 -0800368 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 // If arriving in register
370 bool need_flush = true;
371 RegLocation* t_loc = &ArgLocs[i];
372 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800373 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 need_flush = false;
375 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800376 OpRegCopy(RegStorage::Solo32(v_map->FpReg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 need_flush = false;
378 } else {
379 need_flush = true;
380 }
381
buzbeed0a03b82013-09-14 08:21:05 -0700382 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 if (t_loc->wide) {
384 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700385 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 need_flush |= (p_map->core_location != v_map->core_location) ||
387 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700388 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
389 /*
390 * In Arm, a double is represented as a pair of consecutive single float
391 * registers starting at an even number. It's possible that both Dalvik vRegs
392 * representing the incoming double were independently promoted as singles - but
393 * not in a form usable as a double. If so, we need to flush - even though the
394 * incoming arg appears fully in register. At this point in the code, both
395 * halves of the double are promoted. Make sure they are in a usable form.
396 */
397 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
398 int low_reg = promotion_map_[lowreg_index].FpReg;
399 int high_reg = promotion_map_[lowreg_index + 1].FpReg;
400 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
401 need_flush = true;
402 }
403 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 }
405 if (need_flush) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800406 StoreBaseDisp(TargetReg(kSp), SRegOffset(start_vreg + i), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 }
408 } else {
409 // If arriving in frame & promoted
410 if (v_map->core_location == kLocPhysReg) {
411 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i),
buzbee2700f7e2014-03-07 09:46:20 -0800412 RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 }
414 if (v_map->fp_location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800415 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->FpReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 }
417 }
418 }
419}
420
421/*
422 * Bit of a hack here - in the absence of a real scheduling pass,
423 * emit the next instruction in static & direct invoke sequences.
424 */
425static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
426 int state, const MethodReference& target_method,
427 uint32_t unused,
428 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700429 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 if (direct_code != 0 && direct_method != 0) {
432 switch (state) {
433 case 0: // Get the current Method* [sets kArg0]
434 if (direct_code != static_cast<unsigned int>(-1)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700435 if (cu->instruction_set != kX86) {
436 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
437 }
Mark Mendell55d0eac2014-02-06 11:02:52 -0800438 } else if (cu->instruction_set != kX86) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700439 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 }
441 if (direct_method != static_cast<unsigned int>(-1)) {
442 cg->LoadConstant(cg->TargetReg(kArg0), direct_method);
443 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700444 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 }
446 break;
447 default:
448 return -1;
449 }
450 } else {
451 switch (state) {
452 case 0: // Get the current Method* [sets kArg0]
453 // TUNING: we can save a reg copy if Method* has been promoted.
454 cg->LoadCurrMethodDirect(cg->TargetReg(kArg0));
455 break;
456 case 1: // Get method->dex_cache_resolved_methods_
457 cg->LoadWordDisp(cg->TargetReg(kArg0),
buzbee2700f7e2014-03-07 09:46:20 -0800458 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
459 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 // Set up direct code if known.
461 if (direct_code != 0) {
462 if (direct_code != static_cast<unsigned int>(-1)) {
463 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800464 } else if (cu->instruction_set != kX86) {
Ian Rogers83883d72013-10-21 21:07:24 -0700465 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700466 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 }
468 }
469 break;
470 case 2: // Grab target method*
471 CHECK_EQ(cu->dex_file, target_method.dex_file);
472 cg->LoadWordDisp(cg->TargetReg(kArg0),
473 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbee2700f7e2014-03-07 09:46:20 -0800474 (target_method.dex_method_index * 4), cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 break;
476 case 3: // Grab the code from the method*
477 if (cu->instruction_set != kX86) {
478 if (direct_code == 0) {
479 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800480 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 cg->TargetReg(kInvokeTgt));
482 }
483 break;
484 }
485 // Intentional fallthrough for x86
486 default:
487 return -1;
488 }
489 }
490 return state + 1;
491}
492
493/*
494 * Bit of a hack here - in the absence of a real scheduling pass,
495 * emit the next instruction in a virtual invoke sequence.
496 * We can use kLr as a temp prior to target address loading
497 * Note also that we'll load the first argument ("this") into
498 * kArg1 here rather than the standard LoadArgRegs.
499 */
500static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
501 int state, const MethodReference& target_method,
502 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700503 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
505 /*
506 * This is the fast path in which the target virtual method is
507 * fully resolved at compile time.
508 */
509 switch (state) {
510 case 0: { // Get "this" [set kArg1]
511 RegLocation rl_arg = info->args[0];
512 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
513 break;
514 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700515 case 1: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800516 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 // get this->klass_ [use kArg1, set kInvokeTgt]
518 cg->LoadWordDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
519 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800520 cg->MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700522 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
524 cg->TargetReg(kInvokeTgt));
525 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700526 case 3: // Get target method [use kInvokeTgt, set kArg0]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), (method_idx * 4) +
528 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
529 cg->TargetReg(kArg0));
530 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700531 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 if (cu->instruction_set != kX86) {
533 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800534 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 cg->TargetReg(kInvokeTgt));
536 break;
537 }
538 // Intentional fallthrough for X86
539 default:
540 return -1;
541 }
542 return state + 1;
543}
544
545/*
Jeff Hao88474b42013-10-23 16:24:40 -0700546 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
547 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
548 * more than one interface method map to the same index. Note also that we'll load the first
549 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 */
551static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
552 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700553 uint32_t method_idx, uintptr_t unused,
554 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556
Jeff Hao88474b42013-10-23 16:24:40 -0700557 switch (state) {
558 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700559 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
560 cg->LoadConstant(cg->TargetReg(kHiddenArg), target_method.dex_method_index);
561 if (cu->instruction_set == kX86) {
562 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg), cg->TargetReg(kHiddenArg));
563 }
564 break;
565 case 1: { // Get "this" [set kArg1]
566 RegLocation rl_arg = info->args[0];
567 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
568 break;
569 }
570 case 2: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800571 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700572 // Get this->klass_ [use kArg1, set kInvokeTgt]
573 cg->LoadWordDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
574 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800575 cg->MarkPossibleNullPointerException(info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700576 break;
577 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
578 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
579 cg->TargetReg(kInvokeTgt));
580 break;
581 case 4: // Get target method [use kInvokeTgt, set kArg0]
582 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), ((method_idx % ClassLinker::kImtSize) * 4) +
583 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 cg->TargetReg(kArg0));
585 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700586 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
587 if (cu->instruction_set != kX86) {
588 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800589 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Jeff Hao88474b42013-10-23 16:24:40 -0700590 cg->TargetReg(kInvokeTgt));
591 break;
592 }
593 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 default:
595 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 }
597 return state + 1;
598}
599
Ian Rogers848871b2013-08-05 10:56:33 -0700600static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700602 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
604 /*
605 * This handles the case in which the base method is not fully
606 * resolved at compile time, we bail to a runtime helper.
607 */
608 if (state == 0) {
609 if (cu->instruction_set != kX86) {
610 // Load trampoline target
Ian Rogers848871b2013-08-05 10:56:33 -0700611 cg->LoadWordDisp(cg->TargetReg(kSelf), trampoline.Int32Value(), cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 }
613 // Load kArg0 with method index
614 CHECK_EQ(cu->dex_file, target_method.dex_file);
615 cg->LoadConstant(cg->TargetReg(kArg0), target_method.dex_method_index);
616 return 1;
617 }
618 return -1;
619}
620
621static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
622 int state,
623 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000624 uint32_t unused, uintptr_t unused2,
625 uintptr_t unused3, InvokeType unused4) {
Ian Rogers848871b2013-08-05 10:56:33 -0700626 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
628}
629
630static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
631 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000632 uint32_t unused, uintptr_t unused2,
633 uintptr_t unused3, InvokeType unused4) {
Ian Rogers848871b2013-08-05 10:56:33 -0700634 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
636}
637
638static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
639 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000640 uint32_t unused, uintptr_t unused2,
641 uintptr_t unused3, InvokeType unused4) {
Ian Rogers848871b2013-08-05 10:56:33 -0700642 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
644}
645
646static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
647 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000648 uint32_t unused, uintptr_t unused2,
649 uintptr_t unused3, InvokeType unused4) {
Ian Rogers848871b2013-08-05 10:56:33 -0700650 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
652}
653
654static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
655 CallInfo* info, int state,
656 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000657 uint32_t unused, uintptr_t unused2,
658 uintptr_t unused3, InvokeType unused4) {
Ian Rogers848871b2013-08-05 10:56:33 -0700659 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
661}
662
663int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
664 NextCallInsn next_call_insn,
665 const MethodReference& target_method,
666 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700667 uintptr_t direct_method, InvokeType type, bool skip_this) {
buzbee2700f7e2014-03-07 09:46:20 -0800668 int last_arg_reg = TargetReg(kArg3).GetReg();
669 int next_reg = TargetReg(kArg1).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 int next_arg = 0;
671 if (skip_this) {
672 next_reg++;
673 next_arg++;
674 }
675 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
676 RegLocation rl_arg = info->args[next_arg++];
677 rl_arg = UpdateRawLoc(rl_arg);
buzbee2700f7e2014-03-07 09:46:20 -0800678 if (rl_arg.wide && (next_reg <= TargetReg(kArg2).GetReg())) {
679 RegStorage r_tmp(RegStorage::k64BitPair, next_reg, next_reg + 1);
680 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 next_reg++;
682 next_arg++;
683 } else {
684 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800685 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 rl_arg.is_const = false;
687 }
buzbee2700f7e2014-03-07 09:46:20 -0800688 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(next_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 }
690 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
691 direct_code, direct_method, type);
692 }
693 return call_state;
694}
695
696/*
697 * Load up to 5 arguments, the first three of which will be in
698 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
699 * and as part of the load sequence, it must be replaced with
700 * the target method pointer. Note, this may also be called
701 * for "range" variants if the number of arguments is 5 or fewer.
702 */
703int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
704 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
705 const MethodReference& target_method,
706 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700707 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 RegLocation rl_arg;
709
710 /* If no arguments, just return */
711 if (info->num_arg_words == 0)
712 return call_state;
713
714 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
715 direct_code, direct_method, type);
716
717 DCHECK_LE(info->num_arg_words, 5);
718 if (info->num_arg_words > 3) {
719 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700720 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 RegLocation rl_use0 = info->args[0];
722 RegLocation rl_use1 = info->args[1];
723 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800724 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
725 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 // Wide spans, we need the 2nd half of uses[2].
727 rl_arg = UpdateLocWide(rl_use2);
728 if (rl_arg.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800729 reg = rl_arg.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 } else {
731 // kArg2 & rArg3 can safely be used here
732 reg = TargetReg(kArg3);
733 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
734 call_state = next_call_insn(cu_, info, call_state, target_method,
735 vtable_idx, direct_code, direct_method, type);
736 }
737 StoreBaseDisp(TargetReg(kSp), (next_use + 1) * 4, reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
739 direct_code, direct_method, type);
740 next_use++;
741 }
742 // Loop through the rest
743 while (next_use < info->num_arg_words) {
buzbee2700f7e2014-03-07 09:46:20 -0800744 RegStorage low_reg;
745 RegStorage high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 rl_arg = info->args[next_use];
747 rl_arg = UpdateRawLoc(rl_arg);
748 if (rl_arg.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000749 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800750 low_reg = rl_arg.reg.GetLow();
751 high_reg = rl_arg.reg.GetHigh();
752 } else {
753 low_reg = rl_arg.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000754 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700755 } else {
756 low_reg = TargetReg(kArg2);
757 if (rl_arg.wide) {
758 high_reg = TargetReg(kArg3);
buzbee2700f7e2014-03-07 09:46:20 -0800759 LoadValueDirectWideFixed(rl_arg, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 } else {
761 LoadValueDirectFixed(rl_arg, low_reg);
762 }
763 call_state = next_call_insn(cu_, info, call_state, target_method,
764 vtable_idx, direct_code, direct_method, type);
765 }
766 int outs_offset = (next_use + 1) * 4;
767 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800768 StoreBaseDispWide(TargetReg(kSp), outs_offset, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 next_use += 2;
770 } else {
771 StoreWordDisp(TargetReg(kSp), outs_offset, low_reg);
772 next_use++;
773 }
774 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
775 direct_code, direct_method, type);
776 }
777 }
778
779 call_state = LoadArgRegs(info, call_state, next_call_insn,
780 target_method, vtable_idx, direct_code, direct_method,
781 type, skip_this);
782
783 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700784 if (Runtime::Current()->ExplicitNullChecks()) {
785 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
786 } else {
787 *pcrLabel = nullptr;
788 // In lieu of generating a check for kArg1 being null, we need to
789 // perform a load when doing implicit checks.
790 RegStorage tmp = AllocTemp();
791 LoadWordDisp(TargetReg(kArg1), 0, tmp);
792 MarkPossibleNullPointerException(info->opt_flags);
793 FreeTemp(tmp);
794 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 }
796 return call_state;
797}
798
799/*
800 * May have 0+ arguments (also used for jumbo). Note that
801 * source virtual registers may be in physical registers, so may
802 * need to be flushed to home location before copying. This
803 * applies to arg3 and above (see below).
804 *
805 * Two general strategies:
806 * If < 20 arguments
807 * Pass args 3-18 using vldm/vstm block copy
808 * Pass arg0, arg1 & arg2 in kArg1-kArg3
809 * If 20+ arguments
810 * Pass args arg19+ using memcpy block copy
811 * Pass arg0, arg1 & arg2 in kArg1-kArg3
812 *
813 */
814int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
815 LIR** pcrLabel, NextCallInsn next_call_insn,
816 const MethodReference& target_method,
817 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700818 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 // If we can treat it as non-range (Jumbo ops will use range form)
820 if (info->num_arg_words <= 5)
821 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
822 next_call_insn, target_method, vtable_idx,
823 direct_code, direct_method, type, skip_this);
824 /*
825 * First load the non-register arguments. Both forms expect all
826 * of the source arguments to be in their home frame location, so
827 * scan the s_reg names and flush any that have been promoted to
828 * frame backing storage.
829 */
830 // Scan the rest of the args - if in phys_reg flush to memory
831 for (int next_arg = 0; next_arg < info->num_arg_words;) {
832 RegLocation loc = info->args[next_arg];
833 if (loc.wide) {
834 loc = UpdateLocWide(loc);
835 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800836 StoreBaseDispWide(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 }
838 next_arg += 2;
839 } else {
840 loc = UpdateLoc(loc);
841 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800842 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 }
844 next_arg++;
845 }
846 }
847
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800848 // Logic below assumes that Method pointer is at offset zero from SP.
849 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
850
851 // The first 3 arguments are passed via registers.
852 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
853 // get size of uintptr_t or size of object reference according to model being used.
854 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800856 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
857 DCHECK_GT(regs_left_to_pass_via_stack, 0);
858
859 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
860 // Use vldm/vstm pair using kArg3 as a temp
861 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
862 direct_code, direct_method, type);
863 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
864 LIR* ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
865 // TUNING: loosen barrier
866 ld->u.m.def_mask = ENCODE_ALL;
867 SetMemRefType(ld, true /* is_load */, kDalvikReg);
868 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
869 direct_code, direct_method, type);
870 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
871 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
872 direct_code, direct_method, type);
873 LIR* st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
874 SetMemRefType(st, false /* is_load */, kDalvikReg);
875 st->u.m.def_mask = ENCODE_ALL;
876 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
877 direct_code, direct_method, type);
878 } else if (cu_->instruction_set == kX86) {
879 int current_src_offset = start_offset;
880 int current_dest_offset = outs_offset;
881
882 while (regs_left_to_pass_via_stack > 0) {
883 // This is based on the knowledge that the stack itself is 16-byte aligned.
884 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
885 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
886 size_t bytes_to_move;
887
888 /*
889 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
890 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
891 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
892 * We do this because we could potentially do a smaller move to align.
893 */
894 if (regs_left_to_pass_via_stack == 4 ||
895 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
896 // Moving 128-bits via xmm register.
897 bytes_to_move = sizeof(uint32_t) * 4;
898
899 // Allocate a free xmm temp. Since we are working through the calling sequence,
900 // we expect to have an xmm temporary available.
buzbee2700f7e2014-03-07 09:46:20 -0800901 RegStorage temp = AllocTempDouble();
902 CHECK_GT(temp.GetLowReg(), 0);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800903
904 LIR* ld1 = nullptr;
905 LIR* ld2 = nullptr;
906 LIR* st1 = nullptr;
907 LIR* st2 = nullptr;
908
909 /*
910 * The logic is similar for both loads and stores. If we have 16-byte alignment,
911 * do an aligned move. If we have 8-byte alignment, then do the move in two
912 * parts. This approach prevents possible cache line splits. Finally, fall back
913 * to doing an unaligned move. In most cases we likely won't split the cache
914 * line but we cannot prove it and thus take a conservative approach.
915 */
916 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
917 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
918
919 if (src_is_16b_aligned) {
920 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
921 } else if (src_is_8b_aligned) {
922 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800923 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
924 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800925 } else {
926 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
927 }
928
929 if (dest_is_16b_aligned) {
930 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
931 } else if (dest_is_8b_aligned) {
932 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800933 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
934 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800935 } else {
936 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
937 }
938
939 // TODO If we could keep track of aliasing information for memory accesses that are wider
940 // than 64-bit, we wouldn't need to set up a barrier.
941 if (ld1 != nullptr) {
942 if (ld2 != nullptr) {
943 // For 64-bit load we can actually set up the aliasing information.
944 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
945 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
946 } else {
947 // Set barrier for 128-bit load.
948 SetMemRefType(ld1, true /* is_load */, kDalvikReg);
949 ld1->u.m.def_mask = ENCODE_ALL;
950 }
951 }
952 if (st1 != nullptr) {
953 if (st2 != nullptr) {
954 // For 64-bit store we can actually set up the aliasing information.
955 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
956 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
957 } else {
958 // Set barrier for 128-bit store.
959 SetMemRefType(st1, false /* is_load */, kDalvikReg);
960 st1->u.m.def_mask = ENCODE_ALL;
961 }
962 }
963
964 // Free the temporary used for the data movement.
buzbee2700f7e2014-03-07 09:46:20 -0800965 // CLEANUP: temp is currently a bogus pair, elmiminate extra free when updated.
966 FreeTemp(temp.GetLow());
967 FreeTemp(temp.GetHigh());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800968 } else {
969 // Moving 32-bits via general purpose register.
970 bytes_to_move = sizeof(uint32_t);
971
972 // Instead of allocating a new temp, simply reuse one of the registers being used
973 // for argument passing.
buzbee2700f7e2014-03-07 09:46:20 -0800974 RegStorage temp = TargetReg(kArg3);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800975
976 // Now load the argument VR and store to the outs.
977 LoadWordDisp(TargetReg(kSp), current_src_offset, temp);
978 StoreWordDisp(TargetReg(kSp), current_dest_offset, temp);
979 }
980
981 current_src_offset += bytes_to_move;
982 current_dest_offset += bytes_to_move;
983 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
984 }
985 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 // Generate memcpy
987 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
988 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Ian Rogers7655f292013-07-29 11:07:13 -0700989 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(pMemcpy), TargetReg(kArg0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 }
992
993 call_state = LoadArgRegs(info, call_state, next_call_insn,
994 target_method, vtable_idx, direct_code, direct_method,
995 type, skip_this);
996
997 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
998 direct_code, direct_method, type);
999 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -07001000 if (Runtime::Current()->ExplicitNullChecks()) {
1001 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
1002 } else {
1003 *pcrLabel = nullptr;
1004 // In lieu of generating a check for kArg1 being null, we need to
1005 // perform a load when doing implicit checks.
1006 RegStorage tmp = AllocTemp();
1007 LoadWordDisp(TargetReg(kArg1), 0, tmp);
1008 MarkPossibleNullPointerException(info->opt_flags);
1009 FreeTemp(tmp);
1010 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 }
1012 return call_state;
1013}
1014
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001015RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 RegLocation res;
1017 if (info->result.location == kLocInvalid) {
1018 res = GetReturn(false);
1019 } else {
1020 res = info->result;
1021 }
1022 return res;
1023}
1024
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001025RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 RegLocation res;
1027 if (info->result.location == kLocInvalid) {
1028 res = GetReturnWide(false);
1029 } else {
1030 res = info->result;
1031 }
1032 return res;
1033}
1034
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001035bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 if (cu_->instruction_set == kMips) {
1037 // TODO - add Mips implementation
1038 return false;
1039 }
1040 // Location of reference to data array
1041 int value_offset = mirror::String::ValueOffset().Int32Value();
1042 // Location of count
1043 int count_offset = mirror::String::CountOffset().Int32Value();
1044 // Starting offset within data array
1045 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1046 // Start of char data with array_
1047 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1048
1049 RegLocation rl_obj = info->args[0];
1050 RegLocation rl_idx = info->args[1];
1051 rl_obj = LoadValue(rl_obj, kCoreReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001052 // X86 wants to avoid putting a constant index into a register.
1053 if (!(cu_->instruction_set == kX86 && rl_idx.is_const)) {
1054 rl_idx = LoadValue(rl_idx, kCoreReg);
1055 }
buzbee2700f7e2014-03-07 09:46:20 -08001056 RegStorage reg_max;
1057 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001059 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001060 RegStorage reg_off;
1061 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 if (cu_->instruction_set != kX86) {
1063 reg_off = AllocTemp();
1064 reg_ptr = AllocTemp();
1065 if (range_check) {
1066 reg_max = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001067 LoadWordDisp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001068 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069 }
buzbee2700f7e2014-03-07 09:46:20 -08001070 LoadWordDisp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001071 MarkPossibleNullPointerException(info->opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -08001072 LoadWordDisp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 if (range_check) {
1074 // Set up a launch pad to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001075 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001077 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001078 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001079 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 } else {
1081 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001082 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001084 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001085 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001086 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Vladimir Marko3bc86152014-03-13 14:11:28 +00001087 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001088 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001089 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001090 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001091 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 }
1093 reg_off = AllocTemp();
1094 reg_ptr = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001095 LoadWordDisp(rl_obj.reg, offset_offset, reg_off);
1096 LoadWordDisp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001098 if (rl_idx.is_const) {
1099 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1100 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001101 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001102 }
buzbee2700f7e2014-03-07 09:46:20 -08001103 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001104 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001105 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001106 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 RegLocation rl_dest = InlineTarget(info);
1108 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001109 if (cu_->instruction_set != kX86) {
buzbee2700f7e2014-03-07 09:46:20 -08001110 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001111 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001112 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg,
1113 RegStorage::InvalidReg(), kUnsignedHalf, INVALID_SREG);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001114 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 FreeTemp(reg_off);
1116 FreeTemp(reg_ptr);
1117 StoreValue(rl_dest, rl_result);
1118 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001119 DCHECK(range_check_branch != nullptr);
1120 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
1121 AddIntrinsicLaunchpad(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 return true;
1124}
1125
1126// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001127bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 if (cu_->instruction_set == kMips) {
1129 // TODO - add Mips implementation
1130 return false;
1131 }
1132 // dst = src.length();
1133 RegLocation rl_obj = info->args[0];
1134 rl_obj = LoadValue(rl_obj, kCoreReg);
1135 RegLocation rl_dest = InlineTarget(info);
1136 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001137 GenNullCheck(rl_obj.reg, info->opt_flags);
1138 LoadWordDisp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001139 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 if (is_empty) {
1141 // dst = (dst == 0);
1142 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001143 RegStorage t_reg = AllocTemp();
1144 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1145 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 } else {
1147 DCHECK_EQ(cu_->instruction_set, kX86);
buzbee2700f7e2014-03-07 09:46:20 -08001148 OpRegImm(kOpSub, rl_result.reg, 1);
1149 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 }
1151 }
1152 StoreValue(rl_dest, rl_result);
1153 return true;
1154}
1155
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001156bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1157 if (cu_->instruction_set == kMips) {
1158 // TODO - add Mips implementation
1159 return false;
1160 }
1161 RegLocation rl_src_i = info->args[0];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001162 RegLocation rl_dest = (size == kLong) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001163 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1164 if (size == kLong) {
1165 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001166 RegStorage r_i_low = rl_i.reg.GetLow();
1167 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001168 // 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 +00001169 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001170 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001171 }
buzbee2700f7e2014-03-07 09:46:20 -08001172 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1173 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1174 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001175 FreeTemp(r_i_low);
1176 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001177 StoreValueWide(rl_dest, rl_result);
1178 } else {
1179 DCHECK(size == kWord || size == kSignedHalf);
1180 OpKind op = (size == kWord) ? kOpRev : kOpRevsh;
1181 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001182 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001183 StoreValue(rl_dest, rl_result);
1184 }
1185 return true;
1186}
1187
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001188bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 if (cu_->instruction_set == kMips) {
1190 // TODO - add Mips implementation
1191 return false;
1192 }
1193 RegLocation rl_src = info->args[0];
1194 rl_src = LoadValue(rl_src, kCoreReg);
1195 RegLocation rl_dest = InlineTarget(info);
1196 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001197 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001199 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1200 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1201 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 StoreValue(rl_dest, rl_result);
1203 return true;
1204}
1205
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001206bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001207 if (cu_->instruction_set == kMips) {
1208 // TODO - add Mips implementation
1209 return false;
1210 }
Vladimir Markob9823312014-03-20 17:38:43 +00001211 RegLocation rl_src = info->args[0];
1212 rl_src = LoadValueWide(rl_src, kCoreReg);
1213 RegLocation rl_dest = InlineTargetWide(info);
1214 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1215
1216 // If on x86 or if we would clobber a register needed later, just copy the source first.
buzbee2700f7e2014-03-07 09:46:20 -08001217 if (cu_->instruction_set == kX86 || rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1218 OpRegCopyWide(rl_result.reg, rl_src.reg);
1219 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1220 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1221 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001222 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1223 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001224 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001225 }
1226 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 }
Vladimir Markob9823312014-03-20 17:38:43 +00001228
1229 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001230 RegStorage sign_reg = AllocTemp();
1231 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1232 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1233 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1234 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1235 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001236 StoreValueWide(rl_dest, rl_result);
1237 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238}
1239
Yixin Shoudbb17e32014-02-07 05:09:30 -08001240bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1241 if (cu_->instruction_set == kMips) {
1242 // TODO - add Mips implementation
1243 return false;
1244 }
1245 RegLocation rl_src = info->args[0];
1246 rl_src = LoadValue(rl_src, kCoreReg);
1247 RegLocation rl_dest = InlineTarget(info);
1248 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001249 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001250 StoreValue(rl_dest, rl_result);
1251 return true;
1252}
1253
1254bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1255 if (cu_->instruction_set == kMips) {
1256 // TODO - add Mips implementation
1257 return false;
1258 }
1259 RegLocation rl_src = info->args[0];
1260 rl_src = LoadValueWide(rl_src, kCoreReg);
1261 RegLocation rl_dest = InlineTargetWide(info);
1262 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001263 OpRegCopyWide(rl_result.reg, rl_src.reg);
1264 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001265 StoreValueWide(rl_dest, rl_result);
1266 return true;
1267}
1268
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001269bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001270 if (cu_->instruction_set == kMips) {
1271 // TODO - add Mips implementation
1272 return false;
1273 }
1274 RegLocation rl_src = info->args[0];
1275 RegLocation rl_dest = InlineTarget(info);
1276 StoreValue(rl_dest, rl_src);
1277 return true;
1278}
1279
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001280bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 if (cu_->instruction_set == kMips) {
1282 // TODO - add Mips implementation
1283 return false;
1284 }
1285 RegLocation rl_src = info->args[0];
1286 RegLocation rl_dest = InlineTargetWide(info);
1287 StoreValueWide(rl_dest, rl_src);
1288 return true;
1289}
1290
1291/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001292 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 * otherwise bails to standard library code.
1294 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001295bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 if (cu_->instruction_set == kMips) {
1297 // TODO - add Mips implementation
1298 return false;
1299 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001300 RegLocation rl_obj = info->args[0];
1301 RegLocation rl_char = info->args[1];
1302 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1303 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1304 return false;
1305 }
1306
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001307 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001309 RegStorage reg_ptr = TargetReg(kArg0);
1310 RegStorage reg_char = TargetReg(kArg1);
1311 RegStorage reg_start = TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 LoadValueDirectFixed(rl_obj, reg_ptr);
1314 LoadValueDirectFixed(rl_char, reg_char);
1315 if (zero_based) {
1316 LoadConstant(reg_start, 0);
1317 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001318 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001319 LoadValueDirectFixed(rl_start, reg_start);
1320 }
buzbee2700f7e2014-03-07 09:46:20 -08001321 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001322 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001323 LIR* high_code_point_branch =
1324 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001326 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001327 if (!rl_char.is_const) {
1328 // Add the slow path for code points beyond 0xFFFF.
1329 DCHECK(high_code_point_branch != nullptr);
1330 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1331 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
1332 AddIntrinsicLaunchpad(info, high_code_point_branch, resume_tgt);
1333 } else {
1334 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1335 DCHECK(high_code_point_branch == nullptr);
1336 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 RegLocation rl_return = GetReturn(false);
1338 RegLocation rl_dest = InlineTarget(info);
1339 StoreValue(rl_dest, rl_return);
1340 return true;
1341}
1342
1343/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001344bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 if (cu_->instruction_set == kMips) {
1346 // TODO - add Mips implementation
1347 return false;
1348 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001349 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001351 RegStorage reg_this = TargetReg(kArg0);
1352 RegStorage reg_cmp = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353
1354 RegLocation rl_this = info->args[0];
1355 RegLocation rl_cmp = info->args[1];
1356 LoadValueDirectFixed(rl_this, reg_this);
1357 LoadValueDirectFixed(rl_cmp, reg_cmp);
buzbee2700f7e2014-03-07 09:46:20 -08001358 RegStorage r_tgt = (cu_->instruction_set != kX86) ?
1359 LoadHelper(QUICK_ENTRYPOINT_OFFSET(pStringCompareTo)) : RegStorage::InvalidReg();
Dave Allisonf9439142014-03-27 15:10:22 -07001360 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001361 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001362 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001363 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
1364 AddIntrinsicLaunchpad(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 // NOTE: not a safepoint
1366 if (cu_->instruction_set != kX86) {
1367 OpReg(kOpBlx, r_tgt);
1368 } else {
Ian Rogers7655f292013-07-29 11:07:13 -07001369 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(pStringCompareTo));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 RegLocation rl_return = GetReturn(false);
1372 RegLocation rl_dest = InlineTarget(info);
1373 StoreValue(rl_dest, rl_return);
1374 return true;
1375}
1376
1377bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1378 RegLocation rl_dest = InlineTarget(info);
1379 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001380 ThreadOffset offset = Thread::PeerOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 if (cu_->instruction_set == kThumb2 || cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -08001382 LoadWordDisp(TargetReg(kSelf), offset.Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001383 } else {
1384 CHECK(cu_->instruction_set == kX86);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001385 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg.GetReg(), offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 }
1387 StoreValue(rl_dest, rl_result);
1388 return true;
1389}
1390
1391bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1392 bool is_long, bool is_volatile) {
1393 if (cu_->instruction_set == kMips) {
1394 // TODO - add Mips implementation
1395 return false;
1396 }
1397 // Unused - RegLocation rl_src_unsafe = info->args[0];
1398 RegLocation rl_src_obj = info->args[1]; // Object
1399 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001400 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001401 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001402
Brian Carlstrom7940e442013-07-12 13:46:57 -07001403 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1404 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1405 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1406 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -08001407 OpRegReg(kOpAdd, rl_object.reg, rl_offset.reg);
1408 LoadBaseDispWide(rl_object.reg, 0, rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001410 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, kWord);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001411 }
1412
1413 if (is_volatile) {
1414 // Without context sensitive analysis, we must issue the most conservative barriers.
1415 // In this case, either a load or store may follow so we issue both barriers.
1416 GenMemBarrier(kLoadLoad);
1417 GenMemBarrier(kLoadStore);
1418 }
1419
1420 if (is_long) {
1421 StoreValueWide(rl_dest, rl_result);
1422 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001423 StoreValue(rl_dest, rl_result);
1424 }
1425 return true;
1426}
1427
1428bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1429 bool is_object, bool is_volatile, bool is_ordered) {
1430 if (cu_->instruction_set == kMips) {
1431 // TODO - add Mips implementation
1432 return false;
1433 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 // Unused - RegLocation rl_src_unsafe = info->args[0];
1435 RegLocation rl_src_obj = info->args[1]; // Object
1436 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001437 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 RegLocation rl_src_value = info->args[4]; // value to store
1439 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001440 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 GenMemBarrier(kStoreStore);
1442 }
1443 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1444 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1445 RegLocation rl_value;
1446 if (is_long) {
1447 rl_value = LoadValueWide(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001448 OpRegReg(kOpAdd, rl_object.reg, rl_offset.reg);
1449 StoreBaseDispWide(rl_object.reg, 0, rl_value.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001450 } else {
1451 rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001452 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001453 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001454
1455 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001456 FreeTemp(rl_offset.reg.GetReg());
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001457
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001459 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 GenMemBarrier(kStoreLoad);
1461 }
1462 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001463 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 }
1465 return true;
1466}
1467
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001468void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001469 if ((info->opt_flags & MIR_INLINED) != 0) {
1470 // Already inlined but we may still need the null check.
1471 if (info->type != kStatic &&
1472 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1473 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
1474 RegLocation rl_obj = LoadValue(info->args[0], kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001475 GenImmedCheck(kCondEq, rl_obj.reg, 0, kThrowNullPointer);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001476 }
1477 return;
1478 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001479 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1480 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1481 ->GenIntrinsic(this, info)) {
1482 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001484 GenInvokeNoInline(info);
1485}
1486
1487void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001488 int call_state = 0;
1489 LIR* null_ck;
1490 LIR** p_null_ck = NULL;
1491 NextCallInsn next_call_insn;
1492 FlushAllRegs(); /* Everything to home location */
1493 // Explicit register usage
1494 LockCallTemps();
1495
Vladimir Markof096aad2014-01-23 15:51:58 +00001496 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1497 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1498 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1499 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1500 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001504 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 } else if (info->type == kDirect) {
1506 if (fast_path) {
1507 p_null_ck = &null_ck;
1508 }
1509 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1510 skip_this = false;
1511 } else if (info->type == kStatic) {
1512 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1513 skip_this = false;
1514 } else if (info->type == kSuper) {
1515 DCHECK(!fast_path); // Fast path is a direct call.
1516 next_call_insn = NextSuperCallInsnSP;
1517 skip_this = false;
1518 } else {
1519 DCHECK_EQ(info->type, kVirtual);
1520 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1521 skip_this = fast_path;
1522 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001523 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 if (!info->is_range) {
1525 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001526 next_call_insn, target_method, method_info.VTableIndex(),
1527 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 original_type, skip_this);
1529 } else {
1530 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001531 next_call_insn, target_method, method_info.VTableIndex(),
1532 method_info.DirectCode(), method_info.DirectMethod(),
1533 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001534 }
1535 // Finish up any of the call sequence not interleaved in arg loading
1536 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001537 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1538 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001539 }
1540 LIR* call_inst;
1541 if (cu_->instruction_set != kX86) {
1542 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1543 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001544 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001545 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001546 // We can have the linker fixup a call relative.
1547 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001548 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001549 } else {
1550 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1551 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1552 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001554 ThreadOffset trampoline(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 switch (info->type) {
1556 case kInterface:
Jeff Hao88474b42013-10-23 16:24:40 -07001557 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001558 break;
1559 case kDirect:
Ian Rogers7655f292013-07-29 11:07:13 -07001560 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001561 break;
1562 case kStatic:
Ian Rogers7655f292013-07-29 11:07:13 -07001563 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 break;
1565 case kSuper:
Ian Rogers7655f292013-07-29 11:07:13 -07001566 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001567 break;
1568 case kVirtual:
Ian Rogers7655f292013-07-29 11:07:13 -07001569 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 break;
1571 default:
1572 LOG(FATAL) << "Unexpected invoke type";
1573 }
1574 call_inst = OpThreadMem(kOpBlx, trampoline);
1575 }
1576 }
1577 MarkSafepointPC(call_inst);
1578
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001579 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 if (info->result.location != kLocInvalid) {
1581 // We have a following MOVE_RESULT - do it now.
1582 if (info->result.wide) {
1583 RegLocation ret_loc = GetReturnWide(info->result.fp);
1584 StoreValueWide(info->result, ret_loc);
1585 } else {
1586 RegLocation ret_loc = GetReturn(info->result.fp);
1587 StoreValue(info->result, ret_loc);
1588 }
1589 }
1590}
1591
1592} // namespace art