blob: 4e18278865dcd482193f83ce8ea38e7288670099 [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
37/*
38 * To save scheduling time, helper calls are broken into two parts: generation of
39 * the helper target address, and the actuall call to the helper. Because x86
40 * has a memory call operation, part 1 is a NOP for x86. For other targets,
41 * load arguments between the two parts.
42 */
Ian Rogers848871b2013-08-05 10:56:33 -070043int Mir2Lir::CallHelperSetup(ThreadOffset helper_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 return (cu_->instruction_set == kX86) ? 0 : LoadHelper(helper_offset);
45}
46
47/* NOTE: if r_tgt is a temp, it will be freed following use */
Ian Rogers848871b2013-08-05 10:56:33 -070048LIR* Mir2Lir::CallHelper(int r_tgt, ThreadOffset helper_offset, bool safepoint_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 LIR* call_inst;
50 if (cu_->instruction_set == kX86) {
51 call_inst = OpThreadMem(kOpBlx, helper_offset);
52 } else {
53 call_inst = OpReg(kOpBlx, r_tgt);
54 FreeTemp(r_tgt);
55 }
56 if (safepoint_pc) {
57 MarkSafepointPC(call_inst);
58 }
59 return call_inst;
60}
61
Ian Rogers848871b2013-08-05 10:56:33 -070062void Mir2Lir::CallRuntimeHelperImm(ThreadOffset helper_offset, int arg0, bool safepoint_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070063 int r_tgt = CallHelperSetup(helper_offset);
64 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +000065 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 CallHelper(r_tgt, helper_offset, safepoint_pc);
67}
68
Ian Rogers848871b2013-08-05 10:56:33 -070069void Mir2Lir::CallRuntimeHelperReg(ThreadOffset helper_offset, int arg0, bool safepoint_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 int r_tgt = CallHelperSetup(helper_offset);
71 OpRegCopy(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +000072 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 CallHelper(r_tgt, helper_offset, safepoint_pc);
74}
75
Ian Rogers848871b2013-08-05 10:56:33 -070076void Mir2Lir::CallRuntimeHelperRegLocation(ThreadOffset helper_offset, RegLocation arg0,
77 bool safepoint_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 int r_tgt = CallHelperSetup(helper_offset);
79 if (arg0.wide == 0) {
80 LoadValueDirectFixed(arg0, TargetReg(kArg0));
81 } else {
82 LoadValueDirectWideFixed(arg0, TargetReg(kArg0), TargetReg(kArg1));
83 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +000084 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070085 CallHelper(r_tgt, helper_offset, safepoint_pc);
86}
87
Ian Rogers848871b2013-08-05 10:56:33 -070088void Mir2Lir::CallRuntimeHelperImmImm(ThreadOffset helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 bool safepoint_pc) {
90 int r_tgt = CallHelperSetup(helper_offset);
91 LoadConstant(TargetReg(kArg0), arg0);
92 LoadConstant(TargetReg(kArg1), arg1);
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
Ian Rogers848871b2013-08-05 10:56:33 -070097void Mir2Lir::CallRuntimeHelperImmRegLocation(ThreadOffset helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 RegLocation arg1, bool safepoint_pc) {
99 int r_tgt = CallHelperSetup(helper_offset);
100 if (arg1.wide == 0) {
101 LoadValueDirectFixed(arg1, TargetReg(kArg1));
102 } else {
103 LoadValueDirectWideFixed(arg1, TargetReg(kArg1), TargetReg(kArg2));
104 }
105 LoadConstant(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 Rogers848871b2013-08-05 10:56:33 -0700110void Mir2Lir::CallRuntimeHelperRegLocationImm(ThreadOffset helper_offset, RegLocation arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 bool safepoint_pc) {
112 int r_tgt = CallHelperSetup(helper_offset);
113 LoadValueDirectFixed(arg0, TargetReg(kArg0));
114 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000115 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 CallHelper(r_tgt, helper_offset, safepoint_pc);
117}
118
Ian Rogers848871b2013-08-05 10:56:33 -0700119void Mir2Lir::CallRuntimeHelperImmReg(ThreadOffset helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 bool safepoint_pc) {
121 int r_tgt = CallHelperSetup(helper_offset);
122 OpRegCopy(TargetReg(kArg1), arg1);
123 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000124 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 CallHelper(r_tgt, helper_offset, safepoint_pc);
126}
127
Ian Rogers848871b2013-08-05 10:56:33 -0700128void Mir2Lir::CallRuntimeHelperRegImm(ThreadOffset helper_offset, int arg0, int arg1,
129 bool safepoint_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 int r_tgt = CallHelperSetup(helper_offset);
131 OpRegCopy(TargetReg(kArg0), arg0);
132 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000133 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 CallHelper(r_tgt, helper_offset, safepoint_pc);
135}
136
Ian Rogers848871b2013-08-05 10:56:33 -0700137void Mir2Lir::CallRuntimeHelperImmMethod(ThreadOffset helper_offset, int arg0, bool safepoint_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 int r_tgt = CallHelperSetup(helper_offset);
139 LoadCurrMethodDirect(TargetReg(kArg1));
140 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000141 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 CallHelper(r_tgt, helper_offset, safepoint_pc);
143}
144
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800145void Mir2Lir::CallRuntimeHelperRegMethod(ThreadOffset helper_offset, int arg0, bool safepoint_pc) {
146 int r_tgt = CallHelperSetup(helper_offset);
147 DCHECK_NE(TargetReg(kArg1), arg0);
148 if (TargetReg(kArg0) != arg0) {
149 OpRegCopy(TargetReg(kArg0), arg0);
150 }
151 LoadCurrMethodDirect(TargetReg(kArg1));
152 ClobberCallerSave();
153 CallHelper(r_tgt, helper_offset, safepoint_pc);
154}
155
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800156void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(ThreadOffset helper_offset, int arg0,
157 RegLocation arg2, bool safepoint_pc) {
158 int r_tgt = CallHelperSetup(helper_offset);
159 DCHECK_NE(TargetReg(kArg1), arg0);
160 if (TargetReg(kArg0) != arg0) {
161 OpRegCopy(TargetReg(kArg0), arg0);
162 }
163 LoadCurrMethodDirect(TargetReg(kArg1));
164 LoadValueDirectFixed(arg2, TargetReg(kArg2));
165 ClobberCallerSave();
166 CallHelper(r_tgt, helper_offset, safepoint_pc);
167}
168
Ian Rogers848871b2013-08-05 10:56:33 -0700169void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(ThreadOffset helper_offset, RegLocation arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 RegLocation arg1, bool safepoint_pc) {
171 int r_tgt = CallHelperSetup(helper_offset);
172 if (arg0.wide == 0) {
173 LoadValueDirectFixed(arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
174 if (arg1.wide == 0) {
175 if (cu_->instruction_set == kMips) {
176 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1));
177 } else {
178 LoadValueDirectFixed(arg1, TargetReg(kArg1));
179 }
180 } else {
181 if (cu_->instruction_set == kMips) {
182 LoadValueDirectWideFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1), arg1.fp ? TargetReg(kFArg3) : TargetReg(kArg2));
183 } else {
184 LoadValueDirectWideFixed(arg1, TargetReg(kArg1), TargetReg(kArg2));
185 }
186 }
187 } else {
188 LoadValueDirectWideFixed(arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0), arg0.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
189 if (arg1.wide == 0) {
190 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2));
191 } else {
192 LoadValueDirectWideFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2), arg1.fp ? TargetReg(kFArg3) : TargetReg(kArg3));
193 }
194 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000195 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 CallHelper(r_tgt, helper_offset, safepoint_pc);
197}
198
Ian Rogers848871b2013-08-05 10:56:33 -0700199void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset helper_offset, int arg0, int arg1,
200 bool safepoint_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 int r_tgt = CallHelperSetup(helper_offset);
202 DCHECK_NE(TargetReg(kArg0), arg1); // check copy into arg0 won't clobber arg1
203 OpRegCopy(TargetReg(kArg0), arg0);
204 OpRegCopy(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000205 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 CallHelper(r_tgt, helper_offset, safepoint_pc);
207}
208
Ian Rogers848871b2013-08-05 10:56:33 -0700209void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 int arg2, bool safepoint_pc) {
211 int r_tgt = CallHelperSetup(helper_offset);
212 DCHECK_NE(TargetReg(kArg0), arg1); // check copy into arg0 won't clobber arg1
213 OpRegCopy(TargetReg(kArg0), arg0);
214 OpRegCopy(TargetReg(kArg1), arg1);
215 LoadConstant(TargetReg(kArg2), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000216 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 CallHelper(r_tgt, helper_offset, safepoint_pc);
218}
219
Ian Rogers848871b2013-08-05 10:56:33 -0700220void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 int arg0, RegLocation arg2, bool safepoint_pc) {
222 int r_tgt = CallHelperSetup(helper_offset);
223 LoadValueDirectFixed(arg2, TargetReg(kArg2));
224 LoadCurrMethodDirect(TargetReg(kArg1));
225 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000226 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 CallHelper(r_tgt, helper_offset, safepoint_pc);
228}
229
Ian Rogers848871b2013-08-05 10:56:33 -0700230void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 int arg2, bool safepoint_pc) {
232 int r_tgt = CallHelperSetup(helper_offset);
233 LoadCurrMethodDirect(TargetReg(kArg1));
234 LoadConstant(TargetReg(kArg2), arg2);
235 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000236 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 CallHelper(r_tgt, helper_offset, safepoint_pc);
238}
239
Ian Rogers848871b2013-08-05 10:56:33 -0700240void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 int arg0, RegLocation arg1,
242 RegLocation arg2, bool safepoint_pc) {
243 int r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700244 DCHECK_EQ(arg1.wide, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 LoadValueDirectFixed(arg1, TargetReg(kArg1));
246 if (arg2.wide == 0) {
247 LoadValueDirectFixed(arg2, TargetReg(kArg2));
248 } else {
249 LoadValueDirectWideFixed(arg2, TargetReg(kArg2), TargetReg(kArg3));
250 }
251 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000252 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 CallHelper(r_tgt, helper_offset, safepoint_pc);
254}
255
Ian Rogersa9a82542013-10-04 11:17:26 -0700256void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset helper_offset,
257 RegLocation arg0, RegLocation arg1,
258 RegLocation arg2,
259 bool safepoint_pc) {
260 int r_tgt = CallHelperSetup(helper_offset);
261 DCHECK_EQ(arg0.wide, 0U);
262 LoadValueDirectFixed(arg0, TargetReg(kArg0));
263 DCHECK_EQ(arg1.wide, 0U);
264 LoadValueDirectFixed(arg1, TargetReg(kArg1));
265 DCHECK_EQ(arg1.wide, 0U);
266 LoadValueDirectFixed(arg2, TargetReg(kArg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000267 ClobberCallerSave();
Ian Rogersa9a82542013-10-04 11:17:26 -0700268 CallHelper(r_tgt, helper_offset, safepoint_pc);
269}
270
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271/*
272 * If there are any ins passed in registers that have not been promoted
273 * to a callee-save register, flush them to the frame. Perform intial
274 * assignment of promoted arguments.
275 *
276 * ArgLocs is an array of location records describing the incoming arguments
277 * with one location record per word of argument.
278 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700279void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 /*
281 * Dummy up a RegLocation for the incoming Method*
282 * It will attempt to keep kArg0 live (or copy it to home location
283 * if promoted).
284 */
285 RegLocation rl_src = rl_method;
286 rl_src.location = kLocPhysReg;
287 rl_src.low_reg = TargetReg(kArg0);
288 rl_src.home = false;
289 MarkLive(rl_src.low_reg, rl_src.s_reg_low);
290 StoreValue(rl_method, rl_src);
291 // If Method* has been promoted, explicitly flush
292 if (rl_method.location == kLocPhysReg) {
293 StoreWordDisp(TargetReg(kSp), 0, TargetReg(kArg0));
294 }
295
296 if (cu_->num_ins == 0)
297 return;
298 const int num_arg_regs = 3;
299 static SpecialTargetRegister arg_regs[] = {kArg1, kArg2, kArg3};
300 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
301 /*
302 * Copy incoming arguments to their proper home locations.
303 * NOTE: an older version of dx had an issue in which
304 * it would reuse static method argument registers.
305 * This could result in the same Dalvik virtual register
306 * being promoted to both core and fp regs. To account for this,
307 * we only copy to the corresponding promoted physical register
308 * if it matches the type of the SSA name for the incoming
309 * argument. It is also possible that long and double arguments
310 * end up half-promoted. In those cases, we must flush the promoted
311 * half to memory as well.
312 */
313 for (int i = 0; i < cu_->num_ins; i++) {
314 PromotionMap* v_map = &promotion_map_[start_vreg + i];
315 if (i < num_arg_regs) {
316 // If arriving in register
317 bool need_flush = true;
318 RegLocation* t_loc = &ArgLocs[i];
319 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
320 OpRegCopy(v_map->core_reg, TargetReg(arg_regs[i]));
321 need_flush = false;
322 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
323 OpRegCopy(v_map->FpReg, TargetReg(arg_regs[i]));
324 need_flush = false;
325 } else {
326 need_flush = true;
327 }
328
buzbeed0a03b82013-09-14 08:21:05 -0700329 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 if (t_loc->wide) {
331 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700332 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 need_flush |= (p_map->core_location != v_map->core_location) ||
334 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700335 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
336 /*
337 * In Arm, a double is represented as a pair of consecutive single float
338 * registers starting at an even number. It's possible that both Dalvik vRegs
339 * representing the incoming double were independently promoted as singles - but
340 * not in a form usable as a double. If so, we need to flush - even though the
341 * incoming arg appears fully in register. At this point in the code, both
342 * halves of the double are promoted. Make sure they are in a usable form.
343 */
344 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
345 int low_reg = promotion_map_[lowreg_index].FpReg;
346 int high_reg = promotion_map_[lowreg_index + 1].FpReg;
347 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
348 need_flush = true;
349 }
350 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 }
352 if (need_flush) {
353 StoreBaseDisp(TargetReg(kSp), SRegOffset(start_vreg + i),
354 TargetReg(arg_regs[i]), kWord);
355 }
356 } else {
357 // If arriving in frame & promoted
358 if (v_map->core_location == kLocPhysReg) {
359 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i),
360 v_map->core_reg);
361 }
362 if (v_map->fp_location == kLocPhysReg) {
363 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i),
364 v_map->FpReg);
365 }
366 }
367 }
368}
369
370/*
371 * Bit of a hack here - in the absence of a real scheduling pass,
372 * emit the next instruction in static & direct invoke sequences.
373 */
374static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
375 int state, const MethodReference& target_method,
376 uint32_t unused,
377 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700378 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380 if (direct_code != 0 && direct_method != 0) {
381 switch (state) {
382 case 0: // Get the current Method* [sets kArg0]
383 if (direct_code != static_cast<unsigned int>(-1)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700384 if (cu->instruction_set != kX86) {
385 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
386 }
Mark Mendell55d0eac2014-02-06 11:02:52 -0800387 } else if (cu->instruction_set != kX86) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 CHECK_EQ(cu->dex_file, target_method.dex_file);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800389 cg->LoadCodeAddress(target_method.dex_method_index, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 }
391 if (direct_method != static_cast<unsigned int>(-1)) {
392 cg->LoadConstant(cg->TargetReg(kArg0), direct_method);
393 } else {
394 CHECK_EQ(cu->dex_file, target_method.dex_file);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800395 cg->LoadMethodAddress(target_method.dex_method_index, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 }
397 break;
398 default:
399 return -1;
400 }
401 } else {
402 switch (state) {
403 case 0: // Get the current Method* [sets kArg0]
404 // TUNING: we can save a reg copy if Method* has been promoted.
405 cg->LoadCurrMethodDirect(cg->TargetReg(kArg0));
406 break;
407 case 1: // Get method->dex_cache_resolved_methods_
408 cg->LoadWordDisp(cg->TargetReg(kArg0),
Brian Carlstromea46f952013-07-30 01:26:50 -0700409 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(), cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 // Set up direct code if known.
411 if (direct_code != 0) {
412 if (direct_code != static_cast<unsigned int>(-1)) {
413 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800414 } else if (cu->instruction_set != kX86) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 CHECK_EQ(cu->dex_file, target_method.dex_file);
Ian Rogers83883d72013-10-21 21:07:24 -0700416 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Mark Mendell55d0eac2014-02-06 11:02:52 -0800417 cg->LoadCodeAddress(target_method.dex_method_index, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 }
419 }
420 break;
421 case 2: // Grab target method*
422 CHECK_EQ(cu->dex_file, target_method.dex_file);
423 cg->LoadWordDisp(cg->TargetReg(kArg0),
424 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
425 (target_method.dex_method_index * 4),
426 cg-> TargetReg(kArg0));
427 break;
428 case 3: // Grab the code from the method*
429 if (cu->instruction_set != kX86) {
430 if (direct_code == 0) {
431 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800432 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 cg->TargetReg(kInvokeTgt));
434 }
435 break;
436 }
437 // Intentional fallthrough for x86
438 default:
439 return -1;
440 }
441 }
442 return state + 1;
443}
444
445/*
446 * Bit of a hack here - in the absence of a real scheduling pass,
447 * emit the next instruction in a virtual invoke sequence.
448 * We can use kLr as a temp prior to target address loading
449 * Note also that we'll load the first argument ("this") into
450 * kArg1 here rather than the standard LoadArgRegs.
451 */
452static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
453 int state, const MethodReference& target_method,
454 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700455 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
457 /*
458 * This is the fast path in which the target virtual method is
459 * fully resolved at compile time.
460 */
461 switch (state) {
462 case 0: { // Get "this" [set kArg1]
463 RegLocation rl_arg = info->args[0];
464 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
465 break;
466 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700467 case 1: // Is "this" null? [use kArg1]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 cg->GenNullCheck(info->args[0].s_reg_low, cg->TargetReg(kArg1), info->opt_flags);
469 // get this->klass_ [use kArg1, set kInvokeTgt]
470 cg->LoadWordDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
471 cg->TargetReg(kInvokeTgt));
472 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700473 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
475 cg->TargetReg(kInvokeTgt));
476 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700477 case 3: // Get target method [use kInvokeTgt, set kArg0]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), (method_idx * 4) +
479 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
480 cg->TargetReg(kArg0));
481 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700482 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 if (cu->instruction_set != kX86) {
484 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800485 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 cg->TargetReg(kInvokeTgt));
487 break;
488 }
489 // Intentional fallthrough for X86
490 default:
491 return -1;
492 }
493 return state + 1;
494}
495
496/*
Jeff Hao88474b42013-10-23 16:24:40 -0700497 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
498 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
499 * more than one interface method map to the same index. Note also that we'll load the first
500 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 */
502static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
503 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700504 uint32_t method_idx, uintptr_t unused,
505 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507
Jeff Hao88474b42013-10-23 16:24:40 -0700508 switch (state) {
509 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 CHECK_EQ(cu->dex_file, target_method.dex_file);
Jeff Hao88474b42013-10-23 16:24:40 -0700511 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
512 cg->LoadConstant(cg->TargetReg(kHiddenArg), target_method.dex_method_index);
513 if (cu->instruction_set == kX86) {
514 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg), cg->TargetReg(kHiddenArg));
515 }
516 break;
517 case 1: { // Get "this" [set kArg1]
518 RegLocation rl_arg = info->args[0];
519 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
520 break;
521 }
522 case 2: // Is "this" null? [use kArg1]
523 cg->GenNullCheck(info->args[0].s_reg_low, cg->TargetReg(kArg1), info->opt_flags);
524 // Get this->klass_ [use kArg1, set kInvokeTgt]
525 cg->LoadWordDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
526 cg->TargetReg(kInvokeTgt));
527 break;
528 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
529 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
530 cg->TargetReg(kInvokeTgt));
531 break;
532 case 4: // Get target method [use kInvokeTgt, set kArg0]
533 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), ((method_idx % ClassLinker::kImtSize) * 4) +
534 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 cg->TargetReg(kArg0));
536 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700537 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
538 if (cu->instruction_set != kX86) {
539 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800540 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Jeff Hao88474b42013-10-23 16:24:40 -0700541 cg->TargetReg(kInvokeTgt));
542 break;
543 }
544 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 default:
546 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 }
548 return state + 1;
549}
550
Ian Rogers848871b2013-08-05 10:56:33 -0700551static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700553 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
555 /*
556 * This handles the case in which the base method is not fully
557 * resolved at compile time, we bail to a runtime helper.
558 */
559 if (state == 0) {
560 if (cu->instruction_set != kX86) {
561 // Load trampoline target
Ian Rogers848871b2013-08-05 10:56:33 -0700562 cg->LoadWordDisp(cg->TargetReg(kSelf), trampoline.Int32Value(), cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 }
564 // Load kArg0 with method index
565 CHECK_EQ(cu->dex_file, target_method.dex_file);
566 cg->LoadConstant(cg->TargetReg(kArg0), target_method.dex_method_index);
567 return 1;
568 }
569 return -1;
570}
571
572static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
573 int state,
574 const MethodReference& target_method,
575 uint32_t method_idx,
576 uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700577 InvokeType unused3) {
Ian Rogers848871b2013-08-05 10:56:33 -0700578 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
580}
581
582static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
583 const MethodReference& target_method,
584 uint32_t method_idx, uintptr_t unused,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700585 uintptr_t unused2, InvokeType unused3) {
Ian Rogers848871b2013-08-05 10:56:33 -0700586 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
588}
589
590static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
591 const MethodReference& target_method,
592 uint32_t method_idx, uintptr_t unused,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700593 uintptr_t unused2, InvokeType unused3) {
Ian Rogers848871b2013-08-05 10:56:33 -0700594 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
596}
597
598static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
599 const MethodReference& target_method,
600 uint32_t method_idx, uintptr_t unused,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700601 uintptr_t unused2, InvokeType unused3) {
Ian Rogers848871b2013-08-05 10:56:33 -0700602 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
604}
605
606static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
607 CallInfo* info, int state,
608 const MethodReference& target_method,
609 uint32_t unused,
610 uintptr_t unused2, uintptr_t unused3,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700611 InvokeType unused4) {
Ian Rogers848871b2013-08-05 10:56:33 -0700612 ThreadOffset trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
614}
615
616int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
617 NextCallInsn next_call_insn,
618 const MethodReference& target_method,
619 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700620 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 int last_arg_reg = TargetReg(kArg3);
622 int next_reg = TargetReg(kArg1);
623 int next_arg = 0;
624 if (skip_this) {
625 next_reg++;
626 next_arg++;
627 }
628 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
629 RegLocation rl_arg = info->args[next_arg++];
630 rl_arg = UpdateRawLoc(rl_arg);
631 if (rl_arg.wide && (next_reg <= TargetReg(kArg2))) {
632 LoadValueDirectWideFixed(rl_arg, next_reg, next_reg + 1);
633 next_reg++;
634 next_arg++;
635 } else {
636 if (rl_arg.wide) {
637 rl_arg.wide = false;
638 rl_arg.is_const = false;
639 }
640 LoadValueDirectFixed(rl_arg, next_reg);
641 }
642 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
643 direct_code, direct_method, type);
644 }
645 return call_state;
646}
647
648/*
649 * Load up to 5 arguments, the first three of which will be in
650 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
651 * and as part of the load sequence, it must be replaced with
652 * the target method pointer. Note, this may also be called
653 * for "range" variants if the number of arguments is 5 or fewer.
654 */
655int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
656 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
657 const MethodReference& target_method,
658 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700659 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 RegLocation rl_arg;
661
662 /* If no arguments, just return */
663 if (info->num_arg_words == 0)
664 return call_state;
665
666 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
667 direct_code, direct_method, type);
668
669 DCHECK_LE(info->num_arg_words, 5);
670 if (info->num_arg_words > 3) {
671 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700672 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 RegLocation rl_use0 = info->args[0];
674 RegLocation rl_use1 = info->args[1];
675 RegLocation rl_use2 = info->args[2];
676 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) &&
677 rl_use2.wide) {
678 int reg = -1;
679 // Wide spans, we need the 2nd half of uses[2].
680 rl_arg = UpdateLocWide(rl_use2);
681 if (rl_arg.location == kLocPhysReg) {
682 reg = rl_arg.high_reg;
683 } else {
684 // kArg2 & rArg3 can safely be used here
685 reg = TargetReg(kArg3);
686 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
687 call_state = next_call_insn(cu_, info, call_state, target_method,
688 vtable_idx, direct_code, direct_method, type);
689 }
690 StoreBaseDisp(TargetReg(kSp), (next_use + 1) * 4, reg, kWord);
691 StoreBaseDisp(TargetReg(kSp), 16 /* (3+1)*4 */, reg, kWord);
692 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
693 direct_code, direct_method, type);
694 next_use++;
695 }
696 // Loop through the rest
697 while (next_use < info->num_arg_words) {
698 int low_reg;
699 int high_reg = -1;
700 rl_arg = info->args[next_use];
701 rl_arg = UpdateRawLoc(rl_arg);
702 if (rl_arg.location == kLocPhysReg) {
703 low_reg = rl_arg.low_reg;
704 high_reg = rl_arg.high_reg;
705 } else {
706 low_reg = TargetReg(kArg2);
707 if (rl_arg.wide) {
708 high_reg = TargetReg(kArg3);
709 LoadValueDirectWideFixed(rl_arg, low_reg, high_reg);
710 } else {
711 LoadValueDirectFixed(rl_arg, low_reg);
712 }
713 call_state = next_call_insn(cu_, info, call_state, target_method,
714 vtable_idx, direct_code, direct_method, type);
715 }
716 int outs_offset = (next_use + 1) * 4;
717 if (rl_arg.wide) {
718 StoreBaseDispWide(TargetReg(kSp), outs_offset, low_reg, high_reg);
719 next_use += 2;
720 } else {
721 StoreWordDisp(TargetReg(kSp), outs_offset, low_reg);
722 next_use++;
723 }
724 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
725 direct_code, direct_method, type);
726 }
727 }
728
729 call_state = LoadArgRegs(info, call_state, next_call_insn,
730 target_method, vtable_idx, direct_code, direct_method,
731 type, skip_this);
732
733 if (pcrLabel) {
734 *pcrLabel = GenNullCheck(info->args[0].s_reg_low, TargetReg(kArg1), info->opt_flags);
735 }
736 return call_state;
737}
738
739/*
740 * May have 0+ arguments (also used for jumbo). Note that
741 * source virtual registers may be in physical registers, so may
742 * need to be flushed to home location before copying. This
743 * applies to arg3 and above (see below).
744 *
745 * Two general strategies:
746 * If < 20 arguments
747 * Pass args 3-18 using vldm/vstm block copy
748 * Pass arg0, arg1 & arg2 in kArg1-kArg3
749 * If 20+ arguments
750 * Pass args arg19+ using memcpy block copy
751 * Pass arg0, arg1 & arg2 in kArg1-kArg3
752 *
753 */
754int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
755 LIR** pcrLabel, NextCallInsn next_call_insn,
756 const MethodReference& target_method,
757 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700758 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 // If we can treat it as non-range (Jumbo ops will use range form)
760 if (info->num_arg_words <= 5)
761 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
762 next_call_insn, target_method, vtable_idx,
763 direct_code, direct_method, type, skip_this);
764 /*
765 * First load the non-register arguments. Both forms expect all
766 * of the source arguments to be in their home frame location, so
767 * scan the s_reg names and flush any that have been promoted to
768 * frame backing storage.
769 */
770 // Scan the rest of the args - if in phys_reg flush to memory
771 for (int next_arg = 0; next_arg < info->num_arg_words;) {
772 RegLocation loc = info->args[next_arg];
773 if (loc.wide) {
774 loc = UpdateLocWide(loc);
775 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
776 StoreBaseDispWide(TargetReg(kSp), SRegOffset(loc.s_reg_low),
777 loc.low_reg, loc.high_reg);
778 }
779 next_arg += 2;
780 } else {
781 loc = UpdateLoc(loc);
782 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
783 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low),
784 loc.low_reg, kWord);
785 }
786 next_arg++;
787 }
788 }
789
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800790 // Logic below assumes that Method pointer is at offset zero from SP.
791 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
792
793 // The first 3 arguments are passed via registers.
794 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
795 // get size of uintptr_t or size of object reference according to model being used.
796 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800798 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
799 DCHECK_GT(regs_left_to_pass_via_stack, 0);
800
801 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
802 // Use vldm/vstm pair using kArg3 as a temp
803 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
804 direct_code, direct_method, type);
805 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
806 LIR* ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
807 // TUNING: loosen barrier
808 ld->u.m.def_mask = ENCODE_ALL;
809 SetMemRefType(ld, true /* is_load */, kDalvikReg);
810 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
811 direct_code, direct_method, type);
812 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
813 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
814 direct_code, direct_method, type);
815 LIR* st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
816 SetMemRefType(st, false /* is_load */, kDalvikReg);
817 st->u.m.def_mask = ENCODE_ALL;
818 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
819 direct_code, direct_method, type);
820 } else if (cu_->instruction_set == kX86) {
821 int current_src_offset = start_offset;
822 int current_dest_offset = outs_offset;
823
824 while (regs_left_to_pass_via_stack > 0) {
825 // This is based on the knowledge that the stack itself is 16-byte aligned.
826 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
827 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
828 size_t bytes_to_move;
829
830 /*
831 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
832 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
833 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
834 * We do this because we could potentially do a smaller move to align.
835 */
836 if (regs_left_to_pass_via_stack == 4 ||
837 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
838 // Moving 128-bits via xmm register.
839 bytes_to_move = sizeof(uint32_t) * 4;
840
841 // Allocate a free xmm temp. Since we are working through the calling sequence,
842 // we expect to have an xmm temporary available.
843 int temp = AllocTempDouble();
844 CHECK_GT(temp, 0);
845
846 LIR* ld1 = nullptr;
847 LIR* ld2 = nullptr;
848 LIR* st1 = nullptr;
849 LIR* st2 = nullptr;
850
851 /*
852 * The logic is similar for both loads and stores. If we have 16-byte alignment,
853 * do an aligned move. If we have 8-byte alignment, then do the move in two
854 * parts. This approach prevents possible cache line splits. Finally, fall back
855 * to doing an unaligned move. In most cases we likely won't split the cache
856 * line but we cannot prove it and thus take a conservative approach.
857 */
858 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
859 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
860
861 if (src_is_16b_aligned) {
862 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
863 } else if (src_is_8b_aligned) {
864 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
865 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1), kMovHi128FP);
866 } else {
867 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
868 }
869
870 if (dest_is_16b_aligned) {
871 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
872 } else if (dest_is_8b_aligned) {
873 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
874 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1), temp, kMovHi128FP);
875 } else {
876 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
877 }
878
879 // TODO If we could keep track of aliasing information for memory accesses that are wider
880 // than 64-bit, we wouldn't need to set up a barrier.
881 if (ld1 != nullptr) {
882 if (ld2 != nullptr) {
883 // For 64-bit load we can actually set up the aliasing information.
884 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
885 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
886 } else {
887 // Set barrier for 128-bit load.
888 SetMemRefType(ld1, true /* is_load */, kDalvikReg);
889 ld1->u.m.def_mask = ENCODE_ALL;
890 }
891 }
892 if (st1 != nullptr) {
893 if (st2 != nullptr) {
894 // For 64-bit store we can actually set up the aliasing information.
895 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
896 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
897 } else {
898 // Set barrier for 128-bit store.
899 SetMemRefType(st1, false /* is_load */, kDalvikReg);
900 st1->u.m.def_mask = ENCODE_ALL;
901 }
902 }
903
904 // Free the temporary used for the data movement.
905 FreeTemp(temp);
906 } else {
907 // Moving 32-bits via general purpose register.
908 bytes_to_move = sizeof(uint32_t);
909
910 // Instead of allocating a new temp, simply reuse one of the registers being used
911 // for argument passing.
912 int temp = TargetReg(kArg3);
913
914 // Now load the argument VR and store to the outs.
915 LoadWordDisp(TargetReg(kSp), current_src_offset, temp);
916 StoreWordDisp(TargetReg(kSp), current_dest_offset, temp);
917 }
918
919 current_src_offset += bytes_to_move;
920 current_dest_offset += bytes_to_move;
921 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
922 }
923 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 // Generate memcpy
925 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
926 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Ian Rogers7655f292013-07-29 11:07:13 -0700927 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(pMemcpy), TargetReg(kArg0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700929 }
930
931 call_state = LoadArgRegs(info, call_state, next_call_insn,
932 target_method, vtable_idx, direct_code, direct_method,
933 type, skip_this);
934
935 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
936 direct_code, direct_method, type);
937 if (pcrLabel) {
938 *pcrLabel = GenNullCheck(info->args[0].s_reg_low, TargetReg(kArg1), info->opt_flags);
939 }
940 return call_state;
941}
942
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700943RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 RegLocation res;
945 if (info->result.location == kLocInvalid) {
946 res = GetReturn(false);
947 } else {
948 res = info->result;
949 }
950 return res;
951}
952
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700953RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 RegLocation res;
955 if (info->result.location == kLocInvalid) {
956 res = GetReturnWide(false);
957 } else {
958 res = info->result;
959 }
960 return res;
961}
962
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700963bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 if (cu_->instruction_set == kMips) {
965 // TODO - add Mips implementation
966 return false;
967 }
968 // Location of reference to data array
969 int value_offset = mirror::String::ValueOffset().Int32Value();
970 // Location of count
971 int count_offset = mirror::String::CountOffset().Int32Value();
972 // Starting offset within data array
973 int offset_offset = mirror::String::OffsetOffset().Int32Value();
974 // Start of char data with array_
975 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
976
977 RegLocation rl_obj = info->args[0];
978 RegLocation rl_idx = info->args[1];
979 rl_obj = LoadValue(rl_obj, kCoreReg);
980 rl_idx = LoadValue(rl_idx, kCoreReg);
981 int reg_max;
982 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, info->opt_flags);
983 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
984 LIR* launch_pad = NULL;
985 int reg_off = INVALID_REG;
986 int reg_ptr = INVALID_REG;
987 if (cu_->instruction_set != kX86) {
988 reg_off = AllocTemp();
989 reg_ptr = AllocTemp();
990 if (range_check) {
991 reg_max = AllocTemp();
992 LoadWordDisp(rl_obj.low_reg, count_offset, reg_max);
993 }
994 LoadWordDisp(rl_obj.low_reg, offset_offset, reg_off);
995 LoadWordDisp(rl_obj.low_reg, value_offset, reg_ptr);
996 if (range_check) {
997 // Set up a launch pad to allow retry in case of bounds violation */
buzbee0d829482013-10-11 15:24:55 -0700998 launch_pad = RawLIR(0, kPseudoIntrinsicRetry, WrapPointer(info));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 intrinsic_launchpads_.Insert(launch_pad);
1000 OpRegReg(kOpCmp, rl_idx.low_reg, reg_max);
1001 FreeTemp(reg_max);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001002 OpCondBranch(kCondUge, launch_pad);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001003 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 } else {
1005 if (range_check) {
1006 reg_max = AllocTemp();
1007 LoadWordDisp(rl_obj.low_reg, count_offset, reg_max);
1008 // Set up a launch pad to allow retry in case of bounds violation */
buzbee0d829482013-10-11 15:24:55 -07001009 launch_pad = RawLIR(0, kPseudoIntrinsicRetry, WrapPointer(info));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 intrinsic_launchpads_.Insert(launch_pad);
1011 OpRegReg(kOpCmp, rl_idx.low_reg, reg_max);
1012 FreeTemp(reg_max);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001013 OpCondBranch(kCondUge, launch_pad);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 }
1015 reg_off = AllocTemp();
1016 reg_ptr = AllocTemp();
1017 LoadWordDisp(rl_obj.low_reg, offset_offset, reg_off);
1018 LoadWordDisp(rl_obj.low_reg, value_offset, reg_ptr);
1019 }
1020 OpRegImm(kOpAdd, reg_ptr, data_offset);
1021 OpRegReg(kOpAdd, reg_off, rl_idx.low_reg);
1022 FreeTemp(rl_obj.low_reg);
1023 FreeTemp(rl_idx.low_reg);
1024 RegLocation rl_dest = InlineTarget(info);
1025 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1026 LoadBaseIndexed(reg_ptr, reg_off, rl_result.low_reg, 1, kUnsignedHalf);
1027 FreeTemp(reg_off);
1028 FreeTemp(reg_ptr);
1029 StoreValue(rl_dest, rl_result);
1030 if (range_check) {
1031 launch_pad->operands[2] = 0; // no resumption
1032 }
1033 // Record that we've already inlined & null checked
1034 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
1035 return true;
1036}
1037
1038// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001039bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 if (cu_->instruction_set == kMips) {
1041 // TODO - add Mips implementation
1042 return false;
1043 }
1044 // dst = src.length();
1045 RegLocation rl_obj = info->args[0];
1046 rl_obj = LoadValue(rl_obj, kCoreReg);
1047 RegLocation rl_dest = InlineTarget(info);
1048 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1049 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, info->opt_flags);
1050 LoadWordDisp(rl_obj.low_reg, mirror::String::CountOffset().Int32Value(), rl_result.low_reg);
1051 if (is_empty) {
1052 // dst = (dst == 0);
1053 if (cu_->instruction_set == kThumb2) {
1054 int t_reg = AllocTemp();
1055 OpRegReg(kOpNeg, t_reg, rl_result.low_reg);
1056 OpRegRegReg(kOpAdc, rl_result.low_reg, rl_result.low_reg, t_reg);
1057 } else {
1058 DCHECK_EQ(cu_->instruction_set, kX86);
1059 OpRegImm(kOpSub, rl_result.low_reg, 1);
1060 OpRegImm(kOpLsr, rl_result.low_reg, 31);
1061 }
1062 }
1063 StoreValue(rl_dest, rl_result);
1064 return true;
1065}
1066
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001067bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1068 if (cu_->instruction_set == kMips) {
1069 // TODO - add Mips implementation
1070 return false;
1071 }
1072 RegLocation rl_src_i = info->args[0];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001073 RegLocation rl_dest = (size == kLong) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001074 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1075 if (size == kLong) {
1076 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
Vladimir Markof246af22013-11-27 12:30:15 +00001077 int r_i_low = rl_i.low_reg;
1078 if (rl_i.low_reg == rl_result.low_reg) {
1079 // First REV shall clobber rl_result.low_reg, save the value in a temp for the second REV.
1080 r_i_low = AllocTemp();
1081 OpRegCopy(r_i_low, rl_i.low_reg);
1082 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001083 OpRegReg(kOpRev, rl_result.low_reg, rl_i.high_reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001084 OpRegReg(kOpRev, rl_result.high_reg, r_i_low);
1085 if (rl_i.low_reg == rl_result.low_reg) {
1086 FreeTemp(r_i_low);
1087 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001088 StoreValueWide(rl_dest, rl_result);
1089 } else {
1090 DCHECK(size == kWord || size == kSignedHalf);
1091 OpKind op = (size == kWord) ? kOpRev : kOpRevsh;
1092 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
1093 OpRegReg(op, rl_result.low_reg, rl_i.low_reg);
1094 StoreValue(rl_dest, rl_result);
1095 }
1096 return true;
1097}
1098
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001099bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 if (cu_->instruction_set == kMips) {
1101 // TODO - add Mips implementation
1102 return false;
1103 }
1104 RegLocation rl_src = info->args[0];
1105 rl_src = LoadValue(rl_src, kCoreReg);
1106 RegLocation rl_dest = InlineTarget(info);
1107 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1108 int sign_reg = AllocTemp();
1109 // abs(x) = y<=x>>31, (x+y)^y.
1110 OpRegRegImm(kOpAsr, sign_reg, rl_src.low_reg, 31);
1111 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, sign_reg);
1112 OpRegReg(kOpXor, rl_result.low_reg, sign_reg);
1113 StoreValue(rl_dest, rl_result);
1114 return true;
1115}
1116
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001117bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 if (cu_->instruction_set == kMips) {
1119 // TODO - add Mips implementation
1120 return false;
1121 }
1122 if (cu_->instruction_set == kThumb2) {
1123 RegLocation rl_src = info->args[0];
1124 rl_src = LoadValueWide(rl_src, kCoreReg);
1125 RegLocation rl_dest = InlineTargetWide(info);
1126 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1127 int sign_reg = AllocTemp();
1128 // abs(x) = y<=x>>31, (x+y)^y.
1129 OpRegRegImm(kOpAsr, sign_reg, rl_src.high_reg, 31);
1130 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, sign_reg);
1131 OpRegRegReg(kOpAdc, rl_result.high_reg, rl_src.high_reg, sign_reg);
1132 OpRegReg(kOpXor, rl_result.low_reg, sign_reg);
1133 OpRegReg(kOpXor, rl_result.high_reg, sign_reg);
1134 StoreValueWide(rl_dest, rl_result);
1135 return true;
1136 } else {
1137 DCHECK_EQ(cu_->instruction_set, kX86);
1138 // Reuse source registers to avoid running out of temps
1139 RegLocation rl_src = info->args[0];
1140 rl_src = LoadValueWide(rl_src, kCoreReg);
1141 RegLocation rl_dest = InlineTargetWide(info);
1142 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1143 OpRegCopyWide(rl_result.low_reg, rl_result.high_reg, rl_src.low_reg, rl_src.high_reg);
1144 FreeTemp(rl_src.low_reg);
1145 FreeTemp(rl_src.high_reg);
1146 int sign_reg = AllocTemp();
1147 // abs(x) = y<=x>>31, (x+y)^y.
1148 OpRegRegImm(kOpAsr, sign_reg, rl_result.high_reg, 31);
1149 OpRegReg(kOpAdd, rl_result.low_reg, sign_reg);
1150 OpRegReg(kOpAdc, rl_result.high_reg, sign_reg);
1151 OpRegReg(kOpXor, rl_result.low_reg, sign_reg);
1152 OpRegReg(kOpXor, rl_result.high_reg, sign_reg);
1153 StoreValueWide(rl_dest, rl_result);
1154 return true;
1155 }
1156}
1157
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001158bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 if (cu_->instruction_set == kMips) {
1160 // TODO - add Mips implementation
1161 return false;
1162 }
1163 RegLocation rl_src = info->args[0];
1164 RegLocation rl_dest = InlineTarget(info);
1165 StoreValue(rl_dest, rl_src);
1166 return true;
1167}
1168
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001169bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 if (cu_->instruction_set == kMips) {
1171 // TODO - add Mips implementation
1172 return false;
1173 }
1174 RegLocation rl_src = info->args[0];
1175 RegLocation rl_dest = InlineTargetWide(info);
1176 StoreValueWide(rl_dest, rl_src);
1177 return true;
1178}
1179
1180/*
1181 * Fast string.index_of(I) & (II). Tests for simple case of char <= 0xffff,
1182 * otherwise bails to standard library code.
1183 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001184bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 if (cu_->instruction_set == kMips) {
1186 // TODO - add Mips implementation
1187 return false;
1188 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001189 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 LockCallTemps(); // Using fixed registers
1191 int reg_ptr = TargetReg(kArg0);
1192 int reg_char = TargetReg(kArg1);
1193 int reg_start = TargetReg(kArg2);
1194
1195 RegLocation rl_obj = info->args[0];
1196 RegLocation rl_char = info->args[1];
1197 RegLocation rl_start = info->args[2];
1198 LoadValueDirectFixed(rl_obj, reg_ptr);
1199 LoadValueDirectFixed(rl_char, reg_char);
1200 if (zero_based) {
1201 LoadConstant(reg_start, 0);
1202 } else {
1203 LoadValueDirectFixed(rl_start, reg_start);
1204 }
Ian Rogers7655f292013-07-29 11:07:13 -07001205 int r_tgt = (cu_->instruction_set != kX86) ? LoadHelper(QUICK_ENTRYPOINT_OFFSET(pIndexOf)) : 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 GenNullCheck(rl_obj.s_reg_low, reg_ptr, info->opt_flags);
buzbee0d829482013-10-11 15:24:55 -07001207 LIR* launch_pad = RawLIR(0, kPseudoIntrinsicRetry, WrapPointer(info));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 intrinsic_launchpads_.Insert(launch_pad);
1209 OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, launch_pad);
1210 // NOTE: not a safepoint
1211 if (cu_->instruction_set != kX86) {
1212 OpReg(kOpBlx, r_tgt);
1213 } else {
Ian Rogers7655f292013-07-29 11:07:13 -07001214 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(pIndexOf));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 }
1216 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
buzbee0d829482013-10-11 15:24:55 -07001217 launch_pad->operands[2] = WrapPointer(resume_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 // Record that we've already inlined & null checked
1219 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
1220 RegLocation rl_return = GetReturn(false);
1221 RegLocation rl_dest = InlineTarget(info);
1222 StoreValue(rl_dest, rl_return);
1223 return true;
1224}
1225
1226/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001227bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 if (cu_->instruction_set == kMips) {
1229 // TODO - add Mips implementation
1230 return false;
1231 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001232 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 LockCallTemps(); // Using fixed registers
1234 int reg_this = TargetReg(kArg0);
1235 int reg_cmp = TargetReg(kArg1);
1236
1237 RegLocation rl_this = info->args[0];
1238 RegLocation rl_cmp = info->args[1];
1239 LoadValueDirectFixed(rl_this, reg_this);
1240 LoadValueDirectFixed(rl_cmp, reg_cmp);
1241 int r_tgt = (cu_->instruction_set != kX86) ?
Ian Rogers7655f292013-07-29 11:07:13 -07001242 LoadHelper(QUICK_ENTRYPOINT_OFFSET(pStringCompareTo)) : 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 GenNullCheck(rl_this.s_reg_low, reg_this, info->opt_flags);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001244 // TUNING: check if rl_cmp.s_reg_low is already null checked
buzbee0d829482013-10-11 15:24:55 -07001245 LIR* launch_pad = RawLIR(0, kPseudoIntrinsicRetry, WrapPointer(info));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 intrinsic_launchpads_.Insert(launch_pad);
1247 OpCmpImmBranch(kCondEq, reg_cmp, 0, launch_pad);
1248 // NOTE: not a safepoint
1249 if (cu_->instruction_set != kX86) {
1250 OpReg(kOpBlx, r_tgt);
1251 } else {
Ian Rogers7655f292013-07-29 11:07:13 -07001252 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(pStringCompareTo));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 }
1254 launch_pad->operands[2] = 0; // No return possible
1255 // Record that we've already inlined & null checked
1256 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
1257 RegLocation rl_return = GetReturn(false);
1258 RegLocation rl_dest = InlineTarget(info);
1259 StoreValue(rl_dest, rl_return);
1260 return true;
1261}
1262
1263bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1264 RegLocation rl_dest = InlineTarget(info);
1265 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001266 ThreadOffset offset = Thread::PeerOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 if (cu_->instruction_set == kThumb2 || cu_->instruction_set == kMips) {
Ian Rogers848871b2013-08-05 10:56:33 -07001268 LoadWordDisp(TargetReg(kSelf), offset.Int32Value(), rl_result.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 } else {
1270 CHECK(cu_->instruction_set == kX86);
Brian Carlstrom2d888622013-07-18 17:02:00 -07001271 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.low_reg, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 }
1273 StoreValue(rl_dest, rl_result);
1274 return true;
1275}
1276
1277bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1278 bool is_long, bool is_volatile) {
1279 if (cu_->instruction_set == kMips) {
1280 // TODO - add Mips implementation
1281 return false;
1282 }
1283 // Unused - RegLocation rl_src_unsafe = info->args[0];
1284 RegLocation rl_src_obj = info->args[1]; // Object
1285 RegLocation rl_src_offset = info->args[2]; // long low
1286 rl_src_offset.wide = 0; // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001287 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 if (is_volatile) {
1289 GenMemBarrier(kLoadLoad);
1290 }
1291 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1292 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1293 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1294 if (is_long) {
1295 OpRegReg(kOpAdd, rl_object.low_reg, rl_offset.low_reg);
1296 LoadBaseDispWide(rl_object.low_reg, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
1297 StoreValueWide(rl_dest, rl_result);
1298 } else {
1299 LoadBaseIndexed(rl_object.low_reg, rl_offset.low_reg, rl_result.low_reg, 0, kWord);
1300 StoreValue(rl_dest, rl_result);
1301 }
1302 return true;
1303}
1304
1305bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1306 bool is_object, bool is_volatile, bool is_ordered) {
1307 if (cu_->instruction_set == kMips) {
1308 // TODO - add Mips implementation
1309 return false;
1310 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 // Unused - RegLocation rl_src_unsafe = info->args[0];
1312 RegLocation rl_src_obj = info->args[1]; // Object
1313 RegLocation rl_src_offset = info->args[2]; // long low
1314 rl_src_offset.wide = 0; // ignore high half in info->args[3]
1315 RegLocation rl_src_value = info->args[4]; // value to store
1316 if (is_volatile || is_ordered) {
1317 GenMemBarrier(kStoreStore);
1318 }
1319 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1320 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1321 RegLocation rl_value;
1322 if (is_long) {
1323 rl_value = LoadValueWide(rl_src_value, kCoreReg);
1324 OpRegReg(kOpAdd, rl_object.low_reg, rl_offset.low_reg);
1325 StoreBaseDispWide(rl_object.low_reg, 0, rl_value.low_reg, rl_value.high_reg);
1326 } else {
1327 rl_value = LoadValue(rl_src_value, kCoreReg);
1328 StoreBaseIndexed(rl_object.low_reg, rl_offset.low_reg, rl_value.low_reg, 0, kWord);
1329 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001330
1331 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
1332 FreeTemp(rl_offset.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 if (is_volatile) {
1334 GenMemBarrier(kStoreLoad);
1335 }
1336 if (is_object) {
1337 MarkGCCard(rl_value.low_reg, rl_object.low_reg);
1338 }
1339 return true;
1340}
1341
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001342void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko5c96e6b2013-11-14 15:34:17 +00001343 if (!(info->opt_flags & MIR_INLINED)) {
Vladimir Marko5816ed42013-11-27 17:04:20 +00001344 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1345 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1346 ->GenIntrinsic(this, info)) {
Vladimir Marko5c96e6b2013-11-14 15:34:17 +00001347 return;
1348 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 }
1350 InvokeType original_type = info->type; // avoiding mutation by ComputeInvokeInfo
1351 int call_state = 0;
1352 LIR* null_ck;
1353 LIR** p_null_ck = NULL;
1354 NextCallInsn next_call_insn;
1355 FlushAllRegs(); /* Everything to home location */
1356 // Explicit register usage
1357 LockCallTemps();
1358
1359 DexCompilationUnit* cUnit = mir_graph_->GetCurrentDexCompilationUnit();
1360 MethodReference target_method(cUnit->GetDexFile(), info->index);
1361 int vtable_idx;
1362 uintptr_t direct_code;
1363 uintptr_t direct_method;
1364 bool skip_this;
1365 bool fast_path =
1366 cu_->compiler_driver->ComputeInvokeInfo(mir_graph_->GetCurrentDexCompilationUnit(),
1367 current_dalvik_offset_,
Ian Rogers65ec92c2013-09-06 10:49:58 -07001368 true, true,
1369 &info->type, &target_method,
1370 &vtable_idx,
1371 &direct_code, &direct_method) && !SLOW_INVOKE_PATH;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001374 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 } else if (info->type == kDirect) {
1376 if (fast_path) {
1377 p_null_ck = &null_ck;
1378 }
1379 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1380 skip_this = false;
1381 } else if (info->type == kStatic) {
1382 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1383 skip_this = false;
1384 } else if (info->type == kSuper) {
1385 DCHECK(!fast_path); // Fast path is a direct call.
1386 next_call_insn = NextSuperCallInsnSP;
1387 skip_this = false;
1388 } else {
1389 DCHECK_EQ(info->type, kVirtual);
1390 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1391 skip_this = fast_path;
1392 }
1393 if (!info->is_range) {
1394 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
1395 next_call_insn, target_method,
1396 vtable_idx, direct_code, direct_method,
1397 original_type, skip_this);
1398 } else {
1399 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
1400 next_call_insn, target_method, vtable_idx,
1401 direct_code, direct_method, original_type,
1402 skip_this);
1403 }
1404 // Finish up any of the call sequence not interleaved in arg loading
1405 while (call_state >= 0) {
1406 call_state = next_call_insn(cu_, info, call_state, target_method,
1407 vtable_idx, direct_code, direct_method,
1408 original_type);
1409 }
1410 LIR* call_inst;
1411 if (cu_->instruction_set != kX86) {
1412 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1413 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001414 if (fast_path) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001415 if (direct_code == static_cast<unsigned int>(-1)) {
1416 // We can have the linker fixup a call relative.
1417 call_inst =
1418 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(
1419 target_method.dex_method_index, info->type);
1420 } else {
1421 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1422 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1423 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001425 ThreadOffset trampoline(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 switch (info->type) {
1427 case kInterface:
Jeff Hao88474b42013-10-23 16:24:40 -07001428 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 break;
1430 case kDirect:
Ian Rogers7655f292013-07-29 11:07:13 -07001431 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432 break;
1433 case kStatic:
Ian Rogers7655f292013-07-29 11:07:13 -07001434 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001435 break;
1436 case kSuper:
Ian Rogers7655f292013-07-29 11:07:13 -07001437 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 break;
1439 case kVirtual:
Ian Rogers7655f292013-07-29 11:07:13 -07001440 trampoline = QUICK_ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 break;
1442 default:
1443 LOG(FATAL) << "Unexpected invoke type";
1444 }
1445 call_inst = OpThreadMem(kOpBlx, trampoline);
1446 }
1447 }
1448 MarkSafepointPC(call_inst);
1449
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001450 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 if (info->result.location != kLocInvalid) {
1452 // We have a following MOVE_RESULT - do it now.
1453 if (info->result.wide) {
1454 RegLocation ret_loc = GetReturnWide(info->result.fp);
1455 StoreValueWide(info->result, ret_loc);
1456 } else {
1457 RegLocation ret_loc = GetReturn(info->result.fp);
1458 StoreValue(info->result, ret_loc);
1459 }
1460 }
1461}
1462
1463} // namespace art