blob: 2af847c7dfe0d67be34f8e68ea0228195598b091 [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"
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070025#include "mirror/object_array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "mirror/string.h"
27#include "mir_to_lir-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "x86/codegen_x86.h"
29
30namespace art {
31
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070032// Shortcuts to repeatedly used long types.
33typedef mirror::ObjectArray<mirror::Object> ObjArray;
34
Brian Carlstrom7940e442013-07-12 13:46:57 -070035/*
36 * This source files contains "gen" codegen routines that should
37 * be applicable to most targets. Only mid-level support utilities
38 * and "op" calls may be used here.
39 */
40
Mingyao Yang3a74d152014-04-21 15:39:44 -070041void Mir2Lir::AddIntrinsicSlowPath(CallInfo* info, LIR* branch, LIR* resume) {
42 class IntrinsicSlowPathPath : public Mir2Lir::LIRSlowPath {
Vladimir Marko3bc86152014-03-13 14:11:28 +000043 public:
Mingyao Yang3a74d152014-04-21 15:39:44 -070044 IntrinsicSlowPathPath(Mir2Lir* m2l, CallInfo* info, LIR* branch, LIR* resume = nullptr)
Vladimir Marko3bc86152014-03-13 14:11:28 +000045 : LIRSlowPath(m2l, info->offset, branch, resume), info_(info) {
46 }
47
48 void Compile() {
49 m2l_->ResetRegPool();
50 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070051 GenerateTargetLabel(kPseudoIntrinsicRetry);
Vladimir Marko3bc86152014-03-13 14:11:28 +000052 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
53 m2l_->GenInvokeNoInline(info_);
54 if (cont_ != nullptr) {
55 m2l_->OpUnconditionalBranch(cont_);
56 }
57 }
58
59 private:
60 CallInfo* const info_;
61 };
62
Mingyao Yang3a74d152014-04-21 15:39:44 -070063 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000064}
65
Andreas Gampe2f244e92014-05-08 03:35:25 -070066// Macro to help instantiate.
67// TODO: This might be used to only instantiate <4> on pure 32b systems.
68#define INSTANTIATE(sig_part1, ...) \
69 template sig_part1(ThreadOffset<4>, __VA_ARGS__); \
70 template sig_part1(ThreadOffset<8>, __VA_ARGS__); \
71
72
Brian Carlstrom7940e442013-07-12 13:46:57 -070073/*
74 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000075 * the helper target address, and the actual call to the helper. Because x86
76 * has a memory call operation, part 1 is a NOP for x86. For other targets,
77 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 */
Andreas Gampe2f244e92014-05-08 03:35:25 -070079// template <size_t pointer_size>
Ian Rogersdd7624d2014-03-14 17:43:00 -070080RegStorage Mir2Lir::CallHelperSetup(ThreadOffset<4> helper_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070081 // All CallRuntimeHelperXXX call this first. So make a central check here.
82 DCHECK_EQ(4U, GetInstructionSetPointerSize(cu_->instruction_set));
83
84 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
85 return RegStorage::InvalidReg();
86 } else {
87 return LoadHelper(helper_offset);
88 }
89}
90
91RegStorage Mir2Lir::CallHelperSetup(ThreadOffset<8> helper_offset) {
92 // All CallRuntimeHelperXXX call this first. So make a central check here.
93 DCHECK_EQ(8U, GetInstructionSetPointerSize(cu_->instruction_set));
94
95 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
96 return RegStorage::InvalidReg();
97 } else {
98 return LoadHelper(helper_offset);
99 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100}
101
102/* NOTE: if r_tgt is a temp, it will be freed following use */
Andreas Gampe2f244e92014-05-08 03:35:25 -0700103template <size_t pointer_size>
104LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<pointer_size> helper_offset,
105 bool safepoint_pc, bool use_link) {
Dave Allisond6ed6422014-04-09 23:36:15 +0000106 LIR* call_inst;
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700107 OpKind op = use_link ? kOpBlx : kOpBx;
Dave Allisond6ed6422014-04-09 23:36:15 +0000108 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
109 call_inst = OpThreadMem(op, helper_offset);
110 } else {
111 call_inst = OpReg(op, r_tgt);
112 FreeTemp(r_tgt);
113 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 if (safepoint_pc) {
115 MarkSafepointPC(call_inst);
116 }
117 return call_inst;
118}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700119template LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<4> helper_offset,
120 bool safepoint_pc, bool use_link);
121template LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<8> helper_offset,
122 bool safepoint_pc, bool use_link);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123
Andreas Gampe2f244e92014-05-08 03:35:25 -0700124template <size_t pointer_size>
125void Mir2Lir::CallRuntimeHelper(ThreadOffset<pointer_size> helper_offset, bool safepoint_pc) {
Mingyao Yang42894562014-04-07 12:42:16 -0700126 RegStorage r_tgt = CallHelperSetup(helper_offset);
127 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700128 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Mingyao Yang42894562014-04-07 12:42:16 -0700129}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700130INSTANTIATE(void Mir2Lir::CallRuntimeHelper, bool safepoint_pc)
Mingyao Yang42894562014-04-07 12:42:16 -0700131
Andreas Gampe2f244e92014-05-08 03:35:25 -0700132template <size_t pointer_size>
133void Mir2Lir::CallRuntimeHelperImm(ThreadOffset<pointer_size> helper_offset, int arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800134 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000136 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700137 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700139INSTANTIATE(void Mir2Lir::CallRuntimeHelperImm, int arg0, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140
Andreas Gampe2f244e92014-05-08 03:35:25 -0700141template <size_t pointer_size>
142void Mir2Lir::CallRuntimeHelperReg(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700143 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800144 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 OpRegCopy(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000146 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700147 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700149INSTANTIATE(void Mir2Lir::CallRuntimeHelperReg, RegStorage arg0, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150
Andreas Gampe2f244e92014-05-08 03:35:25 -0700151template <size_t pointer_size>
152void Mir2Lir::CallRuntimeHelperRegLocation(ThreadOffset<pointer_size> helper_offset,
153 RegLocation arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800154 RegStorage r_tgt = CallHelperSetup(helper_offset);
155 if (arg0.wide == 0) {
156 LoadValueDirectFixed(arg0, TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800158 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
159 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000161 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700162 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700164INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegLocation, RegLocation arg0, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165
Andreas Gampe2f244e92014-05-08 03:35:25 -0700166template <size_t pointer_size>
167void Mir2Lir::CallRuntimeHelperImmImm(ThreadOffset<pointer_size> helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800169 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 LoadConstant(TargetReg(kArg0), arg0);
171 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000172 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700173 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700175INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmImm, int arg0, int arg1, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176
Andreas Gampe2f244e92014-05-08 03:35:25 -0700177template <size_t pointer_size>
178void Mir2Lir::CallRuntimeHelperImmRegLocation(ThreadOffset<pointer_size> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 RegLocation arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800180 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 if (arg1.wide == 0) {
182 LoadValueDirectFixed(arg1, TargetReg(kArg1));
183 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800184 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
185 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 }
187 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000188 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700189 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700191INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmRegLocation, int arg0, RegLocation arg1,
192 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193
Andreas Gampe2f244e92014-05-08 03:35:25 -0700194template <size_t pointer_size>
195void Mir2Lir::CallRuntimeHelperRegLocationImm(ThreadOffset<pointer_size> helper_offset,
196 RegLocation arg0, int arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800197 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 LoadValueDirectFixed(arg0, TargetReg(kArg0));
199 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000200 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700201 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700203INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegLocationImm, RegLocation arg0, int arg1,
204 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205
Andreas Gampe2f244e92014-05-08 03:35:25 -0700206template <size_t pointer_size>
207void Mir2Lir::CallRuntimeHelperImmReg(ThreadOffset<pointer_size> helper_offset, int arg0,
208 RegStorage arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800209 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 OpRegCopy(TargetReg(kArg1), arg1);
211 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000212 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700213 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700215INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmReg, int arg0, RegStorage arg1, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216
Andreas Gampe2f244e92014-05-08 03:35:25 -0700217template <size_t pointer_size>
218void Mir2Lir::CallRuntimeHelperRegImm(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
219 int arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800220 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 OpRegCopy(TargetReg(kArg0), arg0);
222 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000223 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700224 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700226INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegImm, RegStorage arg0, int arg1, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227
Andreas Gampe2f244e92014-05-08 03:35:25 -0700228template <size_t pointer_size>
229void Mir2Lir::CallRuntimeHelperImmMethod(ThreadOffset<pointer_size> helper_offset, int arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700230 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800231 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 LoadCurrMethodDirect(TargetReg(kArg1));
233 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000234 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700235 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700237INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmMethod, int arg0, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238
Andreas Gampe2f244e92014-05-08 03:35:25 -0700239template <size_t pointer_size>
240void Mir2Lir::CallRuntimeHelperRegMethod(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800241 bool safepoint_pc) {
242 RegStorage r_tgt = CallHelperSetup(helper_offset);
243 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800244 if (TargetReg(kArg0) != arg0) {
245 OpRegCopy(TargetReg(kArg0), arg0);
246 }
247 LoadCurrMethodDirect(TargetReg(kArg1));
248 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700249 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800250}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700251INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegMethod, RegStorage arg0, bool safepoint_pc)
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800252
Andreas Gampe2f244e92014-05-08 03:35:25 -0700253template <size_t pointer_size>
254void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(ThreadOffset<pointer_size> helper_offset,
255 RegStorage arg0, RegLocation arg2,
256 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800257 RegStorage r_tgt = CallHelperSetup(helper_offset);
258 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800259 if (TargetReg(kArg0) != arg0) {
260 OpRegCopy(TargetReg(kArg0), arg0);
261 }
262 LoadCurrMethodDirect(TargetReg(kArg1));
263 LoadValueDirectFixed(arg2, TargetReg(kArg2));
264 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700265 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800266}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700267INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegMethodRegLocation, RegStorage arg0, RegLocation arg2,
268 bool safepoint_pc)
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800269
Andreas Gampe2f244e92014-05-08 03:35:25 -0700270template <size_t pointer_size>
271void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(ThreadOffset<pointer_size> helper_offset,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700272 RegLocation arg0, RegLocation arg1,
273 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800274 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 if (arg0.wide == 0) {
276 LoadValueDirectFixed(arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
277 if (arg1.wide == 0) {
278 if (cu_->instruction_set == kMips) {
279 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1));
Zheng Xu2d41a652014-06-09 11:05:31 +0800280 } else if (cu_->instruction_set == kArm64) {
281 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 } else {
283 LoadValueDirectFixed(arg1, TargetReg(kArg1));
284 }
285 } else {
286 if (cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -0800287 RegStorage r_tmp;
288 if (arg1.fp) {
289 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
290 } else {
291 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
292 }
293 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 } else {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700295 RegStorage r_tmp;
296 if (cu_->instruction_set == kX86_64) {
297 r_tmp = RegStorage::Solo64(TargetReg(kArg1).GetReg());
298 } else {
299 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
300 }
buzbee2700f7e2014-03-07 09:46:20 -0800301 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 }
303 }
304 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800305 RegStorage r_tmp;
306 if (arg0.fp) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700307 if (cu_->instruction_set == kX86_64) {
308 r_tmp = RegStorage::FloatSolo64(TargetReg(kFArg0).GetReg());
309 } else {
310 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg0), TargetReg(kFArg1));
311 }
buzbee2700f7e2014-03-07 09:46:20 -0800312 } else {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700313 if (cu_->instruction_set == kX86_64) {
314 r_tmp = RegStorage::Solo64(TargetReg(kArg0).GetReg());
315 } else {
316 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
317 }
buzbee2700f7e2014-03-07 09:46:20 -0800318 }
319 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 if (arg1.wide == 0) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700321 if (cu_->instruction_set == kX86_64) {
322 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
323 } else {
324 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2));
325 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800327 RegStorage r_tmp;
328 if (arg1.fp) {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700329 if (cu_->instruction_set == kX86_64) {
330 r_tmp = RegStorage::FloatSolo64(TargetReg(kFArg1).GetReg());
331 } else {
332 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
333 }
buzbee2700f7e2014-03-07 09:46:20 -0800334 } else {
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700335 if (cu_->instruction_set == kX86_64) {
336 r_tmp = RegStorage::Solo64(TargetReg(kArg1).GetReg());
337 } else {
338 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
339 }
buzbee2700f7e2014-03-07 09:46:20 -0800340 }
341 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 }
343 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000344 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700345 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700347INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegLocationRegLocation, RegLocation arg0,
348 RegLocation arg1, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349
Mingyao Yang80365d92014-04-18 12:10:58 -0700350void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
351 if (arg1.GetReg() == TargetReg(kArg0).GetReg()) {
352 if (arg0.GetReg() == TargetReg(kArg1).GetReg()) {
353 // Swap kArg0 and kArg1 with kArg2 as temp.
354 OpRegCopy(TargetReg(kArg2), arg1);
355 OpRegCopy(TargetReg(kArg0), arg0);
356 OpRegCopy(TargetReg(kArg1), TargetReg(kArg2));
357 } else {
358 OpRegCopy(TargetReg(kArg1), arg1);
359 OpRegCopy(TargetReg(kArg0), arg0);
360 }
361 } else {
362 OpRegCopy(TargetReg(kArg0), arg0);
363 OpRegCopy(TargetReg(kArg1), arg1);
364 }
365}
366
Andreas Gampe2f244e92014-05-08 03:35:25 -0700367template <size_t pointer_size>
368void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800369 RegStorage arg1, bool safepoint_pc) {
370 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700371 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000372 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700373 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700375INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegReg, RegStorage arg0, RegStorage arg1,
376 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377
Andreas Gampe2f244e92014-05-08 03:35:25 -0700378template <size_t pointer_size>
379void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800380 RegStorage arg1, int arg2, bool safepoint_pc) {
381 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700382 CopyToArgumentRegs(arg0, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 LoadConstant(TargetReg(kArg2), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000384 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700385 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700387INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegRegImm, RegStorage arg0, RegStorage arg1, int arg2,
388 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389
Andreas Gampe2f244e92014-05-08 03:35:25 -0700390template <size_t pointer_size>
391void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset<pointer_size> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 int arg0, RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800393 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 LoadValueDirectFixed(arg2, TargetReg(kArg2));
395 LoadCurrMethodDirect(TargetReg(kArg1));
396 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000397 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700398 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700400INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmMethodRegLocation, int arg0, RegLocation arg2,
401 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402
Andreas Gampe2f244e92014-05-08 03:35:25 -0700403template <size_t pointer_size>
404void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset<pointer_size> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 int arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800406 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 LoadCurrMethodDirect(TargetReg(kArg1));
408 LoadConstant(TargetReg(kArg2), arg2);
409 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000410 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700411 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700413INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmMethodImm, int arg0, int arg2, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414
Andreas Gampe2f244e92014-05-08 03:35:25 -0700415template <size_t pointer_size>
416void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset<pointer_size> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 int arg0, RegLocation arg1,
418 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800419 RegStorage r_tgt = CallHelperSetup(helper_offset);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700420 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
421 // instantiation bug in GCC.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 LoadValueDirectFixed(arg1, TargetReg(kArg1));
423 if (arg2.wide == 0) {
424 LoadValueDirectFixed(arg2, TargetReg(kArg2));
425 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800426 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
427 LoadValueDirectWideFixed(arg2, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 }
429 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000430 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700431 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700433INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation, int arg0, RegLocation arg1,
434 RegLocation arg2, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435
Andreas Gampe2f244e92014-05-08 03:35:25 -0700436template <size_t pointer_size>
437void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset<pointer_size> helper_offset,
Ian Rogersa9a82542013-10-04 11:17:26 -0700438 RegLocation arg0, RegLocation arg1,
439 RegLocation arg2,
440 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800441 RegStorage r_tgt = CallHelperSetup(helper_offset);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700442 DCHECK_EQ(static_cast<unsigned int>(arg0.wide), 0U);
Ian Rogersa9a82542013-10-04 11:17:26 -0700443 LoadValueDirectFixed(arg0, TargetReg(kArg0));
Andreas Gampe2f244e92014-05-08 03:35:25 -0700444 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U);
Ian Rogersa9a82542013-10-04 11:17:26 -0700445 LoadValueDirectFixed(arg1, TargetReg(kArg1));
Andreas Gampe2f244e92014-05-08 03:35:25 -0700446 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U);
Ian Rogersa9a82542013-10-04 11:17:26 -0700447 LoadValueDirectFixed(arg2, TargetReg(kArg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000448 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700449 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700450}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700451INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation, RegLocation arg0,
452 RegLocation arg1, RegLocation arg2, bool safepoint_pc)
Ian Rogersa9a82542013-10-04 11:17:26 -0700453
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454/*
455 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100456 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 * assignment of promoted arguments.
458 *
459 * ArgLocs is an array of location records describing the incoming arguments
460 * with one location record per word of argument.
461 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700462void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800464 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 * It will attempt to keep kArg0 live (or copy it to home location
466 * if promoted).
467 */
468 RegLocation rl_src = rl_method;
469 rl_src.location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800470 rl_src.reg = TargetReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700472 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700473 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 // If Method* has been promoted, explicitly flush
475 if (rl_method.location == kLocPhysReg) {
buzbeef2c3e562014-05-29 12:37:25 -0700476 StoreRefDisp(TargetReg(kSp), 0, TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 }
478
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800479 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800481 }
482
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
484 /*
485 * Copy incoming arguments to their proper home locations.
486 * NOTE: an older version of dx had an issue in which
487 * it would reuse static method argument registers.
488 * This could result in the same Dalvik virtual register
489 * being promoted to both core and fp regs. To account for this,
490 * we only copy to the corresponding promoted physical register
491 * if it matches the type of the SSA name for the incoming
492 * argument. It is also possible that long and double arguments
493 * end up half-promoted. In those cases, we must flush the promoted
494 * half to memory as well.
495 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100496 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 for (int i = 0; i < cu_->num_ins; i++) {
498 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800499 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800500
buzbee2700f7e2014-03-07 09:46:20 -0800501 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 // If arriving in register
503 bool need_flush = true;
504 RegLocation* t_loc = &ArgLocs[i];
505 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800506 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 need_flush = false;
508 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800509 OpRegCopy(RegStorage::Solo32(v_map->FpReg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 need_flush = false;
511 } else {
512 need_flush = true;
513 }
514
buzbeed0a03b82013-09-14 08:21:05 -0700515 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 if (t_loc->wide) {
517 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700518 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 need_flush |= (p_map->core_location != v_map->core_location) ||
520 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700521 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
522 /*
523 * In Arm, a double is represented as a pair of consecutive single float
524 * registers starting at an even number. It's possible that both Dalvik vRegs
525 * representing the incoming double were independently promoted as singles - but
526 * not in a form usable as a double. If so, we need to flush - even though the
527 * incoming arg appears fully in register. At this point in the code, both
528 * halves of the double are promoted. Make sure they are in a usable form.
529 */
530 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
531 int low_reg = promotion_map_[lowreg_index].FpReg;
532 int high_reg = promotion_map_[lowreg_index + 1].FpReg;
533 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
534 need_flush = true;
535 }
536 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 }
538 if (need_flush) {
buzbee695d13a2014-04-19 13:32:20 -0700539 Store32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 }
541 } else {
542 // If arriving in frame & promoted
543 if (v_map->core_location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700544 Load32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 }
546 if (v_map->fp_location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700547 Load32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->FpReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 }
549 }
550 }
551}
552
553/*
554 * Bit of a hack here - in the absence of a real scheduling pass,
555 * emit the next instruction in static & direct invoke sequences.
556 */
557static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
558 int state, const MethodReference& target_method,
559 uint32_t unused,
560 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700561 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 if (direct_code != 0 && direct_method != 0) {
564 switch (state) {
565 case 0: // Get the current Method* [sets kArg0]
Ian Rogersff093b32014-04-30 19:04:27 -0700566 if (direct_code != static_cast<uintptr_t>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700567 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700568 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
569 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700570 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700571 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 }
Ian Rogersff093b32014-04-30 19:04:27 -0700573 if (direct_method != static_cast<uintptr_t>(-1)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 cg->LoadConstant(cg->TargetReg(kArg0), direct_method);
575 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700576 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 }
578 break;
579 default:
580 return -1;
581 }
582 } else {
583 switch (state) {
584 case 0: // Get the current Method* [sets kArg0]
585 // TUNING: we can save a reg copy if Method* has been promoted.
586 cg->LoadCurrMethodDirect(cg->TargetReg(kArg0));
587 break;
588 case 1: // Get method->dex_cache_resolved_methods_
buzbee695d13a2014-04-19 13:32:20 -0700589 cg->LoadRefDisp(cg->TargetReg(kArg0),
590 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
591 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 // Set up direct code if known.
593 if (direct_code != 0) {
Ian Rogersff093b32014-04-30 19:04:27 -0700594 if (direct_code != static_cast<uintptr_t>(-1)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700596 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700597 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700598 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 }
600 }
601 break;
602 case 2: // Grab target method*
603 CHECK_EQ(cu->dex_file, target_method.dex_file);
buzbee695d13a2014-04-19 13:32:20 -0700604 cg->LoadRefDisp(cg->TargetReg(kArg0),
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700605 ObjArray::OffsetOfElement(target_method.dex_method_index).Int32Value(),
606 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 break;
608 case 3: // Grab the code from the method*
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700609 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 if (direct_code == 0) {
611 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800612 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 cg->TargetReg(kInvokeTgt));
614 }
615 break;
616 }
617 // Intentional fallthrough for x86
618 default:
619 return -1;
620 }
621 }
622 return state + 1;
623}
624
625/*
626 * Bit of a hack here - in the absence of a real scheduling pass,
627 * emit the next instruction in a virtual invoke sequence.
628 * We can use kLr as a temp prior to target address loading
629 * Note also that we'll load the first argument ("this") into
630 * kArg1 here rather than the standard LoadArgRegs.
631 */
632static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
633 int state, const MethodReference& target_method,
634 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700635 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
637 /*
638 * This is the fast path in which the target virtual method is
639 * fully resolved at compile time.
640 */
641 switch (state) {
642 case 0: { // Get "this" [set kArg1]
643 RegLocation rl_arg = info->args[0];
644 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
645 break;
646 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700647 case 1: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800648 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 // get this->klass_ [use kArg1, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700650 cg->LoadRefDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
651 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800652 cg->MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700654 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700655 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
656 cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700658 case 3: // Get target method [use kInvokeTgt, set kArg0]
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700659 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt),
660 ObjArray::OffsetOfElement(method_idx).Int32Value(),
buzbee695d13a2014-04-19 13:32:20 -0700661 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700663 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700664 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800666 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 cg->TargetReg(kInvokeTgt));
668 break;
669 }
670 // Intentional fallthrough for X86
671 default:
672 return -1;
673 }
674 return state + 1;
675}
676
677/*
Jeff Hao88474b42013-10-23 16:24:40 -0700678 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
679 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
680 * more than one interface method map to the same index. Note also that we'll load the first
681 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 */
683static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
684 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700685 uint32_t method_idx, uintptr_t unused,
686 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688
Jeff Hao88474b42013-10-23 16:24:40 -0700689 switch (state) {
690 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700691 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
692 cg->LoadConstant(cg->TargetReg(kHiddenArg), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400693 if (cu->instruction_set == kX86) {
Jeff Hao88474b42013-10-23 16:24:40 -0700694 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg), cg->TargetReg(kHiddenArg));
695 }
696 break;
697 case 1: { // Get "this" [set kArg1]
698 RegLocation rl_arg = info->args[0];
699 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
700 break;
701 }
702 case 2: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800703 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700704 // Get this->klass_ [use kArg1, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700705 cg->LoadRefDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
706 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800707 cg->MarkPossibleNullPointerException(info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700708 break;
709 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700710 // NOTE: native pointer.
711 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
712 cg->TargetReg(kInvokeTgt));
Jeff Hao88474b42013-10-23 16:24:40 -0700713 break;
714 case 4: // Get target method [use kInvokeTgt, set kArg0]
buzbee695d13a2014-04-19 13:32:20 -0700715 // NOTE: native pointer.
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700716 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt),
717 ObjArray::OffsetOfElement(method_idx % ClassLinker::kImtSize).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 cg->TargetReg(kArg0));
719 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700720 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700721 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700722 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800723 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Jeff Hao88474b42013-10-23 16:24:40 -0700724 cg->TargetReg(kInvokeTgt));
725 break;
726 }
727 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 default:
729 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 }
731 return state + 1;
732}
733
Andreas Gampe2f244e92014-05-08 03:35:25 -0700734template <size_t pointer_size>
735static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset<pointer_size> trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700737 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
739 /*
740 * This handles the case in which the base method is not fully
741 * resolved at compile time, we bail to a runtime helper.
742 */
743 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700744 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 // Load trampoline target
Ian Rogers848871b2013-08-05 10:56:33 -0700746 cg->LoadWordDisp(cg->TargetReg(kSelf), trampoline.Int32Value(), cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 }
748 // Load kArg0 with method index
749 CHECK_EQ(cu->dex_file, target_method.dex_file);
750 cg->LoadConstant(cg->TargetReg(kArg0), target_method.dex_method_index);
751 return 1;
752 }
753 return -1;
754}
755
756static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
757 int state,
758 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000759 uint32_t unused, uintptr_t unused2,
760 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700761 if (Is64BitInstructionSet(cu->instruction_set)) {
762 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeStaticTrampolineWithAccessCheck);
763 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
764 } else {
765 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
766 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
767 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768}
769
770static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
771 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000772 uint32_t unused, uintptr_t unused2,
773 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700774 if (Is64BitInstructionSet(cu->instruction_set)) {
775 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeDirectTrampolineWithAccessCheck);
776 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
777 } else {
778 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
779 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
780 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781}
782
783static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
784 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000785 uint32_t unused, uintptr_t unused2,
786 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700787 if (Is64BitInstructionSet(cu->instruction_set)) {
788 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeSuperTrampolineWithAccessCheck);
789 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
790 } else {
791 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
792 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
793 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794}
795
796static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
797 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000798 uint32_t unused, uintptr_t unused2,
799 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700800 if (Is64BitInstructionSet(cu->instruction_set)) {
801 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeVirtualTrampolineWithAccessCheck);
802 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
803 } else {
804 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
805 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
806 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807}
808
809static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
810 CallInfo* info, int state,
811 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000812 uint32_t unused, uintptr_t unused2,
813 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700814 if (Is64BitInstructionSet(cu->instruction_set)) {
815 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeInterfaceTrampolineWithAccessCheck);
816 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
817 } else {
818 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
819 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
820 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821}
822
823int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
824 NextCallInsn next_call_insn,
825 const MethodReference& target_method,
826 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700827 uintptr_t direct_method, InvokeType type, bool skip_this) {
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700828 int last_arg_reg = 3 - 1;
829 int arg_regs[3] = {TargetReg(kArg1).GetReg(), TargetReg(kArg2).GetReg(), TargetReg(kArg3).GetReg()};
830
831 int next_reg = 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 int next_arg = 0;
833 if (skip_this) {
834 next_reg++;
835 next_arg++;
836 }
837 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
838 RegLocation rl_arg = info->args[next_arg++];
839 rl_arg = UpdateRawLoc(rl_arg);
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700840 if (rl_arg.wide && (next_reg <= last_arg_reg - 1)) {
841 RegStorage r_tmp(RegStorage::k64BitPair, arg_regs[next_reg], arg_regs[next_reg + 1]);
buzbee2700f7e2014-03-07 09:46:20 -0800842 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 next_reg++;
844 next_arg++;
845 } else {
846 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800847 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 rl_arg.is_const = false;
849 }
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700850 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(arg_regs[next_reg]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 }
852 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
853 direct_code, direct_method, type);
854 }
855 return call_state;
856}
857
858/*
859 * Load up to 5 arguments, the first three of which will be in
860 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
861 * and as part of the load sequence, it must be replaced with
862 * the target method pointer. Note, this may also be called
863 * for "range" variants if the number of arguments is 5 or fewer.
864 */
865int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
866 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
867 const MethodReference& target_method,
868 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700869 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 RegLocation rl_arg;
871
872 /* If no arguments, just return */
873 if (info->num_arg_words == 0)
874 return call_state;
875
876 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
877 direct_code, direct_method, type);
878
879 DCHECK_LE(info->num_arg_words, 5);
880 if (info->num_arg_words > 3) {
881 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700882 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 RegLocation rl_use0 = info->args[0];
884 RegLocation rl_use1 = info->args[1];
885 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800886 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
887 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 // Wide spans, we need the 2nd half of uses[2].
889 rl_arg = UpdateLocWide(rl_use2);
890 if (rl_arg.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -0700891 if (rl_arg.reg.IsPair()) {
892 reg = rl_arg.reg.GetHigh();
893 } else {
894 RegisterInfo* info = GetRegInfo(rl_arg.reg);
895 info = info->FindMatchingView(RegisterInfo::kHighSingleStorageMask);
896 if (info == nullptr) {
897 // NOTE: For hard float convention we won't split arguments across reg/mem.
898 UNIMPLEMENTED(FATAL) << "Needs hard float api.";
899 }
900 reg = info->GetReg();
901 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 } else {
903 // kArg2 & rArg3 can safely be used here
904 reg = TargetReg(kArg3);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100905 {
906 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
907 Load32Disp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
908 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 call_state = next_call_insn(cu_, info, call_state, target_method,
910 vtable_idx, direct_code, direct_method, type);
911 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100912 {
913 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
914 Store32Disp(TargetReg(kSp), (next_use + 1) * 4, reg);
915 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
917 direct_code, direct_method, type);
918 next_use++;
919 }
920 // Loop through the rest
921 while (next_use < info->num_arg_words) {
buzbee091cc402014-03-31 10:14:40 -0700922 RegStorage arg_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 rl_arg = info->args[next_use];
924 rl_arg = UpdateRawLoc(rl_arg);
925 if (rl_arg.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700926 arg_reg = rl_arg.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 } else {
buzbee091cc402014-03-31 10:14:40 -0700928 arg_reg = rl_arg.wide ? RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)) :
929 TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700930 if (rl_arg.wide) {
buzbee091cc402014-03-31 10:14:40 -0700931 LoadValueDirectWideFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 } else {
buzbee091cc402014-03-31 10:14:40 -0700933 LoadValueDirectFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 }
935 call_state = next_call_insn(cu_, info, call_state, target_method,
936 vtable_idx, direct_code, direct_method, type);
937 }
938 int outs_offset = (next_use + 1) * 4;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100939 {
940 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
941 if (rl_arg.wide) {
942 StoreBaseDisp(TargetReg(kSp), outs_offset, arg_reg, k64);
943 next_use += 2;
944 } else {
945 Store32Disp(TargetReg(kSp), outs_offset, arg_reg);
946 next_use++;
947 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948 }
949 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
950 direct_code, direct_method, type);
951 }
952 }
953
954 call_state = LoadArgRegs(info, call_state, next_call_insn,
955 target_method, vtable_idx, direct_code, direct_method,
956 type, skip_this);
957
958 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700959 if (Runtime::Current()->ExplicitNullChecks()) {
960 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
961 } else {
962 *pcrLabel = nullptr;
963 // In lieu of generating a check for kArg1 being null, we need to
964 // perform a load when doing implicit checks.
965 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700966 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -0700967 MarkPossibleNullPointerException(info->opt_flags);
968 FreeTemp(tmp);
969 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 }
971 return call_state;
972}
973
974/*
975 * May have 0+ arguments (also used for jumbo). Note that
976 * source virtual registers may be in physical registers, so may
977 * need to be flushed to home location before copying. This
978 * applies to arg3 and above (see below).
979 *
980 * Two general strategies:
981 * If < 20 arguments
982 * Pass args 3-18 using vldm/vstm block copy
983 * Pass arg0, arg1 & arg2 in kArg1-kArg3
984 * If 20+ arguments
985 * Pass args arg19+ using memcpy block copy
986 * Pass arg0, arg1 & arg2 in kArg1-kArg3
987 *
988 */
989int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
990 LIR** pcrLabel, NextCallInsn next_call_insn,
991 const MethodReference& target_method,
992 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700993 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 // If we can treat it as non-range (Jumbo ops will use range form)
995 if (info->num_arg_words <= 5)
996 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
997 next_call_insn, target_method, vtable_idx,
998 direct_code, direct_method, type, skip_this);
999 /*
1000 * First load the non-register arguments. Both forms expect all
1001 * of the source arguments to be in their home frame location, so
1002 * scan the s_reg names and flush any that have been promoted to
1003 * frame backing storage.
1004 */
1005 // Scan the rest of the args - if in phys_reg flush to memory
1006 for (int next_arg = 0; next_arg < info->num_arg_words;) {
1007 RegLocation loc = info->args[next_arg];
1008 if (loc.wide) {
1009 loc = UpdateLocWide(loc);
1010 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001011 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Vladimir Marko455759b2014-05-06 20:49:36 +01001012 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 }
1014 next_arg += 2;
1015 } else {
1016 loc = UpdateLoc(loc);
1017 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001018 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee695d13a2014-04-19 13:32:20 -07001019 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 }
1021 next_arg++;
1022 }
1023 }
1024
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001025 // Logic below assumes that Method pointer is at offset zero from SP.
1026 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
1027
1028 // The first 3 arguments are passed via registers.
1029 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
1030 // get size of uintptr_t or size of object reference according to model being used.
1031 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001033 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
1034 DCHECK_GT(regs_left_to_pass_via_stack, 0);
1035
1036 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
1037 // Use vldm/vstm pair using kArg3 as a temp
1038 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1039 direct_code, direct_method, type);
1040 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001041 LIR* ld = nullptr;
1042 {
1043 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1044 ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
1045 }
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001046 // TUNING: loosen barrier
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001047 ld->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001048 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1049 direct_code, direct_method, type);
1050 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
1051 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1052 direct_code, direct_method, type);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001053 LIR* st = nullptr;
1054 {
1055 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
1056 st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
1057 }
1058 st->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001059 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1060 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001061 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001062 int current_src_offset = start_offset;
1063 int current_dest_offset = outs_offset;
1064
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001065 // Only davik regs are accessed in this loop; no next_call_insn() calls.
1066 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001067 while (regs_left_to_pass_via_stack > 0) {
1068 // This is based on the knowledge that the stack itself is 16-byte aligned.
1069 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
1070 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
1071 size_t bytes_to_move;
1072
1073 /*
1074 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
1075 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
1076 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
1077 * We do this because we could potentially do a smaller move to align.
1078 */
1079 if (regs_left_to_pass_via_stack == 4 ||
1080 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
1081 // Moving 128-bits via xmm register.
1082 bytes_to_move = sizeof(uint32_t) * 4;
1083
1084 // Allocate a free xmm temp. Since we are working through the calling sequence,
Mark Mendelle87f9b52014-04-30 14:13:18 -04001085 // we expect to have an xmm temporary available. AllocTempDouble will abort if
1086 // there are no free registers.
buzbee2700f7e2014-03-07 09:46:20 -08001087 RegStorage temp = AllocTempDouble();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001088
1089 LIR* ld1 = nullptr;
1090 LIR* ld2 = nullptr;
1091 LIR* st1 = nullptr;
1092 LIR* st2 = nullptr;
1093
1094 /*
1095 * The logic is similar for both loads and stores. If we have 16-byte alignment,
1096 * do an aligned move. If we have 8-byte alignment, then do the move in two
1097 * parts. This approach prevents possible cache line splits. Finally, fall back
1098 * to doing an unaligned move. In most cases we likely won't split the cache
1099 * line but we cannot prove it and thus take a conservative approach.
1100 */
1101 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
1102 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
1103
1104 if (src_is_16b_aligned) {
1105 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
1106 } else if (src_is_8b_aligned) {
1107 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -08001108 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
1109 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001110 } else {
1111 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
1112 }
1113
1114 if (dest_is_16b_aligned) {
1115 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
1116 } else if (dest_is_8b_aligned) {
1117 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -08001118 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
1119 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001120 } else {
1121 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
1122 }
1123
1124 // TODO If we could keep track of aliasing information for memory accesses that are wider
1125 // than 64-bit, we wouldn't need to set up a barrier.
1126 if (ld1 != nullptr) {
1127 if (ld2 != nullptr) {
1128 // For 64-bit load we can actually set up the aliasing information.
1129 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
1130 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
1131 } else {
1132 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001133 ld1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001134 }
1135 }
1136 if (st1 != nullptr) {
1137 if (st2 != nullptr) {
1138 // For 64-bit store we can actually set up the aliasing information.
1139 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
1140 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
1141 } else {
1142 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001143 st1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001144 }
1145 }
1146
1147 // Free the temporary used for the data movement.
buzbee091cc402014-03-31 10:14:40 -07001148 FreeTemp(temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001149 } else {
1150 // Moving 32-bits via general purpose register.
1151 bytes_to_move = sizeof(uint32_t);
1152
1153 // Instead of allocating a new temp, simply reuse one of the registers being used
1154 // for argument passing.
buzbee2700f7e2014-03-07 09:46:20 -08001155 RegStorage temp = TargetReg(kArg3);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001156
1157 // Now load the argument VR and store to the outs.
buzbee695d13a2014-04-19 13:32:20 -07001158 Load32Disp(TargetReg(kSp), current_src_offset, temp);
1159 Store32Disp(TargetReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001160 }
1161
1162 current_src_offset += bytes_to_move;
1163 current_dest_offset += bytes_to_move;
1164 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1165 }
1166 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 // Generate memcpy
1168 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
1169 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001170 if (Is64BitInstructionSet(cu_->instruction_set)) {
1171 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(8, pMemcpy), TargetReg(kArg0),
1172 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
1173 } else {
1174 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(4, pMemcpy), TargetReg(kArg0),
1175 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
1176 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 }
1178
1179 call_state = LoadArgRegs(info, call_state, next_call_insn,
1180 target_method, vtable_idx, direct_code, direct_method,
1181 type, skip_this);
1182
1183 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1184 direct_code, direct_method, type);
1185 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -07001186 if (Runtime::Current()->ExplicitNullChecks()) {
1187 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
1188 } else {
1189 *pcrLabel = nullptr;
1190 // In lieu of generating a check for kArg1 being null, we need to
1191 // perform a load when doing implicit checks.
1192 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001193 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -07001194 MarkPossibleNullPointerException(info->opt_flags);
1195 FreeTemp(tmp);
1196 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 }
1198 return call_state;
1199}
1200
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001201RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 RegLocation res;
1203 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001204 res = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 } else {
1206 res = info->result;
1207 }
1208 return res;
1209}
1210
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001211RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 RegLocation res;
1213 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001214 res = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 } else {
1216 res = info->result;
1217 }
1218 return res;
1219}
1220
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001221bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 if (cu_->instruction_set == kMips) {
1223 // TODO - add Mips implementation
1224 return false;
1225 }
1226 // Location of reference to data array
1227 int value_offset = mirror::String::ValueOffset().Int32Value();
1228 // Location of count
1229 int count_offset = mirror::String::CountOffset().Int32Value();
1230 // Starting offset within data array
1231 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1232 // Start of char data with array_
1233 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1234
1235 RegLocation rl_obj = info->args[0];
1236 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -07001237 rl_obj = LoadValue(rl_obj, kRefReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001238 // X86 wants to avoid putting a constant index into a register.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001239 if (!((cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64)&& rl_idx.is_const)) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001240 rl_idx = LoadValue(rl_idx, kCoreReg);
1241 }
buzbee2700f7e2014-03-07 09:46:20 -08001242 RegStorage reg_max;
1243 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001245 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001246 RegStorage reg_off;
1247 RegStorage reg_ptr;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001248 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 reg_off = AllocTemp();
buzbeea0cd2d72014-06-01 09:33:49 -07001250 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 if (range_check) {
1252 reg_max = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001253 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001254 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 }
buzbee695d13a2014-04-19 13:32:20 -07001256 Load32Disp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001257 MarkPossibleNullPointerException(info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001258 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 if (range_check) {
Mingyao Yang3a74d152014-04-21 15:39:44 -07001260 // Set up a slow path to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001261 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001263 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001264 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001265 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 } else {
1267 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001268 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001270 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001271 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001272 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Vladimir Marko3bc86152014-03-13 14:11:28 +00001273 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001274 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001275 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001276 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001277 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 }
1279 reg_off = AllocTemp();
buzbeea0cd2d72014-06-01 09:33:49 -07001280 reg_ptr = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -07001281 Load32Disp(rl_obj.reg, offset_offset, reg_off);
buzbeea0cd2d72014-06-01 09:33:49 -07001282 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001284 if (rl_idx.is_const) {
1285 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1286 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001287 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001288 }
buzbee2700f7e2014-03-07 09:46:20 -08001289 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001290 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001291 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001292 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 RegLocation rl_dest = InlineTarget(info);
1294 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001295 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee2700f7e2014-03-07 09:46:20 -08001296 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001297 } else {
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001298 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001299 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 FreeTemp(reg_off);
1301 FreeTemp(reg_ptr);
1302 StoreValue(rl_dest, rl_result);
1303 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001304 DCHECK(range_check_branch != nullptr);
1305 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001306 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 return true;
1309}
1310
1311// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001312bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 if (cu_->instruction_set == kMips) {
1314 // TODO - add Mips implementation
1315 return false;
1316 }
1317 // dst = src.length();
1318 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001319 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 RegLocation rl_dest = InlineTarget(info);
1321 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001322 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001323 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001324 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 if (is_empty) {
1326 // dst = (dst == 0);
1327 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001328 RegStorage t_reg = AllocTemp();
1329 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1330 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001331 } else if (cu_->instruction_set == kArm64) {
1332 OpRegImm(kOpSub, rl_result.reg, 1);
1333 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001335 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001336 OpRegImm(kOpSub, rl_result.reg, 1);
1337 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338 }
1339 }
1340 StoreValue(rl_dest, rl_result);
1341 return true;
1342}
1343
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001344bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1345 if (cu_->instruction_set == kMips) {
1346 // TODO - add Mips implementation
1347 return false;
1348 }
1349 RegLocation rl_src_i = info->args[0];
buzbee695d13a2014-04-19 13:32:20 -07001350 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001351 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001352 if (size == k64) {
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001353 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001354 if (cu_->instruction_set == kArm64) {
1355 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1356 StoreValueWide(rl_dest, rl_result);
1357 return true;
1358 }
buzbee2700f7e2014-03-07 09:46:20 -08001359 RegStorage r_i_low = rl_i.reg.GetLow();
1360 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001361 // 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 +00001362 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001363 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001364 }
buzbee2700f7e2014-03-07 09:46:20 -08001365 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1366 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1367 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001368 FreeTemp(r_i_low);
1369 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001370 StoreValueWide(rl_dest, rl_result);
1371 } else {
buzbee695d13a2014-04-19 13:32:20 -07001372 DCHECK(size == k32 || size == kSignedHalf);
1373 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001374 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001375 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001376 StoreValue(rl_dest, rl_result);
1377 }
1378 return true;
1379}
1380
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001381bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 if (cu_->instruction_set == kMips) {
1383 // TODO - add Mips implementation
1384 return false;
1385 }
1386 RegLocation rl_src = info->args[0];
1387 rl_src = LoadValue(rl_src, kCoreReg);
1388 RegLocation rl_dest = InlineTarget(info);
1389 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001390 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001392 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1393 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1394 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 StoreValue(rl_dest, rl_result);
1396 return true;
1397}
1398
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001399bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001400 if (cu_->instruction_set == kMips) {
1401 // TODO - add Mips implementation
1402 return false;
1403 }
Vladimir Markob9823312014-03-20 17:38:43 +00001404 RegLocation rl_src = info->args[0];
1405 rl_src = LoadValueWide(rl_src, kCoreReg);
1406 RegLocation rl_dest = InlineTargetWide(info);
1407 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1408
1409 // If on x86 or if we would clobber a register needed later, just copy the source first.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001410 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 || rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
buzbee2700f7e2014-03-07 09:46:20 -08001411 OpRegCopyWide(rl_result.reg, rl_src.reg);
1412 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1413 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1414 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001415 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1416 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001417 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001418 }
1419 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001420 }
Vladimir Markob9823312014-03-20 17:38:43 +00001421
1422 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001423 RegStorage sign_reg = AllocTemp();
1424 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1425 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1426 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1427 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1428 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
buzbee082833c2014-05-17 23:16:26 -07001429 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001430 StoreValueWide(rl_dest, rl_result);
1431 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432}
1433
Yixin Shoudbb17e32014-02-07 05:09:30 -08001434bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1435 if (cu_->instruction_set == kMips) {
1436 // TODO - add Mips implementation
1437 return false;
1438 }
1439 RegLocation rl_src = info->args[0];
1440 rl_src = LoadValue(rl_src, kCoreReg);
1441 RegLocation rl_dest = InlineTarget(info);
1442 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001443 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001444 StoreValue(rl_dest, rl_result);
1445 return true;
1446}
1447
1448bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1449 if (cu_->instruction_set == kMips) {
1450 // TODO - add Mips implementation
1451 return false;
1452 }
1453 RegLocation rl_src = info->args[0];
1454 rl_src = LoadValueWide(rl_src, kCoreReg);
1455 RegLocation rl_dest = InlineTargetWide(info);
1456 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001457
1458 if (cu_->instruction_set == kArm64) {
1459 // TODO - Can ecode ? UBXF otherwise
1460 // OpRegRegImm(kOpAnd, rl_result.reg, 0x7fffffffffffffff);
1461 return false;
1462 } else {
1463 OpRegCopyWide(rl_result.reg, rl_src.reg);
1464 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
1465 }
Yixin Shoudbb17e32014-02-07 05:09:30 -08001466 StoreValueWide(rl_dest, rl_result);
1467 return true;
1468}
1469
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001470bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 if (cu_->instruction_set == kMips) {
1472 // TODO - add Mips implementation
1473 return false;
1474 }
1475 RegLocation rl_src = info->args[0];
1476 RegLocation rl_dest = InlineTarget(info);
1477 StoreValue(rl_dest, rl_src);
1478 return true;
1479}
1480
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001481bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 if (cu_->instruction_set == kMips) {
1483 // TODO - add Mips implementation
1484 return false;
1485 }
1486 RegLocation rl_src = info->args[0];
1487 RegLocation rl_dest = InlineTargetWide(info);
1488 StoreValueWide(rl_dest, rl_src);
1489 return true;
1490}
1491
1492/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001493 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001494 * otherwise bails to standard library code.
1495 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001496bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 if (cu_->instruction_set == kMips) {
1498 // TODO - add Mips implementation
1499 return false;
1500 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001501 RegLocation rl_obj = info->args[0];
1502 RegLocation rl_char = info->args[1];
1503 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1504 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1505 return false;
1506 }
1507
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001508 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001510 RegStorage reg_ptr = TargetReg(kArg0);
1511 RegStorage reg_char = TargetReg(kArg1);
1512 RegStorage reg_start = TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 LoadValueDirectFixed(rl_obj, reg_ptr);
1515 LoadValueDirectFixed(rl_char, reg_char);
1516 if (zero_based) {
1517 LoadConstant(reg_start, 0);
1518 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001519 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 LoadValueDirectFixed(rl_start, reg_start);
1521 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001522 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1523 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pIndexOf)) :
1524 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001525 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001526 LIR* high_code_point_branch =
1527 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001529 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001530 if (!rl_char.is_const) {
1531 // Add the slow path for code points beyond 0xFFFF.
1532 DCHECK(high_code_point_branch != nullptr);
1533 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1534 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001535 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001536 } else {
1537 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1538 DCHECK(high_code_point_branch == nullptr);
1539 }
buzbeea0cd2d72014-06-01 09:33:49 -07001540 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 RegLocation rl_dest = InlineTarget(info);
1542 StoreValue(rl_dest, rl_return);
1543 return true;
1544}
1545
1546/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001547bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 if (cu_->instruction_set == kMips) {
1549 // TODO - add Mips implementation
1550 return false;
1551 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001552 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001554 RegStorage reg_this = TargetReg(kArg0);
1555 RegStorage reg_cmp = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556
1557 RegLocation rl_this = info->args[0];
1558 RegLocation rl_cmp = info->args[1];
1559 LoadValueDirectFixed(rl_this, reg_this);
1560 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001561 RegStorage r_tgt;
1562 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
1563 if (Is64BitInstructionSet(cu_->instruction_set)) {
1564 r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pStringCompareTo));
1565 } else {
1566 r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
1567 }
1568 } else {
1569 r_tgt = RegStorage::InvalidReg();
1570 }
Dave Allisonf9439142014-03-27 15:10:22 -07001571 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001572 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001573 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001574 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001575 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 // NOTE: not a safepoint
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001577 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 OpReg(kOpBlx, r_tgt);
1579 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001580 if (Is64BitInstructionSet(cu_->instruction_set)) {
1581 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(8, pStringCompareTo));
1582 } else {
1583 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
1584 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 }
buzbeea0cd2d72014-06-01 09:33:49 -07001586 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 RegLocation rl_dest = InlineTarget(info);
1588 StoreValue(rl_dest, rl_return);
1589 return true;
1590}
1591
1592bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1593 RegLocation rl_dest = InlineTarget(info);
1594 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001595
1596 switch (cu_->instruction_set) {
1597 case kArm:
1598 // Fall-through.
1599 case kThumb2:
1600 // Fall-through.
1601 case kMips:
1602 Load32Disp(TargetReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
1603 break;
1604
1605 case kArm64:
1606 Load32Disp(TargetReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg);
1607 break;
1608
1609 case kX86:
1610 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg,
1611 Thread::PeerOffset<4>());
1612 break;
1613
1614 case kX86_64:
1615 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg,
1616 Thread::PeerOffset<8>());
1617 break;
1618
1619 default:
1620 LOG(FATAL) << "Unexpected isa " << cu_->instruction_set;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 }
1622 StoreValue(rl_dest, rl_result);
1623 return true;
1624}
1625
1626bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1627 bool is_long, bool is_volatile) {
1628 if (cu_->instruction_set == kMips) {
1629 // TODO - add Mips implementation
1630 return false;
1631 }
1632 // Unused - RegLocation rl_src_unsafe = info->args[0];
1633 RegLocation rl_src_obj = info->args[1]; // Object
1634 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001635 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001636 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001637
buzbeea0cd2d72014-06-01 09:33:49 -07001638 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1640 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1641 if (is_long) {
Dmitry Petrochenko9bf549d2014-05-12 11:14:46 +07001642 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001643 LoadBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_result.reg, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001644 } else {
1645 RegStorage rl_temp_offset = AllocTemp();
1646 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001647 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64);
buzbee091cc402014-03-31 10:14:40 -07001648 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001649 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 } else {
buzbee695d13a2014-04-19 13:32:20 -07001651 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001652 }
1653
1654 if (is_volatile) {
1655 // Without context sensitive analysis, we must issue the most conservative barriers.
1656 // In this case, either a load or store may follow so we issue both barriers.
1657 GenMemBarrier(kLoadLoad);
1658 GenMemBarrier(kLoadStore);
1659 }
1660
1661 if (is_long) {
1662 StoreValueWide(rl_dest, rl_result);
1663 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 StoreValue(rl_dest, rl_result);
1665 }
1666 return true;
1667}
1668
1669bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1670 bool is_object, bool is_volatile, bool is_ordered) {
1671 if (cu_->instruction_set == kMips) {
1672 // TODO - add Mips implementation
1673 return false;
1674 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 // Unused - RegLocation rl_src_unsafe = info->args[0];
1676 RegLocation rl_src_obj = info->args[1]; // Object
1677 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001678 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 RegLocation rl_src_value = info->args[4]; // value to store
1680 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001681 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001682 GenMemBarrier(kStoreStore);
1683 }
buzbeea0cd2d72014-06-01 09:33:49 -07001684 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1686 RegLocation rl_value;
1687 if (is_long) {
1688 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Dmitry Petrochenko9bf549d2014-05-12 11:14:46 +07001689 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001690 StoreBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_value.reg, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001691 } else {
1692 RegStorage rl_temp_offset = AllocTemp();
1693 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Vladimir Marko455759b2014-05-06 20:49:36 +01001694 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64);
buzbee091cc402014-03-31 10:14:40 -07001695 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001696 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001697 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001698 rl_value = LoadValue(rl_src_value);
buzbee695d13a2014-04-19 13:32:20 -07001699 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001700 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001701
1702 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001703 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001704
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001706 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 GenMemBarrier(kStoreLoad);
1708 }
1709 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001710 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 }
1712 return true;
1713}
1714
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001715void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001716 if ((info->opt_flags & MIR_INLINED) != 0) {
1717 // Already inlined but we may still need the null check.
1718 if (info->type != kStatic &&
1719 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1720 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
buzbeea0cd2d72014-06-01 09:33:49 -07001721 RegLocation rl_obj = LoadValue(info->args[0], kRefReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001722 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001723 }
1724 return;
1725 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001726 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Dmitry Petrochenko4c800432014-05-08 12:20:24 +07001727 // TODO: Enable instrinsics for x86_64
1728 // Temporary disable intrinsics for x86_64. We will enable them later step by step.
1729 if (cu_->instruction_set != kX86_64) {
1730 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1731 ->GenIntrinsic(this, info)) {
1732 return;
1733 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001735 GenInvokeNoInline(info);
1736}
1737
Andreas Gampe2f244e92014-05-08 03:35:25 -07001738template <size_t pointer_size>
1739static LIR* GenInvokeNoInlineCall(Mir2Lir* mir_to_lir, InvokeType type) {
1740 ThreadOffset<pointer_size> trampoline(-1);
1741 switch (type) {
1742 case kInterface:
1743 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeInterfaceTrampolineWithAccessCheck);
1744 break;
1745 case kDirect:
1746 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeDirectTrampolineWithAccessCheck);
1747 break;
1748 case kStatic:
1749 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeStaticTrampolineWithAccessCheck);
1750 break;
1751 case kSuper:
1752 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeSuperTrampolineWithAccessCheck);
1753 break;
1754 case kVirtual:
1755 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeVirtualTrampolineWithAccessCheck);
1756 break;
1757 default:
1758 LOG(FATAL) << "Unexpected invoke type";
1759 }
1760 return mir_to_lir->OpThreadMem(kOpBlx, trampoline);
1761}
1762
Vladimir Marko3bc86152014-03-13 14:11:28 +00001763void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001764 int call_state = 0;
1765 LIR* null_ck;
1766 LIR** p_null_ck = NULL;
1767 NextCallInsn next_call_insn;
1768 FlushAllRegs(); /* Everything to home location */
1769 // Explicit register usage
1770 LockCallTemps();
1771
Vladimir Markof096aad2014-01-23 15:51:58 +00001772 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1773 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
Mark Mendelle87f9b52014-04-30 14:13:18 -04001774 BeginInvoke(info);
Vladimir Markof096aad2014-01-23 15:51:58 +00001775 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1776 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1777 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001778 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001779 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001780 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001781 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 } else if (info->type == kDirect) {
1783 if (fast_path) {
1784 p_null_ck = &null_ck;
1785 }
1786 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1787 skip_this = false;
1788 } else if (info->type == kStatic) {
1789 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1790 skip_this = false;
1791 } else if (info->type == kSuper) {
1792 DCHECK(!fast_path); // Fast path is a direct call.
1793 next_call_insn = NextSuperCallInsnSP;
1794 skip_this = false;
1795 } else {
1796 DCHECK_EQ(info->type, kVirtual);
1797 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1798 skip_this = fast_path;
1799 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001800 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 if (!info->is_range) {
1802 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001803 next_call_insn, target_method, method_info.VTableIndex(),
1804 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001805 original_type, skip_this);
1806 } else {
1807 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001808 next_call_insn, target_method, method_info.VTableIndex(),
1809 method_info.DirectCode(), method_info.DirectMethod(),
1810 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001811 }
1812 // Finish up any of the call sequence not interleaved in arg loading
1813 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001814 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1815 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001816 }
1817 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001818 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001819 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1820 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001821 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001822 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001823 // We can have the linker fixup a call relative.
1824 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001825 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001826 } else {
1827 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1828 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1829 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001831 // TODO: Extract?
1832 if (Is64BitInstructionSet(cu_->instruction_set)) {
1833 call_inst = GenInvokeNoInlineCall<8>(this, info->type);
1834 } else {
Andreas Gampe3ec5da22014-05-12 18:43:28 -07001835 call_inst = GenInvokeNoInlineCall<4>(this, info->type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001836 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001837 }
1838 }
Mark Mendelle87f9b52014-04-30 14:13:18 -04001839 EndInvoke(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 MarkSafepointPC(call_inst);
1841
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001842 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001843 if (info->result.location != kLocInvalid) {
1844 // We have a following MOVE_RESULT - do it now.
1845 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001846 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001847 StoreValueWide(info->result, ret_loc);
1848 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001849 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850 StoreValue(info->result, ret_loc);
1851 }
1852 }
1853}
1854
1855} // namespace art