blob: 3cfc9a6c937b9ba39d43b377dfe4fdbff201043c [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "mirror/class-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070026#include "mirror/dex_cache.h"
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070027#include "mirror/object_array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "mirror/string.h"
29#include "mir_to_lir-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "x86/codegen_x86.h"
31
32namespace art {
33
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070034// Shortcuts to repeatedly used long types.
35typedef mirror::ObjectArray<mirror::Object> ObjArray;
36
Brian Carlstrom7940e442013-07-12 13:46:57 -070037/*
38 * This source files contains "gen" codegen routines that should
39 * be applicable to most targets. Only mid-level support utilities
40 * and "op" calls may be used here.
41 */
42
Mingyao Yang3a74d152014-04-21 15:39:44 -070043void Mir2Lir::AddIntrinsicSlowPath(CallInfo* info, LIR* branch, LIR* resume) {
44 class IntrinsicSlowPathPath : public Mir2Lir::LIRSlowPath {
Vladimir Marko3bc86152014-03-13 14:11:28 +000045 public:
Mingyao Yang3a74d152014-04-21 15:39:44 -070046 IntrinsicSlowPathPath(Mir2Lir* m2l, CallInfo* info, LIR* branch, LIR* resume = nullptr)
Vladimir Marko3bc86152014-03-13 14:11:28 +000047 : LIRSlowPath(m2l, info->offset, branch, resume), info_(info) {
48 }
49
50 void Compile() {
51 m2l_->ResetRegPool();
52 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070053 GenerateTargetLabel(kPseudoIntrinsicRetry);
Vladimir Marko3bc86152014-03-13 14:11:28 +000054 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
55 m2l_->GenInvokeNoInline(info_);
56 if (cont_ != nullptr) {
57 m2l_->OpUnconditionalBranch(cont_);
58 }
59 }
60
61 private:
62 CallInfo* const info_;
63 };
64
Mingyao Yang3a74d152014-04-21 15:39:44 -070065 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000066}
67
Brian Carlstrom7940e442013-07-12 13:46:57 -070068/*
69 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000070 * the helper target address, and the actual call to the helper. Because x86
71 * has a memory call operation, part 1 is a NOP for x86. For other targets,
72 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 */
Andreas Gampe2f244e92014-05-08 03:35:25 -070074// template <size_t pointer_size>
Andreas Gampe98430592014-07-27 19:44:50 -070075RegStorage Mir2Lir::CallHelperSetup(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070076 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
77 return RegStorage::InvalidReg();
78 } else {
Andreas Gampe98430592014-07-27 19:44:50 -070079 return LoadHelper(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070080 }
81}
82
Andreas Gampe98430592014-07-27 19:44:50 -070083LIR* Mir2Lir::CallHelper(RegStorage r_tgt, QuickEntrypointEnum trampoline, bool safepoint_pc,
84 bool use_link) {
85 LIR* call_inst = InvokeTrampoline(use_link ? kOpBlx : kOpBx, r_tgt, trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070086
Andreas Gampe98430592014-07-27 19:44:50 -070087 if (r_tgt.Valid()) {
Dave Allisond6ed6422014-04-09 23:36:15 +000088 FreeTemp(r_tgt);
89 }
Andreas Gampe98430592014-07-27 19:44:50 -070090
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 if (safepoint_pc) {
92 MarkSafepointPC(call_inst);
93 }
94 return call_inst;
95}
96
Andreas Gampe98430592014-07-27 19:44:50 -070097void Mir2Lir::CallRuntimeHelper(QuickEntrypointEnum trampoline, bool safepoint_pc) {
98 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang42894562014-04-07 12:42:16 -070099 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700100 CallHelper(r_tgt, trampoline, safepoint_pc);
Mingyao Yang42894562014-04-07 12:42:16 -0700101}
102
Andreas Gampe98430592014-07-27 19:44:50 -0700103void Mir2Lir::CallRuntimeHelperImm(QuickEntrypointEnum trampoline, int arg0, bool safepoint_pc) {
104 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700105 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000106 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700107 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108}
109
Andreas Gampe98430592014-07-27 19:44:50 -0700110void Mir2Lir::CallRuntimeHelperReg(QuickEntrypointEnum trampoline, RegStorage arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700111 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700112 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700113 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000114 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700115 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116}
117
Andreas Gampe98430592014-07-27 19:44:50 -0700118void Mir2Lir::CallRuntimeHelperRegLocation(QuickEntrypointEnum trampoline, RegLocation arg0,
119 bool safepoint_pc) {
120 RegStorage r_tgt = CallHelperSetup(trampoline);
buzbee2700f7e2014-03-07 09:46:20 -0800121 if (arg0.wide == 0) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700122 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, arg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700124 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000126 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700127 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128}
129
Andreas Gampe98430592014-07-27 19:44:50 -0700130void Mir2Lir::CallRuntimeHelperImmImm(QuickEntrypointEnum trampoline, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700132 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700133 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
134 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000135 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700136 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137}
138
Andreas Gampe98430592014-07-27 19:44:50 -0700139void Mir2Lir::CallRuntimeHelperImmRegLocation(QuickEntrypointEnum trampoline, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700141 RegStorage r_tgt = CallHelperSetup(trampoline);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 if (arg1.wide == 0) {
Andreas Gampef9872f02014-07-01 19:00:09 -0700143 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700145 RegStorage r_tmp = TargetReg(cu_->instruction_set == kMips ? kArg2 : kArg1, kWide);
buzbee2700f7e2014-03-07 09:46:20 -0800146 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700148 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000149 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700150 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151}
152
Andreas Gampe98430592014-07-27 19:44:50 -0700153void Mir2Lir::CallRuntimeHelperRegLocationImm(QuickEntrypointEnum trampoline, RegLocation arg0,
154 int arg1, bool safepoint_pc) {
155 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampef9872f02014-07-01 19:00:09 -0700156 DCHECK(!arg0.wide);
157 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700158 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000159 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700160 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161}
162
Andreas Gampe98430592014-07-27 19:44:50 -0700163void Mir2Lir::CallRuntimeHelperImmReg(QuickEntrypointEnum trampoline, int arg0, RegStorage arg1,
164 bool safepoint_pc) {
165 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700166 OpRegCopy(TargetReg(kArg1, arg1.GetWideKind()), arg1);
167 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000168 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700169 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170}
171
Andreas Gampe98430592014-07-27 19:44:50 -0700172void Mir2Lir::CallRuntimeHelperRegImm(QuickEntrypointEnum trampoline, RegStorage arg0, int arg1,
173 bool safepoint_pc) {
174 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700175 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
176 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000177 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700178 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179}
180
Andreas Gampe98430592014-07-27 19:44:50 -0700181void Mir2Lir::CallRuntimeHelperImmMethod(QuickEntrypointEnum trampoline, int arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700182 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700183 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700184 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
185 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000186 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700187 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188}
189
Andreas Gampe98430592014-07-27 19:44:50 -0700190void Mir2Lir::CallRuntimeHelperRegMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800191 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700192 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700193 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
194 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
195 if (r_tmp.NotExactlyEquals(arg0)) {
196 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800197 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700198 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800199 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700200 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800201}
202
Andreas Gampe98430592014-07-27 19:44:50 -0700203void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(QuickEntrypointEnum trampoline, RegStorage arg0,
204 RegLocation arg2, bool safepoint_pc) {
205 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700206 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
207 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
208 if (r_tmp.NotExactlyEquals(arg0)) {
209 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800210 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700211 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700212 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800213 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700214 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800215}
216
Andreas Gampe98430592014-07-27 19:44:50 -0700217void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(QuickEntrypointEnum trampoline,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700218 RegLocation arg0, RegLocation arg1,
219 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700220 RegStorage r_tgt = CallHelperSetup(trampoline);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700221 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700222 RegStorage arg0_reg = TargetReg((arg0.fp) ? kFArg0 : kArg0, arg0);
223
224 RegStorage arg1_reg;
225 if (arg1.fp == arg0.fp) {
226 arg1_reg = TargetReg((arg1.fp) ? kFArg1 : kArg1, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700228 arg1_reg = TargetReg((arg1.fp) ? kFArg0 : kArg0, arg1);
229 }
230
231 if (arg0.wide == 0) {
232 LoadValueDirectFixed(arg0, arg0_reg);
233 } else {
234 LoadValueDirectWideFixed(arg0, arg0_reg);
235 }
236
237 if (arg1.wide == 0) {
238 LoadValueDirectFixed(arg1, arg1_reg);
239 } else {
240 LoadValueDirectWideFixed(arg1, arg1_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 }
242 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700243 DCHECK(!cu_->target64);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700244 if (arg0.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700245 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700246 if (arg1.wide == 0) {
247 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700248 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700249 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700250 LoadValueDirectFixed(arg1, TargetReg(kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700251 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700252 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700253 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700254 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700255 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700256 LoadValueDirectWideFixed(arg1, TargetReg(kArg1, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700257 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700258 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700260 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700261 if (arg1.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700262 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700263 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700264 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700265 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 }
267 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000268 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700269 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270}
271
Mingyao Yang80365d92014-04-18 12:10:58 -0700272void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700273 WideKind arg0_kind = arg0.GetWideKind();
274 WideKind arg1_kind = arg1.GetWideKind();
275 if (IsSameReg(arg1, TargetReg(kArg0, arg1_kind))) {
276 if (IsSameReg(arg0, TargetReg(kArg1, arg0_kind))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700277 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampeccc60262014-07-04 18:02:38 -0700278 OpRegCopy(TargetReg(kArg2, arg1_kind), arg1);
279 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
280 OpRegCopy(TargetReg(kArg1, arg1_kind), TargetReg(kArg2, arg1_kind));
Mingyao Yang80365d92014-04-18 12:10:58 -0700281 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700282 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
283 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700284 }
285 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700286 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
287 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700288 }
289}
290
Andreas Gampe98430592014-07-27 19:44:50 -0700291void Mir2Lir::CallRuntimeHelperRegReg(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800292 RegStorage arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700293 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700294 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000295 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700296 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297}
298
Andreas Gampe98430592014-07-27 19:44:50 -0700299void Mir2Lir::CallRuntimeHelperRegRegImm(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800300 RegStorage arg1, int arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700301 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700302 CopyToArgumentRegs(arg0, arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700303 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000304 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700305 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306}
307
Andreas Gampe98430592014-07-27 19:44:50 -0700308void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(QuickEntrypointEnum trampoline, int arg0,
309 RegLocation arg2, bool safepoint_pc) {
310 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700311 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Andreas Gampeccc60262014-07-04 18:02:38 -0700312 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
313 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000314 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700315 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316}
317
Andreas Gampe98430592014-07-27 19:44:50 -0700318void Mir2Lir::CallRuntimeHelperImmMethodImm(QuickEntrypointEnum trampoline, int arg0, int arg2,
319 bool safepoint_pc) {
320 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700321 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
322 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
323 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000324 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700325 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326}
327
Andreas Gampe98430592014-07-27 19:44:50 -0700328void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(QuickEntrypointEnum trampoline, int arg0,
329 RegLocation arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 RegLocation arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700331 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700332 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
333 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700334 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700336 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700338 LoadValueDirectWideFixed(arg2, TargetReg(kArg2, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700340 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000341 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700342 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343}
344
Andreas Gampeccc60262014-07-04 18:02:38 -0700345void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(
Andreas Gampe98430592014-07-27 19:44:50 -0700346 QuickEntrypointEnum trampoline,
Andreas Gampeccc60262014-07-04 18:02:38 -0700347 RegLocation arg0,
348 RegLocation arg1,
349 RegLocation arg2,
350 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700351 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700352 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
353 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
354 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000355 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700356 CallHelper(r_tgt, trampoline, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700357}
358
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359/*
360 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100361 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 * assignment of promoted arguments.
363 *
364 * ArgLocs is an array of location records describing the incoming arguments
365 * with one location record per word of argument.
366 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700367void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800369 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 * It will attempt to keep kArg0 live (or copy it to home location
371 * if promoted).
372 */
373 RegLocation rl_src = rl_method;
374 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -0700375 rl_src.reg = TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700377 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700378 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 // If Method* has been promoted, explicitly flush
380 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700381 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 }
383
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800384 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800386 }
387
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
389 /*
390 * Copy incoming arguments to their proper home locations.
391 * NOTE: an older version of dx had an issue in which
392 * it would reuse static method argument registers.
393 * This could result in the same Dalvik virtual register
394 * being promoted to both core and fp regs. To account for this,
395 * we only copy to the corresponding promoted physical register
396 * if it matches the type of the SSA name for the incoming
397 * argument. It is also possible that long and double arguments
398 * end up half-promoted. In those cases, we must flush the promoted
399 * half to memory as well.
400 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100401 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 for (int i = 0; i < cu_->num_ins; i++) {
403 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800404 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800405
buzbee2700f7e2014-03-07 09:46:20 -0800406 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 // If arriving in register
408 bool need_flush = true;
409 RegLocation* t_loc = &ArgLocs[i];
410 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800411 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 need_flush = false;
413 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbeeb5860fb2014-06-21 15:31:01 -0700414 OpRegCopy(RegStorage::Solo32(v_map->fp_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 need_flush = false;
416 } else {
417 need_flush = true;
418 }
419
buzbeed0a03b82013-09-14 08:21:05 -0700420 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 if (t_loc->wide) {
422 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700423 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 need_flush |= (p_map->core_location != v_map->core_location) ||
425 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700426 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
427 /*
428 * In Arm, a double is represented as a pair of consecutive single float
429 * registers starting at an even number. It's possible that both Dalvik vRegs
430 * representing the incoming double were independently promoted as singles - but
431 * not in a form usable as a double. If so, we need to flush - even though the
432 * incoming arg appears fully in register. At this point in the code, both
433 * halves of the double are promoted. Make sure they are in a usable form.
434 */
435 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
buzbeeb5860fb2014-06-21 15:31:01 -0700436 int low_reg = promotion_map_[lowreg_index].fp_reg;
437 int high_reg = promotion_map_[lowreg_index + 1].fp_reg;
buzbeed0a03b82013-09-14 08:21:05 -0700438 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
439 need_flush = true;
440 }
441 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 }
443 if (need_flush) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700444 Store32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 }
446 } else {
447 // If arriving in frame & promoted
448 if (v_map->core_location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700449 Load32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i),
450 RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 }
452 if (v_map->fp_location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700453 Load32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i),
454 RegStorage::Solo32(v_map->fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 }
456 }
457 }
458}
459
Andreas Gampeccc60262014-07-04 18:02:38 -0700460static void CommonCallCodeLoadThisIntoArg1(const CallInfo* info, Mir2Lir* cg) {
461 RegLocation rl_arg = info->args[0];
462 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1, kRef));
463}
464
465static void CommonCallCodeLoadClassIntoArg0(const CallInfo* info, Mir2Lir* cg) {
466 cg->GenNullCheck(cg->TargetReg(kArg1, kRef), info->opt_flags);
467 // get this->klass_ [use kArg1, set kArg0]
468 cg->LoadRefDisp(cg->TargetReg(kArg1, kRef), mirror::Object::ClassOffset().Int32Value(),
469 cg->TargetReg(kArg0, kRef),
470 kNotVolatile);
471 cg->MarkPossibleNullPointerException(info->opt_flags);
472}
473
474static bool CommonCallCodeLoadCodePointerIntoInvokeTgt(const CallInfo* info,
475 const RegStorage* alt_from,
476 const CompilationUnit* cu, Mir2Lir* cg) {
477 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
478 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
479 cg->LoadWordDisp(alt_from == nullptr ? cg->TargetReg(kArg0, kRef) : *alt_from,
480 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
481 cg->TargetPtrReg(kInvokeTgt));
482 return true;
483 }
484 return false;
485}
486
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487/*
488 * Bit of a hack here - in the absence of a real scheduling pass,
489 * emit the next instruction in static & direct invoke sequences.
490 */
491static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
492 int state, const MethodReference& target_method,
493 uint32_t unused,
494 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700495 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 if (direct_code != 0 && direct_method != 0) {
498 switch (state) {
499 case 0: // Get the current Method* [sets kArg0]
Ian Rogersff093b32014-04-30 19:04:27 -0700500 if (direct_code != static_cast<uintptr_t>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700501 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700502 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
Ian Rogers83883d72013-10-21 21:07:24 -0700503 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700504 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700505 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 }
Ian Rogersff093b32014-04-30 19:04:27 -0700507 if (direct_method != static_cast<uintptr_t>(-1)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700508 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700510 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 }
512 break;
513 default:
514 return -1;
515 }
516 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700517 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 switch (state) {
519 case 0: // Get the current Method* [sets kArg0]
520 // TUNING: we can save a reg copy if Method* has been promoted.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700521 cg->LoadCurrMethodDirect(arg0_ref);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 break;
523 case 1: // Get method->dex_cache_resolved_methods_
Andreas Gampe4b537a82014-06-30 22:24:53 -0700524 cg->LoadRefDisp(arg0_ref,
buzbee695d13a2014-04-19 13:32:20 -0700525 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700526 arg0_ref,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000527 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 // Set up direct code if known.
529 if (direct_code != 0) {
Ian Rogersff093b32014-04-30 19:04:27 -0700530 if (direct_code != static_cast<uintptr_t>(-1)) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700531 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700532 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700533 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700534 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 }
536 }
537 break;
538 case 2: // Grab target method*
539 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700540 cg->LoadRefDisp(arg0_ref,
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700541 ObjArray::OffsetOfElement(target_method.dex_method_index).Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700542 arg0_ref,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000543 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 break;
545 case 3: // Grab the code from the method*
Andreas Gampeccc60262014-07-04 18:02:38 -0700546 if (direct_code == 0) {
547 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, &arg0_ref, cu, cg)) {
548 break; // kInvokeTgt := arg0_ref->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700550 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 break;
552 }
553 // Intentional fallthrough for x86
554 default:
555 return -1;
556 }
557 }
558 return state + 1;
559}
560
561/*
562 * Bit of a hack here - in the absence of a real scheduling pass,
563 * emit the next instruction in a virtual invoke sequence.
564 * We can use kLr as a temp prior to target address loading
565 * Note also that we'll load the first argument ("this") into
566 * kArg1 here rather than the standard LoadArgRegs.
567 */
568static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
569 int state, const MethodReference& target_method,
570 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700571 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
573 /*
574 * This is the fast path in which the target virtual method is
575 * fully resolved at compile time.
576 */
577 switch (state) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700578 case 0:
579 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700581 case 1:
582 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
583 // Includes a null-check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700585 case 2: {
586 // Get this->klass_.embedded_vtable[method_idx] [usr kArg0, set kArg0]
587 int32_t offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
588 method_idx * sizeof(mirror::Class::VTableEntry);
589 // Load target method from embedded vtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700590 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700592 }
593 case 3:
Andreas Gampeccc60262014-07-04 18:02:38 -0700594 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, nullptr, cu, cg)) {
595 break; // kInvokeTgt := kArg0->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 }
597 // Intentional fallthrough for X86
598 default:
599 return -1;
600 }
601 return state + 1;
602}
603
604/*
Jeff Hao88474b42013-10-23 16:24:40 -0700605 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
606 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
607 * more than one interface method map to the same index. Note also that we'll load the first
608 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 */
610static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
611 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700612 uint32_t method_idx, uintptr_t unused,
613 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615
Jeff Hao88474b42013-10-23 16:24:40 -0700616 switch (state) {
617 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700618 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Andreas Gampeccc60262014-07-04 18:02:38 -0700619 cg->LoadConstant(cg->TargetReg(kHiddenArg, kNotWide), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400620 if (cu->instruction_set == kX86) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700621 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, kNotWide), cg->TargetReg(kHiddenArg, kNotWide));
Jeff Hao88474b42013-10-23 16:24:40 -0700622 }
623 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700624 case 1:
625 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Jeff Hao88474b42013-10-23 16:24:40 -0700626 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700627 case 2:
628 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
629 // Includes a null-check.
Jeff Hao88474b42013-10-23 16:24:40 -0700630 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700631 case 3: { // Get target method [use kInvokeTgt, set kArg0]
632 int32_t offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
633 (method_idx % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
634 // Load target method from embedded imtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700635 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700636 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700637 }
638 case 4:
Andreas Gampeccc60262014-07-04 18:02:38 -0700639 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, nullptr, cu, cg)) {
640 break; // kInvokeTgt := kArg0->entrypoint
Jeff Hao88474b42013-10-23 16:24:40 -0700641 }
642 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 default:
644 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 }
646 return state + 1;
647}
648
Andreas Gampeccc60262014-07-04 18:02:38 -0700649static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info,
Andreas Gampe98430592014-07-27 19:44:50 -0700650 QuickEntrypointEnum trampoline, int state,
Andreas Gampeccc60262014-07-04 18:02:38 -0700651 const MethodReference& target_method, uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Andreas Gampe98430592014-07-27 19:44:50 -0700653
654
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 /*
656 * This handles the case in which the base method is not fully
657 * resolved at compile time, we bail to a runtime helper.
658 */
659 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700660 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 // Load trampoline target
Andreas Gampe98430592014-07-27 19:44:50 -0700662 int32_t disp;
663 if (cu->target64) {
664 disp = GetThreadOffset<8>(trampoline).Int32Value();
665 } else {
666 disp = GetThreadOffset<4>(trampoline).Int32Value();
667 }
668 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), disp, cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 }
670 // Load kArg0 with method index
671 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampeccc60262014-07-04 18:02:38 -0700672 cg->LoadConstant(cg->TargetReg(kArg0, kNotWide), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 return 1;
674 }
675 return -1;
676}
677
678static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
679 int state,
680 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000681 uint32_t unused, uintptr_t unused2,
682 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700683 return NextInvokeInsnSP(cu, info, kQuickInvokeStaticTrampolineWithAccessCheck, state,
684 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685}
686
687static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
688 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000689 uint32_t unused, uintptr_t unused2,
690 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700691 return NextInvokeInsnSP(cu, info, kQuickInvokeDirectTrampolineWithAccessCheck, state,
692 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693}
694
695static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
696 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000697 uint32_t unused, uintptr_t unused2,
698 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700699 return NextInvokeInsnSP(cu, info, kQuickInvokeSuperTrampolineWithAccessCheck, state,
700 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701}
702
703static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
704 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000705 uint32_t unused, uintptr_t unused2,
706 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700707 return NextInvokeInsnSP(cu, info, kQuickInvokeVirtualTrampolineWithAccessCheck, state,
708 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709}
710
711static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
712 CallInfo* info, int state,
713 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000714 uint32_t unused, uintptr_t unused2,
715 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700716 return NextInvokeInsnSP(cu, info, kQuickInvokeInterfaceTrampolineWithAccessCheck, state,
717 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718}
719
720int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
721 NextCallInsn next_call_insn,
722 const MethodReference& target_method,
723 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700724 uintptr_t direct_method, InvokeType type, bool skip_this) {
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700725 int last_arg_reg = 3 - 1;
Andreas Gampeccc60262014-07-04 18:02:38 -0700726 int arg_regs[3] = {TargetReg(kArg1, kNotWide).GetReg(), TargetReg(kArg2, kNotWide).GetReg(),
727 TargetReg(kArg3, kNotWide).GetReg()};
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700728
729 int next_reg = 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 int next_arg = 0;
731 if (skip_this) {
732 next_reg++;
733 next_arg++;
734 }
735 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
736 RegLocation rl_arg = info->args[next_arg++];
737 rl_arg = UpdateRawLoc(rl_arg);
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700738 if (rl_arg.wide && (next_reg <= last_arg_reg - 1)) {
739 RegStorage r_tmp(RegStorage::k64BitPair, arg_regs[next_reg], arg_regs[next_reg + 1]);
buzbee2700f7e2014-03-07 09:46:20 -0800740 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 next_reg++;
742 next_arg++;
743 } else {
744 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800745 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 rl_arg.is_const = false;
747 }
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700748 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(arg_regs[next_reg]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 }
750 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
751 direct_code, direct_method, type);
752 }
753 return call_state;
754}
755
756/*
757 * Load up to 5 arguments, the first three of which will be in
758 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
759 * and as part of the load sequence, it must be replaced with
760 * the target method pointer. Note, this may also be called
761 * for "range" variants if the number of arguments is 5 or fewer.
762 */
763int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
764 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
765 const MethodReference& target_method,
766 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700767 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 RegLocation rl_arg;
769
770 /* If no arguments, just return */
771 if (info->num_arg_words == 0)
772 return call_state;
773
774 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
775 direct_code, direct_method, type);
776
777 DCHECK_LE(info->num_arg_words, 5);
778 if (info->num_arg_words > 3) {
779 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700780 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 RegLocation rl_use0 = info->args[0];
782 RegLocation rl_use1 = info->args[1];
783 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800784 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
785 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 // Wide spans, we need the 2nd half of uses[2].
787 rl_arg = UpdateLocWide(rl_use2);
788 if (rl_arg.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -0700789 if (rl_arg.reg.IsPair()) {
790 reg = rl_arg.reg.GetHigh();
791 } else {
792 RegisterInfo* info = GetRegInfo(rl_arg.reg);
793 info = info->FindMatchingView(RegisterInfo::kHighSingleStorageMask);
794 if (info == nullptr) {
795 // NOTE: For hard float convention we won't split arguments across reg/mem.
796 UNIMPLEMENTED(FATAL) << "Needs hard float api.";
797 }
798 reg = info->GetReg();
799 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 } else {
801 // kArg2 & rArg3 can safely be used here
Andreas Gampeccc60262014-07-04 18:02:38 -0700802 reg = TargetReg(kArg3, kNotWide);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100803 {
804 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700805 Load32Disp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100806 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 call_state = next_call_insn(cu_, info, call_state, target_method,
808 vtable_idx, direct_code, direct_method, type);
809 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100810 {
811 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700812 Store32Disp(TargetPtrReg(kSp), (next_use + 1) * 4, reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100813 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
815 direct_code, direct_method, type);
816 next_use++;
817 }
818 // Loop through the rest
819 while (next_use < info->num_arg_words) {
buzbee091cc402014-03-31 10:14:40 -0700820 RegStorage arg_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 rl_arg = info->args[next_use];
822 rl_arg = UpdateRawLoc(rl_arg);
823 if (rl_arg.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700824 arg_reg = rl_arg.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700826 arg_reg = TargetReg(kArg2, rl_arg.wide ? kWide : kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 if (rl_arg.wide) {
buzbee091cc402014-03-31 10:14:40 -0700828 LoadValueDirectWideFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 } else {
buzbee091cc402014-03-31 10:14:40 -0700830 LoadValueDirectFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 }
832 call_state = next_call_insn(cu_, info, call_state, target_method,
833 vtable_idx, direct_code, direct_method, type);
834 }
835 int outs_offset = (next_use + 1) * 4;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100836 {
837 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
838 if (rl_arg.wide) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700839 StoreBaseDisp(TargetPtrReg(kSp), outs_offset, arg_reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100840 next_use += 2;
841 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700842 Store32Disp(TargetPtrReg(kSp), outs_offset, arg_reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100843 next_use++;
844 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 }
846 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
847 direct_code, direct_method, type);
848 }
849 }
850
851 call_state = LoadArgRegs(info, call_state, next_call_insn,
852 target_method, vtable_idx, direct_code, direct_method,
853 type, skip_this);
854
855 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +0000856 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700857 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700858 } else {
859 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +0000860 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) &&
861 (info->opt_flags & MIR_IGNORE_NULL_CHECK)) {
862 return call_state;
863 }
Dave Allisonf9439142014-03-27 15:10:22 -0700864 // In lieu of generating a check for kArg1 being null, we need to
865 // perform a load when doing implicit checks.
Dave Allison69dfe512014-07-11 17:11:58 +0000866 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700867 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 }
869 return call_state;
870}
871
Dave Allison69dfe512014-07-11 17:11:58 +0000872// Default implementation of implicit null pointer check.
873// Overridden by arch specific as necessary.
874void Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
875 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
876 return;
877 }
878 RegStorage tmp = AllocTemp();
879 Load32Disp(reg, 0, tmp);
880 MarkPossibleNullPointerException(opt_flags);
881 FreeTemp(tmp);
882}
883
884
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885/*
886 * May have 0+ arguments (also used for jumbo). Note that
887 * source virtual registers may be in physical registers, so may
888 * need to be flushed to home location before copying. This
889 * applies to arg3 and above (see below).
890 *
891 * Two general strategies:
892 * If < 20 arguments
893 * Pass args 3-18 using vldm/vstm block copy
894 * Pass arg0, arg1 & arg2 in kArg1-kArg3
895 * If 20+ arguments
896 * Pass args arg19+ using memcpy block copy
897 * Pass arg0, arg1 & arg2 in kArg1-kArg3
898 *
899 */
900int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
901 LIR** pcrLabel, NextCallInsn next_call_insn,
902 const MethodReference& target_method,
903 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700904 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 // If we can treat it as non-range (Jumbo ops will use range form)
906 if (info->num_arg_words <= 5)
907 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
908 next_call_insn, target_method, vtable_idx,
909 direct_code, direct_method, type, skip_this);
910 /*
911 * First load the non-register arguments. Both forms expect all
912 * of the source arguments to be in their home frame location, so
913 * scan the s_reg names and flush any that have been promoted to
914 * frame backing storage.
915 */
916 // Scan the rest of the args - if in phys_reg flush to memory
917 for (int next_arg = 0; next_arg < info->num_arg_words;) {
918 RegLocation loc = info->args[next_arg];
919 if (loc.wide) {
920 loc = UpdateLocWide(loc);
921 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100922 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700923 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 }
925 next_arg += 2;
926 } else {
927 loc = UpdateLoc(loc);
928 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100929 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700930 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931 }
932 next_arg++;
933 }
934 }
935
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800936 // Logic below assumes that Method pointer is at offset zero from SP.
937 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
938
939 // The first 3 arguments are passed via registers.
940 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
941 // get size of uintptr_t or size of object reference according to model being used.
942 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700943 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800944 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
945 DCHECK_GT(regs_left_to_pass_via_stack, 0);
946
947 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
948 // Use vldm/vstm pair using kArg3 as a temp
949 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
950 direct_code, direct_method, type);
Andreas Gampeccc60262014-07-04 18:02:38 -0700951 OpRegRegImm(kOpAdd, TargetReg(kArg3, kRef), TargetPtrReg(kSp), start_offset);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100952 LIR* ld = nullptr;
953 {
954 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -0700955 ld = OpVldm(TargetReg(kArg3, kRef), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100956 }
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800957 // TUNING: loosen barrier
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100958 ld->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800959 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
960 direct_code, direct_method, type);
Andreas Gampeccc60262014-07-04 18:02:38 -0700961 OpRegRegImm(kOpAdd, TargetReg(kArg3, kRef), TargetPtrReg(kSp), 4 /* Method* */ + (3 * 4));
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800962 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
963 direct_code, direct_method, type);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100964 LIR* st = nullptr;
965 {
966 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -0700967 st = OpVstm(TargetReg(kArg3, kRef), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100968 }
969 st->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800970 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
971 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700972 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800973 int current_src_offset = start_offset;
974 int current_dest_offset = outs_offset;
975
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100976 // Only davik regs are accessed in this loop; no next_call_insn() calls.
977 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800978 while (regs_left_to_pass_via_stack > 0) {
979 // This is based on the knowledge that the stack itself is 16-byte aligned.
980 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
981 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
982 size_t bytes_to_move;
983
984 /*
985 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
986 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
987 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
988 * We do this because we could potentially do a smaller move to align.
989 */
990 if (regs_left_to_pass_via_stack == 4 ||
991 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
992 // Moving 128-bits via xmm register.
993 bytes_to_move = sizeof(uint32_t) * 4;
994
995 // Allocate a free xmm temp. Since we are working through the calling sequence,
Mark Mendelle87f9b52014-04-30 14:13:18 -0400996 // we expect to have an xmm temporary available. AllocTempDouble will abort if
997 // there are no free registers.
buzbee2700f7e2014-03-07 09:46:20 -0800998 RegStorage temp = AllocTempDouble();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800999
1000 LIR* ld1 = nullptr;
1001 LIR* ld2 = nullptr;
1002 LIR* st1 = nullptr;
1003 LIR* st2 = nullptr;
1004
1005 /*
1006 * The logic is similar for both loads and stores. If we have 16-byte alignment,
1007 * do an aligned move. If we have 8-byte alignment, then do the move in two
1008 * parts. This approach prevents possible cache line splits. Finally, fall back
1009 * to doing an unaligned move. In most cases we likely won't split the cache
1010 * line but we cannot prove it and thus take a conservative approach.
1011 */
1012 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
1013 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
1014
1015 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001016 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001017 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001018 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovLo128FP);
1019 ld2 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001020 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001021 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001022 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001023 }
1024
1025 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001026 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001027 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001028 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovLo128FP);
1029 st2 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001030 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001031 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001032 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001033 }
1034
1035 // TODO If we could keep track of aliasing information for memory accesses that are wider
1036 // than 64-bit, we wouldn't need to set up a barrier.
1037 if (ld1 != nullptr) {
1038 if (ld2 != nullptr) {
1039 // For 64-bit load we can actually set up the aliasing information.
1040 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001041 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true,
1042 true);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001043 } else {
1044 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001045 ld1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001046 }
1047 }
1048 if (st1 != nullptr) {
1049 if (st2 != nullptr) {
1050 // For 64-bit store we can actually set up the aliasing information.
1051 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001052 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false,
1053 true);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001054 } else {
1055 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001056 st1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001057 }
1058 }
1059
1060 // Free the temporary used for the data movement.
buzbee091cc402014-03-31 10:14:40 -07001061 FreeTemp(temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001062 } else {
1063 // Moving 32-bits via general purpose register.
1064 bytes_to_move = sizeof(uint32_t);
1065
1066 // Instead of allocating a new temp, simply reuse one of the registers being used
1067 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07001068 RegStorage temp = TargetReg(kArg3, kNotWide);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001069
1070 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07001071 Load32Disp(TargetPtrReg(kSp), current_src_offset, temp);
1072 Store32Disp(TargetPtrReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001073 }
1074
1075 current_src_offset += bytes_to_move;
1076 current_dest_offset += bytes_to_move;
1077 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1078 }
1079 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 // Generate memcpy
Andreas Gampeccc60262014-07-04 18:02:38 -07001081 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
1082 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -07001083 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
1084 (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 }
1086
1087 call_state = LoadArgRegs(info, call_state, next_call_insn,
1088 target_method, vtable_idx, direct_code, direct_method,
1089 type, skip_this);
1090
1091 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1092 direct_code, direct_method, type);
1093 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00001094 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001095 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001096 } else {
1097 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +00001098 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) &&
1099 (info->opt_flags & MIR_IGNORE_NULL_CHECK)) {
1100 return call_state;
1101 }
Dave Allisonf9439142014-03-27 15:10:22 -07001102 // In lieu of generating a check for kArg1 being null, we need to
1103 // perform a load when doing implicit checks.
Dave Allison69dfe512014-07-11 17:11:58 +00001104 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001105 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 }
1107 return call_state;
1108}
1109
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001110RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 RegLocation res;
1112 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001113 res = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 } else {
1115 res = info->result;
1116 }
1117 return res;
1118}
1119
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001120RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 RegLocation res;
1122 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001123 res = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 } else {
1125 res = info->result;
1126 }
1127 return res;
1128}
1129
Fred Shihe7f82e22014-08-06 10:46:37 -07001130bool Mir2Lir::GenInlinedReferenceGet(CallInfo* info) {
Fred Shih4ee7a662014-07-11 09:59:27 -07001131 if (cu_->instruction_set == kMips) {
1132 // TODO - add Mips implementation
1133 return false;
1134 }
1135
Fred Shih4ee7a662014-07-11 09:59:27 -07001136 bool use_direct_type_ptr;
1137 uintptr_t direct_type_ptr;
Fred Shihe7f82e22014-08-06 10:46:37 -07001138 ClassReference ref;
1139 if (!cu_->compiler_driver->CanEmbedReferenceTypeInCode(&ref,
1140 &use_direct_type_ptr, &direct_type_ptr)) {
1141 return false;
1142 }
1143
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001144 RegStorage reg_class = TargetReg(kArg1, kRef);
1145 Clobber(reg_class);
1146 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -07001147 if (use_direct_type_ptr) {
1148 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -07001149 } else {
Fred Shihe7f82e22014-08-06 10:46:37 -07001150 uint16_t type_idx = ref.first->GetClassDef(ref.second).class_idx_;
1151 LoadClassType(*ref.first, type_idx, kArg1);
Fred Shih4ee7a662014-07-11 09:59:27 -07001152 }
Fred Shih4ee7a662014-07-11 09:59:27 -07001153
Fred Shihe7f82e22014-08-06 10:46:37 -07001154 uint32_t slow_path_flag_offset = cu_->compiler_driver->GetReferenceSlowFlagOffset();
1155 uint32_t disable_flag_offset = cu_->compiler_driver->GetReferenceDisableFlagOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -07001156 CHECK(slow_path_flag_offset && disable_flag_offset &&
1157 (slow_path_flag_offset != disable_flag_offset));
1158
1159 // intrinsic logic start.
1160 RegLocation rl_obj = info->args[0];
1161 rl_obj = LoadValue(rl_obj);
1162
1163 RegStorage reg_slow_path = AllocTemp();
1164 RegStorage reg_disabled = AllocTemp();
1165 Load32Disp(reg_class, slow_path_flag_offset, reg_slow_path);
1166 Load32Disp(reg_class, disable_flag_offset, reg_disabled);
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001167 FreeTemp(reg_class);
1168 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -07001169 FreeTemp(reg_disabled);
1170
1171 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001172 LIR* slow_path_branch;
1173 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
1174 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
1175 slow_path_branch = OpCondBranch(kCondNe, nullptr);
1176 } else {
1177 // Generate compare and branch.
1178 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
1179 }
Fred Shih4ee7a662014-07-11 09:59:27 -07001180 FreeTemp(reg_slow_path);
1181
1182 // slow path not enabled, simply load the referent of the reference object
1183 RegLocation rl_dest = InlineTarget(info);
1184 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
1185 GenNullCheck(rl_obj.reg, info->opt_flags);
1186 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
1187 kNotVolatile);
1188 MarkPossibleNullPointerException(info->opt_flags);
1189 StoreValue(rl_dest, rl_result);
1190
1191 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
1192 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
1193
1194 return true;
1195}
1196
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001197bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 if (cu_->instruction_set == kMips) {
1199 // TODO - add Mips implementation
1200 return false;
1201 }
1202 // Location of reference to data array
1203 int value_offset = mirror::String::ValueOffset().Int32Value();
1204 // Location of count
1205 int count_offset = mirror::String::CountOffset().Int32Value();
1206 // Starting offset within data array
1207 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1208 // Start of char data with array_
1209 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1210
1211 RegLocation rl_obj = info->args[0];
1212 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -07001213 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -07001214 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001215 RegStorage reg_max;
1216 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001218 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001219 RegStorage reg_off;
1220 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -07001221 reg_off = AllocTemp();
1222 reg_ptr = AllocTempRef();
1223 if (range_check) {
1224 reg_max = AllocTemp();
1225 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001226 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 }
Andreas Gampe98430592014-07-27 19:44:50 -07001228 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1229 MarkPossibleNullPointerException(info->opt_flags);
1230 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
1231 if (range_check) {
1232 // Set up a slow path to allow retry in case of bounds violation */
1233 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
1234 FreeTemp(reg_max);
1235 range_check_branch = OpCondBranch(kCondUge, nullptr);
1236 }
1237 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001238 if (rl_idx.is_const) {
1239 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1240 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001241 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001242 }
buzbee2700f7e2014-03-07 09:46:20 -08001243 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001244 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001245 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001246 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 RegLocation rl_dest = InlineTarget(info);
1248 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001249 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 FreeTemp(reg_off);
1251 FreeTemp(reg_ptr);
1252 StoreValue(rl_dest, rl_result);
1253 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001254 DCHECK(range_check_branch != nullptr);
1255 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001256 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 return true;
1259}
1260
1261// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001262bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 if (cu_->instruction_set == kMips) {
1264 // TODO - add Mips implementation
1265 return false;
1266 }
1267 // dst = src.length();
1268 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001269 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001270 RegLocation rl_dest = InlineTarget(info);
1271 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001272 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001273 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001274 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 if (is_empty) {
1276 // dst = (dst == 0);
1277 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001278 RegStorage t_reg = AllocTemp();
1279 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1280 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001281 } else if (cu_->instruction_set == kArm64) {
1282 OpRegImm(kOpSub, rl_result.reg, 1);
1283 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001285 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001286 OpRegImm(kOpSub, rl_result.reg, 1);
1287 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 }
1289 }
1290 StoreValue(rl_dest, rl_result);
1291 return true;
1292}
1293
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001294bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Zheng Xua3fe7422014-07-09 14:03:15 +08001295 if (cu_->instruction_set == kMips) {
1296 // TODO - add Mips implementation.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001297 return false;
1298 }
1299 RegLocation rl_src_i = info->args[0];
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001300 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -07001301 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001302 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001303 if (size == k64) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001304 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001305 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1306 StoreValueWide(rl_dest, rl_result);
1307 return true;
1308 }
buzbee2700f7e2014-03-07 09:46:20 -08001309 RegStorage r_i_low = rl_i.reg.GetLow();
1310 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001311 // 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 +00001312 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001313 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001314 }
buzbee2700f7e2014-03-07 09:46:20 -08001315 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1316 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1317 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001318 FreeTemp(r_i_low);
1319 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001320 StoreValueWide(rl_dest, rl_result);
1321 } else {
buzbee695d13a2014-04-19 13:32:20 -07001322 DCHECK(size == k32 || size == kSignedHalf);
1323 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001324 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001325 StoreValue(rl_dest, rl_result);
1326 }
1327 return true;
1328}
1329
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001330bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 if (cu_->instruction_set == kMips) {
1332 // TODO - add Mips implementation
1333 return false;
1334 }
1335 RegLocation rl_src = info->args[0];
1336 rl_src = LoadValue(rl_src, kCoreReg);
1337 RegLocation rl_dest = InlineTarget(info);
1338 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001339 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001341 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1342 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1343 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 StoreValue(rl_dest, rl_result);
1345 return true;
1346}
1347
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001348bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 if (cu_->instruction_set == kMips) {
1350 // TODO - add Mips implementation
1351 return false;
1352 }
Vladimir Markob9823312014-03-20 17:38:43 +00001353 RegLocation rl_src = info->args[0];
1354 rl_src = LoadValueWide(rl_src, kCoreReg);
1355 RegLocation rl_dest = InlineTargetWide(info);
1356 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1357
1358 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001359 if (cu_->instruction_set != kX86_64 &&
1360 (cu_->instruction_set == kX86 ||
1361 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001362 OpRegCopyWide(rl_result.reg, rl_src.reg);
1363 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1364 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1365 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001366 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1367 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001368 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001369 }
1370 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 }
Vladimir Markob9823312014-03-20 17:38:43 +00001372
1373 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001374 RegStorage sign_reg;
1375 if (cu_->instruction_set == kX86_64) {
1376 sign_reg = AllocTempWide();
1377 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1378 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1379 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1380 } else {
1381 sign_reg = AllocTemp();
1382 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1383 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1384 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1385 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1386 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1387 }
buzbee082833c2014-05-17 23:16:26 -07001388 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001389 StoreValueWide(rl_dest, rl_result);
1390 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391}
1392
Serban Constantinescu23abec92014-07-02 16:13:38 +01001393bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
1394 // Currently implemented only for ARM64
1395 return false;
1396}
1397
1398bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
1399 // Currently implemented only for ARM64
1400 return false;
1401}
1402
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001403bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
1404 return false;
1405}
1406
1407bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
1408 return false;
1409}
1410
1411bool Mir2Lir::GenInlinedRint(CallInfo* info) {
1412 return false;
1413}
1414
1415bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
1416 return false;
1417}
1418
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001419bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001420 if (cu_->instruction_set == kMips) {
1421 // TODO - add Mips implementation
1422 return false;
1423 }
1424 RegLocation rl_src = info->args[0];
1425 RegLocation rl_dest = InlineTarget(info);
1426 StoreValue(rl_dest, rl_src);
1427 return true;
1428}
1429
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001430bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431 if (cu_->instruction_set == kMips) {
1432 // TODO - add Mips implementation
1433 return false;
1434 }
1435 RegLocation rl_src = info->args[0];
1436 RegLocation rl_dest = InlineTargetWide(info);
1437 StoreValueWide(rl_dest, rl_src);
1438 return true;
1439}
1440
DaniilSokolov70c4f062014-06-24 17:34:00 -07001441bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
1442 return false;
1443}
1444
1445
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001447 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001448 * otherwise bails to standard library code.
1449 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001450bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 if (cu_->instruction_set == kMips) {
1452 // TODO - add Mips implementation
1453 return false;
1454 }
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001455 if (cu_->instruction_set == kX86_64) {
1456 // TODO - add kX86_64 implementation
1457 return false;
1458 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001459 RegLocation rl_obj = info->args[0];
1460 RegLocation rl_char = info->args[1];
1461 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1462 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1463 return false;
1464 }
1465
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001466 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001468 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1469 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1470 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 LoadValueDirectFixed(rl_obj, reg_ptr);
1473 LoadValueDirectFixed(rl_char, reg_char);
1474 if (zero_based) {
1475 LoadConstant(reg_start, 0);
1476 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001477 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 LoadValueDirectFixed(rl_start, reg_start);
1479 }
Andreas Gampe98430592014-07-27 19:44:50 -07001480 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001481 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001482 LIR* high_code_point_branch =
1483 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001485 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001486 if (!rl_char.is_const) {
1487 // Add the slow path for code points beyond 0xFFFF.
1488 DCHECK(high_code_point_branch != nullptr);
1489 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1490 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001491 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001492 } else {
1493 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1494 DCHECK(high_code_point_branch == nullptr);
1495 }
buzbeea0cd2d72014-06-01 09:33:49 -07001496 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 RegLocation rl_dest = InlineTarget(info);
1498 StoreValue(rl_dest, rl_return);
1499 return true;
1500}
1501
1502/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001503bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 if (cu_->instruction_set == kMips) {
1505 // TODO - add Mips implementation
1506 return false;
1507 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001508 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001510 RegStorage reg_this = TargetReg(kArg0, kRef);
1511 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512
1513 RegLocation rl_this = info->args[0];
1514 RegLocation rl_cmp = info->args[1];
1515 LoadValueDirectFixed(rl_this, reg_this);
1516 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001517 RegStorage r_tgt;
1518 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001519 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001520 } else {
1521 r_tgt = RegStorage::InvalidReg();
1522 }
Dave Allisonf9439142014-03-27 15:10:22 -07001523 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001524 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001525 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001526 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001527 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001529 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001530 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 RegLocation rl_dest = InlineTarget(info);
1532 StoreValue(rl_dest, rl_return);
1533 return true;
1534}
1535
1536bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1537 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001538
1539 // Early exit if the result is unused.
1540 if (rl_dest.orig_sreg < 0) {
1541 return true;
1542 }
1543
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001544 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001545
1546 switch (cu_->instruction_set) {
1547 case kArm:
1548 // Fall-through.
1549 case kThumb2:
1550 // Fall-through.
1551 case kMips:
Chao-ying Fua77ee512014-07-01 17:43:41 -07001552 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001553 break;
1554
1555 case kArm64:
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001556 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1557 kNotVolatile);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001558 break;
1559
Andreas Gampe2f244e92014-05-08 03:35:25 -07001560 default:
1561 LOG(FATAL) << "Unexpected isa " << cu_->instruction_set;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 }
1563 StoreValue(rl_dest, rl_result);
1564 return true;
1565}
1566
1567bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1568 bool is_long, bool is_volatile) {
1569 if (cu_->instruction_set == kMips) {
1570 // TODO - add Mips implementation
1571 return false;
1572 }
1573 // Unused - RegLocation rl_src_unsafe = info->args[0];
1574 RegLocation rl_src_obj = info->args[1]; // Object
1575 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001576 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001577 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001578
buzbeea0cd2d72014-06-01 09:33:49 -07001579 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001581 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001583 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1584 || cu_->instruction_set == kArm64) {
1585 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001586 } else {
1587 RegStorage rl_temp_offset = AllocTemp();
1588 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001589 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001590 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001591 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001592 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001593 if (rl_result.ref) {
1594 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1595 } else {
1596 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1597 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001598 }
1599
1600 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001601 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001602 }
1603
1604 if (is_long) {
1605 StoreValueWide(rl_dest, rl_result);
1606 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001607 StoreValue(rl_dest, rl_result);
1608 }
1609 return true;
1610}
1611
1612bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1613 bool is_object, bool is_volatile, bool is_ordered) {
1614 if (cu_->instruction_set == kMips) {
1615 // TODO - add Mips implementation
1616 return false;
1617 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 // Unused - RegLocation rl_src_unsafe = info->args[0];
1619 RegLocation rl_src_obj = info->args[1]; // Object
1620 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001621 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 RegLocation rl_src_value = info->args[4]; // value to store
1623 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001624 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 }
buzbeea0cd2d72014-06-01 09:33:49 -07001626 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001627 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1628 RegLocation rl_value;
1629 if (is_long) {
1630 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001631 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1632 || cu_->instruction_set == kArm64) {
1633 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001634 } else {
1635 RegStorage rl_temp_offset = AllocTemp();
1636 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001637 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001638 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001639 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001641 rl_value = LoadValue(rl_src_value);
Matteo Franchin255e0142014-07-04 13:50:41 +01001642 if (rl_value.ref) {
1643 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1644 } else {
1645 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1646 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001648
1649 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001650 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001651
Brian Carlstrom7940e442013-07-12 13:46:57 -07001652 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001653 // Prevent reordering with a subsequent volatile load.
1654 // May also be needed to address store atomicity issues.
1655 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 }
1657 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001658 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 }
1660 return true;
1661}
1662
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001663void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001664 if ((info->opt_flags & MIR_INLINED) != 0) {
1665 // Already inlined but we may still need the null check.
1666 if (info->type != kStatic &&
1667 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1668 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
buzbeea0cd2d72014-06-01 09:33:49 -07001669 RegLocation rl_obj = LoadValue(info->args[0], kRefReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001670 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001671 }
1672 return;
1673 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001674 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001675 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1676 ->GenIntrinsic(this, info)) {
1677 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001679 GenInvokeNoInline(info);
1680}
1681
Andreas Gampe2f244e92014-05-08 03:35:25 -07001682static LIR* GenInvokeNoInlineCall(Mir2Lir* mir_to_lir, InvokeType type) {
Andreas Gampe98430592014-07-27 19:44:50 -07001683 QuickEntrypointEnum trampoline;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001684 switch (type) {
1685 case kInterface:
Andreas Gampe98430592014-07-27 19:44:50 -07001686 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001687 break;
1688 case kDirect:
Andreas Gampe98430592014-07-27 19:44:50 -07001689 trampoline = kQuickInvokeDirectTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001690 break;
1691 case kStatic:
Andreas Gampe98430592014-07-27 19:44:50 -07001692 trampoline = kQuickInvokeStaticTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001693 break;
1694 case kSuper:
Andreas Gampe98430592014-07-27 19:44:50 -07001695 trampoline = kQuickInvokeSuperTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001696 break;
1697 case kVirtual:
Andreas Gampe98430592014-07-27 19:44:50 -07001698 trampoline = kQuickInvokeVirtualTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001699 break;
1700 default:
1701 LOG(FATAL) << "Unexpected invoke type";
Andreas Gampe98430592014-07-27 19:44:50 -07001702 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001703 }
Andreas Gampe98430592014-07-27 19:44:50 -07001704 return mir_to_lir->InvokeTrampoline(kOpBlx, RegStorage::InvalidReg(), trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001705}
1706
Vladimir Marko3bc86152014-03-13 14:11:28 +00001707void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 int call_state = 0;
1709 LIR* null_ck;
1710 LIR** p_null_ck = NULL;
1711 NextCallInsn next_call_insn;
1712 FlushAllRegs(); /* Everything to home location */
1713 // Explicit register usage
1714 LockCallTemps();
1715
Vladimir Markof096aad2014-01-23 15:51:58 +00001716 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1717 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
Mark Mendelle87f9b52014-04-30 14:13:18 -04001718 BeginInvoke(info);
Vladimir Markof096aad2014-01-23 15:51:58 +00001719 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1720 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1721 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001725 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 } else if (info->type == kDirect) {
1727 if (fast_path) {
1728 p_null_ck = &null_ck;
1729 }
1730 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1731 skip_this = false;
1732 } else if (info->type == kStatic) {
1733 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1734 skip_this = false;
1735 } else if (info->type == kSuper) {
1736 DCHECK(!fast_path); // Fast path is a direct call.
1737 next_call_insn = NextSuperCallInsnSP;
1738 skip_this = false;
1739 } else {
1740 DCHECK_EQ(info->type, kVirtual);
1741 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1742 skip_this = fast_path;
1743 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001744 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 if (!info->is_range) {
1746 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001747 next_call_insn, target_method, method_info.VTableIndex(),
1748 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001749 original_type, skip_this);
1750 } else {
1751 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001752 next_call_insn, target_method, method_info.VTableIndex(),
1753 method_info.DirectCode(), method_info.DirectMethod(),
1754 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 }
1756 // Finish up any of the call sequence not interleaved in arg loading
1757 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001758 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1759 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001760 }
1761 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001762 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001763 call_inst = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001764 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001765 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001766 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001767 // We can have the linker fixup a call relative.
1768 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001769 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001770 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001771 call_inst = OpMem(kOpBlx, TargetReg(kArg0, kRef),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001772 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1773 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001774 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001775 call_inst = GenInvokeNoInlineCall(this, info->type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001776 }
1777 }
Mark Mendelle87f9b52014-04-30 14:13:18 -04001778 EndInvoke(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001779 MarkSafepointPC(call_inst);
1780
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001781 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 if (info->result.location != kLocInvalid) {
1783 // We have a following MOVE_RESULT - do it now.
1784 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001785 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 StoreValueWide(info->result, ret_loc);
1787 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001788 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001789 StoreValue(info->result, ret_loc);
1790 }
1791 }
1792}
1793
1794} // namespace art