blob: 960f21790b562257dd0ab90ad52564550ba7da83 [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) {
buzbee90a21f82014-09-07 11:37:51 -07001110 // If result is unused, return a sink target based on type of invoke target.
1111 res = GetReturn(ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 } else {
1113 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -07001114 DCHECK_EQ(LocToRegClass(res),
1115 ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 }
1117 return res;
1118}
1119
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001120RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 RegLocation res;
1122 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -07001123 // If result is unused, return a sink target based on type of invoke target.
1124 res = GetReturnWide(ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 } else {
1126 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -07001127 DCHECK_EQ(LocToRegClass(res),
1128 ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 }
1130 return res;
1131}
1132
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -07001133bool Mir2Lir::GenInlinedReferenceGetReferent(CallInfo* info) {
Fred Shih4ee7a662014-07-11 09:59:27 -07001134 if (cu_->instruction_set == kMips) {
1135 // TODO - add Mips implementation
1136 return false;
1137 }
1138
Fred Shih4ee7a662014-07-11 09:59:27 -07001139 bool use_direct_type_ptr;
1140 uintptr_t direct_type_ptr;
Fred Shihe7f82e22014-08-06 10:46:37 -07001141 ClassReference ref;
1142 if (!cu_->compiler_driver->CanEmbedReferenceTypeInCode(&ref,
1143 &use_direct_type_ptr, &direct_type_ptr)) {
1144 return false;
1145 }
1146
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001147 RegStorage reg_class = TargetReg(kArg1, kRef);
1148 Clobber(reg_class);
1149 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -07001150 if (use_direct_type_ptr) {
1151 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -07001152 } else {
Fred Shihe7f82e22014-08-06 10:46:37 -07001153 uint16_t type_idx = ref.first->GetClassDef(ref.second).class_idx_;
1154 LoadClassType(*ref.first, type_idx, kArg1);
Fred Shih4ee7a662014-07-11 09:59:27 -07001155 }
Fred Shih4ee7a662014-07-11 09:59:27 -07001156
Fred Shihe7f82e22014-08-06 10:46:37 -07001157 uint32_t slow_path_flag_offset = cu_->compiler_driver->GetReferenceSlowFlagOffset();
1158 uint32_t disable_flag_offset = cu_->compiler_driver->GetReferenceDisableFlagOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -07001159 CHECK(slow_path_flag_offset && disable_flag_offset &&
1160 (slow_path_flag_offset != disable_flag_offset));
1161
1162 // intrinsic logic start.
1163 RegLocation rl_obj = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001164 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih4ee7a662014-07-11 09:59:27 -07001165
1166 RegStorage reg_slow_path = AllocTemp();
1167 RegStorage reg_disabled = AllocTemp();
Fred Shih37f05ef2014-07-16 18:38:08 -07001168 Load8Disp(reg_class, slow_path_flag_offset, reg_slow_path);
1169 Load8Disp(reg_class, disable_flag_offset, reg_disabled);
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001170 FreeTemp(reg_class);
1171 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -07001172 FreeTemp(reg_disabled);
1173
1174 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001175 LIR* slow_path_branch;
1176 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
1177 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
1178 slow_path_branch = OpCondBranch(kCondNe, nullptr);
1179 } else {
1180 // Generate compare and branch.
1181 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
1182 }
Fred Shih4ee7a662014-07-11 09:59:27 -07001183 FreeTemp(reg_slow_path);
1184
1185 // slow path not enabled, simply load the referent of the reference object
1186 RegLocation rl_dest = InlineTarget(info);
1187 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
1188 GenNullCheck(rl_obj.reg, info->opt_flags);
1189 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
1190 kNotVolatile);
1191 MarkPossibleNullPointerException(info->opt_flags);
1192 StoreValue(rl_dest, rl_result);
1193
1194 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
1195 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001196 ClobberCallerSave(); // We must clobber everything because slow path will return here
Fred Shih4ee7a662014-07-11 09:59:27 -07001197 return true;
1198}
1199
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001200bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 if (cu_->instruction_set == kMips) {
1202 // TODO - add Mips implementation
1203 return false;
1204 }
1205 // Location of reference to data array
1206 int value_offset = mirror::String::ValueOffset().Int32Value();
1207 // Location of count
1208 int count_offset = mirror::String::CountOffset().Int32Value();
1209 // Starting offset within data array
1210 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1211 // Start of char data with array_
1212 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1213
1214 RegLocation rl_obj = info->args[0];
1215 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -07001216 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -07001217 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001218 RegStorage reg_max;
1219 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001221 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001222 RegStorage reg_off;
1223 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -07001224 reg_off = AllocTemp();
1225 reg_ptr = AllocTempRef();
1226 if (range_check) {
1227 reg_max = AllocTemp();
1228 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001229 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 }
Andreas Gampe98430592014-07-27 19:44:50 -07001231 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1232 MarkPossibleNullPointerException(info->opt_flags);
1233 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
1234 if (range_check) {
1235 // Set up a slow path to allow retry in case of bounds violation */
1236 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
1237 FreeTemp(reg_max);
1238 range_check_branch = OpCondBranch(kCondUge, nullptr);
1239 }
1240 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001241 if (rl_idx.is_const) {
1242 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1243 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001244 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001245 }
buzbee2700f7e2014-03-07 09:46:20 -08001246 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001247 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001248 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001249 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 RegLocation rl_dest = InlineTarget(info);
1251 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001252 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 FreeTemp(reg_off);
1254 FreeTemp(reg_ptr);
1255 StoreValue(rl_dest, rl_result);
1256 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001257 DCHECK(range_check_branch != nullptr);
1258 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001259 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001261 return true;
1262}
1263
1264// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001265bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 if (cu_->instruction_set == kMips) {
1267 // TODO - add Mips implementation
1268 return false;
1269 }
1270 // dst = src.length();
1271 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001272 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273 RegLocation rl_dest = InlineTarget(info);
1274 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001275 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001276 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001277 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 if (is_empty) {
1279 // dst = (dst == 0);
1280 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001281 RegStorage t_reg = AllocTemp();
1282 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1283 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001284 } else if (cu_->instruction_set == kArm64) {
1285 OpRegImm(kOpSub, rl_result.reg, 1);
1286 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001288 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001289 OpRegImm(kOpSub, rl_result.reg, 1);
1290 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 }
1292 }
1293 StoreValue(rl_dest, rl_result);
1294 return true;
1295}
1296
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001297bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Zheng Xua3fe7422014-07-09 14:03:15 +08001298 if (cu_->instruction_set == kMips) {
1299 // TODO - add Mips implementation.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001300 return false;
1301 }
1302 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001303 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
1304 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001305 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001306 if (IsWide(size)) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001307 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001308 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1309 StoreValueWide(rl_dest, rl_result);
1310 return true;
1311 }
buzbee2700f7e2014-03-07 09:46:20 -08001312 RegStorage r_i_low = rl_i.reg.GetLow();
1313 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001314 // 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 +00001315 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001316 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001317 }
buzbee2700f7e2014-03-07 09:46:20 -08001318 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1319 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1320 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001321 FreeTemp(r_i_low);
1322 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001323 StoreValueWide(rl_dest, rl_result);
1324 } else {
buzbee695d13a2014-04-19 13:32:20 -07001325 DCHECK(size == k32 || size == kSignedHalf);
1326 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001327 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001328 StoreValue(rl_dest, rl_result);
1329 }
1330 return true;
1331}
1332
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001333bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 if (cu_->instruction_set == kMips) {
1335 // TODO - add Mips implementation
1336 return false;
1337 }
1338 RegLocation rl_src = info->args[0];
1339 rl_src = LoadValue(rl_src, kCoreReg);
1340 RegLocation rl_dest = InlineTarget(info);
1341 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001342 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001344 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1345 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1346 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 StoreValue(rl_dest, rl_result);
1348 return true;
1349}
1350
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001351bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 if (cu_->instruction_set == kMips) {
1353 // TODO - add Mips implementation
1354 return false;
1355 }
Vladimir Markob9823312014-03-20 17:38:43 +00001356 RegLocation rl_src = info->args[0];
1357 rl_src = LoadValueWide(rl_src, kCoreReg);
1358 RegLocation rl_dest = InlineTargetWide(info);
1359 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1360
1361 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001362 if (cu_->instruction_set != kX86_64 &&
1363 (cu_->instruction_set == kX86 ||
1364 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001365 OpRegCopyWide(rl_result.reg, rl_src.reg);
1366 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1367 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1368 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001369 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1370 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001371 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001372 }
1373 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 }
Vladimir Markob9823312014-03-20 17:38:43 +00001375
1376 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001377 RegStorage sign_reg;
1378 if (cu_->instruction_set == kX86_64) {
1379 sign_reg = AllocTempWide();
1380 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1381 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1382 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1383 } else {
1384 sign_reg = AllocTemp();
1385 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1386 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1387 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1388 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1389 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1390 }
buzbee082833c2014-05-17 23:16:26 -07001391 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001392 StoreValueWide(rl_dest, rl_result);
1393 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394}
1395
Serban Constantinescu23abec92014-07-02 16:13:38 +01001396bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
1397 // Currently implemented only for ARM64
1398 return false;
1399}
1400
1401bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
1402 // Currently implemented only for ARM64
1403 return false;
1404}
1405
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001406bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
1407 return false;
1408}
1409
1410bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
1411 return false;
1412}
1413
1414bool Mir2Lir::GenInlinedRint(CallInfo* info) {
1415 return false;
1416}
1417
1418bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
1419 return false;
1420}
1421
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001422bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001423 if (cu_->instruction_set == kMips) {
1424 // TODO - add Mips implementation
1425 return false;
1426 }
1427 RegLocation rl_src = info->args[0];
1428 RegLocation rl_dest = InlineTarget(info);
1429 StoreValue(rl_dest, rl_src);
1430 return true;
1431}
1432
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001433bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 if (cu_->instruction_set == kMips) {
1435 // TODO - add Mips implementation
1436 return false;
1437 }
1438 RegLocation rl_src = info->args[0];
1439 RegLocation rl_dest = InlineTargetWide(info);
1440 StoreValueWide(rl_dest, rl_src);
1441 return true;
1442}
1443
DaniilSokolov70c4f062014-06-24 17:34:00 -07001444bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
1445 return false;
1446}
1447
1448
Brian Carlstrom7940e442013-07-12 13:46:57 -07001449/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001450 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 * otherwise bails to standard library code.
1452 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001453bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454 if (cu_->instruction_set == kMips) {
1455 // TODO - add Mips implementation
1456 return false;
1457 }
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001458 if (cu_->instruction_set == kX86_64) {
1459 // TODO - add kX86_64 implementation
1460 return false;
1461 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001462 RegLocation rl_obj = info->args[0];
1463 RegLocation rl_char = info->args[1];
1464 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1465 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1466 return false;
1467 }
1468
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001469 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001470 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001471 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1472 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1473 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 LoadValueDirectFixed(rl_obj, reg_ptr);
1476 LoadValueDirectFixed(rl_char, reg_char);
1477 if (zero_based) {
1478 LoadConstant(reg_start, 0);
1479 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001480 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 LoadValueDirectFixed(rl_start, reg_start);
1482 }
Andreas Gampe98430592014-07-27 19:44:50 -07001483 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001484 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001485 LIR* high_code_point_branch =
1486 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001488 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001489 if (!rl_char.is_const) {
1490 // Add the slow path for code points beyond 0xFFFF.
1491 DCHECK(high_code_point_branch != nullptr);
1492 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1493 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001494 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001495 ClobberCallerSave(); // We must clobber everything because slow path will return here
Vladimir Marko3bc86152014-03-13 14:11:28 +00001496 } else {
1497 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1498 DCHECK(high_code_point_branch == nullptr);
1499 }
buzbeea0cd2d72014-06-01 09:33:49 -07001500 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 RegLocation rl_dest = InlineTarget(info);
1502 StoreValue(rl_dest, rl_return);
1503 return true;
1504}
1505
1506/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001507bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508 if (cu_->instruction_set == kMips) {
1509 // TODO - add Mips implementation
1510 return false;
1511 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001512 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001514 RegStorage reg_this = TargetReg(kArg0, kRef);
1515 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001516
1517 RegLocation rl_this = info->args[0];
1518 RegLocation rl_cmp = info->args[1];
1519 LoadValueDirectFixed(rl_this, reg_this);
1520 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001521 RegStorage r_tgt;
1522 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001523 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001524 } else {
1525 r_tgt = RegStorage::InvalidReg();
1526 }
Dave Allisonf9439142014-03-27 15:10:22 -07001527 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001528 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001529 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001530 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001531 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001533 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001534 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 RegLocation rl_dest = InlineTarget(info);
1536 StoreValue(rl_dest, rl_return);
1537 return true;
1538}
1539
1540bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1541 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001542
1543 // Early exit if the result is unused.
1544 if (rl_dest.orig_sreg < 0) {
1545 return true;
1546 }
1547
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001548 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001549
1550 switch (cu_->instruction_set) {
1551 case kArm:
1552 // Fall-through.
1553 case kThumb2:
1554 // Fall-through.
1555 case kMips:
Chao-ying Fua77ee512014-07-01 17:43:41 -07001556 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001557 break;
1558
1559 case kArm64:
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001560 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1561 kNotVolatile);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001562 break;
1563
Andreas Gampe2f244e92014-05-08 03:35:25 -07001564 default:
1565 LOG(FATAL) << "Unexpected isa " << cu_->instruction_set;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 }
1567 StoreValue(rl_dest, rl_result);
1568 return true;
1569}
1570
1571bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1572 bool is_long, bool is_volatile) {
1573 if (cu_->instruction_set == kMips) {
1574 // TODO - add Mips implementation
1575 return false;
1576 }
1577 // Unused - RegLocation rl_src_unsafe = info->args[0];
1578 RegLocation rl_src_obj = info->args[1]; // Object
1579 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001580 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001581 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001582
buzbeea0cd2d72014-06-01 09:33:49 -07001583 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001585 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001586 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001587 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1588 || cu_->instruction_set == kArm64) {
1589 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001590 } else {
1591 RegStorage rl_temp_offset = AllocTemp();
1592 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001593 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001594 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001595 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001597 if (rl_result.ref) {
1598 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1599 } else {
1600 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1601 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001602 }
1603
1604 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001605 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001606 }
1607
1608 if (is_long) {
1609 StoreValueWide(rl_dest, rl_result);
1610 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 StoreValue(rl_dest, rl_result);
1612 }
1613 return true;
1614}
1615
1616bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1617 bool is_object, bool is_volatile, bool is_ordered) {
1618 if (cu_->instruction_set == kMips) {
1619 // TODO - add Mips implementation
1620 return false;
1621 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 // Unused - RegLocation rl_src_unsafe = info->args[0];
1623 RegLocation rl_src_obj = info->args[1]; // Object
1624 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001625 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001626 RegLocation rl_src_value = info->args[4]; // value to store
1627 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001628 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 }
buzbeea0cd2d72014-06-01 09:33:49 -07001630 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1632 RegLocation rl_value;
1633 if (is_long) {
1634 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001635 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1636 || cu_->instruction_set == kArm64) {
1637 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001638 } else {
1639 RegStorage rl_temp_offset = AllocTemp();
1640 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001641 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001642 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001643 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001645 rl_value = LoadValue(rl_src_value);
Matteo Franchin255e0142014-07-04 13:50:41 +01001646 if (rl_value.ref) {
1647 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1648 } else {
1649 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1650 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001652
1653 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001654 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001655
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001657 // Prevent reordering with a subsequent volatile load.
1658 // May also be needed to address store atomicity issues.
1659 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 }
1661 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001662 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001663 }
1664 return true;
1665}
1666
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001667void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001668 if ((info->opt_flags & MIR_INLINED) != 0) {
1669 // Already inlined but we may still need the null check.
1670 if (info->type != kStatic &&
1671 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1672 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
buzbeea0cd2d72014-06-01 09:33:49 -07001673 RegLocation rl_obj = LoadValue(info->args[0], kRefReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001674 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001675 }
1676 return;
1677 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001678 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001679 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1680 ->GenIntrinsic(this, info)) {
1681 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001682 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001683 GenInvokeNoInline(info);
1684}
1685
Andreas Gampe2f244e92014-05-08 03:35:25 -07001686static LIR* GenInvokeNoInlineCall(Mir2Lir* mir_to_lir, InvokeType type) {
Andreas Gampe98430592014-07-27 19:44:50 -07001687 QuickEntrypointEnum trampoline;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001688 switch (type) {
1689 case kInterface:
Andreas Gampe98430592014-07-27 19:44:50 -07001690 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001691 break;
1692 case kDirect:
Andreas Gampe98430592014-07-27 19:44:50 -07001693 trampoline = kQuickInvokeDirectTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001694 break;
1695 case kStatic:
Andreas Gampe98430592014-07-27 19:44:50 -07001696 trampoline = kQuickInvokeStaticTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001697 break;
1698 case kSuper:
Andreas Gampe98430592014-07-27 19:44:50 -07001699 trampoline = kQuickInvokeSuperTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001700 break;
1701 case kVirtual:
Andreas Gampe98430592014-07-27 19:44:50 -07001702 trampoline = kQuickInvokeVirtualTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001703 break;
1704 default:
1705 LOG(FATAL) << "Unexpected invoke type";
Andreas Gampe98430592014-07-27 19:44:50 -07001706 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001707 }
Andreas Gampe98430592014-07-27 19:44:50 -07001708 return mir_to_lir->InvokeTrampoline(kOpBlx, RegStorage::InvalidReg(), trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001709}
1710
Vladimir Marko3bc86152014-03-13 14:11:28 +00001711void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001712 int call_state = 0;
1713 LIR* null_ck;
1714 LIR** p_null_ck = NULL;
1715 NextCallInsn next_call_insn;
1716 FlushAllRegs(); /* Everything to home location */
1717 // Explicit register usage
1718 LockCallTemps();
1719
Vladimir Markof096aad2014-01-23 15:51:58 +00001720 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1721 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
Mark Mendelle87f9b52014-04-30 14:13:18 -04001722 BeginInvoke(info);
Vladimir Markof096aad2014-01-23 15:51:58 +00001723 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1724 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1725 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001729 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730 } else if (info->type == kDirect) {
1731 if (fast_path) {
1732 p_null_ck = &null_ck;
1733 }
1734 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1735 skip_this = false;
1736 } else if (info->type == kStatic) {
1737 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1738 skip_this = false;
1739 } else if (info->type == kSuper) {
1740 DCHECK(!fast_path); // Fast path is a direct call.
1741 next_call_insn = NextSuperCallInsnSP;
1742 skip_this = false;
1743 } else {
1744 DCHECK_EQ(info->type, kVirtual);
1745 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1746 skip_this = fast_path;
1747 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001748 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001749 if (!info->is_range) {
1750 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001751 next_call_insn, target_method, method_info.VTableIndex(),
1752 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753 original_type, skip_this);
1754 } else {
1755 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001756 next_call_insn, target_method, method_info.VTableIndex(),
1757 method_info.DirectCode(), method_info.DirectMethod(),
1758 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 }
1760 // Finish up any of the call sequence not interleaved in arg loading
1761 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001762 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1763 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001764 }
1765 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001766 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001767 call_inst = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001769 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001770 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001771 // We can have the linker fixup a call relative.
1772 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001773 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001774 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001775 call_inst = OpMem(kOpBlx, TargetReg(kArg0, kRef),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001776 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1777 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001778 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001779 call_inst = GenInvokeNoInlineCall(this, info->type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001780 }
1781 }
Mark Mendelle87f9b52014-04-30 14:13:18 -04001782 EndInvoke(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 MarkSafepointPC(call_inst);
1784
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001785 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 if (info->result.location != kLocInvalid) {
1787 // We have a following MOVE_RESULT - do it now.
1788 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001789 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001790 StoreValueWide(info->result, ret_loc);
1791 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001792 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 StoreValue(info->result, ret_loc);
1794 }
1795 }
1796}
1797
1798} // namespace art