blob: 4fdb32eae7e912fc83f68492011c2c717986036b [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
Brian Carlstrom7940e442013-07-12 13:46:57 -070066/*
67 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000068 * the helper target address, and the actual call to the helper. Because x86
69 * has a memory call operation, part 1 is a NOP for x86. For other targets,
70 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 */
Ian Rogersdd7624d2014-03-14 17:43:00 -070072RegStorage Mir2Lir::CallHelperSetup(ThreadOffset<4> helper_offset) {
Dave Allisond6ed6422014-04-09 23:36:15 +000073 return (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) ? RegStorage::InvalidReg() : LoadHelper(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070074}
75
76/* NOTE: if r_tgt is a temp, it will be freed following use */
Ian Rogersdd7624d2014-03-14 17:43:00 -070077LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<4> helper_offset, bool safepoint_pc,
buzbee2700f7e2014-03-07 09:46:20 -080078 bool use_link) {
Dave Allisond6ed6422014-04-09 23:36:15 +000079 LIR* call_inst;
Brian Carlstrom60d7a652014-03-13 18:10:08 -070080 OpKind op = use_link ? kOpBlx : kOpBx;
Dave Allisond6ed6422014-04-09 23:36:15 +000081 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
82 call_inst = OpThreadMem(op, helper_offset);
83 } else {
84 call_inst = OpReg(op, r_tgt);
85 FreeTemp(r_tgt);
86 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 if (safepoint_pc) {
88 MarkSafepointPC(call_inst);
89 }
90 return call_inst;
91}
92
Mingyao Yang42894562014-04-07 12:42:16 -070093void Mir2Lir::CallRuntimeHelper(ThreadOffset<4> helper_offset, bool safepoint_pc) {
94 RegStorage r_tgt = CallHelperSetup(helper_offset);
95 ClobberCallerSave();
96 CallHelper(r_tgt, helper_offset, safepoint_pc);
97}
98
Ian Rogersdd7624d2014-03-14 17:43:00 -070099void Mir2Lir::CallRuntimeHelperImm(ThreadOffset<4> helper_offset, int arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800100 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000102 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103 CallHelper(r_tgt, helper_offset, safepoint_pc);
104}
105
Ian Rogersdd7624d2014-03-14 17:43:00 -0700106void Mir2Lir::CallRuntimeHelperReg(ThreadOffset<4> helper_offset, RegStorage arg0,
107 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800108 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 OpRegCopy(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000110 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 CallHelper(r_tgt, helper_offset, safepoint_pc);
112}
113
Ian Rogersdd7624d2014-03-14 17:43:00 -0700114void Mir2Lir::CallRuntimeHelperRegLocation(ThreadOffset<4> helper_offset, RegLocation arg0,
Ian Rogers848871b2013-08-05 10:56:33 -0700115 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800116 RegStorage r_tgt = CallHelperSetup(helper_offset);
117 if (arg0.wide == 0) {
118 LoadValueDirectFixed(arg0, TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800120 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
121 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000123 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 CallHelper(r_tgt, helper_offset, safepoint_pc);
125}
126
Ian Rogersdd7624d2014-03-14 17:43:00 -0700127void Mir2Lir::CallRuntimeHelperImmImm(ThreadOffset<4> helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800129 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 LoadConstant(TargetReg(kArg0), arg0);
131 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000132 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 CallHelper(r_tgt, helper_offset, safepoint_pc);
134}
135
Ian Rogersdd7624d2014-03-14 17:43:00 -0700136void Mir2Lir::CallRuntimeHelperImmRegLocation(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 RegLocation arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800138 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 if (arg1.wide == 0) {
140 LoadValueDirectFixed(arg1, TargetReg(kArg1));
141 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800142 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
143 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 }
145 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000146 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 CallHelper(r_tgt, helper_offset, safepoint_pc);
148}
149
Ian Rogersdd7624d2014-03-14 17:43:00 -0700150void Mir2Lir::CallRuntimeHelperRegLocationImm(ThreadOffset<4> helper_offset, RegLocation arg0,
151 int arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800152 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 LoadValueDirectFixed(arg0, TargetReg(kArg0));
154 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000155 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 CallHelper(r_tgt, helper_offset, safepoint_pc);
157}
158
Ian Rogersdd7624d2014-03-14 17:43:00 -0700159void Mir2Lir::CallRuntimeHelperImmReg(ThreadOffset<4> helper_offset, int arg0, RegStorage arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800161 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 OpRegCopy(TargetReg(kArg1), arg1);
163 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000164 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 CallHelper(r_tgt, helper_offset, safepoint_pc);
166}
167
Ian Rogersdd7624d2014-03-14 17:43:00 -0700168void Mir2Lir::CallRuntimeHelperRegImm(ThreadOffset<4> helper_offset, RegStorage arg0, int arg1,
Ian Rogers848871b2013-08-05 10:56:33 -0700169 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800170 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 OpRegCopy(TargetReg(kArg0), arg0);
172 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000173 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 CallHelper(r_tgt, helper_offset, safepoint_pc);
175}
176
Ian Rogersdd7624d2014-03-14 17:43:00 -0700177void Mir2Lir::CallRuntimeHelperImmMethod(ThreadOffset<4> helper_offset, int arg0,
178 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800179 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 LoadCurrMethodDirect(TargetReg(kArg1));
181 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000182 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 CallHelper(r_tgt, helper_offset, safepoint_pc);
184}
185
Ian Rogersdd7624d2014-03-14 17:43:00 -0700186void Mir2Lir::CallRuntimeHelperRegMethod(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800187 bool safepoint_pc) {
188 RegStorage r_tgt = CallHelperSetup(helper_offset);
189 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800190 if (TargetReg(kArg0) != arg0) {
191 OpRegCopy(TargetReg(kArg0), arg0);
192 }
193 LoadCurrMethodDirect(TargetReg(kArg1));
194 ClobberCallerSave();
195 CallHelper(r_tgt, helper_offset, safepoint_pc);
196}
197
Ian Rogersdd7624d2014-03-14 17:43:00 -0700198void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(ThreadOffset<4> helper_offset, RegStorage arg0,
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800199 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800200 RegStorage r_tgt = CallHelperSetup(helper_offset);
201 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800202 if (TargetReg(kArg0) != arg0) {
203 OpRegCopy(TargetReg(kArg0), arg0);
204 }
205 LoadCurrMethodDirect(TargetReg(kArg1));
206 LoadValueDirectFixed(arg2, TargetReg(kArg2));
207 ClobberCallerSave();
208 CallHelper(r_tgt, helper_offset, safepoint_pc);
209}
210
Ian Rogersdd7624d2014-03-14 17:43:00 -0700211void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(ThreadOffset<4> helper_offset,
212 RegLocation arg0, RegLocation arg1,
213 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800214 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 if (arg0.wide == 0) {
216 LoadValueDirectFixed(arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
217 if (arg1.wide == 0) {
218 if (cu_->instruction_set == kMips) {
219 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1));
220 } else {
221 LoadValueDirectFixed(arg1, TargetReg(kArg1));
222 }
223 } else {
224 if (cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -0800225 RegStorage r_tmp;
226 if (arg1.fp) {
227 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
228 } else {
229 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
230 }
231 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800233 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
234 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 }
236 }
237 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800238 RegStorage r_tmp;
239 if (arg0.fp) {
240 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg0), TargetReg(kFArg1));
241 } else {
242 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
243 }
244 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 if (arg1.wide == 0) {
246 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2));
247 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800248 RegStorage r_tmp;
249 if (arg1.fp) {
250 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
251 } else {
252 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
253 }
254 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 }
256 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000257 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 CallHelper(r_tgt, helper_offset, safepoint_pc);
259}
260
Mingyao Yang80365d92014-04-18 12:10:58 -0700261void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
262 if (arg1.GetReg() == TargetReg(kArg0).GetReg()) {
263 if (arg0.GetReg() == TargetReg(kArg1).GetReg()) {
264 // Swap kArg0 and kArg1 with kArg2 as temp.
265 OpRegCopy(TargetReg(kArg2), arg1);
266 OpRegCopy(TargetReg(kArg0), arg0);
267 OpRegCopy(TargetReg(kArg1), TargetReg(kArg2));
268 } else {
269 OpRegCopy(TargetReg(kArg1), arg1);
270 OpRegCopy(TargetReg(kArg0), arg0);
271 }
272 } else {
273 OpRegCopy(TargetReg(kArg0), arg0);
274 OpRegCopy(TargetReg(kArg1), arg1);
275 }
276}
277
Ian Rogersdd7624d2014-03-14 17:43:00 -0700278void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800279 RegStorage arg1, bool safepoint_pc) {
280 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700281 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000282 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 CallHelper(r_tgt, helper_offset, safepoint_pc);
284}
285
Ian Rogersdd7624d2014-03-14 17:43:00 -0700286void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800287 RegStorage arg1, int arg2, bool safepoint_pc) {
288 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700289 CopyToArgumentRegs(arg0, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 LoadConstant(TargetReg(kArg2), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000291 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 CallHelper(r_tgt, helper_offset, safepoint_pc);
293}
294
Ian Rogersdd7624d2014-03-14 17:43:00 -0700295void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 int arg0, RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800297 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 LoadValueDirectFixed(arg2, TargetReg(kArg2));
299 LoadCurrMethodDirect(TargetReg(kArg1));
300 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000301 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 CallHelper(r_tgt, helper_offset, safepoint_pc);
303}
304
Ian Rogersdd7624d2014-03-14 17:43:00 -0700305void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 int arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800307 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 LoadCurrMethodDirect(TargetReg(kArg1));
309 LoadConstant(TargetReg(kArg2), arg2);
310 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000311 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 CallHelper(r_tgt, helper_offset, safepoint_pc);
313}
314
Ian Rogersdd7624d2014-03-14 17:43:00 -0700315void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 int arg0, RegLocation arg1,
317 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800318 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700319 DCHECK_EQ(arg1.wide, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 LoadValueDirectFixed(arg1, TargetReg(kArg1));
321 if (arg2.wide == 0) {
322 LoadValueDirectFixed(arg2, TargetReg(kArg2));
323 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800324 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
325 LoadValueDirectWideFixed(arg2, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 }
327 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000328 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 CallHelper(r_tgt, helper_offset, safepoint_pc);
330}
331
Ian Rogersdd7624d2014-03-14 17:43:00 -0700332void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset<4> helper_offset,
Ian Rogersa9a82542013-10-04 11:17:26 -0700333 RegLocation arg0, RegLocation arg1,
334 RegLocation arg2,
335 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800336 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700337 DCHECK_EQ(arg0.wide, 0U);
338 LoadValueDirectFixed(arg0, TargetReg(kArg0));
339 DCHECK_EQ(arg1.wide, 0U);
340 LoadValueDirectFixed(arg1, TargetReg(kArg1));
341 DCHECK_EQ(arg1.wide, 0U);
342 LoadValueDirectFixed(arg2, TargetReg(kArg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000343 ClobberCallerSave();
Ian Rogersa9a82542013-10-04 11:17:26 -0700344 CallHelper(r_tgt, helper_offset, safepoint_pc);
345}
346
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347/*
348 * If there are any ins passed in registers that have not been promoted
349 * to a callee-save register, flush them to the frame. Perform intial
350 * assignment of promoted arguments.
351 *
352 * ArgLocs is an array of location records describing the incoming arguments
353 * with one location record per word of argument.
354 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700355void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 /*
357 * Dummy up a RegLocation for the incoming Method*
358 * It will attempt to keep kArg0 live (or copy it to home location
359 * if promoted).
360 */
361 RegLocation rl_src = rl_method;
362 rl_src.location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800363 rl_src.reg = TargetReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 rl_src.home = false;
buzbee2700f7e2014-03-07 09:46:20 -0800365 MarkLive(rl_src.reg, rl_src.s_reg_low);
buzbee695d13a2014-04-19 13:32:20 -0700366 if (rl_method.wide) {
367 StoreValueWide(rl_method, rl_src);
368 } else {
369 StoreValue(rl_method, rl_src);
370 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 // If Method* has been promoted, explicitly flush
372 if (rl_method.location == kLocPhysReg) {
373 StoreWordDisp(TargetReg(kSp), 0, TargetReg(kArg0));
374 }
375
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800376 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800378 }
379
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
381 /*
382 * Copy incoming arguments to their proper home locations.
383 * NOTE: an older version of dx had an issue in which
384 * it would reuse static method argument registers.
385 * This could result in the same Dalvik virtual register
386 * being promoted to both core and fp regs. To account for this,
387 * we only copy to the corresponding promoted physical register
388 * if it matches the type of the SSA name for the incoming
389 * argument. It is also possible that long and double arguments
390 * end up half-promoted. In those cases, we must flush the promoted
391 * half to memory as well.
392 */
393 for (int i = 0; i < cu_->num_ins; i++) {
394 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800395 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800396
buzbee2700f7e2014-03-07 09:46:20 -0800397 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 // If arriving in register
399 bool need_flush = true;
400 RegLocation* t_loc = &ArgLocs[i];
401 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800402 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 need_flush = false;
404 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800405 OpRegCopy(RegStorage::Solo32(v_map->FpReg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 need_flush = false;
407 } else {
408 need_flush = true;
409 }
410
buzbeed0a03b82013-09-14 08:21:05 -0700411 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 if (t_loc->wide) {
413 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700414 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 need_flush |= (p_map->core_location != v_map->core_location) ||
416 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700417 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
418 /*
419 * In Arm, a double is represented as a pair of consecutive single float
420 * registers starting at an even number. It's possible that both Dalvik vRegs
421 * representing the incoming double were independently promoted as singles - but
422 * not in a form usable as a double. If so, we need to flush - even though the
423 * incoming arg appears fully in register. At this point in the code, both
424 * halves of the double are promoted. Make sure they are in a usable form.
425 */
426 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
427 int low_reg = promotion_map_[lowreg_index].FpReg;
428 int high_reg = promotion_map_[lowreg_index + 1].FpReg;
429 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
430 need_flush = true;
431 }
432 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 }
434 if (need_flush) {
buzbee695d13a2014-04-19 13:32:20 -0700435 Store32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436 }
437 } else {
438 // If arriving in frame & promoted
439 if (v_map->core_location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700440 Load32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 }
442 if (v_map->fp_location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700443 Load32Disp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->FpReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 }
445 }
446 }
447}
448
449/*
450 * Bit of a hack here - in the absence of a real scheduling pass,
451 * emit the next instruction in static & direct invoke sequences.
452 */
453static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
454 int state, const MethodReference& target_method,
455 uint32_t unused,
456 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700457 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 if (direct_code != 0 && direct_method != 0) {
460 switch (state) {
461 case 0: // Get the current Method* [sets kArg0]
Ian Rogersff093b32014-04-30 19:04:27 -0700462 if (direct_code != static_cast<uintptr_t>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700463 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700464 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
465 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700466 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700467 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 }
Ian Rogersff093b32014-04-30 19:04:27 -0700469 if (direct_method != static_cast<uintptr_t>(-1)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 cg->LoadConstant(cg->TargetReg(kArg0), direct_method);
471 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700472 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 }
474 break;
475 default:
476 return -1;
477 }
478 } else {
479 switch (state) {
480 case 0: // Get the current Method* [sets kArg0]
481 // TUNING: we can save a reg copy if Method* has been promoted.
482 cg->LoadCurrMethodDirect(cg->TargetReg(kArg0));
483 break;
484 case 1: // Get method->dex_cache_resolved_methods_
buzbee695d13a2014-04-19 13:32:20 -0700485 cg->LoadRefDisp(cg->TargetReg(kArg0),
486 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
487 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 // Set up direct code if known.
489 if (direct_code != 0) {
Ian Rogersff093b32014-04-30 19:04:27 -0700490 if (direct_code != static_cast<uintptr_t>(-1)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700492 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700493 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700494 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 }
496 }
497 break;
498 case 2: // Grab target method*
499 CHECK_EQ(cu->dex_file, target_method.dex_file);
buzbee695d13a2014-04-19 13:32:20 -0700500 cg->LoadRefDisp(cg->TargetReg(kArg0),
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700501 ObjArray::OffsetOfElement(target_method.dex_method_index).Int32Value(),
502 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 break;
504 case 3: // Grab the code from the method*
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700505 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 if (direct_code == 0) {
507 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800508 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 cg->TargetReg(kInvokeTgt));
510 }
511 break;
512 }
513 // Intentional fallthrough for x86
514 default:
515 return -1;
516 }
517 }
518 return state + 1;
519}
520
521/*
522 * Bit of a hack here - in the absence of a real scheduling pass,
523 * emit the next instruction in a virtual invoke sequence.
524 * We can use kLr as a temp prior to target address loading
525 * Note also that we'll load the first argument ("this") into
526 * kArg1 here rather than the standard LoadArgRegs.
527 */
528static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
529 int state, const MethodReference& target_method,
530 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700531 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
533 /*
534 * This is the fast path in which the target virtual method is
535 * fully resolved at compile time.
536 */
537 switch (state) {
538 case 0: { // Get "this" [set kArg1]
539 RegLocation rl_arg = info->args[0];
540 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
541 break;
542 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700543 case 1: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800544 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 // get this->klass_ [use kArg1, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700546 cg->LoadRefDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
547 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800548 cg->MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700550 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700551 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
552 cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700554 case 3: // Get target method [use kInvokeTgt, set kArg0]
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700555 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt),
556 ObjArray::OffsetOfElement(method_idx).Int32Value(),
buzbee695d13a2014-04-19 13:32:20 -0700557 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700559 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700560 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800562 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 cg->TargetReg(kInvokeTgt));
564 break;
565 }
566 // Intentional fallthrough for X86
567 default:
568 return -1;
569 }
570 return state + 1;
571}
572
573/*
Jeff Hao88474b42013-10-23 16:24:40 -0700574 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
575 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
576 * more than one interface method map to the same index. Note also that we'll load the first
577 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 */
579static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
580 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700581 uint32_t method_idx, uintptr_t unused,
582 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584
Jeff Hao88474b42013-10-23 16:24:40 -0700585 switch (state) {
586 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700587 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
588 cg->LoadConstant(cg->TargetReg(kHiddenArg), target_method.dex_method_index);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700589 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700590 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg), cg->TargetReg(kHiddenArg));
591 }
592 break;
593 case 1: { // Get "this" [set kArg1]
594 RegLocation rl_arg = info->args[0];
595 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
596 break;
597 }
598 case 2: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800599 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700600 // Get this->klass_ [use kArg1, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700601 cg->LoadRefDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
602 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800603 cg->MarkPossibleNullPointerException(info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700604 break;
605 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700606 // NOTE: native pointer.
607 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
608 cg->TargetReg(kInvokeTgt));
Jeff Hao88474b42013-10-23 16:24:40 -0700609 break;
610 case 4: // Get target method [use kInvokeTgt, set kArg0]
buzbee695d13a2014-04-19 13:32:20 -0700611 // NOTE: native pointer.
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700612 cg->LoadRefDisp(cg->TargetReg(kInvokeTgt),
613 ObjArray::OffsetOfElement(method_idx % ClassLinker::kImtSize).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 cg->TargetReg(kArg0));
615 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700616 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700617 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700618 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800619 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Jeff Hao88474b42013-10-23 16:24:40 -0700620 cg->TargetReg(kInvokeTgt));
621 break;
622 }
623 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 default:
625 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626 }
627 return state + 1;
628}
629
Ian Rogersdd7624d2014-03-14 17:43:00 -0700630static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset<4> trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700632 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
634 /*
635 * This handles the case in which the base method is not fully
636 * resolved at compile time, we bail to a runtime helper.
637 */
638 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700639 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 // Load trampoline target
Ian Rogers848871b2013-08-05 10:56:33 -0700641 cg->LoadWordDisp(cg->TargetReg(kSelf), trampoline.Int32Value(), cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 }
643 // Load kArg0 with method index
644 CHECK_EQ(cu->dex_file, target_method.dex_file);
645 cg->LoadConstant(cg->TargetReg(kArg0), target_method.dex_method_index);
646 return 1;
647 }
648 return -1;
649}
650
651static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
652 int state,
653 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000654 uint32_t unused, uintptr_t unused2,
655 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700656 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
658}
659
660static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
661 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000662 uint32_t unused, uintptr_t unused2,
663 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700664 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
666}
667
668static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
669 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000670 uint32_t unused, uintptr_t unused2,
671 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700672 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
674}
675
676static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
677 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000678 uint32_t unused, uintptr_t unused2,
679 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700680 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
682}
683
684static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
685 CallInfo* info, int state,
686 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000687 uint32_t unused, uintptr_t unused2,
688 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700689 ThreadOffset<4> trampoline =
690 QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
692}
693
694int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
695 NextCallInsn next_call_insn,
696 const MethodReference& target_method,
697 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700698 uintptr_t direct_method, InvokeType type, bool skip_this) {
buzbee2700f7e2014-03-07 09:46:20 -0800699 int last_arg_reg = TargetReg(kArg3).GetReg();
700 int next_reg = TargetReg(kArg1).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 int next_arg = 0;
702 if (skip_this) {
703 next_reg++;
704 next_arg++;
705 }
706 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
707 RegLocation rl_arg = info->args[next_arg++];
708 rl_arg = UpdateRawLoc(rl_arg);
buzbee2700f7e2014-03-07 09:46:20 -0800709 if (rl_arg.wide && (next_reg <= TargetReg(kArg2).GetReg())) {
710 RegStorage r_tmp(RegStorage::k64BitPair, next_reg, next_reg + 1);
711 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 next_reg++;
713 next_arg++;
714 } else {
715 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800716 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 rl_arg.is_const = false;
718 }
buzbee2700f7e2014-03-07 09:46:20 -0800719 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(next_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 }
721 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
722 direct_code, direct_method, type);
723 }
724 return call_state;
725}
726
727/*
728 * Load up to 5 arguments, the first three of which will be in
729 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
730 * and as part of the load sequence, it must be replaced with
731 * the target method pointer. Note, this may also be called
732 * for "range" variants if the number of arguments is 5 or fewer.
733 */
734int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
735 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
736 const MethodReference& target_method,
737 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700738 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700739 RegLocation rl_arg;
740
741 /* If no arguments, just return */
742 if (info->num_arg_words == 0)
743 return call_state;
744
745 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
746 direct_code, direct_method, type);
747
748 DCHECK_LE(info->num_arg_words, 5);
749 if (info->num_arg_words > 3) {
750 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700751 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 RegLocation rl_use0 = info->args[0];
753 RegLocation rl_use1 = info->args[1];
754 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800755 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
756 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 // Wide spans, we need the 2nd half of uses[2].
758 rl_arg = UpdateLocWide(rl_use2);
759 if (rl_arg.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800760 reg = rl_arg.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 } else {
762 // kArg2 & rArg3 can safely be used here
763 reg = TargetReg(kArg3);
buzbee695d13a2014-04-19 13:32:20 -0700764 Load32Disp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 call_state = next_call_insn(cu_, info, call_state, target_method,
766 vtable_idx, direct_code, direct_method, type);
767 }
buzbee695d13a2014-04-19 13:32:20 -0700768 Store32Disp(TargetReg(kSp), (next_use + 1) * 4, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
770 direct_code, direct_method, type);
771 next_use++;
772 }
773 // Loop through the rest
774 while (next_use < info->num_arg_words) {
buzbee2700f7e2014-03-07 09:46:20 -0800775 RegStorage low_reg;
776 RegStorage high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 rl_arg = info->args[next_use];
778 rl_arg = UpdateRawLoc(rl_arg);
779 if (rl_arg.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000780 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800781 low_reg = rl_arg.reg.GetLow();
782 high_reg = rl_arg.reg.GetHigh();
783 } else {
784 low_reg = rl_arg.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000785 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 } else {
787 low_reg = TargetReg(kArg2);
788 if (rl_arg.wide) {
789 high_reg = TargetReg(kArg3);
buzbee2700f7e2014-03-07 09:46:20 -0800790 LoadValueDirectWideFixed(rl_arg, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 } else {
792 LoadValueDirectFixed(rl_arg, low_reg);
793 }
794 call_state = next_call_insn(cu_, info, call_state, target_method,
795 vtable_idx, direct_code, direct_method, type);
796 }
797 int outs_offset = (next_use + 1) * 4;
798 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800799 StoreBaseDispWide(TargetReg(kSp), outs_offset, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 next_use += 2;
801 } else {
buzbee695d13a2014-04-19 13:32:20 -0700802 Store32Disp(TargetReg(kSp), outs_offset, low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 next_use++;
804 }
805 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
806 direct_code, direct_method, type);
807 }
808 }
809
810 call_state = LoadArgRegs(info, call_state, next_call_insn,
811 target_method, vtable_idx, direct_code, direct_method,
812 type, skip_this);
813
814 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700815 if (Runtime::Current()->ExplicitNullChecks()) {
816 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
817 } else {
818 *pcrLabel = nullptr;
819 // In lieu of generating a check for kArg1 being null, we need to
820 // perform a load when doing implicit checks.
821 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700822 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -0700823 MarkPossibleNullPointerException(info->opt_flags);
824 FreeTemp(tmp);
825 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 }
827 return call_state;
828}
829
830/*
831 * May have 0+ arguments (also used for jumbo). Note that
832 * source virtual registers may be in physical registers, so may
833 * need to be flushed to home location before copying. This
834 * applies to arg3 and above (see below).
835 *
836 * Two general strategies:
837 * If < 20 arguments
838 * Pass args 3-18 using vldm/vstm block copy
839 * Pass arg0, arg1 & arg2 in kArg1-kArg3
840 * If 20+ arguments
841 * Pass args arg19+ using memcpy block copy
842 * Pass arg0, arg1 & arg2 in kArg1-kArg3
843 *
844 */
845int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
846 LIR** pcrLabel, NextCallInsn next_call_insn,
847 const MethodReference& target_method,
848 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700849 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 // If we can treat it as non-range (Jumbo ops will use range form)
851 if (info->num_arg_words <= 5)
852 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
853 next_call_insn, target_method, vtable_idx,
854 direct_code, direct_method, type, skip_this);
855 /*
856 * First load the non-register arguments. Both forms expect all
857 * of the source arguments to be in their home frame location, so
858 * scan the s_reg names and flush any that have been promoted to
859 * frame backing storage.
860 */
861 // Scan the rest of the args - if in phys_reg flush to memory
862 for (int next_arg = 0; next_arg < info->num_arg_words;) {
863 RegLocation loc = info->args[next_arg];
864 if (loc.wide) {
865 loc = UpdateLocWide(loc);
866 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800867 StoreBaseDispWide(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 }
869 next_arg += 2;
870 } else {
871 loc = UpdateLoc(loc);
872 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
buzbee695d13a2014-04-19 13:32:20 -0700873 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 }
875 next_arg++;
876 }
877 }
878
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800879 // Logic below assumes that Method pointer is at offset zero from SP.
880 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
881
882 // The first 3 arguments are passed via registers.
883 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
884 // get size of uintptr_t or size of object reference according to model being used.
885 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800887 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
888 DCHECK_GT(regs_left_to_pass_via_stack, 0);
889
890 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
891 // Use vldm/vstm pair using kArg3 as a temp
892 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
893 direct_code, direct_method, type);
894 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
895 LIR* ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
896 // TUNING: loosen barrier
897 ld->u.m.def_mask = ENCODE_ALL;
898 SetMemRefType(ld, true /* is_load */, kDalvikReg);
899 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
900 direct_code, direct_method, type);
901 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
902 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
903 direct_code, direct_method, type);
904 LIR* st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
905 SetMemRefType(st, false /* is_load */, kDalvikReg);
906 st->u.m.def_mask = ENCODE_ALL;
907 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
908 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700909 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800910 int current_src_offset = start_offset;
911 int current_dest_offset = outs_offset;
912
913 while (regs_left_to_pass_via_stack > 0) {
914 // This is based on the knowledge that the stack itself is 16-byte aligned.
915 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
916 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
917 size_t bytes_to_move;
918
919 /*
920 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
921 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
922 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
923 * We do this because we could potentially do a smaller move to align.
924 */
925 if (regs_left_to_pass_via_stack == 4 ||
926 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
927 // Moving 128-bits via xmm register.
928 bytes_to_move = sizeof(uint32_t) * 4;
929
930 // Allocate a free xmm temp. Since we are working through the calling sequence,
931 // we expect to have an xmm temporary available.
buzbee2700f7e2014-03-07 09:46:20 -0800932 RegStorage temp = AllocTempDouble();
933 CHECK_GT(temp.GetLowReg(), 0);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800934
935 LIR* ld1 = nullptr;
936 LIR* ld2 = nullptr;
937 LIR* st1 = nullptr;
938 LIR* st2 = nullptr;
939
940 /*
941 * The logic is similar for both loads and stores. If we have 16-byte alignment,
942 * do an aligned move. If we have 8-byte alignment, then do the move in two
943 * parts. This approach prevents possible cache line splits. Finally, fall back
944 * to doing an unaligned move. In most cases we likely won't split the cache
945 * line but we cannot prove it and thus take a conservative approach.
946 */
947 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
948 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
949
950 if (src_is_16b_aligned) {
951 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
952 } else if (src_is_8b_aligned) {
953 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800954 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
955 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800956 } else {
957 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
958 }
959
960 if (dest_is_16b_aligned) {
961 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
962 } else if (dest_is_8b_aligned) {
963 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800964 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
965 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800966 } else {
967 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
968 }
969
970 // TODO If we could keep track of aliasing information for memory accesses that are wider
971 // than 64-bit, we wouldn't need to set up a barrier.
972 if (ld1 != nullptr) {
973 if (ld2 != nullptr) {
974 // For 64-bit load we can actually set up the aliasing information.
975 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
976 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
977 } else {
978 // Set barrier for 128-bit load.
979 SetMemRefType(ld1, true /* is_load */, kDalvikReg);
980 ld1->u.m.def_mask = ENCODE_ALL;
981 }
982 }
983 if (st1 != nullptr) {
984 if (st2 != nullptr) {
985 // For 64-bit store we can actually set up the aliasing information.
986 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
987 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
988 } else {
989 // Set barrier for 128-bit store.
990 SetMemRefType(st1, false /* is_load */, kDalvikReg);
991 st1->u.m.def_mask = ENCODE_ALL;
992 }
993 }
994
995 // Free the temporary used for the data movement.
buzbee2700f7e2014-03-07 09:46:20 -0800996 // CLEANUP: temp is currently a bogus pair, elmiminate extra free when updated.
997 FreeTemp(temp.GetLow());
998 FreeTemp(temp.GetHigh());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800999 } else {
1000 // Moving 32-bits via general purpose register.
1001 bytes_to_move = sizeof(uint32_t);
1002
1003 // Instead of allocating a new temp, simply reuse one of the registers being used
1004 // for argument passing.
buzbee2700f7e2014-03-07 09:46:20 -08001005 RegStorage temp = TargetReg(kArg3);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001006
1007 // Now load the argument VR and store to the outs.
buzbee695d13a2014-04-19 13:32:20 -07001008 Load32Disp(TargetReg(kSp), current_src_offset, temp);
1009 Store32Disp(TargetReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001010 }
1011
1012 current_src_offset += bytes_to_move;
1013 current_dest_offset += bytes_to_move;
1014 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1015 }
1016 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 // Generate memcpy
1018 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
1019 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001020 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(4, pMemcpy), TargetReg(kArg0),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 }
1023
1024 call_state = LoadArgRegs(info, call_state, next_call_insn,
1025 target_method, vtable_idx, direct_code, direct_method,
1026 type, skip_this);
1027
1028 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1029 direct_code, direct_method, type);
1030 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -07001031 if (Runtime::Current()->ExplicitNullChecks()) {
1032 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
1033 } else {
1034 *pcrLabel = nullptr;
1035 // In lieu of generating a check for kArg1 being null, we need to
1036 // perform a load when doing implicit checks.
1037 RegStorage tmp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001038 Load32Disp(TargetReg(kArg1), 0, tmp);
Dave Allisonf9439142014-03-27 15:10:22 -07001039 MarkPossibleNullPointerException(info->opt_flags);
1040 FreeTemp(tmp);
1041 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 }
1043 return call_state;
1044}
1045
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001046RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 RegLocation res;
1048 if (info->result.location == kLocInvalid) {
1049 res = GetReturn(false);
1050 } else {
1051 res = info->result;
1052 }
1053 return res;
1054}
1055
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001056RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 RegLocation res;
1058 if (info->result.location == kLocInvalid) {
1059 res = GetReturnWide(false);
1060 } else {
1061 res = info->result;
1062 }
1063 return res;
1064}
1065
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001066bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 if (cu_->instruction_set == kMips) {
1068 // TODO - add Mips implementation
1069 return false;
1070 }
1071 // Location of reference to data array
1072 int value_offset = mirror::String::ValueOffset().Int32Value();
1073 // Location of count
1074 int count_offset = mirror::String::CountOffset().Int32Value();
1075 // Starting offset within data array
1076 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1077 // Start of char data with array_
1078 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1079
1080 RegLocation rl_obj = info->args[0];
1081 RegLocation rl_idx = info->args[1];
1082 rl_obj = LoadValue(rl_obj, kCoreReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001083 // X86 wants to avoid putting a constant index into a register.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001084 if (!((cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64)&& rl_idx.is_const)) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001085 rl_idx = LoadValue(rl_idx, kCoreReg);
1086 }
buzbee2700f7e2014-03-07 09:46:20 -08001087 RegStorage reg_max;
1088 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001090 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001091 RegStorage reg_off;
1092 RegStorage reg_ptr;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001093 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 reg_off = AllocTemp();
1095 reg_ptr = AllocTemp();
1096 if (range_check) {
1097 reg_max = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001098 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001099 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 }
buzbee695d13a2014-04-19 13:32:20 -07001101 Load32Disp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001102 MarkPossibleNullPointerException(info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001103 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 if (range_check) {
Mingyao Yang3a74d152014-04-21 15:39:44 -07001105 // Set up a slow path to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001106 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001108 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001109 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001110 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 } else {
1112 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001113 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001115 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001116 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001117 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Vladimir Marko3bc86152014-03-13 14:11:28 +00001118 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001119 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001120 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001121 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001122 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 }
1124 reg_off = AllocTemp();
1125 reg_ptr = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001126 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1127 Load32Disp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001129 if (rl_idx.is_const) {
1130 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1131 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001132 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001133 }
buzbee2700f7e2014-03-07 09:46:20 -08001134 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001135 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001136 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001137 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 RegLocation rl_dest = InlineTarget(info);
1139 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001140 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee2700f7e2014-03-07 09:46:20 -08001141 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001142 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001143 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg,
1144 RegStorage::InvalidReg(), kUnsignedHalf, INVALID_SREG);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001145 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 FreeTemp(reg_off);
1147 FreeTemp(reg_ptr);
1148 StoreValue(rl_dest, rl_result);
1149 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001150 DCHECK(range_check_branch != nullptr);
1151 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001152 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 return true;
1155}
1156
1157// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001158bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 if (cu_->instruction_set == kMips) {
1160 // TODO - add Mips implementation
1161 return false;
1162 }
1163 // dst = src.length();
1164 RegLocation rl_obj = info->args[0];
1165 rl_obj = LoadValue(rl_obj, kCoreReg);
1166 RegLocation rl_dest = InlineTarget(info);
1167 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001168 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001169 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001170 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 if (is_empty) {
1172 // dst = (dst == 0);
1173 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001174 RegStorage t_reg = AllocTemp();
1175 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1176 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001178 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001179 OpRegImm(kOpSub, rl_result.reg, 1);
1180 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 }
1182 }
1183 StoreValue(rl_dest, rl_result);
1184 return true;
1185}
1186
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001187bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1188 if (cu_->instruction_set == kMips) {
1189 // TODO - add Mips implementation
1190 return false;
1191 }
1192 RegLocation rl_src_i = info->args[0];
buzbee695d13a2014-04-19 13:32:20 -07001193 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001194 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001195 if (size == k64) {
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001196 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001197 RegStorage r_i_low = rl_i.reg.GetLow();
1198 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001199 // 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 +00001200 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001201 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001202 }
buzbee2700f7e2014-03-07 09:46:20 -08001203 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1204 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1205 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001206 FreeTemp(r_i_low);
1207 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001208 StoreValueWide(rl_dest, rl_result);
1209 } else {
buzbee695d13a2014-04-19 13:32:20 -07001210 DCHECK(size == k32 || size == kSignedHalf);
1211 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001212 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001213 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001214 StoreValue(rl_dest, rl_result);
1215 }
1216 return true;
1217}
1218
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001219bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 if (cu_->instruction_set == kMips) {
1221 // TODO - add Mips implementation
1222 return false;
1223 }
1224 RegLocation rl_src = info->args[0];
1225 rl_src = LoadValue(rl_src, kCoreReg);
1226 RegLocation rl_dest = InlineTarget(info);
1227 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001228 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001230 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1231 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1232 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 StoreValue(rl_dest, rl_result);
1234 return true;
1235}
1236
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001237bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 if (cu_->instruction_set == kMips) {
1239 // TODO - add Mips implementation
1240 return false;
1241 }
Vladimir Markob9823312014-03-20 17:38:43 +00001242 RegLocation rl_src = info->args[0];
1243 rl_src = LoadValueWide(rl_src, kCoreReg);
1244 RegLocation rl_dest = InlineTargetWide(info);
1245 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1246
1247 // If on x86 or if we would clobber a register needed later, just copy the source first.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001248 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 || rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
buzbee2700f7e2014-03-07 09:46:20 -08001249 OpRegCopyWide(rl_result.reg, rl_src.reg);
1250 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1251 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1252 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001253 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1254 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001255 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001256 }
1257 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 }
Vladimir Markob9823312014-03-20 17:38:43 +00001259
1260 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001261 RegStorage sign_reg = AllocTemp();
1262 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1263 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1264 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1265 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1266 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001267 StoreValueWide(rl_dest, rl_result);
1268 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269}
1270
Yixin Shoudbb17e32014-02-07 05:09:30 -08001271bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1272 if (cu_->instruction_set == kMips) {
1273 // TODO - add Mips implementation
1274 return false;
1275 }
1276 RegLocation rl_src = info->args[0];
1277 rl_src = LoadValue(rl_src, kCoreReg);
1278 RegLocation rl_dest = InlineTarget(info);
1279 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001280 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001281 StoreValue(rl_dest, rl_result);
1282 return true;
1283}
1284
1285bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1286 if (cu_->instruction_set == kMips) {
1287 // TODO - add Mips implementation
1288 return false;
1289 }
1290 RegLocation rl_src = info->args[0];
1291 rl_src = LoadValueWide(rl_src, kCoreReg);
1292 RegLocation rl_dest = InlineTargetWide(info);
1293 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001294 OpRegCopyWide(rl_result.reg, rl_src.reg);
1295 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001296 StoreValueWide(rl_dest, rl_result);
1297 return true;
1298}
1299
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001300bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 if (cu_->instruction_set == kMips) {
1302 // TODO - add Mips implementation
1303 return false;
1304 }
1305 RegLocation rl_src = info->args[0];
1306 RegLocation rl_dest = InlineTarget(info);
1307 StoreValue(rl_dest, rl_src);
1308 return true;
1309}
1310
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001311bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 if (cu_->instruction_set == kMips) {
1313 // TODO - add Mips implementation
1314 return false;
1315 }
1316 RegLocation rl_src = info->args[0];
1317 RegLocation rl_dest = InlineTargetWide(info);
1318 StoreValueWide(rl_dest, rl_src);
1319 return true;
1320}
1321
1322/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001323 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 * otherwise bails to standard library code.
1325 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001326bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001327 if (cu_->instruction_set == kMips) {
1328 // TODO - add Mips implementation
1329 return false;
1330 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001331 RegLocation rl_obj = info->args[0];
1332 RegLocation rl_char = info->args[1];
1333 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1334 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1335 return false;
1336 }
1337
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001338 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001340 RegStorage reg_ptr = TargetReg(kArg0);
1341 RegStorage reg_char = TargetReg(kArg1);
1342 RegStorage reg_start = TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 LoadValueDirectFixed(rl_obj, reg_ptr);
1345 LoadValueDirectFixed(rl_char, reg_char);
1346 if (zero_based) {
1347 LoadConstant(reg_start, 0);
1348 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001349 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 LoadValueDirectFixed(rl_start, reg_start);
1351 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001352 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001353 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001354 LIR* high_code_point_branch =
1355 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001357 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001358 if (!rl_char.is_const) {
1359 // Add the slow path for code points beyond 0xFFFF.
1360 DCHECK(high_code_point_branch != nullptr);
1361 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1362 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001363 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001364 } else {
1365 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1366 DCHECK(high_code_point_branch == nullptr);
1367 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 RegLocation rl_return = GetReturn(false);
1369 RegLocation rl_dest = InlineTarget(info);
1370 StoreValue(rl_dest, rl_return);
1371 return true;
1372}
1373
1374/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001375bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 if (cu_->instruction_set == kMips) {
1377 // TODO - add Mips implementation
1378 return false;
1379 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001380 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001382 RegStorage reg_this = TargetReg(kArg0);
1383 RegStorage reg_cmp = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384
1385 RegLocation rl_this = info->args[0];
1386 RegLocation rl_cmp = info->args[1];
1387 LoadValueDirectFixed(rl_this, reg_this);
1388 LoadValueDirectFixed(rl_cmp, reg_cmp);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001389 RegStorage r_tgt = (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) ?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001390 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo)) : RegStorage::InvalidReg();
Dave Allisonf9439142014-03-27 15:10:22 -07001391 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001392 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001393 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001394 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001395 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001396 // NOTE: not a safepoint
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001397 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 OpReg(kOpBlx, r_tgt);
1399 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001400 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 RegLocation rl_return = GetReturn(false);
1403 RegLocation rl_dest = InlineTarget(info);
1404 StoreValue(rl_dest, rl_return);
1405 return true;
1406}
1407
1408bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1409 RegLocation rl_dest = InlineTarget(info);
1410 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001411 ThreadOffset<4> offset = Thread::PeerOffset<4>();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 if (cu_->instruction_set == kThumb2 || cu_->instruction_set == kMips) {
buzbee695d13a2014-04-19 13:32:20 -07001413 Load32Disp(TargetReg(kSelf), offset.Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001414 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001415 CHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001416 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg.GetReg(), offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 }
1418 StoreValue(rl_dest, rl_result);
1419 return true;
1420}
1421
1422bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1423 bool is_long, bool is_volatile) {
1424 if (cu_->instruction_set == kMips) {
1425 // TODO - add Mips implementation
1426 return false;
1427 }
1428 // Unused - RegLocation rl_src_unsafe = info->args[0];
1429 RegLocation rl_src_obj = info->args[1]; // Object
1430 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001431 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001432 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001433
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1435 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1436 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1437 if (is_long) {
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001438 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001439 LoadBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_result.reg.GetLow(),
buzbee695d13a2014-04-19 13:32:20 -07001440 rl_result.reg.GetHigh(), k64, INVALID_SREG);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001441 } else {
1442 RegStorage rl_temp_offset = AllocTemp();
1443 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1444 LoadBaseDispWide(rl_temp_offset, 0, rl_result.reg, INVALID_SREG);
1445 FreeTemp(rl_temp_offset.GetReg());
1446 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 } else {
buzbee695d13a2014-04-19 13:32:20 -07001448 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001449 }
1450
1451 if (is_volatile) {
1452 // Without context sensitive analysis, we must issue the most conservative barriers.
1453 // In this case, either a load or store may follow so we issue both barriers.
1454 GenMemBarrier(kLoadLoad);
1455 GenMemBarrier(kLoadStore);
1456 }
1457
1458 if (is_long) {
1459 StoreValueWide(rl_dest, rl_result);
1460 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001461 StoreValue(rl_dest, rl_result);
1462 }
1463 return true;
1464}
1465
1466bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1467 bool is_object, bool is_volatile, bool is_ordered) {
1468 if (cu_->instruction_set == kMips) {
1469 // TODO - add Mips implementation
1470 return false;
1471 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 // Unused - RegLocation rl_src_unsafe = info->args[0];
1473 RegLocation rl_src_obj = info->args[1]; // Object
1474 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001475 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001476 RegLocation rl_src_value = info->args[4]; // value to store
1477 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001478 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 GenMemBarrier(kStoreStore);
1480 }
1481 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1482 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1483 RegLocation rl_value;
1484 if (is_long) {
1485 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001486 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001487 StoreBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_value.reg.GetLow(),
buzbee695d13a2014-04-19 13:32:20 -07001488 rl_value.reg.GetHigh(), k64, INVALID_SREG);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001489 } else {
1490 RegStorage rl_temp_offset = AllocTemp();
1491 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1492 StoreBaseDispWide(rl_temp_offset, 0, rl_value.reg);
1493 FreeTemp(rl_temp_offset.GetReg());
1494 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 } else {
1496 rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -07001497 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001499
1500 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001501 FreeTemp(rl_offset.reg.GetReg());
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001502
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001504 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 GenMemBarrier(kStoreLoad);
1506 }
1507 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001508 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 }
1510 return true;
1511}
1512
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001513void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001514 if ((info->opt_flags & MIR_INLINED) != 0) {
1515 // Already inlined but we may still need the null check.
1516 if (info->type != kStatic &&
1517 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1518 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
1519 RegLocation rl_obj = LoadValue(info->args[0], kCoreReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001520 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001521 }
1522 return;
1523 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001524 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1525 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1526 ->GenIntrinsic(this, info)) {
1527 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001529 GenInvokeNoInline(info);
1530}
1531
1532void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533 int call_state = 0;
1534 LIR* null_ck;
1535 LIR** p_null_ck = NULL;
1536 NextCallInsn next_call_insn;
1537 FlushAllRegs(); /* Everything to home location */
1538 // Explicit register usage
1539 LockCallTemps();
1540
Vladimir Markof096aad2014-01-23 15:51:58 +00001541 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1542 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1543 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1544 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1545 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001547 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001549 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001550 } else if (info->type == kDirect) {
1551 if (fast_path) {
1552 p_null_ck = &null_ck;
1553 }
1554 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1555 skip_this = false;
1556 } else if (info->type == kStatic) {
1557 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1558 skip_this = false;
1559 } else if (info->type == kSuper) {
1560 DCHECK(!fast_path); // Fast path is a direct call.
1561 next_call_insn = NextSuperCallInsnSP;
1562 skip_this = false;
1563 } else {
1564 DCHECK_EQ(info->type, kVirtual);
1565 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1566 skip_this = fast_path;
1567 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001568 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 if (!info->is_range) {
1570 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001571 next_call_insn, target_method, method_info.VTableIndex(),
1572 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573 original_type, skip_this);
1574 } else {
1575 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001576 next_call_insn, target_method, method_info.VTableIndex(),
1577 method_info.DirectCode(), method_info.DirectMethod(),
1578 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 }
1580 // Finish up any of the call sequence not interleaved in arg loading
1581 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001582 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1583 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 }
1585 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001586 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1588 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001589 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001590 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001591 // We can have the linker fixup a call relative.
1592 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001593 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001594 } else {
1595 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1596 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1597 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001598 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001599 ThreadOffset<4> trampoline(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 switch (info->type) {
1601 case kInterface:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001602 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 break;
1604 case kDirect:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001605 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001606 break;
1607 case kStatic:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001608 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 break;
1610 case kSuper:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001611 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 break;
1613 case kVirtual:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001614 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 break;
1616 default:
1617 LOG(FATAL) << "Unexpected invoke type";
1618 }
1619 call_inst = OpThreadMem(kOpBlx, trampoline);
1620 }
1621 }
1622 MarkSafepointPC(call_inst);
1623
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001624 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 if (info->result.location != kLocInvalid) {
1626 // We have a following MOVE_RESULT - do it now.
1627 if (info->result.wide) {
1628 RegLocation ret_loc = GetReturnWide(info->result.fp);
1629 StoreValueWide(info->result, ret_loc);
1630 } else {
1631 RegLocation ret_loc = GetReturn(info->result.fp);
1632 StoreValue(info->result, ret_loc);
1633 }
1634 }
1635}
1636
1637} // namespace art