blob: e70b0c5a480c2e2416fe0c0854f6d43c500b170b [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"
Fred Shih4ee7a662014-07-11 09:59:27 -070028#include "mirror/reference-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "mirror/string.h"
30#include "mir_to_lir-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070031#include "scoped_thread_state_change.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "x86/codegen_x86.h"
33
34namespace art {
35
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070036// Shortcuts to repeatedly used long types.
37typedef mirror::ObjectArray<mirror::Object> ObjArray;
38
Brian Carlstrom7940e442013-07-12 13:46:57 -070039/*
40 * This source files contains "gen" codegen routines that should
41 * be applicable to most targets. Only mid-level support utilities
42 * and "op" calls may be used here.
43 */
44
Mingyao Yang3a74d152014-04-21 15:39:44 -070045void Mir2Lir::AddIntrinsicSlowPath(CallInfo* info, LIR* branch, LIR* resume) {
46 class IntrinsicSlowPathPath : public Mir2Lir::LIRSlowPath {
Vladimir Marko3bc86152014-03-13 14:11:28 +000047 public:
Mingyao Yang3a74d152014-04-21 15:39:44 -070048 IntrinsicSlowPathPath(Mir2Lir* m2l, CallInfo* info, LIR* branch, LIR* resume = nullptr)
Vladimir Marko3bc86152014-03-13 14:11:28 +000049 : LIRSlowPath(m2l, info->offset, branch, resume), info_(info) {
50 }
51
52 void Compile() {
53 m2l_->ResetRegPool();
54 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070055 GenerateTargetLabel(kPseudoIntrinsicRetry);
Vladimir Marko3bc86152014-03-13 14:11:28 +000056 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
57 m2l_->GenInvokeNoInline(info_);
58 if (cont_ != nullptr) {
59 m2l_->OpUnconditionalBranch(cont_);
60 }
61 }
62
63 private:
64 CallInfo* const info_;
65 };
66
Mingyao Yang3a74d152014-04-21 15:39:44 -070067 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000068}
69
Brian Carlstrom7940e442013-07-12 13:46:57 -070070/*
71 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000072 * the helper target address, and the actual call to the helper. Because x86
73 * has a memory call operation, part 1 is a NOP for x86. For other targets,
74 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 */
Andreas Gampe2f244e92014-05-08 03:35:25 -070076// template <size_t pointer_size>
Andreas Gampe98430592014-07-27 19:44:50 -070077RegStorage Mir2Lir::CallHelperSetup(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070078 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
79 return RegStorage::InvalidReg();
80 } else {
Andreas Gampe98430592014-07-27 19:44:50 -070081 return LoadHelper(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070082 }
83}
84
Andreas Gampe98430592014-07-27 19:44:50 -070085LIR* Mir2Lir::CallHelper(RegStorage r_tgt, QuickEntrypointEnum trampoline, bool safepoint_pc,
86 bool use_link) {
87 LIR* call_inst = InvokeTrampoline(use_link ? kOpBlx : kOpBx, r_tgt, trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070088
Andreas Gampe98430592014-07-27 19:44:50 -070089 if (r_tgt.Valid()) {
Dave Allisond6ed6422014-04-09 23:36:15 +000090 FreeTemp(r_tgt);
91 }
Andreas Gampe98430592014-07-27 19:44:50 -070092
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 if (safepoint_pc) {
94 MarkSafepointPC(call_inst);
95 }
96 return call_inst;
97}
98
Andreas Gampe98430592014-07-27 19:44:50 -070099void Mir2Lir::CallRuntimeHelper(QuickEntrypointEnum trampoline, bool safepoint_pc) {
100 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang42894562014-04-07 12:42:16 -0700101 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700102 CallHelper(r_tgt, trampoline, safepoint_pc);
Mingyao Yang42894562014-04-07 12:42:16 -0700103}
104
Andreas Gampe98430592014-07-27 19:44:50 -0700105void Mir2Lir::CallRuntimeHelperImm(QuickEntrypointEnum trampoline, int arg0, bool safepoint_pc) {
106 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700107 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000108 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700109 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110}
111
Andreas Gampe98430592014-07-27 19:44:50 -0700112void Mir2Lir::CallRuntimeHelperReg(QuickEntrypointEnum trampoline, RegStorage arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700113 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700114 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700115 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000116 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700117 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118}
119
Andreas Gampe98430592014-07-27 19:44:50 -0700120void Mir2Lir::CallRuntimeHelperRegLocation(QuickEntrypointEnum trampoline, RegLocation arg0,
121 bool safepoint_pc) {
122 RegStorage r_tgt = CallHelperSetup(trampoline);
buzbee2700f7e2014-03-07 09:46:20 -0800123 if (arg0.wide == 0) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700124 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, arg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700126 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000128 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700129 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130}
131
Andreas Gampe98430592014-07-27 19:44:50 -0700132void Mir2Lir::CallRuntimeHelperImmImm(QuickEntrypointEnum trampoline, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700134 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700135 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
136 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000137 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700138 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139}
140
Andreas Gampe98430592014-07-27 19:44:50 -0700141void Mir2Lir::CallRuntimeHelperImmRegLocation(QuickEntrypointEnum trampoline, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700143 RegStorage r_tgt = CallHelperSetup(trampoline);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 if (arg1.wide == 0) {
Andreas Gampef9872f02014-07-01 19:00:09 -0700145 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700147 RegStorage r_tmp = TargetReg(cu_->instruction_set == kMips ? kArg2 : kArg1, kWide);
buzbee2700f7e2014-03-07 09:46:20 -0800148 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700150 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000151 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700152 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153}
154
Andreas Gampe98430592014-07-27 19:44:50 -0700155void Mir2Lir::CallRuntimeHelperRegLocationImm(QuickEntrypointEnum trampoline, RegLocation arg0,
156 int arg1, bool safepoint_pc) {
157 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampef9872f02014-07-01 19:00:09 -0700158 DCHECK(!arg0.wide);
159 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700160 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000161 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700162 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163}
164
Andreas Gampe98430592014-07-27 19:44:50 -0700165void Mir2Lir::CallRuntimeHelperImmReg(QuickEntrypointEnum trampoline, int arg0, RegStorage arg1,
166 bool safepoint_pc) {
167 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700168 OpRegCopy(TargetReg(kArg1, arg1.GetWideKind()), arg1);
169 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000170 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700171 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172}
173
Andreas Gampe98430592014-07-27 19:44:50 -0700174void Mir2Lir::CallRuntimeHelperRegImm(QuickEntrypointEnum trampoline, RegStorage arg0, int arg1,
175 bool safepoint_pc) {
176 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700177 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
178 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000179 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700180 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181}
182
Andreas Gampe98430592014-07-27 19:44:50 -0700183void Mir2Lir::CallRuntimeHelperImmMethod(QuickEntrypointEnum trampoline, int arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700184 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700185 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700186 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
187 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000188 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700189 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190}
191
Andreas Gampe98430592014-07-27 19:44:50 -0700192void Mir2Lir::CallRuntimeHelperRegMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800193 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700194 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700195 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
196 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
197 if (r_tmp.NotExactlyEquals(arg0)) {
198 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800199 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700200 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800201 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700202 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800203}
204
Andreas Gampe98430592014-07-27 19:44:50 -0700205void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(QuickEntrypointEnum trampoline, RegStorage arg0,
206 RegLocation arg2, bool safepoint_pc) {
207 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700208 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
209 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
210 if (r_tmp.NotExactlyEquals(arg0)) {
211 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800212 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700213 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700214 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800215 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700216 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800217}
218
Andreas Gampe98430592014-07-27 19:44:50 -0700219void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(QuickEntrypointEnum trampoline,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700220 RegLocation arg0, RegLocation arg1,
221 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700222 RegStorage r_tgt = CallHelperSetup(trampoline);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700223 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700224 RegStorage arg0_reg = TargetReg((arg0.fp) ? kFArg0 : kArg0, arg0);
225
226 RegStorage arg1_reg;
227 if (arg1.fp == arg0.fp) {
228 arg1_reg = TargetReg((arg1.fp) ? kFArg1 : kArg1, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700230 arg1_reg = TargetReg((arg1.fp) ? kFArg0 : kArg0, arg1);
231 }
232
233 if (arg0.wide == 0) {
234 LoadValueDirectFixed(arg0, arg0_reg);
235 } else {
236 LoadValueDirectWideFixed(arg0, arg0_reg);
237 }
238
239 if (arg1.wide == 0) {
240 LoadValueDirectFixed(arg1, arg1_reg);
241 } else {
242 LoadValueDirectWideFixed(arg1, arg1_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 }
244 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700245 DCHECK(!cu_->target64);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700246 if (arg0.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700247 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700248 if (arg1.wide == 0) {
249 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700250 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700251 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700252 LoadValueDirectFixed(arg1, TargetReg(kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700253 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700254 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700255 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700256 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700257 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700258 LoadValueDirectWideFixed(arg1, TargetReg(kArg1, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700259 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700260 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700262 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700263 if (arg1.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700264 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700265 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700266 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700267 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 }
269 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000270 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700271 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272}
273
Mingyao Yang80365d92014-04-18 12:10:58 -0700274void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700275 WideKind arg0_kind = arg0.GetWideKind();
276 WideKind arg1_kind = arg1.GetWideKind();
277 if (IsSameReg(arg1, TargetReg(kArg0, arg1_kind))) {
278 if (IsSameReg(arg0, TargetReg(kArg1, arg0_kind))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700279 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampeccc60262014-07-04 18:02:38 -0700280 OpRegCopy(TargetReg(kArg2, arg1_kind), arg1);
281 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
282 OpRegCopy(TargetReg(kArg1, arg1_kind), TargetReg(kArg2, arg1_kind));
Mingyao Yang80365d92014-04-18 12:10:58 -0700283 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700284 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
285 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700286 }
287 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700288 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
289 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700290 }
291}
292
Andreas Gampe98430592014-07-27 19:44:50 -0700293void Mir2Lir::CallRuntimeHelperRegReg(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800294 RegStorage arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700295 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700296 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000297 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700298 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299}
300
Andreas Gampe98430592014-07-27 19:44:50 -0700301void Mir2Lir::CallRuntimeHelperRegRegImm(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800302 RegStorage arg1, int arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700303 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700304 CopyToArgumentRegs(arg0, arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700305 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000306 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700307 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308}
309
Andreas Gampe98430592014-07-27 19:44:50 -0700310void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(QuickEntrypointEnum trampoline, int arg0,
311 RegLocation arg2, bool safepoint_pc) {
312 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700313 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Andreas Gampeccc60262014-07-04 18:02:38 -0700314 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
315 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000316 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700317 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318}
319
Andreas Gampe98430592014-07-27 19:44:50 -0700320void Mir2Lir::CallRuntimeHelperImmMethodImm(QuickEntrypointEnum trampoline, int arg0, int arg2,
321 bool safepoint_pc) {
322 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700323 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
324 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
325 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000326 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700327 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328}
329
Andreas Gampe98430592014-07-27 19:44:50 -0700330void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(QuickEntrypointEnum trampoline, int arg0,
331 RegLocation arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 RegLocation arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700333 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700334 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
335 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700336 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700338 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700340 LoadValueDirectWideFixed(arg2, TargetReg(kArg2, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700342 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000343 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700344 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345}
346
Andreas Gampeccc60262014-07-04 18:02:38 -0700347void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(
Andreas Gampe98430592014-07-27 19:44:50 -0700348 QuickEntrypointEnum trampoline,
Andreas Gampeccc60262014-07-04 18:02:38 -0700349 RegLocation arg0,
350 RegLocation arg1,
351 RegLocation arg2,
352 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700353 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700354 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
355 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
356 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000357 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700358 CallHelper(r_tgt, trampoline, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700359}
360
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361/*
362 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100363 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 * assignment of promoted arguments.
365 *
366 * ArgLocs is an array of location records describing the incoming arguments
367 * with one location record per word of argument.
368 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700369void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800371 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 * It will attempt to keep kArg0 live (or copy it to home location
373 * if promoted).
374 */
375 RegLocation rl_src = rl_method;
376 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -0700377 rl_src.reg = TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700379 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700380 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 // If Method* has been promoted, explicitly flush
382 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700383 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 }
385
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800386 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800388 }
389
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
391 /*
392 * Copy incoming arguments to their proper home locations.
393 * NOTE: an older version of dx had an issue in which
394 * it would reuse static method argument registers.
395 * This could result in the same Dalvik virtual register
396 * being promoted to both core and fp regs. To account for this,
397 * we only copy to the corresponding promoted physical register
398 * if it matches the type of the SSA name for the incoming
399 * argument. It is also possible that long and double arguments
400 * end up half-promoted. In those cases, we must flush the promoted
401 * half to memory as well.
402 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100403 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 for (int i = 0; i < cu_->num_ins; i++) {
405 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800406 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800407
buzbee2700f7e2014-03-07 09:46:20 -0800408 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 // If arriving in register
410 bool need_flush = true;
411 RegLocation* t_loc = &ArgLocs[i];
412 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800413 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 need_flush = false;
415 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbeeb5860fb2014-06-21 15:31:01 -0700416 OpRegCopy(RegStorage::Solo32(v_map->fp_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 need_flush = false;
418 } else {
419 need_flush = true;
420 }
421
buzbeed0a03b82013-09-14 08:21:05 -0700422 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 if (t_loc->wide) {
424 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700425 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 need_flush |= (p_map->core_location != v_map->core_location) ||
427 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700428 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
429 /*
430 * In Arm, a double is represented as a pair of consecutive single float
431 * registers starting at an even number. It's possible that both Dalvik vRegs
432 * representing the incoming double were independently promoted as singles - but
433 * not in a form usable as a double. If so, we need to flush - even though the
434 * incoming arg appears fully in register. At this point in the code, both
435 * halves of the double are promoted. Make sure they are in a usable form.
436 */
437 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
buzbeeb5860fb2014-06-21 15:31:01 -0700438 int low_reg = promotion_map_[lowreg_index].fp_reg;
439 int high_reg = promotion_map_[lowreg_index + 1].fp_reg;
buzbeed0a03b82013-09-14 08:21:05 -0700440 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
441 need_flush = true;
442 }
443 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 }
445 if (need_flush) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700446 Store32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 }
448 } else {
449 // If arriving in frame & promoted
450 if (v_map->core_location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700451 Load32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i),
452 RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 }
454 if (v_map->fp_location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700455 Load32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i),
456 RegStorage::Solo32(v_map->fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 }
458 }
459 }
460}
461
Andreas Gampeccc60262014-07-04 18:02:38 -0700462static void CommonCallCodeLoadThisIntoArg1(const CallInfo* info, Mir2Lir* cg) {
463 RegLocation rl_arg = info->args[0];
464 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1, kRef));
465}
466
467static void CommonCallCodeLoadClassIntoArg0(const CallInfo* info, Mir2Lir* cg) {
468 cg->GenNullCheck(cg->TargetReg(kArg1, kRef), info->opt_flags);
469 // get this->klass_ [use kArg1, set kArg0]
470 cg->LoadRefDisp(cg->TargetReg(kArg1, kRef), mirror::Object::ClassOffset().Int32Value(),
471 cg->TargetReg(kArg0, kRef),
472 kNotVolatile);
473 cg->MarkPossibleNullPointerException(info->opt_flags);
474}
475
476static bool CommonCallCodeLoadCodePointerIntoInvokeTgt(const CallInfo* info,
477 const RegStorage* alt_from,
478 const CompilationUnit* cu, Mir2Lir* cg) {
479 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
480 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
481 cg->LoadWordDisp(alt_from == nullptr ? cg->TargetReg(kArg0, kRef) : *alt_from,
482 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
483 cg->TargetPtrReg(kInvokeTgt));
484 return true;
485 }
486 return false;
487}
488
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489/*
490 * Bit of a hack here - in the absence of a real scheduling pass,
491 * emit the next instruction in static & direct invoke sequences.
492 */
493static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
494 int state, const MethodReference& target_method,
495 uint32_t unused,
496 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700497 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 if (direct_code != 0 && direct_method != 0) {
500 switch (state) {
501 case 0: // Get the current Method* [sets kArg0]
Ian Rogersff093b32014-04-30 19:04:27 -0700502 if (direct_code != static_cast<uintptr_t>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700503 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700504 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
Ian Rogers83883d72013-10-21 21:07:24 -0700505 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700506 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700507 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 }
Ian Rogersff093b32014-04-30 19:04:27 -0700509 if (direct_method != static_cast<uintptr_t>(-1)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700510 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700512 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 }
514 break;
515 default:
516 return -1;
517 }
518 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700519 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 switch (state) {
521 case 0: // Get the current Method* [sets kArg0]
522 // TUNING: we can save a reg copy if Method* has been promoted.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700523 cg->LoadCurrMethodDirect(arg0_ref);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 break;
525 case 1: // Get method->dex_cache_resolved_methods_
Andreas Gampe4b537a82014-06-30 22:24:53 -0700526 cg->LoadRefDisp(arg0_ref,
buzbee695d13a2014-04-19 13:32:20 -0700527 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700528 arg0_ref,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000529 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 // Set up direct code if known.
531 if (direct_code != 0) {
Ian Rogersff093b32014-04-30 19:04:27 -0700532 if (direct_code != static_cast<uintptr_t>(-1)) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700533 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700534 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700535 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700536 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 }
538 }
539 break;
540 case 2: // Grab target method*
541 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700542 cg->LoadRefDisp(arg0_ref,
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700543 ObjArray::OffsetOfElement(target_method.dex_method_index).Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700544 arg0_ref,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000545 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 break;
547 case 3: // Grab the code from the method*
Andreas Gampeccc60262014-07-04 18:02:38 -0700548 if (direct_code == 0) {
549 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, &arg0_ref, cu, cg)) {
550 break; // kInvokeTgt := arg0_ref->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700552 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 break;
554 }
555 // Intentional fallthrough for x86
556 default:
557 return -1;
558 }
559 }
560 return state + 1;
561}
562
563/*
564 * Bit of a hack here - in the absence of a real scheduling pass,
565 * emit the next instruction in a virtual invoke sequence.
566 * We can use kLr as a temp prior to target address loading
567 * Note also that we'll load the first argument ("this") into
568 * kArg1 here rather than the standard LoadArgRegs.
569 */
570static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
571 int state, const MethodReference& target_method,
572 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700573 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
575 /*
576 * This is the fast path in which the target virtual method is
577 * fully resolved at compile time.
578 */
579 switch (state) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700580 case 0:
581 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700583 case 1:
584 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
585 // Includes a null-check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700587 case 2: {
588 // Get this->klass_.embedded_vtable[method_idx] [usr kArg0, set kArg0]
589 int32_t offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
590 method_idx * sizeof(mirror::Class::VTableEntry);
591 // Load target method from embedded vtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700592 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700594 }
595 case 3:
Andreas Gampeccc60262014-07-04 18:02:38 -0700596 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, nullptr, cu, cg)) {
597 break; // kInvokeTgt := kArg0->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 }
599 // Intentional fallthrough for X86
600 default:
601 return -1;
602 }
603 return state + 1;
604}
605
606/*
Jeff Hao88474b42013-10-23 16:24:40 -0700607 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
608 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
609 * more than one interface method map to the same index. Note also that we'll load the first
610 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 */
612static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
613 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700614 uint32_t method_idx, uintptr_t unused,
615 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617
Jeff Hao88474b42013-10-23 16:24:40 -0700618 switch (state) {
619 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700620 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Andreas Gampeccc60262014-07-04 18:02:38 -0700621 cg->LoadConstant(cg->TargetReg(kHiddenArg, kNotWide), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400622 if (cu->instruction_set == kX86) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700623 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, kNotWide), cg->TargetReg(kHiddenArg, kNotWide));
Jeff Hao88474b42013-10-23 16:24:40 -0700624 }
625 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700626 case 1:
627 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Jeff Hao88474b42013-10-23 16:24:40 -0700628 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700629 case 2:
630 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
631 // Includes a null-check.
Jeff Hao88474b42013-10-23 16:24:40 -0700632 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700633 case 3: { // Get target method [use kInvokeTgt, set kArg0]
634 int32_t offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
635 (method_idx % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
636 // Load target method from embedded imtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700637 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700638 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700639 }
640 case 4:
Andreas Gampeccc60262014-07-04 18:02:38 -0700641 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, nullptr, cu, cg)) {
642 break; // kInvokeTgt := kArg0->entrypoint
Jeff Hao88474b42013-10-23 16:24:40 -0700643 }
644 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 default:
646 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 }
648 return state + 1;
649}
650
Andreas Gampeccc60262014-07-04 18:02:38 -0700651static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info,
Andreas Gampe98430592014-07-27 19:44:50 -0700652 QuickEntrypointEnum trampoline, int state,
Andreas Gampeccc60262014-07-04 18:02:38 -0700653 const MethodReference& target_method, uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Andreas Gampe98430592014-07-27 19:44:50 -0700655
656
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 /*
658 * This handles the case in which the base method is not fully
659 * resolved at compile time, we bail to a runtime helper.
660 */
661 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700662 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 // Load trampoline target
Andreas Gampe98430592014-07-27 19:44:50 -0700664 int32_t disp;
665 if (cu->target64) {
666 disp = GetThreadOffset<8>(trampoline).Int32Value();
667 } else {
668 disp = GetThreadOffset<4>(trampoline).Int32Value();
669 }
670 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), disp, cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 }
672 // Load kArg0 with method index
673 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampeccc60262014-07-04 18:02:38 -0700674 cg->LoadConstant(cg->TargetReg(kArg0, kNotWide), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 return 1;
676 }
677 return -1;
678}
679
680static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
681 int state,
682 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000683 uint32_t unused, uintptr_t unused2,
684 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700685 return NextInvokeInsnSP(cu, info, kQuickInvokeStaticTrampolineWithAccessCheck, state,
686 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687}
688
689static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
690 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000691 uint32_t unused, uintptr_t unused2,
692 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700693 return NextInvokeInsnSP(cu, info, kQuickInvokeDirectTrampolineWithAccessCheck, state,
694 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695}
696
697static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
698 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000699 uint32_t unused, uintptr_t unused2,
700 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700701 return NextInvokeInsnSP(cu, info, kQuickInvokeSuperTrampolineWithAccessCheck, state,
702 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703}
704
705static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
706 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000707 uint32_t unused, uintptr_t unused2,
708 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700709 return NextInvokeInsnSP(cu, info, kQuickInvokeVirtualTrampolineWithAccessCheck, state,
710 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711}
712
713static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
714 CallInfo* info, int state,
715 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000716 uint32_t unused, uintptr_t unused2,
717 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700718 return NextInvokeInsnSP(cu, info, kQuickInvokeInterfaceTrampolineWithAccessCheck, state,
719 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720}
721
722int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
723 NextCallInsn next_call_insn,
724 const MethodReference& target_method,
725 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700726 uintptr_t direct_method, InvokeType type, bool skip_this) {
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700727 int last_arg_reg = 3 - 1;
Andreas Gampeccc60262014-07-04 18:02:38 -0700728 int arg_regs[3] = {TargetReg(kArg1, kNotWide).GetReg(), TargetReg(kArg2, kNotWide).GetReg(),
729 TargetReg(kArg3, kNotWide).GetReg()};
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700730
731 int next_reg = 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 int next_arg = 0;
733 if (skip_this) {
734 next_reg++;
735 next_arg++;
736 }
737 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
738 RegLocation rl_arg = info->args[next_arg++];
739 rl_arg = UpdateRawLoc(rl_arg);
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700740 if (rl_arg.wide && (next_reg <= last_arg_reg - 1)) {
741 RegStorage r_tmp(RegStorage::k64BitPair, arg_regs[next_reg], arg_regs[next_reg + 1]);
buzbee2700f7e2014-03-07 09:46:20 -0800742 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 next_reg++;
744 next_arg++;
745 } else {
746 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800747 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 rl_arg.is_const = false;
749 }
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700750 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(arg_regs[next_reg]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 }
752 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
753 direct_code, direct_method, type);
754 }
755 return call_state;
756}
757
758/*
759 * Load up to 5 arguments, the first three of which will be in
760 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
761 * and as part of the load sequence, it must be replaced with
762 * the target method pointer. Note, this may also be called
763 * for "range" variants if the number of arguments is 5 or fewer.
764 */
765int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
766 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
767 const MethodReference& target_method,
768 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700769 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 RegLocation rl_arg;
771
772 /* If no arguments, just return */
773 if (info->num_arg_words == 0)
774 return call_state;
775
776 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
777 direct_code, direct_method, type);
778
779 DCHECK_LE(info->num_arg_words, 5);
780 if (info->num_arg_words > 3) {
781 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700782 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 RegLocation rl_use0 = info->args[0];
784 RegLocation rl_use1 = info->args[1];
785 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800786 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
787 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 // Wide spans, we need the 2nd half of uses[2].
789 rl_arg = UpdateLocWide(rl_use2);
790 if (rl_arg.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -0700791 if (rl_arg.reg.IsPair()) {
792 reg = rl_arg.reg.GetHigh();
793 } else {
794 RegisterInfo* info = GetRegInfo(rl_arg.reg);
795 info = info->FindMatchingView(RegisterInfo::kHighSingleStorageMask);
796 if (info == nullptr) {
797 // NOTE: For hard float convention we won't split arguments across reg/mem.
798 UNIMPLEMENTED(FATAL) << "Needs hard float api.";
799 }
800 reg = info->GetReg();
801 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 } else {
803 // kArg2 & rArg3 can safely be used here
Andreas Gampeccc60262014-07-04 18:02:38 -0700804 reg = TargetReg(kArg3, kNotWide);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100805 {
806 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700807 Load32Disp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100808 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 call_state = next_call_insn(cu_, info, call_state, target_method,
810 vtable_idx, direct_code, direct_method, type);
811 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100812 {
813 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700814 Store32Disp(TargetPtrReg(kSp), (next_use + 1) * 4, reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100815 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
817 direct_code, direct_method, type);
818 next_use++;
819 }
820 // Loop through the rest
821 while (next_use < info->num_arg_words) {
buzbee091cc402014-03-31 10:14:40 -0700822 RegStorage arg_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 rl_arg = info->args[next_use];
824 rl_arg = UpdateRawLoc(rl_arg);
825 if (rl_arg.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700826 arg_reg = rl_arg.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700828 arg_reg = TargetReg(kArg2, rl_arg.wide ? kWide : kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 if (rl_arg.wide) {
buzbee091cc402014-03-31 10:14:40 -0700830 LoadValueDirectWideFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 } else {
buzbee091cc402014-03-31 10:14:40 -0700832 LoadValueDirectFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 }
834 call_state = next_call_insn(cu_, info, call_state, target_method,
835 vtable_idx, direct_code, direct_method, type);
836 }
837 int outs_offset = (next_use + 1) * 4;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100838 {
839 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
840 if (rl_arg.wide) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700841 StoreBaseDisp(TargetPtrReg(kSp), outs_offset, arg_reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100842 next_use += 2;
843 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700844 Store32Disp(TargetPtrReg(kSp), outs_offset, arg_reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100845 next_use++;
846 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 }
848 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
849 direct_code, direct_method, type);
850 }
851 }
852
853 call_state = LoadArgRegs(info, call_state, next_call_insn,
854 target_method, vtable_idx, direct_code, direct_method,
855 type, skip_this);
856
857 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +0000858 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700859 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700860 } else {
861 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +0000862 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) &&
863 (info->opt_flags & MIR_IGNORE_NULL_CHECK)) {
864 return call_state;
865 }
Dave Allisonf9439142014-03-27 15:10:22 -0700866 // In lieu of generating a check for kArg1 being null, we need to
867 // perform a load when doing implicit checks.
Dave Allison69dfe512014-07-11 17:11:58 +0000868 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700869 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 }
871 return call_state;
872}
873
Dave Allison69dfe512014-07-11 17:11:58 +0000874// Default implementation of implicit null pointer check.
875// Overridden by arch specific as necessary.
876void Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
877 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
878 return;
879 }
880 RegStorage tmp = AllocTemp();
881 Load32Disp(reg, 0, tmp);
882 MarkPossibleNullPointerException(opt_flags);
883 FreeTemp(tmp);
884}
885
886
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887/*
888 * May have 0+ arguments (also used for jumbo). Note that
889 * source virtual registers may be in physical registers, so may
890 * need to be flushed to home location before copying. This
891 * applies to arg3 and above (see below).
892 *
893 * Two general strategies:
894 * If < 20 arguments
895 * Pass args 3-18 using vldm/vstm block copy
896 * Pass arg0, arg1 & arg2 in kArg1-kArg3
897 * If 20+ arguments
898 * Pass args arg19+ using memcpy block copy
899 * Pass arg0, arg1 & arg2 in kArg1-kArg3
900 *
901 */
902int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
903 LIR** pcrLabel, NextCallInsn next_call_insn,
904 const MethodReference& target_method,
905 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700906 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 // If we can treat it as non-range (Jumbo ops will use range form)
908 if (info->num_arg_words <= 5)
909 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
910 next_call_insn, target_method, vtable_idx,
911 direct_code, direct_method, type, skip_this);
912 /*
913 * First load the non-register arguments. Both forms expect all
914 * of the source arguments to be in their home frame location, so
915 * scan the s_reg names and flush any that have been promoted to
916 * frame backing storage.
917 */
918 // Scan the rest of the args - if in phys_reg flush to memory
919 for (int next_arg = 0; next_arg < info->num_arg_words;) {
920 RegLocation loc = info->args[next_arg];
921 if (loc.wide) {
922 loc = UpdateLocWide(loc);
923 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100924 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700925 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 }
927 next_arg += 2;
928 } else {
929 loc = UpdateLoc(loc);
930 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100931 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700932 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 }
934 next_arg++;
935 }
936 }
937
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800938 // Logic below assumes that Method pointer is at offset zero from SP.
939 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
940
941 // The first 3 arguments are passed via registers.
942 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
943 // get size of uintptr_t or size of object reference according to model being used.
944 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700945 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800946 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
947 DCHECK_GT(regs_left_to_pass_via_stack, 0);
948
949 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
950 // Use vldm/vstm pair using kArg3 as a temp
951 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
952 direct_code, direct_method, type);
Andreas Gampeccc60262014-07-04 18:02:38 -0700953 OpRegRegImm(kOpAdd, TargetReg(kArg3, kRef), TargetPtrReg(kSp), start_offset);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100954 LIR* ld = nullptr;
955 {
956 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -0700957 ld = OpVldm(TargetReg(kArg3, kRef), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100958 }
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800959 // TUNING: loosen barrier
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100960 ld->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800961 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
962 direct_code, direct_method, type);
Andreas Gampeccc60262014-07-04 18:02:38 -0700963 OpRegRegImm(kOpAdd, TargetReg(kArg3, kRef), TargetPtrReg(kSp), 4 /* Method* */ + (3 * 4));
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800964 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
965 direct_code, direct_method, type);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100966 LIR* st = nullptr;
967 {
968 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -0700969 st = OpVstm(TargetReg(kArg3, kRef), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100970 }
971 st->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800972 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
973 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700974 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800975 int current_src_offset = start_offset;
976 int current_dest_offset = outs_offset;
977
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100978 // Only davik regs are accessed in this loop; no next_call_insn() calls.
979 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800980 while (regs_left_to_pass_via_stack > 0) {
981 // This is based on the knowledge that the stack itself is 16-byte aligned.
982 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
983 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
984 size_t bytes_to_move;
985
986 /*
987 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
988 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
989 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
990 * We do this because we could potentially do a smaller move to align.
991 */
992 if (regs_left_to_pass_via_stack == 4 ||
993 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
994 // Moving 128-bits via xmm register.
995 bytes_to_move = sizeof(uint32_t) * 4;
996
997 // Allocate a free xmm temp. Since we are working through the calling sequence,
Mark Mendelle87f9b52014-04-30 14:13:18 -0400998 // we expect to have an xmm temporary available. AllocTempDouble will abort if
999 // there are no free registers.
buzbee2700f7e2014-03-07 09:46:20 -08001000 RegStorage temp = AllocTempDouble();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001001
1002 LIR* ld1 = nullptr;
1003 LIR* ld2 = nullptr;
1004 LIR* st1 = nullptr;
1005 LIR* st2 = nullptr;
1006
1007 /*
1008 * The logic is similar for both loads and stores. If we have 16-byte alignment,
1009 * do an aligned move. If we have 8-byte alignment, then do the move in two
1010 * parts. This approach prevents possible cache line splits. Finally, fall back
1011 * to doing an unaligned move. In most cases we likely won't split the cache
1012 * line but we cannot prove it and thus take a conservative approach.
1013 */
1014 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
1015 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
1016
1017 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001018 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001019 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001020 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovLo128FP);
1021 ld2 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001022 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001023 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001024 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001025 }
1026
1027 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001028 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001029 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001030 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovLo128FP);
1031 st2 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001032 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001033 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001034 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001035 }
1036
1037 // TODO If we could keep track of aliasing information for memory accesses that are wider
1038 // than 64-bit, we wouldn't need to set up a barrier.
1039 if (ld1 != nullptr) {
1040 if (ld2 != nullptr) {
1041 // For 64-bit load we can actually set up the aliasing information.
1042 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001043 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true,
1044 true);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001045 } else {
1046 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001047 ld1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001048 }
1049 }
1050 if (st1 != nullptr) {
1051 if (st2 != nullptr) {
1052 // For 64-bit store we can actually set up the aliasing information.
1053 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001054 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false,
1055 true);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001056 } else {
1057 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001058 st1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001059 }
1060 }
1061
1062 // Free the temporary used for the data movement.
buzbee091cc402014-03-31 10:14:40 -07001063 FreeTemp(temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001064 } else {
1065 // Moving 32-bits via general purpose register.
1066 bytes_to_move = sizeof(uint32_t);
1067
1068 // Instead of allocating a new temp, simply reuse one of the registers being used
1069 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07001070 RegStorage temp = TargetReg(kArg3, kNotWide);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001071
1072 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07001073 Load32Disp(TargetPtrReg(kSp), current_src_offset, temp);
1074 Store32Disp(TargetPtrReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001075 }
1076
1077 current_src_offset += bytes_to_move;
1078 current_dest_offset += bytes_to_move;
1079 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1080 }
1081 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 // Generate memcpy
Andreas Gampeccc60262014-07-04 18:02:38 -07001083 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
1084 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -07001085 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
1086 (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 }
1088
1089 call_state = LoadArgRegs(info, call_state, next_call_insn,
1090 target_method, vtable_idx, direct_code, direct_method,
1091 type, skip_this);
1092
1093 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1094 direct_code, direct_method, type);
1095 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00001096 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001097 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001098 } else {
1099 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +00001100 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) &&
1101 (info->opt_flags & MIR_IGNORE_NULL_CHECK)) {
1102 return call_state;
1103 }
Dave Allisonf9439142014-03-27 15:10:22 -07001104 // In lieu of generating a check for kArg1 being null, we need to
1105 // perform a load when doing implicit checks.
Dave Allison69dfe512014-07-11 17:11:58 +00001106 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001107 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 }
1109 return call_state;
1110}
1111
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001112RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 RegLocation res;
1114 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001115 res = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 } else {
1117 res = info->result;
1118 }
1119 return res;
1120}
1121
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001122RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 RegLocation res;
1124 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001125 res = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 } else {
1127 res = info->result;
1128 }
1129 return res;
1130}
1131
Fred Shih4ee7a662014-07-11 09:59:27 -07001132bool Mir2Lir::GenInlinedGet(CallInfo* info) {
1133 if (cu_->instruction_set == kMips) {
1134 // TODO - add Mips implementation
1135 return false;
1136 }
1137
1138 // the refrence class is stored in the image dex file which might not be the same as the cu's
1139 // dex file. Query the reference class for the image dex file then reset to starting dex file
1140 // in after loading class type.
1141 uint16_t type_idx = 0;
1142 const DexFile* ref_dex_file = nullptr;
1143 {
1144 ScopedObjectAccess soa(Thread::Current());
1145 type_idx = mirror::Reference::GetJavaLangRefReference()->GetDexTypeIndex();
1146 ref_dex_file = mirror::Reference::GetJavaLangRefReference()->GetDexCache()->GetDexFile();
1147 }
1148 CHECK(LIKELY(ref_dex_file != nullptr));
1149
1150 // address is either static within the image file, or needs to be patched up after compilation.
1151 bool unused_type_initialized;
1152 bool use_direct_type_ptr;
1153 uintptr_t direct_type_ptr;
1154 bool is_finalizable;
1155 const DexFile* old_dex = cu_->dex_file;
1156 cu_->dex_file = ref_dex_file;
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001157 RegStorage reg_class = TargetReg(kArg1, kRef);
1158 Clobber(reg_class);
1159 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -07001160 if (!cu_->compiler_driver->CanEmbedTypeInCode(*ref_dex_file, type_idx, &unused_type_initialized,
1161 &use_direct_type_ptr, &direct_type_ptr,
1162 &is_finalizable) || is_finalizable) {
1163 cu_->dex_file = old_dex;
1164 // address is not known and post-compile patch is not possible, cannot insert intrinsic.
1165 return false;
1166 }
1167 if (use_direct_type_ptr) {
1168 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -07001169 } else if (cu_->dex_file == old_dex) {
1170 // TODO: Bug 16656190 If cu_->dex_file != old_dex the patching could retrieve the wrong class
1171 // since the load class is indexed only by the type_idx. We should include which dex file a
1172 // class is from in the LoadClassType LIR.
Fred Shih4ee7a662014-07-11 09:59:27 -07001173 LoadClassType(type_idx, kArg1);
Alex Lighteb76e112014-07-29 15:22:40 -07001174 } else {
1175 cu_->dex_file = old_dex;
1176 return false;
Fred Shih4ee7a662014-07-11 09:59:27 -07001177 }
1178 cu_->dex_file = old_dex;
1179
1180 // get the offset for flags in reference class.
1181 uint32_t slow_path_flag_offset = 0;
1182 uint32_t disable_flag_offset = 0;
1183 {
1184 ScopedObjectAccess soa(Thread::Current());
1185 mirror::Class* reference_class = mirror::Reference::GetJavaLangRefReference();
1186 slow_path_flag_offset = reference_class->GetSlowPathFlagOffset().Uint32Value();
1187 disable_flag_offset = reference_class->GetDisableIntrinsicFlagOffset().Uint32Value();
1188 }
1189 CHECK(slow_path_flag_offset && disable_flag_offset &&
1190 (slow_path_flag_offset != disable_flag_offset));
1191
1192 // intrinsic logic start.
1193 RegLocation rl_obj = info->args[0];
1194 rl_obj = LoadValue(rl_obj);
1195
1196 RegStorage reg_slow_path = AllocTemp();
1197 RegStorage reg_disabled = AllocTemp();
1198 Load32Disp(reg_class, slow_path_flag_offset, reg_slow_path);
1199 Load32Disp(reg_class, disable_flag_offset, reg_disabled);
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001200 FreeTemp(reg_class);
1201 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -07001202 FreeTemp(reg_disabled);
1203
1204 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001205 LIR* slow_path_branch;
1206 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
1207 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
1208 slow_path_branch = OpCondBranch(kCondNe, nullptr);
1209 } else {
1210 // Generate compare and branch.
1211 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
1212 }
Fred Shih4ee7a662014-07-11 09:59:27 -07001213 FreeTemp(reg_slow_path);
1214
1215 // slow path not enabled, simply load the referent of the reference object
1216 RegLocation rl_dest = InlineTarget(info);
1217 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
1218 GenNullCheck(rl_obj.reg, info->opt_flags);
1219 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
1220 kNotVolatile);
1221 MarkPossibleNullPointerException(info->opt_flags);
1222 StoreValue(rl_dest, rl_result);
1223
1224 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
1225 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
1226
1227 return true;
1228}
1229
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001230bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 if (cu_->instruction_set == kMips) {
1232 // TODO - add Mips implementation
1233 return false;
1234 }
1235 // Location of reference to data array
1236 int value_offset = mirror::String::ValueOffset().Int32Value();
1237 // Location of count
1238 int count_offset = mirror::String::CountOffset().Int32Value();
1239 // Starting offset within data array
1240 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1241 // Start of char data with array_
1242 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1243
1244 RegLocation rl_obj = info->args[0];
1245 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -07001246 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -07001247 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001248 RegStorage reg_max;
1249 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001251 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001252 RegStorage reg_off;
1253 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -07001254 reg_off = AllocTemp();
1255 reg_ptr = AllocTempRef();
1256 if (range_check) {
1257 reg_max = AllocTemp();
1258 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001259 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 }
Andreas Gampe98430592014-07-27 19:44:50 -07001261 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1262 MarkPossibleNullPointerException(info->opt_flags);
1263 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
1264 if (range_check) {
1265 // Set up a slow path to allow retry in case of bounds violation */
1266 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
1267 FreeTemp(reg_max);
1268 range_check_branch = OpCondBranch(kCondUge, nullptr);
1269 }
1270 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001271 if (rl_idx.is_const) {
1272 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1273 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001274 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001275 }
buzbee2700f7e2014-03-07 09:46:20 -08001276 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001277 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001278 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001279 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 RegLocation rl_dest = InlineTarget(info);
1281 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001282 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 FreeTemp(reg_off);
1284 FreeTemp(reg_ptr);
1285 StoreValue(rl_dest, rl_result);
1286 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001287 DCHECK(range_check_branch != nullptr);
1288 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001289 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 return true;
1292}
1293
1294// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001295bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 if (cu_->instruction_set == kMips) {
1297 // TODO - add Mips implementation
1298 return false;
1299 }
1300 // dst = src.length();
1301 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001302 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 RegLocation rl_dest = InlineTarget(info);
1304 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001305 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001306 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001307 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 if (is_empty) {
1309 // dst = (dst == 0);
1310 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001311 RegStorage t_reg = AllocTemp();
1312 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1313 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001314 } else if (cu_->instruction_set == kArm64) {
1315 OpRegImm(kOpSub, rl_result.reg, 1);
1316 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001318 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001319 OpRegImm(kOpSub, rl_result.reg, 1);
1320 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001321 }
1322 }
1323 StoreValue(rl_dest, rl_result);
1324 return true;
1325}
1326
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001327bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Zheng Xua3fe7422014-07-09 14:03:15 +08001328 if (cu_->instruction_set == kMips) {
1329 // TODO - add Mips implementation.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001330 return false;
1331 }
1332 RegLocation rl_src_i = info->args[0];
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001333 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -07001334 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001335 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001336 if (size == k64) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001337 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001338 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1339 StoreValueWide(rl_dest, rl_result);
1340 return true;
1341 }
buzbee2700f7e2014-03-07 09:46:20 -08001342 RegStorage r_i_low = rl_i.reg.GetLow();
1343 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001344 // 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 +00001345 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001346 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001347 }
buzbee2700f7e2014-03-07 09:46:20 -08001348 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1349 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1350 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001351 FreeTemp(r_i_low);
1352 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001353 StoreValueWide(rl_dest, rl_result);
1354 } else {
buzbee695d13a2014-04-19 13:32:20 -07001355 DCHECK(size == k32 || size == kSignedHalf);
1356 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001357 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001358 StoreValue(rl_dest, rl_result);
1359 }
1360 return true;
1361}
1362
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001363bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 if (cu_->instruction_set == kMips) {
1365 // TODO - add Mips implementation
1366 return false;
1367 }
1368 RegLocation rl_src = info->args[0];
1369 rl_src = LoadValue(rl_src, kCoreReg);
1370 RegLocation rl_dest = InlineTarget(info);
1371 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001372 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001374 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1375 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1376 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 StoreValue(rl_dest, rl_result);
1378 return true;
1379}
1380
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001381bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 if (cu_->instruction_set == kMips) {
1383 // TODO - add Mips implementation
1384 return false;
1385 }
Vladimir Markob9823312014-03-20 17:38:43 +00001386 RegLocation rl_src = info->args[0];
1387 rl_src = LoadValueWide(rl_src, kCoreReg);
1388 RegLocation rl_dest = InlineTargetWide(info);
1389 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1390
1391 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001392 if (cu_->instruction_set != kX86_64 &&
1393 (cu_->instruction_set == kX86 ||
1394 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001395 OpRegCopyWide(rl_result.reg, rl_src.reg);
1396 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1397 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1398 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001399 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1400 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001401 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001402 }
1403 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 }
Vladimir Markob9823312014-03-20 17:38:43 +00001405
1406 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001407 RegStorage sign_reg;
1408 if (cu_->instruction_set == kX86_64) {
1409 sign_reg = AllocTempWide();
1410 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1411 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1412 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1413 } else {
1414 sign_reg = AllocTemp();
1415 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1416 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1417 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1418 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1419 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1420 }
buzbee082833c2014-05-17 23:16:26 -07001421 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001422 StoreValueWide(rl_dest, rl_result);
1423 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424}
1425
Serban Constantinescu23abec92014-07-02 16:13:38 +01001426bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
1427 // Currently implemented only for ARM64
1428 return false;
1429}
1430
1431bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
1432 // Currently implemented only for ARM64
1433 return false;
1434}
1435
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001436bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
1437 return false;
1438}
1439
1440bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
1441 return false;
1442}
1443
1444bool Mir2Lir::GenInlinedRint(CallInfo* info) {
1445 return false;
1446}
1447
1448bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
1449 return false;
1450}
1451
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001452bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001453 if (cu_->instruction_set == kMips) {
1454 // TODO - add Mips implementation
1455 return false;
1456 }
1457 RegLocation rl_src = info->args[0];
1458 RegLocation rl_dest = InlineTarget(info);
1459 StoreValue(rl_dest, rl_src);
1460 return true;
1461}
1462
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001463bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 if (cu_->instruction_set == kMips) {
1465 // TODO - add Mips implementation
1466 return false;
1467 }
1468 RegLocation rl_src = info->args[0];
1469 RegLocation rl_dest = InlineTargetWide(info);
1470 StoreValueWide(rl_dest, rl_src);
1471 return true;
1472}
1473
DaniilSokolov70c4f062014-06-24 17:34:00 -07001474bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
1475 return false;
1476}
1477
1478
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001480 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 * otherwise bails to standard library code.
1482 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001483bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484 if (cu_->instruction_set == kMips) {
1485 // TODO - add Mips implementation
1486 return false;
1487 }
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001488 if (cu_->instruction_set == kX86_64) {
1489 // TODO - add kX86_64 implementation
1490 return false;
1491 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001492 RegLocation rl_obj = info->args[0];
1493 RegLocation rl_char = info->args[1];
1494 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1495 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1496 return false;
1497 }
1498
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001499 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001501 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1502 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1503 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 LoadValueDirectFixed(rl_obj, reg_ptr);
1506 LoadValueDirectFixed(rl_char, reg_char);
1507 if (zero_based) {
1508 LoadConstant(reg_start, 0);
1509 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001510 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 LoadValueDirectFixed(rl_start, reg_start);
1512 }
Andreas Gampe98430592014-07-27 19:44:50 -07001513 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001514 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001515 LIR* high_code_point_branch =
1516 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001518 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001519 if (!rl_char.is_const) {
1520 // Add the slow path for code points beyond 0xFFFF.
1521 DCHECK(high_code_point_branch != nullptr);
1522 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1523 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001524 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001525 } else {
1526 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1527 DCHECK(high_code_point_branch == nullptr);
1528 }
buzbeea0cd2d72014-06-01 09:33:49 -07001529 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 RegLocation rl_dest = InlineTarget(info);
1531 StoreValue(rl_dest, rl_return);
1532 return true;
1533}
1534
1535/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001536bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 if (cu_->instruction_set == kMips) {
1538 // TODO - add Mips implementation
1539 return false;
1540 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001541 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001542 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001543 RegStorage reg_this = TargetReg(kArg0, kRef);
1544 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545
1546 RegLocation rl_this = info->args[0];
1547 RegLocation rl_cmp = info->args[1];
1548 LoadValueDirectFixed(rl_this, reg_this);
1549 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001550 RegStorage r_tgt;
1551 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001552 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001553 } else {
1554 r_tgt = RegStorage::InvalidReg();
1555 }
Dave Allisonf9439142014-03-27 15:10:22 -07001556 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001557 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001558 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001559 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001560 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001561 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001562 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001563 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 RegLocation rl_dest = InlineTarget(info);
1565 StoreValue(rl_dest, rl_return);
1566 return true;
1567}
1568
1569bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1570 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001571
1572 // Early exit if the result is unused.
1573 if (rl_dest.orig_sreg < 0) {
1574 return true;
1575 }
1576
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001577 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001578
1579 switch (cu_->instruction_set) {
1580 case kArm:
1581 // Fall-through.
1582 case kThumb2:
1583 // Fall-through.
1584 case kMips:
Chao-ying Fua77ee512014-07-01 17:43:41 -07001585 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001586 break;
1587
1588 case kArm64:
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001589 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1590 kNotVolatile);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001591 break;
1592
Andreas Gampe2f244e92014-05-08 03:35:25 -07001593 default:
1594 LOG(FATAL) << "Unexpected isa " << cu_->instruction_set;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 }
1596 StoreValue(rl_dest, rl_result);
1597 return true;
1598}
1599
1600bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1601 bool is_long, bool is_volatile) {
1602 if (cu_->instruction_set == kMips) {
1603 // TODO - add Mips implementation
1604 return false;
1605 }
1606 // Unused - RegLocation rl_src_unsafe = info->args[0];
1607 RegLocation rl_src_obj = info->args[1]; // Object
1608 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001609 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001610 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001611
buzbeea0cd2d72014-06-01 09:33:49 -07001612 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001614 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001616 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1617 || cu_->instruction_set == kArm64) {
1618 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001619 } else {
1620 RegStorage rl_temp_offset = AllocTemp();
1621 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001622 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001623 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001624 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001626 if (rl_result.ref) {
1627 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1628 } else {
1629 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1630 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001631 }
1632
1633 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001634 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001635 }
1636
1637 if (is_long) {
1638 StoreValueWide(rl_dest, rl_result);
1639 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 StoreValue(rl_dest, rl_result);
1641 }
1642 return true;
1643}
1644
1645bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1646 bool is_object, bool is_volatile, bool is_ordered) {
1647 if (cu_->instruction_set == kMips) {
1648 // TODO - add Mips implementation
1649 return false;
1650 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 // Unused - RegLocation rl_src_unsafe = info->args[0];
1652 RegLocation rl_src_obj = info->args[1]; // Object
1653 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001654 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 RegLocation rl_src_value = info->args[4]; // value to store
1656 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001657 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 }
buzbeea0cd2d72014-06-01 09:33:49 -07001659 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1661 RegLocation rl_value;
1662 if (is_long) {
1663 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001664 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1665 || cu_->instruction_set == kArm64) {
1666 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001667 } else {
1668 RegStorage rl_temp_offset = AllocTemp();
1669 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001670 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001671 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001672 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001674 rl_value = LoadValue(rl_src_value);
Matteo Franchin255e0142014-07-04 13:50:41 +01001675 if (rl_value.ref) {
1676 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1677 } else {
1678 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1679 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001680 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001681
1682 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001683 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001684
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001686 // Prevent reordering with a subsequent volatile load.
1687 // May also be needed to address store atomicity issues.
1688 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001689 }
1690 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001691 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692 }
1693 return true;
1694}
1695
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001696void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001697 if ((info->opt_flags & MIR_INLINED) != 0) {
1698 // Already inlined but we may still need the null check.
1699 if (info->type != kStatic &&
1700 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1701 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
buzbeea0cd2d72014-06-01 09:33:49 -07001702 RegLocation rl_obj = LoadValue(info->args[0], kRefReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001703 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001704 }
1705 return;
1706 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001707 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001708 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1709 ->GenIntrinsic(this, info)) {
1710 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001712 GenInvokeNoInline(info);
1713}
1714
Andreas Gampe2f244e92014-05-08 03:35:25 -07001715static LIR* GenInvokeNoInlineCall(Mir2Lir* mir_to_lir, InvokeType type) {
Andreas Gampe98430592014-07-27 19:44:50 -07001716 QuickEntrypointEnum trampoline;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001717 switch (type) {
1718 case kInterface:
Andreas Gampe98430592014-07-27 19:44:50 -07001719 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001720 break;
1721 case kDirect:
Andreas Gampe98430592014-07-27 19:44:50 -07001722 trampoline = kQuickInvokeDirectTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001723 break;
1724 case kStatic:
Andreas Gampe98430592014-07-27 19:44:50 -07001725 trampoline = kQuickInvokeStaticTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001726 break;
1727 case kSuper:
Andreas Gampe98430592014-07-27 19:44:50 -07001728 trampoline = kQuickInvokeSuperTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001729 break;
1730 case kVirtual:
Andreas Gampe98430592014-07-27 19:44:50 -07001731 trampoline = kQuickInvokeVirtualTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001732 break;
1733 default:
1734 LOG(FATAL) << "Unexpected invoke type";
Andreas Gampe98430592014-07-27 19:44:50 -07001735 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001736 }
Andreas Gampe98430592014-07-27 19:44:50 -07001737 return mir_to_lir->InvokeTrampoline(kOpBlx, RegStorage::InvalidReg(), trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001738}
1739
Vladimir Marko3bc86152014-03-13 14:11:28 +00001740void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001741 int call_state = 0;
1742 LIR* null_ck;
1743 LIR** p_null_ck = NULL;
1744 NextCallInsn next_call_insn;
1745 FlushAllRegs(); /* Everything to home location */
1746 // Explicit register usage
1747 LockCallTemps();
1748
Vladimir Markof096aad2014-01-23 15:51:58 +00001749 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1750 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
Mark Mendelle87f9b52014-04-30 14:13:18 -04001751 BeginInvoke(info);
Vladimir Markof096aad2014-01-23 15:51:58 +00001752 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1753 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1754 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001758 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 } else if (info->type == kDirect) {
1760 if (fast_path) {
1761 p_null_ck = &null_ck;
1762 }
1763 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1764 skip_this = false;
1765 } else if (info->type == kStatic) {
1766 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1767 skip_this = false;
1768 } else if (info->type == kSuper) {
1769 DCHECK(!fast_path); // Fast path is a direct call.
1770 next_call_insn = NextSuperCallInsnSP;
1771 skip_this = false;
1772 } else {
1773 DCHECK_EQ(info->type, kVirtual);
1774 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1775 skip_this = fast_path;
1776 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001777 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001778 if (!info->is_range) {
1779 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001780 next_call_insn, target_method, method_info.VTableIndex(),
1781 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 original_type, skip_this);
1783 } else {
1784 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001785 next_call_insn, target_method, method_info.VTableIndex(),
1786 method_info.DirectCode(), method_info.DirectMethod(),
1787 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 }
1789 // Finish up any of the call sequence not interleaved in arg loading
1790 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001791 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1792 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 }
1794 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001795 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001796 call_inst = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001797 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001798 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001799 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001800 // We can have the linker fixup a call relative.
1801 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001802 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001803 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001804 call_inst = OpMem(kOpBlx, TargetReg(kArg0, kRef),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001805 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1806 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001807 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001808 call_inst = GenInvokeNoInlineCall(this, info->type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001809 }
1810 }
Mark Mendelle87f9b52014-04-30 14:13:18 -04001811 EndInvoke(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001812 MarkSafepointPC(call_inst);
1813
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001814 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 if (info->result.location != kLocInvalid) {
1816 // We have a following MOVE_RESULT - do it now.
1817 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001818 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001819 StoreValueWide(info->result, ret_loc);
1820 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001821 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001822 StoreValue(info->result, ret_loc);
1823 }
1824 }
1825}
1826
1827} // namespace art