blob: ea52b39f09452958b60cd0f667222f427b4fafb1 [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);
Andreas Gampeccc60262014-07-04 18:02:38 -0700144 DCHECK(!arg1.fp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 if (arg1.wide == 0) {
Andreas Gampef9872f02014-07-01 19:00:09 -0700146 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700148 RegStorage r_tmp = TargetReg(cu_->instruction_set == kMips ? kArg2 : kArg1, kWide);
buzbee2700f7e2014-03-07 09:46:20 -0800149 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700151 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000152 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700153 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154}
155
Andreas Gampe98430592014-07-27 19:44:50 -0700156void Mir2Lir::CallRuntimeHelperRegLocationImm(QuickEntrypointEnum trampoline, RegLocation arg0,
157 int arg1, bool safepoint_pc) {
158 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampef9872f02014-07-01 19:00:09 -0700159 DCHECK(!arg0.wide);
160 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700161 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000162 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700163 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164}
165
Andreas Gampe98430592014-07-27 19:44:50 -0700166void Mir2Lir::CallRuntimeHelperImmReg(QuickEntrypointEnum trampoline, int arg0, RegStorage arg1,
167 bool safepoint_pc) {
168 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700169 OpRegCopy(TargetReg(kArg1, arg1.GetWideKind()), arg1);
170 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000171 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700172 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173}
174
Andreas Gampe98430592014-07-27 19:44:50 -0700175void Mir2Lir::CallRuntimeHelperRegImm(QuickEntrypointEnum trampoline, RegStorage arg0, int arg1,
176 bool safepoint_pc) {
177 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700178 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
179 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000180 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700181 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182}
183
Andreas Gampe98430592014-07-27 19:44:50 -0700184void Mir2Lir::CallRuntimeHelperImmMethod(QuickEntrypointEnum trampoline, int arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700185 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700186 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700187 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
188 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000189 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700190 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191}
192
Andreas Gampe98430592014-07-27 19:44:50 -0700193void Mir2Lir::CallRuntimeHelperRegMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800194 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700195 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700196 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
197 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
198 if (r_tmp.NotExactlyEquals(arg0)) {
199 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800200 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700201 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800202 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700203 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800204}
205
Andreas Gampe98430592014-07-27 19:44:50 -0700206void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(QuickEntrypointEnum trampoline, RegStorage arg0,
207 RegLocation arg2, bool safepoint_pc) {
208 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700209 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
210 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
211 if (r_tmp.NotExactlyEquals(arg0)) {
212 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800213 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700214 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700215 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800216 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700217 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800218}
219
Andreas Gampe98430592014-07-27 19:44:50 -0700220void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(QuickEntrypointEnum trampoline,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700221 RegLocation arg0, RegLocation arg1,
222 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700223 RegStorage r_tgt = CallHelperSetup(trampoline);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700224 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700225 RegStorage arg0_reg = TargetReg((arg0.fp) ? kFArg0 : kArg0, arg0);
226
227 RegStorage arg1_reg;
228 if (arg1.fp == arg0.fp) {
229 arg1_reg = TargetReg((arg1.fp) ? kFArg1 : kArg1, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700231 arg1_reg = TargetReg((arg1.fp) ? kFArg0 : kArg0, arg1);
232 }
233
234 if (arg0.wide == 0) {
235 LoadValueDirectFixed(arg0, arg0_reg);
236 } else {
237 LoadValueDirectWideFixed(arg0, arg0_reg);
238 }
239
240 if (arg1.wide == 0) {
241 LoadValueDirectFixed(arg1, arg1_reg);
242 } else {
243 LoadValueDirectWideFixed(arg1, arg1_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 }
245 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700246 DCHECK(!cu_->target64);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700247 if (arg0.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700248 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700249 if (arg1.wide == 0) {
250 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700251 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700252 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700253 LoadValueDirectFixed(arg1, TargetReg(kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700254 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700255 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700256 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700257 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700258 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700259 LoadValueDirectWideFixed(arg1, TargetReg(kArg1, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700260 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700261 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700263 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700264 if (arg1.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700265 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700266 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700267 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700268 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 }
270 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000271 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700272 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273}
274
Mingyao Yang80365d92014-04-18 12:10:58 -0700275void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700276 WideKind arg0_kind = arg0.GetWideKind();
277 WideKind arg1_kind = arg1.GetWideKind();
278 if (IsSameReg(arg1, TargetReg(kArg0, arg1_kind))) {
279 if (IsSameReg(arg0, TargetReg(kArg1, arg0_kind))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700280 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampeccc60262014-07-04 18:02:38 -0700281 OpRegCopy(TargetReg(kArg2, arg1_kind), arg1);
282 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
283 OpRegCopy(TargetReg(kArg1, arg1_kind), TargetReg(kArg2, arg1_kind));
Mingyao Yang80365d92014-04-18 12:10:58 -0700284 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700285 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
286 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700287 }
288 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700289 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
290 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700291 }
292}
293
Andreas Gampe98430592014-07-27 19:44:50 -0700294void Mir2Lir::CallRuntimeHelperRegReg(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800295 RegStorage arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700296 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700297 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000298 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700299 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300}
301
Andreas Gampe98430592014-07-27 19:44:50 -0700302void Mir2Lir::CallRuntimeHelperRegRegImm(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800303 RegStorage arg1, int arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700304 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700305 CopyToArgumentRegs(arg0, arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700306 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000307 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700308 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309}
310
Andreas Gampe98430592014-07-27 19:44:50 -0700311void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(QuickEntrypointEnum trampoline, int arg0,
312 RegLocation arg2, bool safepoint_pc) {
313 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700314 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Andreas Gampeccc60262014-07-04 18:02:38 -0700315 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
316 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000317 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700318 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319}
320
Andreas Gampe98430592014-07-27 19:44:50 -0700321void Mir2Lir::CallRuntimeHelperImmMethodImm(QuickEntrypointEnum trampoline, int arg0, int arg2,
322 bool safepoint_pc) {
323 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700324 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
325 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
326 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000327 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700328 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329}
330
Andreas Gampe98430592014-07-27 19:44:50 -0700331void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(QuickEntrypointEnum trampoline, int arg0,
332 RegLocation arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 RegLocation arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700334 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700335 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
336 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700337 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700339 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700341 LoadValueDirectWideFixed(arg2, TargetReg(kArg2, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700343 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000344 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700345 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346}
347
Andreas Gampeccc60262014-07-04 18:02:38 -0700348void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(
Andreas Gampe98430592014-07-27 19:44:50 -0700349 QuickEntrypointEnum trampoline,
Andreas Gampeccc60262014-07-04 18:02:38 -0700350 RegLocation arg0,
351 RegLocation arg1,
352 RegLocation arg2,
353 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700354 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700355 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
356 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
357 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000358 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700359 CallHelper(r_tgt, trampoline, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700360}
361
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362/*
363 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100364 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 * assignment of promoted arguments.
366 *
367 * ArgLocs is an array of location records describing the incoming arguments
368 * with one location record per word of argument.
369 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700370void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800372 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 * It will attempt to keep kArg0 live (or copy it to home location
374 * if promoted).
375 */
376 RegLocation rl_src = rl_method;
377 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -0700378 rl_src.reg = TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700380 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700381 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 // If Method* has been promoted, explicitly flush
383 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700384 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 }
386
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800387 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800389 }
390
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
392 /*
393 * Copy incoming arguments to their proper home locations.
394 * NOTE: an older version of dx had an issue in which
395 * it would reuse static method argument registers.
396 * This could result in the same Dalvik virtual register
397 * being promoted to both core and fp regs. To account for this,
398 * we only copy to the corresponding promoted physical register
399 * if it matches the type of the SSA name for the incoming
400 * argument. It is also possible that long and double arguments
401 * end up half-promoted. In those cases, we must flush the promoted
402 * half to memory as well.
403 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100404 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 for (int i = 0; i < cu_->num_ins; i++) {
406 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800407 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800408
buzbee2700f7e2014-03-07 09:46:20 -0800409 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 // If arriving in register
411 bool need_flush = true;
412 RegLocation* t_loc = &ArgLocs[i];
413 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800414 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 need_flush = false;
416 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbeeb5860fb2014-06-21 15:31:01 -0700417 OpRegCopy(RegStorage::Solo32(v_map->fp_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 need_flush = false;
419 } else {
420 need_flush = true;
421 }
422
buzbeed0a03b82013-09-14 08:21:05 -0700423 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 if (t_loc->wide) {
425 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700426 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 need_flush |= (p_map->core_location != v_map->core_location) ||
428 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700429 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
430 /*
431 * In Arm, a double is represented as a pair of consecutive single float
432 * registers starting at an even number. It's possible that both Dalvik vRegs
433 * representing the incoming double were independently promoted as singles - but
434 * not in a form usable as a double. If so, we need to flush - even though the
435 * incoming arg appears fully in register. At this point in the code, both
436 * halves of the double are promoted. Make sure they are in a usable form.
437 */
438 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
buzbeeb5860fb2014-06-21 15:31:01 -0700439 int low_reg = promotion_map_[lowreg_index].fp_reg;
440 int high_reg = promotion_map_[lowreg_index + 1].fp_reg;
buzbeed0a03b82013-09-14 08:21:05 -0700441 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
442 need_flush = true;
443 }
444 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 }
446 if (need_flush) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700447 Store32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 }
449 } else {
450 // If arriving in frame & promoted
451 if (v_map->core_location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700452 Load32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i),
453 RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 }
455 if (v_map->fp_location == kLocPhysReg) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700456 Load32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i),
457 RegStorage::Solo32(v_map->fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 }
459 }
460 }
461}
462
Andreas Gampeccc60262014-07-04 18:02:38 -0700463static void CommonCallCodeLoadThisIntoArg1(const CallInfo* info, Mir2Lir* cg) {
464 RegLocation rl_arg = info->args[0];
465 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1, kRef));
466}
467
468static void CommonCallCodeLoadClassIntoArg0(const CallInfo* info, Mir2Lir* cg) {
469 cg->GenNullCheck(cg->TargetReg(kArg1, kRef), info->opt_flags);
470 // get this->klass_ [use kArg1, set kArg0]
471 cg->LoadRefDisp(cg->TargetReg(kArg1, kRef), mirror::Object::ClassOffset().Int32Value(),
472 cg->TargetReg(kArg0, kRef),
473 kNotVolatile);
474 cg->MarkPossibleNullPointerException(info->opt_flags);
475}
476
477static bool CommonCallCodeLoadCodePointerIntoInvokeTgt(const CallInfo* info,
478 const RegStorage* alt_from,
479 const CompilationUnit* cu, Mir2Lir* cg) {
480 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
481 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
482 cg->LoadWordDisp(alt_from == nullptr ? cg->TargetReg(kArg0, kRef) : *alt_from,
483 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
484 cg->TargetPtrReg(kInvokeTgt));
485 return true;
486 }
487 return false;
488}
489
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490/*
491 * Bit of a hack here - in the absence of a real scheduling pass,
492 * emit the next instruction in static & direct invoke sequences.
493 */
494static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
495 int state, const MethodReference& target_method,
496 uint32_t unused,
497 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700498 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 if (direct_code != 0 && direct_method != 0) {
501 switch (state) {
502 case 0: // Get the current Method* [sets kArg0]
Ian Rogersff093b32014-04-30 19:04:27 -0700503 if (direct_code != static_cast<uintptr_t>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700504 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700505 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
Ian Rogers83883d72013-10-21 21:07:24 -0700506 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700507 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700508 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 }
Ian Rogersff093b32014-04-30 19:04:27 -0700510 if (direct_method != static_cast<uintptr_t>(-1)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700511 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700513 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 }
515 break;
516 default:
517 return -1;
518 }
519 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700520 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 switch (state) {
522 case 0: // Get the current Method* [sets kArg0]
523 // TUNING: we can save a reg copy if Method* has been promoted.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700524 cg->LoadCurrMethodDirect(arg0_ref);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 break;
526 case 1: // Get method->dex_cache_resolved_methods_
Andreas Gampe4b537a82014-06-30 22:24:53 -0700527 cg->LoadRefDisp(arg0_ref,
buzbee695d13a2014-04-19 13:32:20 -0700528 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700529 arg0_ref,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000530 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 // Set up direct code if known.
532 if (direct_code != 0) {
Ian Rogersff093b32014-04-30 19:04:27 -0700533 if (direct_code != static_cast<uintptr_t>(-1)) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700534 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700535 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700536 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700537 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 }
539 }
540 break;
541 case 2: // Grab target method*
542 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700543 cg->LoadRefDisp(arg0_ref,
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700544 ObjArray::OffsetOfElement(target_method.dex_method_index).Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700545 arg0_ref,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000546 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 break;
548 case 3: // Grab the code from the method*
Andreas Gampeccc60262014-07-04 18:02:38 -0700549 if (direct_code == 0) {
550 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, &arg0_ref, cu, cg)) {
551 break; // kInvokeTgt := arg0_ref->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700553 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 break;
555 }
556 // Intentional fallthrough for x86
557 default:
558 return -1;
559 }
560 }
561 return state + 1;
562}
563
564/*
565 * Bit of a hack here - in the absence of a real scheduling pass,
566 * emit the next instruction in a virtual invoke sequence.
567 * We can use kLr as a temp prior to target address loading
568 * Note also that we'll load the first argument ("this") into
569 * kArg1 here rather than the standard LoadArgRegs.
570 */
571static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
572 int state, const MethodReference& target_method,
573 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700574 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
576 /*
577 * This is the fast path in which the target virtual method is
578 * fully resolved at compile time.
579 */
580 switch (state) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700581 case 0:
582 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700584 case 1:
585 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
586 // Includes a null-check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700588 case 2: {
589 // Get this->klass_.embedded_vtable[method_idx] [usr kArg0, set kArg0]
590 int32_t offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
591 method_idx * sizeof(mirror::Class::VTableEntry);
592 // Load target method from embedded vtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700593 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700595 }
596 case 3:
Andreas Gampeccc60262014-07-04 18:02:38 -0700597 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, nullptr, cu, cg)) {
598 break; // kInvokeTgt := kArg0->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 }
600 // Intentional fallthrough for X86
601 default:
602 return -1;
603 }
604 return state + 1;
605}
606
607/*
Jeff Hao88474b42013-10-23 16:24:40 -0700608 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
609 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
610 * more than one interface method map to the same index. Note also that we'll load the first
611 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 */
613static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
614 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700615 uint32_t method_idx, uintptr_t unused,
616 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618
Jeff Hao88474b42013-10-23 16:24:40 -0700619 switch (state) {
620 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700621 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Andreas Gampeccc60262014-07-04 18:02:38 -0700622 cg->LoadConstant(cg->TargetReg(kHiddenArg, kNotWide), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400623 if (cu->instruction_set == kX86) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700624 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, kNotWide), cg->TargetReg(kHiddenArg, kNotWide));
Jeff Hao88474b42013-10-23 16:24:40 -0700625 }
626 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700627 case 1:
628 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Jeff Hao88474b42013-10-23 16:24:40 -0700629 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700630 case 2:
631 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
632 // Includes a null-check.
Jeff Hao88474b42013-10-23 16:24:40 -0700633 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700634 case 3: { // Get target method [use kInvokeTgt, set kArg0]
635 int32_t offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
636 (method_idx % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
637 // Load target method from embedded imtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700638 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700639 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700640 }
641 case 4:
Andreas Gampeccc60262014-07-04 18:02:38 -0700642 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(info, nullptr, cu, cg)) {
643 break; // kInvokeTgt := kArg0->entrypoint
Jeff Hao88474b42013-10-23 16:24:40 -0700644 }
645 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 default:
647 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 }
649 return state + 1;
650}
651
Andreas Gampeccc60262014-07-04 18:02:38 -0700652static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info,
Andreas Gampe98430592014-07-27 19:44:50 -0700653 QuickEntrypointEnum trampoline, int state,
Andreas Gampeccc60262014-07-04 18:02:38 -0700654 const MethodReference& target_method, uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Andreas Gampe98430592014-07-27 19:44:50 -0700656
657
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 /*
659 * This handles the case in which the base method is not fully
660 * resolved at compile time, we bail to a runtime helper.
661 */
662 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700663 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 // Load trampoline target
Andreas Gampe98430592014-07-27 19:44:50 -0700665 int32_t disp;
666 if (cu->target64) {
667 disp = GetThreadOffset<8>(trampoline).Int32Value();
668 } else {
669 disp = GetThreadOffset<4>(trampoline).Int32Value();
670 }
671 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), disp, cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 }
673 // Load kArg0 with method index
674 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampeccc60262014-07-04 18:02:38 -0700675 cg->LoadConstant(cg->TargetReg(kArg0, kNotWide), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 return 1;
677 }
678 return -1;
679}
680
681static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
682 int state,
683 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000684 uint32_t unused, uintptr_t unused2,
685 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700686 return NextInvokeInsnSP(cu, info, kQuickInvokeStaticTrampolineWithAccessCheck, state,
687 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688}
689
690static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
691 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000692 uint32_t unused, uintptr_t unused2,
693 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700694 return NextInvokeInsnSP(cu, info, kQuickInvokeDirectTrampolineWithAccessCheck, state,
695 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696}
697
698static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
699 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000700 uint32_t unused, uintptr_t unused2,
701 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700702 return NextInvokeInsnSP(cu, info, kQuickInvokeSuperTrampolineWithAccessCheck, state,
703 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704}
705
706static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
707 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000708 uint32_t unused, uintptr_t unused2,
709 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700710 return NextInvokeInsnSP(cu, info, kQuickInvokeVirtualTrampolineWithAccessCheck, state,
711 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712}
713
714static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
715 CallInfo* info, int state,
716 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000717 uint32_t unused, uintptr_t unused2,
718 uintptr_t unused3, InvokeType unused4) {
Andreas Gampe98430592014-07-27 19:44:50 -0700719 return NextInvokeInsnSP(cu, info, kQuickInvokeInterfaceTrampolineWithAccessCheck, state,
720 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721}
722
723int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
724 NextCallInsn next_call_insn,
725 const MethodReference& target_method,
726 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700727 uintptr_t direct_method, InvokeType type, bool skip_this) {
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700728 int last_arg_reg = 3 - 1;
Andreas Gampeccc60262014-07-04 18:02:38 -0700729 int arg_regs[3] = {TargetReg(kArg1, kNotWide).GetReg(), TargetReg(kArg2, kNotWide).GetReg(),
730 TargetReg(kArg3, kNotWide).GetReg()};
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700731
732 int next_reg = 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 int next_arg = 0;
734 if (skip_this) {
735 next_reg++;
736 next_arg++;
737 }
738 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
739 RegLocation rl_arg = info->args[next_arg++];
740 rl_arg = UpdateRawLoc(rl_arg);
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700741 if (rl_arg.wide && (next_reg <= last_arg_reg - 1)) {
742 RegStorage r_tmp(RegStorage::k64BitPair, arg_regs[next_reg], arg_regs[next_reg + 1]);
buzbee2700f7e2014-03-07 09:46:20 -0800743 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 next_reg++;
745 next_arg++;
746 } else {
747 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800748 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 rl_arg.is_const = false;
750 }
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700751 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(arg_regs[next_reg]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 }
753 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
754 direct_code, direct_method, type);
755 }
756 return call_state;
757}
758
759/*
760 * Load up to 5 arguments, the first three of which will be in
761 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
762 * and as part of the load sequence, it must be replaced with
763 * the target method pointer. Note, this may also be called
764 * for "range" variants if the number of arguments is 5 or fewer.
765 */
766int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
767 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
768 const MethodReference& target_method,
769 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700770 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 RegLocation rl_arg;
772
773 /* If no arguments, just return */
774 if (info->num_arg_words == 0)
775 return call_state;
776
777 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
778 direct_code, direct_method, type);
779
780 DCHECK_LE(info->num_arg_words, 5);
781 if (info->num_arg_words > 3) {
782 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700783 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 RegLocation rl_use0 = info->args[0];
785 RegLocation rl_use1 = info->args[1];
786 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800787 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
788 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 // Wide spans, we need the 2nd half of uses[2].
790 rl_arg = UpdateLocWide(rl_use2);
791 if (rl_arg.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -0700792 if (rl_arg.reg.IsPair()) {
793 reg = rl_arg.reg.GetHigh();
794 } else {
795 RegisterInfo* info = GetRegInfo(rl_arg.reg);
796 info = info->FindMatchingView(RegisterInfo::kHighSingleStorageMask);
797 if (info == nullptr) {
798 // NOTE: For hard float convention we won't split arguments across reg/mem.
799 UNIMPLEMENTED(FATAL) << "Needs hard float api.";
800 }
801 reg = info->GetReg();
802 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 } else {
804 // kArg2 & rArg3 can safely be used here
Andreas Gampeccc60262014-07-04 18:02:38 -0700805 reg = TargetReg(kArg3, kNotWide);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100806 {
807 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700808 Load32Disp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100809 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 call_state = next_call_insn(cu_, info, call_state, target_method,
811 vtable_idx, direct_code, direct_method, type);
812 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100813 {
814 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700815 Store32Disp(TargetPtrReg(kSp), (next_use + 1) * 4, reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100816 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
818 direct_code, direct_method, type);
819 next_use++;
820 }
821 // Loop through the rest
822 while (next_use < info->num_arg_words) {
buzbee091cc402014-03-31 10:14:40 -0700823 RegStorage arg_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824 rl_arg = info->args[next_use];
825 rl_arg = UpdateRawLoc(rl_arg);
826 if (rl_arg.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700827 arg_reg = rl_arg.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700828 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700829 arg_reg = TargetReg(kArg2, rl_arg.wide ? kWide : kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 if (rl_arg.wide) {
buzbee091cc402014-03-31 10:14:40 -0700831 LoadValueDirectWideFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 } else {
buzbee091cc402014-03-31 10:14:40 -0700833 LoadValueDirectFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834 }
835 call_state = next_call_insn(cu_, info, call_state, target_method,
836 vtable_idx, direct_code, direct_method, type);
837 }
838 int outs_offset = (next_use + 1) * 4;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100839 {
840 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
841 if (rl_arg.wide) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700842 StoreBaseDisp(TargetPtrReg(kSp), outs_offset, arg_reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100843 next_use += 2;
844 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700845 Store32Disp(TargetPtrReg(kSp), outs_offset, arg_reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100846 next_use++;
847 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 }
849 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
850 direct_code, direct_method, type);
851 }
852 }
853
854 call_state = LoadArgRegs(info, call_state, next_call_insn,
855 target_method, vtable_idx, direct_code, direct_method,
856 type, skip_this);
857
858 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +0000859 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700860 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700861 } else {
862 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +0000863 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) &&
864 (info->opt_flags & MIR_IGNORE_NULL_CHECK)) {
865 return call_state;
866 }
Dave Allisonf9439142014-03-27 15:10:22 -0700867 // In lieu of generating a check for kArg1 being null, we need to
868 // perform a load when doing implicit checks.
Dave Allison69dfe512014-07-11 17:11:58 +0000869 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700870 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871 }
872 return call_state;
873}
874
Dave Allison69dfe512014-07-11 17:11:58 +0000875// Default implementation of implicit null pointer check.
876// Overridden by arch specific as necessary.
877void Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
878 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
879 return;
880 }
881 RegStorage tmp = AllocTemp();
882 Load32Disp(reg, 0, tmp);
883 MarkPossibleNullPointerException(opt_flags);
884 FreeTemp(tmp);
885}
886
887
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888/*
889 * May have 0+ arguments (also used for jumbo). Note that
890 * source virtual registers may be in physical registers, so may
891 * need to be flushed to home location before copying. This
892 * applies to arg3 and above (see below).
893 *
894 * Two general strategies:
895 * If < 20 arguments
896 * Pass args 3-18 using vldm/vstm block copy
897 * Pass arg0, arg1 & arg2 in kArg1-kArg3
898 * If 20+ arguments
899 * Pass args arg19+ using memcpy block copy
900 * Pass arg0, arg1 & arg2 in kArg1-kArg3
901 *
902 */
903int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
904 LIR** pcrLabel, NextCallInsn next_call_insn,
905 const MethodReference& target_method,
906 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700907 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 // If we can treat it as non-range (Jumbo ops will use range form)
909 if (info->num_arg_words <= 5)
910 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
911 next_call_insn, target_method, vtable_idx,
912 direct_code, direct_method, type, skip_this);
913 /*
914 * First load the non-register arguments. Both forms expect all
915 * of the source arguments to be in their home frame location, so
916 * scan the s_reg names and flush any that have been promoted to
917 * frame backing storage.
918 */
919 // Scan the rest of the args - if in phys_reg flush to memory
920 for (int next_arg = 0; next_arg < info->num_arg_words;) {
921 RegLocation loc = info->args[next_arg];
922 if (loc.wide) {
923 loc = UpdateLocWide(loc);
924 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100925 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700926 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 }
928 next_arg += 2;
929 } else {
930 loc = UpdateLoc(loc);
931 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100932 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700933 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 }
935 next_arg++;
936 }
937 }
938
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800939 // Logic below assumes that Method pointer is at offset zero from SP.
940 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
941
942 // The first 3 arguments are passed via registers.
943 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
944 // get size of uintptr_t or size of object reference according to model being used.
945 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800947 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
948 DCHECK_GT(regs_left_to_pass_via_stack, 0);
949
950 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
951 // Use vldm/vstm pair using kArg3 as a temp
952 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
953 direct_code, direct_method, type);
Andreas Gampeccc60262014-07-04 18:02:38 -0700954 OpRegRegImm(kOpAdd, TargetReg(kArg3, kRef), TargetPtrReg(kSp), start_offset);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100955 LIR* ld = nullptr;
956 {
957 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -0700958 ld = OpVldm(TargetReg(kArg3, kRef), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100959 }
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800960 // TUNING: loosen barrier
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100961 ld->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800962 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
963 direct_code, direct_method, type);
Andreas Gampeccc60262014-07-04 18:02:38 -0700964 OpRegRegImm(kOpAdd, TargetReg(kArg3, kRef), TargetPtrReg(kSp), 4 /* Method* */ + (3 * 4));
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800965 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
966 direct_code, direct_method, type);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100967 LIR* st = nullptr;
968 {
969 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampeccc60262014-07-04 18:02:38 -0700970 st = OpVstm(TargetReg(kArg3, kRef), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100971 }
972 st->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800973 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
974 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700975 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800976 int current_src_offset = start_offset;
977 int current_dest_offset = outs_offset;
978
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100979 // Only davik regs are accessed in this loop; no next_call_insn() calls.
980 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800981 while (regs_left_to_pass_via_stack > 0) {
982 // This is based on the knowledge that the stack itself is 16-byte aligned.
983 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
984 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
985 size_t bytes_to_move;
986
987 /*
988 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
989 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
990 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
991 * We do this because we could potentially do a smaller move to align.
992 */
993 if (regs_left_to_pass_via_stack == 4 ||
994 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
995 // Moving 128-bits via xmm register.
996 bytes_to_move = sizeof(uint32_t) * 4;
997
998 // Allocate a free xmm temp. Since we are working through the calling sequence,
Mark Mendelle87f9b52014-04-30 14:13:18 -0400999 // we expect to have an xmm temporary available. AllocTempDouble will abort if
1000 // there are no free registers.
buzbee2700f7e2014-03-07 09:46:20 -08001001 RegStorage temp = AllocTempDouble();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001002
1003 LIR* ld1 = nullptr;
1004 LIR* ld2 = nullptr;
1005 LIR* st1 = nullptr;
1006 LIR* st2 = nullptr;
1007
1008 /*
1009 * The logic is similar for both loads and stores. If we have 16-byte alignment,
1010 * do an aligned move. If we have 8-byte alignment, then do the move in two
1011 * parts. This approach prevents possible cache line splits. Finally, fall back
1012 * to doing an unaligned move. In most cases we likely won't split the cache
1013 * line but we cannot prove it and thus take a conservative approach.
1014 */
1015 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
1016 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
1017
1018 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001019 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001020 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001021 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovLo128FP);
1022 ld2 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001023 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001024 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001025 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001026 }
1027
1028 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001029 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001030 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001031 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovLo128FP);
1032 st2 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001033 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001034 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001035 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001036 }
1037
1038 // TODO If we could keep track of aliasing information for memory accesses that are wider
1039 // than 64-bit, we wouldn't need to set up a barrier.
1040 if (ld1 != nullptr) {
1041 if (ld2 != nullptr) {
1042 // For 64-bit load we can actually set up the aliasing information.
1043 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001044 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true,
1045 true);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001046 } else {
1047 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001048 ld1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001049 }
1050 }
1051 if (st1 != nullptr) {
1052 if (st2 != nullptr) {
1053 // For 64-bit store we can actually set up the aliasing information.
1054 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001055 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false,
1056 true);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001057 } else {
1058 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001059 st1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001060 }
1061 }
1062
1063 // Free the temporary used for the data movement.
buzbee091cc402014-03-31 10:14:40 -07001064 FreeTemp(temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001065 } else {
1066 // Moving 32-bits via general purpose register.
1067 bytes_to_move = sizeof(uint32_t);
1068
1069 // Instead of allocating a new temp, simply reuse one of the registers being used
1070 // for argument passing.
Andreas Gampeccc60262014-07-04 18:02:38 -07001071 RegStorage temp = TargetReg(kArg3, kNotWide);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001072
1073 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07001074 Load32Disp(TargetPtrReg(kSp), current_src_offset, temp);
1075 Store32Disp(TargetPtrReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001076 }
1077
1078 current_src_offset += bytes_to_move;
1079 current_dest_offset += bytes_to_move;
1080 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1081 }
1082 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 // Generate memcpy
Andreas Gampeccc60262014-07-04 18:02:38 -07001084 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
1085 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -07001086 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
1087 (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 }
1089
1090 call_state = LoadArgRegs(info, call_state, next_call_insn,
1091 target_method, vtable_idx, direct_code, direct_method,
1092 type, skip_this);
1093
1094 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1095 direct_code, direct_method, type);
1096 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +00001097 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001098 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001099 } else {
1100 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +00001101 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) &&
1102 (info->opt_flags & MIR_IGNORE_NULL_CHECK)) {
1103 return call_state;
1104 }
Dave Allisonf9439142014-03-27 15:10:22 -07001105 // In lieu of generating a check for kArg1 being null, we need to
1106 // perform a load when doing implicit checks.
Dave Allison69dfe512014-07-11 17:11:58 +00001107 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001108 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 }
1110 return call_state;
1111}
1112
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001113RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 RegLocation res;
1115 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001116 res = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 } else {
1118 res = info->result;
1119 }
1120 return res;
1121}
1122
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001123RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 RegLocation res;
1125 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001126 res = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 } else {
1128 res = info->result;
1129 }
1130 return res;
1131}
1132
Fred Shih4ee7a662014-07-11 09:59:27 -07001133bool Mir2Lir::GenInlinedGet(CallInfo* info) {
1134 if (cu_->instruction_set == kMips) {
1135 // TODO - add Mips implementation
1136 return false;
1137 }
1138
1139 // the refrence class is stored in the image dex file which might not be the same as the cu's
1140 // dex file. Query the reference class for the image dex file then reset to starting dex file
1141 // in after loading class type.
1142 uint16_t type_idx = 0;
1143 const DexFile* ref_dex_file = nullptr;
1144 {
1145 ScopedObjectAccess soa(Thread::Current());
1146 type_idx = mirror::Reference::GetJavaLangRefReference()->GetDexTypeIndex();
1147 ref_dex_file = mirror::Reference::GetJavaLangRefReference()->GetDexCache()->GetDexFile();
1148 }
1149 CHECK(LIKELY(ref_dex_file != nullptr));
1150
1151 // address is either static within the image file, or needs to be patched up after compilation.
1152 bool unused_type_initialized;
1153 bool use_direct_type_ptr;
1154 uintptr_t direct_type_ptr;
1155 bool is_finalizable;
1156 const DexFile* old_dex = cu_->dex_file;
1157 cu_->dex_file = ref_dex_file;
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001158 RegStorage reg_class = TargetReg(kArg1, kRef);
1159 Clobber(reg_class);
1160 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -07001161 if (!cu_->compiler_driver->CanEmbedTypeInCode(*ref_dex_file, type_idx, &unused_type_initialized,
1162 &use_direct_type_ptr, &direct_type_ptr,
1163 &is_finalizable) || is_finalizable) {
1164 cu_->dex_file = old_dex;
1165 // address is not known and post-compile patch is not possible, cannot insert intrinsic.
1166 return false;
1167 }
1168 if (use_direct_type_ptr) {
1169 LoadConstant(reg_class, direct_type_ptr);
1170 } else {
1171 LoadClassType(type_idx, kArg1);
1172 }
1173 cu_->dex_file = old_dex;
1174
1175 // get the offset for flags in reference class.
1176 uint32_t slow_path_flag_offset = 0;
1177 uint32_t disable_flag_offset = 0;
1178 {
1179 ScopedObjectAccess soa(Thread::Current());
1180 mirror::Class* reference_class = mirror::Reference::GetJavaLangRefReference();
1181 slow_path_flag_offset = reference_class->GetSlowPathFlagOffset().Uint32Value();
1182 disable_flag_offset = reference_class->GetDisableIntrinsicFlagOffset().Uint32Value();
1183 }
1184 CHECK(slow_path_flag_offset && disable_flag_offset &&
1185 (slow_path_flag_offset != disable_flag_offset));
1186
1187 // intrinsic logic start.
1188 RegLocation rl_obj = info->args[0];
1189 rl_obj = LoadValue(rl_obj);
1190
1191 RegStorage reg_slow_path = AllocTemp();
1192 RegStorage reg_disabled = AllocTemp();
1193 Load32Disp(reg_class, slow_path_flag_offset, reg_slow_path);
1194 Load32Disp(reg_class, disable_flag_offset, reg_disabled);
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001195 FreeTemp(reg_class);
1196 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -07001197 FreeTemp(reg_disabled);
1198
1199 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -07001200 LIR* slow_path_branch;
1201 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
1202 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
1203 slow_path_branch = OpCondBranch(kCondNe, nullptr);
1204 } else {
1205 // Generate compare and branch.
1206 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
1207 }
Fred Shih4ee7a662014-07-11 09:59:27 -07001208 FreeTemp(reg_slow_path);
1209
1210 // slow path not enabled, simply load the referent of the reference object
1211 RegLocation rl_dest = InlineTarget(info);
1212 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
1213 GenNullCheck(rl_obj.reg, info->opt_flags);
1214 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
1215 kNotVolatile);
1216 MarkPossibleNullPointerException(info->opt_flags);
1217 StoreValue(rl_dest, rl_result);
1218
1219 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
1220 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
1221
1222 return true;
1223}
1224
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001225bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 if (cu_->instruction_set == kMips) {
1227 // TODO - add Mips implementation
1228 return false;
1229 }
1230 // Location of reference to data array
1231 int value_offset = mirror::String::ValueOffset().Int32Value();
1232 // Location of count
1233 int count_offset = mirror::String::CountOffset().Int32Value();
1234 // Starting offset within data array
1235 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1236 // Start of char data with array_
1237 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1238
1239 RegLocation rl_obj = info->args[0];
1240 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -07001241 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -07001242 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001243 RegStorage reg_max;
1244 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001246 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001247 RegStorage reg_off;
1248 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -07001249 reg_off = AllocTemp();
1250 reg_ptr = AllocTempRef();
1251 if (range_check) {
1252 reg_max = AllocTemp();
1253 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001254 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 }
Andreas Gampe98430592014-07-27 19:44:50 -07001256 Load32Disp(rl_obj.reg, offset_offset, reg_off);
1257 MarkPossibleNullPointerException(info->opt_flags);
1258 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
1259 if (range_check) {
1260 // Set up a slow path to allow retry in case of bounds violation */
1261 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
1262 FreeTemp(reg_max);
1263 range_check_branch = OpCondBranch(kCondUge, nullptr);
1264 }
1265 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001266 if (rl_idx.is_const) {
1267 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1268 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001269 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001270 }
buzbee2700f7e2014-03-07 09:46:20 -08001271 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001272 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001273 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001274 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 RegLocation rl_dest = InlineTarget(info);
1276 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001277 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 FreeTemp(reg_off);
1279 FreeTemp(reg_ptr);
1280 StoreValue(rl_dest, rl_result);
1281 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001282 DCHECK(range_check_branch != nullptr);
1283 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001284 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001286 return true;
1287}
1288
1289// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001290bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 if (cu_->instruction_set == kMips) {
1292 // TODO - add Mips implementation
1293 return false;
1294 }
1295 // dst = src.length();
1296 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001297 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 RegLocation rl_dest = InlineTarget(info);
1299 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001300 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001301 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001302 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 if (is_empty) {
1304 // dst = (dst == 0);
1305 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001306 RegStorage t_reg = AllocTemp();
1307 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1308 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001309 } else if (cu_->instruction_set == kArm64) {
1310 OpRegImm(kOpSub, rl_result.reg, 1);
1311 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001313 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001314 OpRegImm(kOpSub, rl_result.reg, 1);
1315 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 }
1317 }
1318 StoreValue(rl_dest, rl_result);
1319 return true;
1320}
1321
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001322bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Zheng Xua3fe7422014-07-09 14:03:15 +08001323 if (cu_->instruction_set == kMips) {
1324 // TODO - add Mips implementation.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001325 return false;
1326 }
1327 RegLocation rl_src_i = info->args[0];
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001328 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -07001329 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001330 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001331 if (size == k64) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001332 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001333 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1334 StoreValueWide(rl_dest, rl_result);
1335 return true;
1336 }
buzbee2700f7e2014-03-07 09:46:20 -08001337 RegStorage r_i_low = rl_i.reg.GetLow();
1338 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001339 // 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 +00001340 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001341 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001342 }
buzbee2700f7e2014-03-07 09:46:20 -08001343 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1344 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1345 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001346 FreeTemp(r_i_low);
1347 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001348 StoreValueWide(rl_dest, rl_result);
1349 } else {
buzbee695d13a2014-04-19 13:32:20 -07001350 DCHECK(size == k32 || size == kSignedHalf);
1351 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001352 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001353 StoreValue(rl_dest, rl_result);
1354 }
1355 return true;
1356}
1357
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001358bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 if (cu_->instruction_set == kMips) {
1360 // TODO - add Mips implementation
1361 return false;
1362 }
1363 RegLocation rl_src = info->args[0];
1364 rl_src = LoadValue(rl_src, kCoreReg);
1365 RegLocation rl_dest = InlineTarget(info);
1366 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001367 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001369 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1370 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1371 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 StoreValue(rl_dest, rl_result);
1373 return true;
1374}
1375
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001376bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 if (cu_->instruction_set == kMips) {
1378 // TODO - add Mips implementation
1379 return false;
1380 }
Vladimir Markob9823312014-03-20 17:38:43 +00001381 RegLocation rl_src = info->args[0];
1382 rl_src = LoadValueWide(rl_src, kCoreReg);
1383 RegLocation rl_dest = InlineTargetWide(info);
1384 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1385
1386 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001387 if (cu_->instruction_set != kX86_64 &&
1388 (cu_->instruction_set == kX86 ||
1389 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001390 OpRegCopyWide(rl_result.reg, rl_src.reg);
1391 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1392 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1393 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001394 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1395 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001396 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001397 }
1398 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 }
Vladimir Markob9823312014-03-20 17:38:43 +00001400
1401 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001402 RegStorage sign_reg;
1403 if (cu_->instruction_set == kX86_64) {
1404 sign_reg = AllocTempWide();
1405 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1406 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1407 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1408 } else {
1409 sign_reg = AllocTemp();
1410 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1411 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1412 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1413 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1414 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1415 }
buzbee082833c2014-05-17 23:16:26 -07001416 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001417 StoreValueWide(rl_dest, rl_result);
1418 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419}
1420
Serban Constantinescu23abec92014-07-02 16:13:38 +01001421bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
1422 // Currently implemented only for ARM64
1423 return false;
1424}
1425
1426bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
1427 // Currently implemented only for ARM64
1428 return false;
1429}
1430
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001431bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432 if (cu_->instruction_set == kMips) {
1433 // TODO - add Mips implementation
1434 return false;
1435 }
1436 RegLocation rl_src = info->args[0];
1437 RegLocation rl_dest = InlineTarget(info);
1438 StoreValue(rl_dest, rl_src);
1439 return true;
1440}
1441
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001442bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 if (cu_->instruction_set == kMips) {
1444 // TODO - add Mips implementation
1445 return false;
1446 }
1447 RegLocation rl_src = info->args[0];
1448 RegLocation rl_dest = InlineTargetWide(info);
1449 StoreValueWide(rl_dest, rl_src);
1450 return true;
1451}
1452
DaniilSokolov70c4f062014-06-24 17:34:00 -07001453bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
1454 return false;
1455}
1456
1457
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001459 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 * otherwise bails to standard library code.
1461 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001462bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 if (cu_->instruction_set == kMips) {
1464 // TODO - add Mips implementation
1465 return false;
1466 }
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001467 if (cu_->instruction_set == kX86_64) {
1468 // TODO - add kX86_64 implementation
1469 return false;
1470 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001471 RegLocation rl_obj = info->args[0];
1472 RegLocation rl_char = info->args[1];
1473 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1474 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1475 return false;
1476 }
1477
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001478 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001480 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1481 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1482 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484 LoadValueDirectFixed(rl_obj, reg_ptr);
1485 LoadValueDirectFixed(rl_char, reg_char);
1486 if (zero_based) {
1487 LoadConstant(reg_start, 0);
1488 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001489 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001490 LoadValueDirectFixed(rl_start, reg_start);
1491 }
Andreas Gampe98430592014-07-27 19:44:50 -07001492 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001493 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001494 LIR* high_code_point_branch =
1495 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001497 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001498 if (!rl_char.is_const) {
1499 // Add the slow path for code points beyond 0xFFFF.
1500 DCHECK(high_code_point_branch != nullptr);
1501 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1502 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001503 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001504 } else {
1505 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1506 DCHECK(high_code_point_branch == nullptr);
1507 }
buzbeea0cd2d72014-06-01 09:33:49 -07001508 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 RegLocation rl_dest = InlineTarget(info);
1510 StoreValue(rl_dest, rl_return);
1511 return true;
1512}
1513
1514/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001515bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001516 if (cu_->instruction_set == kMips) {
1517 // TODO - add Mips implementation
1518 return false;
1519 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001520 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001522 RegStorage reg_this = TargetReg(kArg0, kRef);
1523 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524
1525 RegLocation rl_this = info->args[0];
1526 RegLocation rl_cmp = info->args[1];
1527 LoadValueDirectFixed(rl_this, reg_this);
1528 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001529 RegStorage r_tgt;
1530 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001531 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001532 } else {
1533 r_tgt = RegStorage::InvalidReg();
1534 }
Dave Allisonf9439142014-03-27 15:10:22 -07001535 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001536 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001537 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001538 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001539 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001540 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001541 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001542 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 RegLocation rl_dest = InlineTarget(info);
1544 StoreValue(rl_dest, rl_return);
1545 return true;
1546}
1547
1548bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1549 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001550
1551 // Early exit if the result is unused.
1552 if (rl_dest.orig_sreg < 0) {
1553 return true;
1554 }
1555
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001556 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001557
1558 switch (cu_->instruction_set) {
1559 case kArm:
1560 // Fall-through.
1561 case kThumb2:
1562 // Fall-through.
1563 case kMips:
Chao-ying Fua77ee512014-07-01 17:43:41 -07001564 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001565 break;
1566
1567 case kArm64:
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001568 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1569 kNotVolatile);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001570 break;
1571
1572 case kX86:
1573 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg,
1574 Thread::PeerOffset<4>());
1575 break;
1576
1577 case kX86_64:
1578 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg,
1579 Thread::PeerOffset<8>());
1580 break;
1581
1582 default:
1583 LOG(FATAL) << "Unexpected isa " << cu_->instruction_set;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 }
1585 StoreValue(rl_dest, rl_result);
1586 return true;
1587}
1588
1589bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1590 bool is_long, bool is_volatile) {
1591 if (cu_->instruction_set == kMips) {
1592 // TODO - add Mips implementation
1593 return false;
1594 }
1595 // Unused - RegLocation rl_src_unsafe = info->args[0];
1596 RegLocation rl_src_obj = info->args[1]; // Object
1597 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001598 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001599 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001600
buzbeea0cd2d72014-06-01 09:33:49 -07001601 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001603 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001605 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1606 || cu_->instruction_set == kArm64) {
1607 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001608 } else {
1609 RegStorage rl_temp_offset = AllocTemp();
1610 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001611 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001612 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001613 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001615 if (rl_result.ref) {
1616 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1617 } else {
1618 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1619 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001620 }
1621
1622 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001623 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001624 }
1625
1626 if (is_long) {
1627 StoreValueWide(rl_dest, rl_result);
1628 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 StoreValue(rl_dest, rl_result);
1630 }
1631 return true;
1632}
1633
1634bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1635 bool is_object, bool is_volatile, bool is_ordered) {
1636 if (cu_->instruction_set == kMips) {
1637 // TODO - add Mips implementation
1638 return false;
1639 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 // Unused - RegLocation rl_src_unsafe = info->args[0];
1641 RegLocation rl_src_obj = info->args[1]; // Object
1642 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001643 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 RegLocation rl_src_value = info->args[4]; // value to store
1645 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001646 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 }
buzbeea0cd2d72014-06-01 09:33:49 -07001648 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1650 RegLocation rl_value;
1651 if (is_long) {
1652 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001653 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1654 || cu_->instruction_set == kArm64) {
1655 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001656 } else {
1657 RegStorage rl_temp_offset = AllocTemp();
1658 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001659 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001660 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001661 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001663 rl_value = LoadValue(rl_src_value);
Matteo Franchin255e0142014-07-04 13:50:41 +01001664 if (rl_value.ref) {
1665 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1666 } else {
1667 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1668 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001670
1671 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001672 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001673
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001675 // Prevent reordering with a subsequent volatile load.
1676 // May also be needed to address store atomicity issues.
1677 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 }
1679 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001680 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 }
1682 return true;
1683}
1684
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001685void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001686 if ((info->opt_flags & MIR_INLINED) != 0) {
1687 // Already inlined but we may still need the null check.
1688 if (info->type != kStatic &&
1689 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1690 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
buzbeea0cd2d72014-06-01 09:33:49 -07001691 RegLocation rl_obj = LoadValue(info->args[0], kRefReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001692 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001693 }
1694 return;
1695 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001696 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001697 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1698 ->GenIntrinsic(this, info)) {
1699 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001700 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001701 GenInvokeNoInline(info);
1702}
1703
Andreas Gampe2f244e92014-05-08 03:35:25 -07001704static LIR* GenInvokeNoInlineCall(Mir2Lir* mir_to_lir, InvokeType type) {
Andreas Gampe98430592014-07-27 19:44:50 -07001705 QuickEntrypointEnum trampoline;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001706 switch (type) {
1707 case kInterface:
Andreas Gampe98430592014-07-27 19:44:50 -07001708 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001709 break;
1710 case kDirect:
Andreas Gampe98430592014-07-27 19:44:50 -07001711 trampoline = kQuickInvokeDirectTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001712 break;
1713 case kStatic:
Andreas Gampe98430592014-07-27 19:44:50 -07001714 trampoline = kQuickInvokeStaticTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001715 break;
1716 case kSuper:
Andreas Gampe98430592014-07-27 19:44:50 -07001717 trampoline = kQuickInvokeSuperTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001718 break;
1719 case kVirtual:
Andreas Gampe98430592014-07-27 19:44:50 -07001720 trampoline = kQuickInvokeVirtualTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001721 break;
1722 default:
1723 LOG(FATAL) << "Unexpected invoke type";
Andreas Gampe98430592014-07-27 19:44:50 -07001724 trampoline = kQuickInvokeInterfaceTrampolineWithAccessCheck;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001725 }
Andreas Gampe98430592014-07-27 19:44:50 -07001726 return mir_to_lir->InvokeTrampoline(kOpBlx, RegStorage::InvalidReg(), trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001727}
1728
Vladimir Marko3bc86152014-03-13 14:11:28 +00001729void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730 int call_state = 0;
1731 LIR* null_ck;
1732 LIR** p_null_ck = NULL;
1733 NextCallInsn next_call_insn;
1734 FlushAllRegs(); /* Everything to home location */
1735 // Explicit register usage
1736 LockCallTemps();
1737
Vladimir Markof096aad2014-01-23 15:51:58 +00001738 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1739 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
Mark Mendelle87f9b52014-04-30 14:13:18 -04001740 BeginInvoke(info);
Vladimir Markof096aad2014-01-23 15:51:58 +00001741 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1742 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1743 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001747 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 } else if (info->type == kDirect) {
1749 if (fast_path) {
1750 p_null_ck = &null_ck;
1751 }
1752 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1753 skip_this = false;
1754 } else if (info->type == kStatic) {
1755 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1756 skip_this = false;
1757 } else if (info->type == kSuper) {
1758 DCHECK(!fast_path); // Fast path is a direct call.
1759 next_call_insn = NextSuperCallInsnSP;
1760 skip_this = false;
1761 } else {
1762 DCHECK_EQ(info->type, kVirtual);
1763 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1764 skip_this = fast_path;
1765 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001766 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001767 if (!info->is_range) {
1768 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001769 next_call_insn, target_method, method_info.VTableIndex(),
1770 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001771 original_type, skip_this);
1772 } else {
1773 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001774 next_call_insn, target_method, method_info.VTableIndex(),
1775 method_info.DirectCode(), method_info.DirectMethod(),
1776 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001777 }
1778 // Finish up any of the call sequence not interleaved in arg loading
1779 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001780 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1781 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 }
1783 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001784 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001785 call_inst = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001787 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001788 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001789 // We can have the linker fixup a call relative.
1790 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001791 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001792 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001793 call_inst = OpMem(kOpBlx, TargetReg(kArg0, kRef),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001794 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1795 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001796 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001797 call_inst = GenInvokeNoInlineCall(this, info->type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001798 }
1799 }
Mark Mendelle87f9b52014-04-30 14:13:18 -04001800 EndInvoke(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 MarkSafepointPC(call_inst);
1802
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001803 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001804 if (info->result.location != kLocInvalid) {
1805 // We have a following MOVE_RESULT - do it now.
1806 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001807 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001808 StoreValueWide(info->result, ret_loc);
1809 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001810 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001811 StoreValue(info->result, ret_loc);
1812 }
1813 }
1814}
1815
1816} // namespace art