blob: e1d32412051a2609fde523b8f7dac74ceb7ec901 [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 Lupusoru8d0d03e2014-06-06 17:04:52 -0700384 if (mir_graph_->GetNumOfInVRs() == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800386 }
387
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700388 int start_vreg = mir_graph_->GetFirstInVR();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 /*
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);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700402 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 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 // The first 3 arguments are passed via registers.
937 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
938 // get size of uintptr_t or size of object reference according to model being used.
939 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800941 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
942 DCHECK_GT(regs_left_to_pass_via_stack, 0);
943
944 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
945 // Use vldm/vstm pair using kArg3 as a temp
946 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
947 direct_code, direct_method, type);
Andreas Gampeccc60262014-07-04 18:02:38 -0700948 OpRegRegImm(kOpAdd, TargetReg(kArg3, kRef), TargetPtrReg(kSp), start_offset);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100949 LIR* ld = nullptr;
950 {
951 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -0700952 ld = OpVldm(TargetReg(kArg3, kRef), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100953 }
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800954 // TUNING: loosen barrier
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100955 ld->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800956 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
957 direct_code, direct_method, type);
Andreas Gampeccc60262014-07-04 18:02:38 -0700958 OpRegRegImm(kOpAdd, TargetReg(kArg3, kRef), TargetPtrReg(kSp), 4 /* Method* */ + (3 * 4));
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);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100961 LIR* st = nullptr;
962 {
963 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -0700964 st = OpVstm(TargetReg(kArg3, kRef), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100965 }
966 st->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800967 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
968 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700969 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800970 int current_src_offset = start_offset;
971 int current_dest_offset = outs_offset;
972
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100973 // Only davik regs are accessed in this loop; no next_call_insn() calls.
974 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800975 while (regs_left_to_pass_via_stack > 0) {
976 // This is based on the knowledge that the stack itself is 16-byte aligned.
977 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
978 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
979 size_t bytes_to_move;
980
981 /*
982 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
983 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
984 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
985 * We do this because we could potentially do a smaller move to align.
986 */
987 if (regs_left_to_pass_via_stack == 4 ||
988 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
989 // Moving 128-bits via xmm register.
990 bytes_to_move = sizeof(uint32_t) * 4;
991
992 // Allocate a free xmm temp. Since we are working through the calling sequence,
Mark Mendelle87f9b52014-04-30 14:13:18 -0400993 // we expect to have an xmm temporary available. AllocTempDouble will abort if
994 // there are no free registers.
buzbee2700f7e2014-03-07 09:46:20 -0800995 RegStorage temp = AllocTempDouble();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800996
997 LIR* ld1 = nullptr;
998 LIR* ld2 = nullptr;
999 LIR* st1 = nullptr;
1000 LIR* st2 = nullptr;
1001
1002 /*
1003 * The logic is similar for both loads and stores. If we have 16-byte alignment,
1004 * do an aligned move. If we have 8-byte alignment, then do the move in two
1005 * parts. This approach prevents possible cache line splits. Finally, fall back
1006 * to doing an unaligned move. In most cases we likely won't split the cache
1007 * line but we cannot prove it and thus take a conservative approach.
1008 */
1009 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
1010 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
1011
1012 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001013 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001014 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001015 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovLo128FP);
1016 ld2 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001017 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001018 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001019 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001020 }
1021
1022 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001023 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001024 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001025 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovLo128FP);
1026 st2 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001027 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001028 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001029 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001030 }
1031
1032 // TODO If we could keep track of aliasing information for memory accesses that are wider
1033 // than 64-bit, we wouldn't need to set up a barrier.
1034 if (ld1 != nullptr) {
1035 if (ld2 != nullptr) {
1036 // For 64-bit load we can actually set up the aliasing information.
1037 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001038 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true,
1039 true);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001040 } else {
1041 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001042 ld1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001043 }
1044 }
1045 if (st1 != nullptr) {
1046 if (st2 != nullptr) {
1047 // For 64-bit store we can actually set up the aliasing information.
1048 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001049 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false,
1050 true);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001051 } else {
1052 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001053 st1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001054 }
1055 }
1056
1057 // Free the temporary used for the data movement.
buzbee091cc402014-03-31 10:14:40 -07001058 FreeTemp(temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001059 } else {
1060 // Moving 32-bits via general purpose register.
1061 bytes_to_move = sizeof(uint32_t);
1062
1063 // Instead of allocating a new temp, simply reuse one of the registers being used
1064 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07001065 RegStorage temp = TargetReg(kArg3, kNotWide);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001066
1067 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07001068 Load32Disp(TargetPtrReg(kSp), current_src_offset, temp);
1069 Store32Disp(TargetPtrReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001070 }
1071
1072 current_src_offset += bytes_to_move;
1073 current_dest_offset += bytes_to_move;
1074 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1075 }
1076 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 // Generate memcpy
Andreas Gampeccc60262014-07-04 18:02:38 -07001078 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
1079 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -07001080 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
1081 (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 }
1083
1084 call_state = LoadArgRegs(info, call_state, next_call_insn,
1085 target_method, vtable_idx, direct_code, direct_method,
1086 type, skip_this);
1087
1088 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1089 direct_code, direct_method, type);
1090 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00001091 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001092 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001093 } else {
1094 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +00001095 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) &&
1096 (info->opt_flags & MIR_IGNORE_NULL_CHECK)) {
1097 return call_state;
1098 }
Dave Allisonf9439142014-03-27 15:10:22 -07001099 // In lieu of generating a check for kArg1 being null, we need to
1100 // perform a load when doing implicit checks.
Dave Allison69dfe512014-07-11 17:11:58 +00001101 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001102 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 }
1104 return call_state;
1105}
1106
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001107RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 RegLocation res;
1109 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001110 res = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 } else {
1112 res = info->result;
1113 }
1114 return res;
1115}
1116
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001117RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 RegLocation res;
1119 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001120 res = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 } else {
1122 res = info->result;
1123 }
1124 return res;
1125}
1126
Fred Shihe7f82e22014-08-06 10:46:37 -07001127bool Mir2Lir::GenInlinedReferenceGet(CallInfo* info) {
Fred Shih4ee7a662014-07-11 09:59:27 -07001128 if (cu_->instruction_set == kMips) {
1129 // TODO - add Mips implementation
1130 return false;
1131 }
1132
Fred Shih4ee7a662014-07-11 09:59:27 -07001133 bool use_direct_type_ptr;
1134 uintptr_t direct_type_ptr;
Fred Shihe7f82e22014-08-06 10:46:37 -07001135 ClassReference ref;
1136 if (!cu_->compiler_driver->CanEmbedReferenceTypeInCode(&ref,
1137 &use_direct_type_ptr, &direct_type_ptr)) {
1138 return false;
1139 }
1140
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001141 RegStorage reg_class = TargetReg(kArg1, kRef);
1142 Clobber(reg_class);
1143 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -07001144 if (use_direct_type_ptr) {
1145 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -07001146 } else {
Fred Shihe7f82e22014-08-06 10:46:37 -07001147 uint16_t type_idx = ref.first->GetClassDef(ref.second).class_idx_;
1148 LoadClassType(*ref.first, type_idx, kArg1);
Fred Shih4ee7a662014-07-11 09:59:27 -07001149 }
Fred Shih4ee7a662014-07-11 09:59:27 -07001150
Fred Shihe7f82e22014-08-06 10:46:37 -07001151 uint32_t slow_path_flag_offset = cu_->compiler_driver->GetReferenceSlowFlagOffset();
1152 uint32_t disable_flag_offset = cu_->compiler_driver->GetReferenceDisableFlagOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -07001153 CHECK(slow_path_flag_offset && disable_flag_offset &&
1154 (slow_path_flag_offset != disable_flag_offset));
1155
1156 // intrinsic logic start.
1157 RegLocation rl_obj = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001158 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih4ee7a662014-07-11 09:59:27 -07001159
1160 RegStorage reg_slow_path = AllocTemp();
1161 RegStorage reg_disabled = AllocTemp();
Fred Shih37f05ef2014-07-16 18:38:08 -07001162 Load8Disp(reg_class, slow_path_flag_offset, reg_slow_path);
1163 Load8Disp(reg_class, disable_flag_offset, reg_disabled);
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001164 FreeTemp(reg_class);
1165 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -07001166 FreeTemp(reg_disabled);
1167
1168 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001169 LIR* slow_path_branch;
1170 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
1171 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
1172 slow_path_branch = OpCondBranch(kCondNe, nullptr);
1173 } else {
1174 // Generate compare and branch.
1175 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
1176 }
Fred Shih4ee7a662014-07-11 09:59:27 -07001177 FreeTemp(reg_slow_path);
1178
1179 // slow path not enabled, simply load the referent of the reference object
1180 RegLocation rl_dest = InlineTarget(info);
1181 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
1182 GenNullCheck(rl_obj.reg, info->opt_flags);
1183 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
1184 kNotVolatile);
1185 MarkPossibleNullPointerException(info->opt_flags);
1186 StoreValue(rl_dest, rl_result);
1187
1188 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
1189 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
1190
1191 return true;
1192}
1193
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001194bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 if (cu_->instruction_set == kMips) {
1196 // TODO - add Mips implementation
1197 return false;
1198 }
1199 // Location of reference to data array
1200 int value_offset = mirror::String::ValueOffset().Int32Value();
1201 // Location of count
1202 int count_offset = mirror::String::CountOffset().Int32Value();
1203 // Starting offset within data array
1204 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1205 // Start of char data with array_
1206 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1207
1208 RegLocation rl_obj = info->args[0];
1209 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -07001210 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -07001211 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001212 RegStorage reg_max;
1213 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001215 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001216 RegStorage reg_off;
1217 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -07001218 reg_off = AllocTemp();
1219 reg_ptr = AllocTempRef();
1220 if (range_check) {
1221 reg_max = AllocTemp();
1222 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001223 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 }
Andreas Gampe98430592014-07-27 19:44:50 -07001225 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1226 MarkPossibleNullPointerException(info->opt_flags);
1227 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
1228 if (range_check) {
1229 // Set up a slow path to allow retry in case of bounds violation */
1230 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
1231 FreeTemp(reg_max);
1232 range_check_branch = OpCondBranch(kCondUge, nullptr);
1233 }
1234 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001235 if (rl_idx.is_const) {
1236 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1237 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001238 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001239 }
buzbee2700f7e2014-03-07 09:46:20 -08001240 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001241 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001242 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001243 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 RegLocation rl_dest = InlineTarget(info);
1245 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001246 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 FreeTemp(reg_off);
1248 FreeTemp(reg_ptr);
1249 StoreValue(rl_dest, rl_result);
1250 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001251 DCHECK(range_check_branch != nullptr);
1252 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001253 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 return true;
1256}
1257
1258// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001259bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 if (cu_->instruction_set == kMips) {
1261 // TODO - add Mips implementation
1262 return false;
1263 }
1264 // dst = src.length();
1265 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001266 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 RegLocation rl_dest = InlineTarget(info);
1268 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001269 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001270 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001271 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 if (is_empty) {
1273 // dst = (dst == 0);
1274 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001275 RegStorage t_reg = AllocTemp();
1276 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1277 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001278 } else if (cu_->instruction_set == kArm64) {
1279 OpRegImm(kOpSub, rl_result.reg, 1);
1280 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001282 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001283 OpRegImm(kOpSub, rl_result.reg, 1);
1284 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 }
1286 }
1287 StoreValue(rl_dest, rl_result);
1288 return true;
1289}
1290
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001291bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Zheng Xua3fe7422014-07-09 14:03:15 +08001292 if (cu_->instruction_set == kMips) {
1293 // TODO - add Mips implementation.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001294 return false;
1295 }
1296 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001297 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
1298 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001299 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001300 if (IsWide(size)) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001301 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001302 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1303 StoreValueWide(rl_dest, rl_result);
1304 return true;
1305 }
buzbee2700f7e2014-03-07 09:46:20 -08001306 RegStorage r_i_low = rl_i.reg.GetLow();
1307 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001308 // 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 +00001309 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001310 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001311 }
buzbee2700f7e2014-03-07 09:46:20 -08001312 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1313 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1314 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001315 FreeTemp(r_i_low);
1316 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001317 StoreValueWide(rl_dest, rl_result);
1318 } else {
buzbee695d13a2014-04-19 13:32:20 -07001319 DCHECK(size == k32 || size == kSignedHalf);
1320 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001321 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001322 StoreValue(rl_dest, rl_result);
1323 }
1324 return true;
1325}
1326
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001327bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 if (cu_->instruction_set == kMips) {
1329 // TODO - add Mips implementation
1330 return false;
1331 }
1332 RegLocation rl_src = info->args[0];
1333 rl_src = LoadValue(rl_src, kCoreReg);
1334 RegLocation rl_dest = InlineTarget(info);
1335 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001336 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001338 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1339 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1340 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 StoreValue(rl_dest, rl_result);
1342 return true;
1343}
1344
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001345bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 if (cu_->instruction_set == kMips) {
1347 // TODO - add Mips implementation
1348 return false;
1349 }
Vladimir Markob9823312014-03-20 17:38:43 +00001350 RegLocation rl_src = info->args[0];
1351 rl_src = LoadValueWide(rl_src, kCoreReg);
1352 RegLocation rl_dest = InlineTargetWide(info);
1353 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1354
1355 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001356 if (cu_->instruction_set != kX86_64 &&
1357 (cu_->instruction_set == kX86 ||
1358 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001359 OpRegCopyWide(rl_result.reg, rl_src.reg);
1360 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1361 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1362 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001363 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1364 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001365 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001366 }
1367 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 }
Vladimir Markob9823312014-03-20 17:38:43 +00001369
1370 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001371 RegStorage sign_reg;
1372 if (cu_->instruction_set == kX86_64) {
1373 sign_reg = AllocTempWide();
1374 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1375 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1376 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1377 } else {
1378 sign_reg = AllocTemp();
1379 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1380 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1381 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1382 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1383 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1384 }
buzbee082833c2014-05-17 23:16:26 -07001385 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001386 StoreValueWide(rl_dest, rl_result);
1387 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388}
1389
Serban Constantinescu23abec92014-07-02 16:13:38 +01001390bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
1391 // Currently implemented only for ARM64
1392 return false;
1393}
1394
1395bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
1396 // Currently implemented only for ARM64
1397 return false;
1398}
1399
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001400bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
1401 return false;
1402}
1403
1404bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
1405 return false;
1406}
1407
1408bool Mir2Lir::GenInlinedRint(CallInfo* info) {
1409 return false;
1410}
1411
1412bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
1413 return false;
1414}
1415
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001416bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 if (cu_->instruction_set == kMips) {
1418 // TODO - add Mips implementation
1419 return false;
1420 }
1421 RegLocation rl_src = info->args[0];
1422 RegLocation rl_dest = InlineTarget(info);
1423 StoreValue(rl_dest, rl_src);
1424 return true;
1425}
1426
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001427bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 if (cu_->instruction_set == kMips) {
1429 // TODO - add Mips implementation
1430 return false;
1431 }
1432 RegLocation rl_src = info->args[0];
1433 RegLocation rl_dest = InlineTargetWide(info);
1434 StoreValueWide(rl_dest, rl_src);
1435 return true;
1436}
1437
DaniilSokolov70c4f062014-06-24 17:34:00 -07001438bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
1439 return false;
1440}
1441
1442
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001444 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 * otherwise bails to standard library code.
1446 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001447bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001448 if (cu_->instruction_set == kMips) {
1449 // TODO - add Mips implementation
1450 return false;
1451 }
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001452 if (cu_->instruction_set == kX86_64) {
1453 // TODO - add kX86_64 implementation
1454 return false;
1455 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001456 RegLocation rl_obj = info->args[0];
1457 RegLocation rl_char = info->args[1];
1458 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1459 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1460 return false;
1461 }
1462
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001463 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001465 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1466 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1467 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 LoadValueDirectFixed(rl_obj, reg_ptr);
1470 LoadValueDirectFixed(rl_char, reg_char);
1471 if (zero_based) {
1472 LoadConstant(reg_start, 0);
1473 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001474 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 LoadValueDirectFixed(rl_start, reg_start);
1476 }
Andreas Gampe98430592014-07-27 19:44:50 -07001477 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001478 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001479 LIR* high_code_point_branch =
1480 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001482 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001483 if (!rl_char.is_const) {
1484 // Add the slow path for code points beyond 0xFFFF.
1485 DCHECK(high_code_point_branch != nullptr);
1486 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1487 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001488 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001489 } else {
1490 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1491 DCHECK(high_code_point_branch == nullptr);
1492 }
buzbeea0cd2d72014-06-01 09:33:49 -07001493 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001494 RegLocation rl_dest = InlineTarget(info);
1495 StoreValue(rl_dest, rl_return);
1496 return true;
1497}
1498
1499/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001500bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 if (cu_->instruction_set == kMips) {
1502 // TODO - add Mips implementation
1503 return false;
1504 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001505 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001507 RegStorage reg_this = TargetReg(kArg0, kRef);
1508 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509
1510 RegLocation rl_this = info->args[0];
1511 RegLocation rl_cmp = info->args[1];
1512 LoadValueDirectFixed(rl_this, reg_this);
1513 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001514 RegStorage r_tgt;
1515 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001516 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001517 } else {
1518 r_tgt = RegStorage::InvalidReg();
1519 }
Dave Allisonf9439142014-03-27 15:10:22 -07001520 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001521 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001522 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001523 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001524 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001526 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001527 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 RegLocation rl_dest = InlineTarget(info);
1529 StoreValue(rl_dest, rl_return);
1530 return true;
1531}
1532
1533bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1534 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001535
1536 // Early exit if the result is unused.
1537 if (rl_dest.orig_sreg < 0) {
1538 return true;
1539 }
1540
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001541 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001542
1543 switch (cu_->instruction_set) {
1544 case kArm:
1545 // Fall-through.
1546 case kThumb2:
1547 // Fall-through.
1548 case kMips:
Chao-ying Fua77ee512014-07-01 17:43:41 -07001549 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001550 break;
1551
1552 case kArm64:
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001553 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1554 kNotVolatile);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001555 break;
1556
Andreas Gampe2f244e92014-05-08 03:35:25 -07001557 default:
1558 LOG(FATAL) << "Unexpected isa " << cu_->instruction_set;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 }
1560 StoreValue(rl_dest, rl_result);
1561 return true;
1562}
1563
1564bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1565 bool is_long, bool is_volatile) {
1566 if (cu_->instruction_set == kMips) {
1567 // TODO - add Mips implementation
1568 return false;
1569 }
1570 // Unused - RegLocation rl_src_unsafe = info->args[0];
1571 RegLocation rl_src_obj = info->args[1]; // Object
1572 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001573 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001574 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001575
buzbeea0cd2d72014-06-01 09:33:49 -07001576 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001578 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001580 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1581 || cu_->instruction_set == kArm64) {
1582 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001583 } else {
1584 RegStorage rl_temp_offset = AllocTemp();
1585 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001586 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001587 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001588 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001589 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001590 if (rl_result.ref) {
1591 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1592 } else {
1593 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1594 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001595 }
1596
1597 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001598 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001599 }
1600
1601 if (is_long) {
1602 StoreValueWide(rl_dest, rl_result);
1603 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 StoreValue(rl_dest, rl_result);
1605 }
1606 return true;
1607}
1608
1609bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1610 bool is_object, bool is_volatile, bool is_ordered) {
1611 if (cu_->instruction_set == kMips) {
1612 // TODO - add Mips implementation
1613 return false;
1614 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 // Unused - RegLocation rl_src_unsafe = info->args[0];
1616 RegLocation rl_src_obj = info->args[1]; // Object
1617 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001618 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 RegLocation rl_src_value = info->args[4]; // value to store
1620 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001621 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 }
buzbeea0cd2d72014-06-01 09:33:49 -07001623 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1625 RegLocation rl_value;
1626 if (is_long) {
1627 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001628 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1629 || cu_->instruction_set == kArm64) {
1630 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001631 } else {
1632 RegStorage rl_temp_offset = AllocTemp();
1633 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001634 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001635 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001636 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001638 rl_value = LoadValue(rl_src_value);
Matteo Franchin255e0142014-07-04 13:50:41 +01001639 if (rl_value.ref) {
1640 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1641 } else {
1642 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1643 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001645
1646 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001647 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001648
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001650 // Prevent reordering with a subsequent volatile load.
1651 // May also be needed to address store atomicity issues.
1652 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 }
1654 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001655 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 }
1657 return true;
1658}
1659
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001660void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001661 if ((info->opt_flags & MIR_INLINED) != 0) {
1662 // Already inlined but we may still need the null check.
1663 if (info->type != kStatic &&
1664 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1665 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
buzbeea0cd2d72014-06-01 09:33:49 -07001666 RegLocation rl_obj = LoadValue(info->args[0], kRefReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001667 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001668 }
1669 return;
1670 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001671 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001672 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1673 ->GenIntrinsic(this, info)) {
1674 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001676 GenInvokeNoInline(info);
1677}
1678
Andreas Gampe2f244e92014-05-08 03:35:25 -07001679static LIR* GenInvokeNoInlineCall(Mir2Lir* mir_to_lir, InvokeType type) {
Andreas Gampe98430592014-07-27 19:44:50 -07001680 QuickEntrypointEnum trampoline;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001681 switch (type) {
1682 case kInterface:
Andreas Gampe98430592014-07-27 19:44:50 -07001683 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001684 break;
1685 case kDirect:
Andreas Gampe98430592014-07-27 19:44:50 -07001686 trampoline = kQuickInvokeDirectTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001687 break;
1688 case kStatic:
Andreas Gampe98430592014-07-27 19:44:50 -07001689 trampoline = kQuickInvokeStaticTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001690 break;
1691 case kSuper:
Andreas Gampe98430592014-07-27 19:44:50 -07001692 trampoline = kQuickInvokeSuperTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001693 break;
1694 case kVirtual:
Andreas Gampe98430592014-07-27 19:44:50 -07001695 trampoline = kQuickInvokeVirtualTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001696 break;
1697 default:
1698 LOG(FATAL) << "Unexpected invoke type";
Andreas Gampe98430592014-07-27 19:44:50 -07001699 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001700 }
Andreas Gampe98430592014-07-27 19:44:50 -07001701 return mir_to_lir->InvokeTrampoline(kOpBlx, RegStorage::InvalidReg(), trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001702}
1703
Vladimir Marko3bc86152014-03-13 14:11:28 +00001704void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 int call_state = 0;
1706 LIR* null_ck;
1707 LIR** p_null_ck = NULL;
1708 NextCallInsn next_call_insn;
1709 FlushAllRegs(); /* Everything to home location */
1710 // Explicit register usage
1711 LockCallTemps();
1712
Vladimir Markof096aad2014-01-23 15:51:58 +00001713 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1714 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
Mark Mendelle87f9b52014-04-30 14:13:18 -04001715 BeginInvoke(info);
Vladimir Markof096aad2014-01-23 15:51:58 +00001716 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1717 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1718 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001722 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 } else if (info->type == kDirect) {
1724 if (fast_path) {
1725 p_null_ck = &null_ck;
1726 }
1727 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1728 skip_this = false;
1729 } else if (info->type == kStatic) {
1730 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1731 skip_this = false;
1732 } else if (info->type == kSuper) {
1733 DCHECK(!fast_path); // Fast path is a direct call.
1734 next_call_insn = NextSuperCallInsnSP;
1735 skip_this = false;
1736 } else {
1737 DCHECK_EQ(info->type, kVirtual);
1738 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1739 skip_this = fast_path;
1740 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001741 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001742 if (!info->is_range) {
1743 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001744 next_call_insn, target_method, method_info.VTableIndex(),
1745 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 original_type, skip_this);
1747 } else {
1748 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001749 next_call_insn, target_method, method_info.VTableIndex(),
1750 method_info.DirectCode(), method_info.DirectMethod(),
1751 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 }
1753 // Finish up any of the call sequence not interleaved in arg loading
1754 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001755 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1756 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 }
1758 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001759 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001760 call_inst = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001761 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001762 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001763 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001764 // We can have the linker fixup a call relative.
1765 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001766 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001767 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001768 call_inst = OpMem(kOpBlx, TargetReg(kArg0, kRef),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001769 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1770 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001771 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001772 call_inst = GenInvokeNoInlineCall(this, info->type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001773 }
1774 }
Mark Mendelle87f9b52014-04-30 14:13:18 -04001775 EndInvoke(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001776 MarkSafepointPC(call_inst);
1777
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001778 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001779 if (info->result.location != kLocInvalid) {
1780 // We have a following MOVE_RESULT - do it now.
1781 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001782 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 StoreValueWide(info->result, ret_loc);
1784 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001785 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 StoreValue(info->result, ret_loc);
1787 }
1788 }
1789}
1790
1791} // namespace art