blob: 2d41ba1795883fd8f9ff099777a46d7cf360c0e0 [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
Andreas Gampe0b9203e2015-01-22 20:39:27 -080017#include "mir_to_lir-inl.h"
18
Vladimir Markof4da6752014-08-01 19:04:18 +010019#include "arm/codegen_arm.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "dex/compiler_ir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080021#include "dex/dex_flags.h"
22#include "dex/mir_graph.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000023#include "dex/quick/dex_file_method_inliner.h"
24#include "dex/quick/dex_file_to_method_inliner_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "dex_file-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080026#include "driver/compiler_driver.h"
Ian Rogers166db042013-07-26 12:05:57 -070027#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "invoke_type.h"
29#include "mirror/array.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070030#include "mirror/class-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070031#include "mirror/dex_cache.h"
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070032#include "mirror/object_array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "mirror/string.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010034#include "scoped_thread_state_change.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
36namespace art {
37
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070038// Shortcuts to repeatedly used long types.
39typedef mirror::ObjectArray<mirror::Object> ObjArray;
40
Brian Carlstrom7940e442013-07-12 13:46:57 -070041/*
42 * This source files contains "gen" codegen routines that should
43 * be applicable to most targets. Only mid-level support utilities
44 * and "op" calls may be used here.
45 */
46
Mingyao Yang3a74d152014-04-21 15:39:44 -070047void Mir2Lir::AddIntrinsicSlowPath(CallInfo* info, LIR* branch, LIR* resume) {
48 class IntrinsicSlowPathPath : public Mir2Lir::LIRSlowPath {
Vladimir Marko3bc86152014-03-13 14:11:28 +000049 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080050 IntrinsicSlowPathPath(Mir2Lir* m2l, CallInfo* info_in, LIR* branch_in, LIR* resume_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +000051 : LIRSlowPath(m2l, branch_in, resume_in), info_(info_in) {
52 DCHECK_EQ(info_in->offset, current_dex_pc_);
Vladimir Marko3bc86152014-03-13 14:11:28 +000053 }
54
55 void Compile() {
56 m2l_->ResetRegPool();
57 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070058 GenerateTargetLabel(kPseudoIntrinsicRetry);
Vladimir Marko3bc86152014-03-13 14:11:28 +000059 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
60 m2l_->GenInvokeNoInline(info_);
61 if (cont_ != nullptr) {
62 m2l_->OpUnconditionalBranch(cont_);
63 }
64 }
65
66 private:
67 CallInfo* const info_;
68 };
69
Mingyao Yang3a74d152014-04-21 15:39:44 -070070 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000071}
72
Brian Carlstrom7940e442013-07-12 13:46:57 -070073/*
74 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000075 * the helper target address, and the actual call to the helper. Because x86
76 * has a memory call operation, part 1 is a NOP for x86. For other targets,
77 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 */
Andreas Gampe2f244e92014-05-08 03:35:25 -070079// template <size_t pointer_size>
Andreas Gampe98430592014-07-27 19:44:50 -070080RegStorage Mir2Lir::CallHelperSetup(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070081 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
82 return RegStorage::InvalidReg();
83 } else {
Andreas Gampe98430592014-07-27 19:44:50 -070084 return LoadHelper(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070085 }
86}
87
Andreas Gampe98430592014-07-27 19:44:50 -070088LIR* Mir2Lir::CallHelper(RegStorage r_tgt, QuickEntrypointEnum trampoline, bool safepoint_pc,
89 bool use_link) {
90 LIR* call_inst = InvokeTrampoline(use_link ? kOpBlx : kOpBx, r_tgt, trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070091
Andreas Gampe98430592014-07-27 19:44:50 -070092 if (r_tgt.Valid()) {
Dave Allisond6ed6422014-04-09 23:36:15 +000093 FreeTemp(r_tgt);
94 }
Andreas Gampe98430592014-07-27 19:44:50 -070095
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 if (safepoint_pc) {
97 MarkSafepointPC(call_inst);
98 }
99 return call_inst;
100}
101
Andreas Gampe98430592014-07-27 19:44:50 -0700102void Mir2Lir::CallRuntimeHelper(QuickEntrypointEnum trampoline, bool safepoint_pc) {
103 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang42894562014-04-07 12:42:16 -0700104 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700105 CallHelper(r_tgt, trampoline, safepoint_pc);
Mingyao Yang42894562014-04-07 12:42:16 -0700106}
107
Andreas Gampe98430592014-07-27 19:44:50 -0700108void Mir2Lir::CallRuntimeHelperImm(QuickEntrypointEnum trampoline, int arg0, bool safepoint_pc) {
109 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700110 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000111 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700112 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113}
114
Andreas Gampe98430592014-07-27 19:44:50 -0700115void Mir2Lir::CallRuntimeHelperReg(QuickEntrypointEnum trampoline, RegStorage arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700116 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700117 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700118 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000119 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700120 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121}
122
Andreas Gampe98430592014-07-27 19:44:50 -0700123void Mir2Lir::CallRuntimeHelperRegLocation(QuickEntrypointEnum trampoline, RegLocation arg0,
124 bool safepoint_pc) {
125 RegStorage r_tgt = CallHelperSetup(trampoline);
buzbee2700f7e2014-03-07 09:46:20 -0800126 if (arg0.wide == 0) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700127 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, arg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700129 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000131 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700132 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133}
134
Andreas Gampe98430592014-07-27 19:44:50 -0700135void Mir2Lir::CallRuntimeHelperImmImm(QuickEntrypointEnum trampoline, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700137 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700138 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
139 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000140 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700141 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142}
143
Andreas Gampe98430592014-07-27 19:44:50 -0700144void Mir2Lir::CallRuntimeHelperImmRegLocation(QuickEntrypointEnum trampoline, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700146 RegStorage r_tgt = CallHelperSetup(trampoline);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 if (arg1.wide == 0) {
Andreas Gampef9872f02014-07-01 19:00:09 -0700148 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700150 RegStorage r_tmp = TargetReg(cu_->instruction_set == kMips ? kArg2 : kArg1, kWide);
buzbee2700f7e2014-03-07 09:46:20 -0800151 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700153 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000154 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700155 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156}
157
Andreas Gampe98430592014-07-27 19:44:50 -0700158void Mir2Lir::CallRuntimeHelperRegLocationImm(QuickEntrypointEnum trampoline, RegLocation arg0,
159 int arg1, bool safepoint_pc) {
160 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampef9872f02014-07-01 19:00:09 -0700161 DCHECK(!arg0.wide);
162 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700163 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000164 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700165 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166}
167
Andreas Gampe98430592014-07-27 19:44:50 -0700168void Mir2Lir::CallRuntimeHelperImmReg(QuickEntrypointEnum trampoline, int arg0, RegStorage arg1,
169 bool safepoint_pc) {
170 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700171 OpRegCopy(TargetReg(kArg1, arg1.GetWideKind()), arg1);
172 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000173 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700174 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175}
176
Andreas Gampe98430592014-07-27 19:44:50 -0700177void Mir2Lir::CallRuntimeHelperRegImm(QuickEntrypointEnum trampoline, RegStorage arg0, int arg1,
178 bool safepoint_pc) {
179 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700180 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
181 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000182 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700183 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184}
185
Andreas Gampe98430592014-07-27 19:44:50 -0700186void Mir2Lir::CallRuntimeHelperImmMethod(QuickEntrypointEnum trampoline, int arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700187 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700188 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700189 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
190 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000191 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700192 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193}
194
Andreas Gampe98430592014-07-27 19:44:50 -0700195void Mir2Lir::CallRuntimeHelperRegMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800196 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700197 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700198 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
199 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
200 if (r_tmp.NotExactlyEquals(arg0)) {
201 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800202 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700203 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800204 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700205 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800206}
207
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800208void Mir2Lir::CallRuntimeHelperRegRegLocationMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
209 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700210 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800211 DCHECK(!IsSameReg(TargetReg(kArg2, arg0.GetWideKind()), arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700212 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
213 if (r_tmp.NotExactlyEquals(arg0)) {
214 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800215 }
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800216 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
217 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800218 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700219 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800220}
221
Andreas Gampe98430592014-07-27 19:44:50 -0700222void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(QuickEntrypointEnum trampoline,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700223 RegLocation arg0, RegLocation arg1,
224 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700225 RegStorage r_tgt = CallHelperSetup(trampoline);
Maja Gagic6ea651f2015-02-24 16:55:04 +0100226 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kMips64 ||
227 cu_->instruction_set == kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700228 RegStorage arg0_reg = TargetReg((arg0.fp) ? kFArg0 : kArg0, arg0);
229
230 RegStorage arg1_reg;
231 if (arg1.fp == arg0.fp) {
232 arg1_reg = TargetReg((arg1.fp) ? kFArg1 : kArg1, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700234 arg1_reg = TargetReg((arg1.fp) ? kFArg0 : kArg0, arg1);
235 }
236
237 if (arg0.wide == 0) {
238 LoadValueDirectFixed(arg0, arg0_reg);
239 } else {
240 LoadValueDirectWideFixed(arg0, arg0_reg);
241 }
242
243 if (arg1.wide == 0) {
244 LoadValueDirectFixed(arg1, arg1_reg);
245 } else {
246 LoadValueDirectWideFixed(arg1, arg1_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 }
248 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700249 DCHECK(!cu_->target64);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700250 if (arg0.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700251 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700252 if (arg1.wide == 0) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800253 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700254 if (cu_->instruction_set == kMips) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800255 LoadValueDirectFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700256 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800257 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700258 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700259 } else {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800260 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700261 if (cu_->instruction_set == kMips) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800262 LoadValueDirectWideFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700263 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800264 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700265 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700266 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700268 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700269 if (arg1.wide == 0) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800270 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
271 if (cu_->instruction_set == kMips) {
272 LoadValueDirectFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kNotWide));
273 } else {
274 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kNotWide));
275 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700276 } else {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800277 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
278 if (cu_->instruction_set == kMips) {
279 LoadValueDirectWideFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kWide));
280 } else {
281 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
282 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700283 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 }
285 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000286 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700287 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288}
289
Mingyao Yang80365d92014-04-18 12:10:58 -0700290void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700291 WideKind arg0_kind = arg0.GetWideKind();
292 WideKind arg1_kind = arg1.GetWideKind();
293 if (IsSameReg(arg1, TargetReg(kArg0, arg1_kind))) {
294 if (IsSameReg(arg0, TargetReg(kArg1, arg0_kind))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700295 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampeccc60262014-07-04 18:02:38 -0700296 OpRegCopy(TargetReg(kArg2, arg1_kind), arg1);
297 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
298 OpRegCopy(TargetReg(kArg1, arg1_kind), TargetReg(kArg2, arg1_kind));
Mingyao Yang80365d92014-04-18 12:10:58 -0700299 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700300 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
301 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700302 }
303 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700304 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
305 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700306 }
307}
308
Andreas Gampe98430592014-07-27 19:44:50 -0700309void Mir2Lir::CallRuntimeHelperRegReg(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800310 RegStorage arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700311 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700312 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000313 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700314 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315}
316
Andreas Gampe98430592014-07-27 19:44:50 -0700317void Mir2Lir::CallRuntimeHelperRegRegImm(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800318 RegStorage arg1, int arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700319 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700320 CopyToArgumentRegs(arg0, arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700321 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000322 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700323 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324}
325
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800326void Mir2Lir::CallRuntimeHelperImmRegLocationMethod(QuickEntrypointEnum trampoline, int arg0,
327 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700328 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800329 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
330 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
Andreas Gampeccc60262014-07-04 18:02:38 -0700331 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000332 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700333 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334}
335
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800336void Mir2Lir::CallRuntimeHelperImmImmMethod(QuickEntrypointEnum trampoline, int arg0, int arg1,
Andreas Gampe98430592014-07-27 19:44:50 -0700337 bool safepoint_pc) {
338 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800339 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
340 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700341 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000342 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700343 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344}
345
Andreas Gampe98430592014-07-27 19:44:50 -0700346void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(QuickEntrypointEnum trampoline, int arg0,
347 RegLocation arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 RegLocation arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700349 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700350 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
351 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700352 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700354 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700356 LoadValueDirectWideFixed(arg2, TargetReg(kArg2, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700358 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000359 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700360 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361}
362
Andreas Gampeccc60262014-07-04 18:02:38 -0700363void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(
Andreas Gampe98430592014-07-27 19:44:50 -0700364 QuickEntrypointEnum trampoline,
Andreas Gampeccc60262014-07-04 18:02:38 -0700365 RegLocation arg0,
366 RegLocation arg1,
367 RegLocation arg2,
368 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700369 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700370 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
371 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
372 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000373 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700374 CallHelper(r_tgt, trampoline, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700375}
376
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377/*
378 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100379 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380 * assignment of promoted arguments.
381 *
382 * ArgLocs is an array of location records describing the incoming arguments
383 * with one location record per word of argument.
384 */
Zheng Xu5667fdb2014-10-23 18:29:55 +0800385// TODO: Support 64-bit argument registers.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700386void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800388 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 * It will attempt to keep kArg0 live (or copy it to home location
390 * if promoted).
391 */
392 RegLocation rl_src = rl_method;
393 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -0700394 rl_src.reg = TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700396 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700397 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 // If Method* has been promoted, explicitly flush
399 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700400 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 }
402
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700403 if (mir_graph_->GetNumOfInVRs() == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800405 }
406
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700407 int start_vreg = mir_graph_->GetFirstInVR();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 /*
409 * Copy incoming arguments to their proper home locations.
410 * NOTE: an older version of dx had an issue in which
411 * it would reuse static method argument registers.
412 * This could result in the same Dalvik virtual register
413 * being promoted to both core and fp regs. To account for this,
414 * we only copy to the corresponding promoted physical register
415 * if it matches the type of the SSA name for the incoming
416 * argument. It is also possible that long and double arguments
417 * end up half-promoted. In those cases, we must flush the promoted
418 * half to memory as well.
419 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100420 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600421 RegLocation* t_loc = nullptr;
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000422 EnsureInitializedArgMappingToPhysicalReg();
Serguei Katkov717a3e42014-11-13 17:19:42 +0600423 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i += t_loc->wide ? 2 : 1) {
424 // get reg corresponding to input
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000425 RegStorage reg = in_to_reg_storage_mapping_.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600426 t_loc = &ArgLocs[i];
427
428 // If the wide input appeared as single, flush it and go
429 // as it comes from memory.
430 if (t_loc->wide && reg.Valid() && !reg.Is64Bit()) {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000431 // The memory already holds the half. Don't do anything.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600432 reg = RegStorage::InvalidReg();
433 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800434
buzbee2700f7e2014-03-07 09:46:20 -0800435 if (reg.Valid()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600436 // If arriving in register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437
Serguei Katkov717a3e42014-11-13 17:19:42 +0600438 // We have already updated the arg location with promoted info
439 // so we can be based on it.
440 if (t_loc->location == kLocPhysReg) {
441 // Just copy it.
442 if (t_loc->wide) {
443 OpRegCopyWide(t_loc->reg, reg);
444 } else {
445 OpRegCopy(t_loc->reg, reg);
446 }
447 } else {
448 // Needs flush.
449 int offset = SRegOffset(start_vreg + i);
450 if (t_loc->ref) {
451 StoreRefDisp(TargetPtrReg(kSp), offset, reg, kNotVolatile);
452 } else {
453 StoreBaseDisp(TargetPtrReg(kSp), offset, reg, t_loc->wide ? k64 : k32, kNotVolatile);
buzbeed0a03b82013-09-14 08:21:05 -0700454 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 } else {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600457 // If arriving in frame & promoted.
458 if (t_loc->location == kLocPhysReg) {
459 int offset = SRegOffset(start_vreg + i);
460 if (t_loc->ref) {
461 LoadRefDisp(TargetPtrReg(kSp), offset, t_loc->reg, kNotVolatile);
462 } else {
463 LoadBaseDisp(TargetPtrReg(kSp), offset, t_loc->reg, t_loc->wide ? k64 : k32,
464 kNotVolatile);
465 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 }
467 }
468 }
469}
470
Andreas Gampeccc60262014-07-04 18:02:38 -0700471static void CommonCallCodeLoadThisIntoArg1(const CallInfo* info, Mir2Lir* cg) {
472 RegLocation rl_arg = info->args[0];
473 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1, kRef));
474}
475
476static void CommonCallCodeLoadClassIntoArg0(const CallInfo* info, Mir2Lir* cg) {
477 cg->GenNullCheck(cg->TargetReg(kArg1, kRef), info->opt_flags);
478 // get this->klass_ [use kArg1, set kArg0]
479 cg->LoadRefDisp(cg->TargetReg(kArg1, kRef), mirror::Object::ClassOffset().Int32Value(),
480 cg->TargetReg(kArg0, kRef),
481 kNotVolatile);
482 cg->MarkPossibleNullPointerException(info->opt_flags);
483}
484
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700485static bool CommonCallCodeLoadCodePointerIntoInvokeTgt(const RegStorage* alt_from,
Andreas Gampeccc60262014-07-04 18:02:38 -0700486 const CompilationUnit* cu, Mir2Lir* cg) {
487 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Mathieu Chartier2d721012014-11-10 11:08:06 -0800488 int32_t offset = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
489 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
Andreas Gampeccc60262014-07-04 18:02:38 -0700490 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
Mathieu Chartier2d721012014-11-10 11:08:06 -0800491 cg->LoadWordDisp(alt_from == nullptr ? cg->TargetReg(kArg0, kRef) : *alt_from, offset,
Andreas Gampeccc60262014-07-04 18:02:38 -0700492 cg->TargetPtrReg(kInvokeTgt));
493 return true;
494 }
495 return false;
496}
497
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498/*
499 * Bit of a hack here - in the absence of a real scheduling pass,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 * emit the next instruction in a virtual invoke sequence.
501 * We can use kLr as a temp prior to target address loading
502 * Note also that we'll load the first argument ("this") into
Serguei Katkov717a3e42014-11-13 17:19:42 +0600503 * kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 */
505static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
506 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700507 uint32_t method_idx, uintptr_t, uintptr_t,
508 InvokeType) {
509 UNUSED(target_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
511 /*
512 * This is the fast path in which the target virtual method is
513 * fully resolved at compile time.
514 */
515 switch (state) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700516 case 0:
517 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700519 case 1:
520 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
521 // Includes a null-check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700523 case 2: {
524 // Get this->klass_.embedded_vtable[method_idx] [usr kArg0, set kArg0]
525 int32_t offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
526 method_idx * sizeof(mirror::Class::VTableEntry);
527 // Load target method from embedded vtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700528 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700530 }
531 case 3:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700532 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700533 break; // kInvokeTgt := kArg0->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700535 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
536 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 default:
538 return -1;
539 }
540 return state + 1;
541}
542
543/*
Jeff Hao88474b42013-10-23 16:24:40 -0700544 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
545 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
546 * more than one interface method map to the same index. Note also that we'll load the first
Serguei Katkov717a3e42014-11-13 17:19:42 +0600547 * argument ("this") into kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 */
549static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
550 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700551 uint32_t method_idx, uintptr_t, uintptr_t, InvokeType) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553
Jeff Hao88474b42013-10-23 16:24:40 -0700554 switch (state) {
555 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700556 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Andreas Gampeccc60262014-07-04 18:02:38 -0700557 cg->LoadConstant(cg->TargetReg(kHiddenArg, kNotWide), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400558 if (cu->instruction_set == kX86) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700559 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, kNotWide), cg->TargetReg(kHiddenArg, kNotWide));
Jeff Hao88474b42013-10-23 16:24:40 -0700560 }
561 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700562 case 1:
563 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Jeff Hao88474b42013-10-23 16:24:40 -0700564 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700565 case 2:
566 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
567 // Includes a null-check.
Jeff Hao88474b42013-10-23 16:24:40 -0700568 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700569 case 3: { // Get target method [use kInvokeTgt, set kArg0]
570 int32_t offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
571 (method_idx % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
572 // Load target method from embedded imtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700573 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700574 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700575 }
576 case 4:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700577 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700578 break; // kInvokeTgt := kArg0->entrypoint
Jeff Hao88474b42013-10-23 16:24:40 -0700579 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700580 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
581 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 default:
583 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 }
585 return state + 1;
586}
587
Andreas Gampeccc60262014-07-04 18:02:38 -0700588static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info,
Andreas Gampe98430592014-07-27 19:44:50 -0700589 QuickEntrypointEnum trampoline, int state,
Andreas Gampeccc60262014-07-04 18:02:38 -0700590 const MethodReference& target_method, uint32_t method_idx) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700591 UNUSED(info, method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Andreas Gampe98430592014-07-27 19:44:50 -0700593
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 /*
595 * This handles the case in which the base method is not fully
596 * resolved at compile time, we bail to a runtime helper.
597 */
598 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700599 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 // Load trampoline target
Andreas Gampe98430592014-07-27 19:44:50 -0700601 int32_t disp;
602 if (cu->target64) {
603 disp = GetThreadOffset<8>(trampoline).Int32Value();
604 } else {
605 disp = GetThreadOffset<4>(trampoline).Int32Value();
606 }
607 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), disp, cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 }
609 // Load kArg0 with method index
610 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampeccc60262014-07-04 18:02:38 -0700611 cg->LoadConstant(cg->TargetReg(kArg0, kNotWide), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 return 1;
613 }
614 return -1;
615}
616
617static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
618 int state,
619 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700620 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700621 return NextInvokeInsnSP(cu, info, kQuickInvokeStaticTrampolineWithAccessCheck, state,
622 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623}
624
625static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
626 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700627 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700628 return NextInvokeInsnSP(cu, info, kQuickInvokeDirectTrampolineWithAccessCheck, state,
629 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630}
631
632static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
633 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700634 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700635 return NextInvokeInsnSP(cu, info, kQuickInvokeSuperTrampolineWithAccessCheck, state,
636 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637}
638
639static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
640 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700641 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700642 return NextInvokeInsnSP(cu, info, kQuickInvokeVirtualTrampolineWithAccessCheck, state,
643 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644}
645
646static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
647 CallInfo* info, int state,
648 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700649 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700650 return NextInvokeInsnSP(cu, info, kQuickInvokeInterfaceTrampolineWithAccessCheck, state,
651 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652}
653
Dave Allison69dfe512014-07-11 17:11:58 +0000654// Default implementation of implicit null pointer check.
655// Overridden by arch specific as necessary.
656void Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
657 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
658 return;
659 }
660 RegStorage tmp = AllocTemp();
661 Load32Disp(reg, 0, tmp);
662 MarkPossibleNullPointerException(opt_flags);
663 FreeTemp(tmp);
664}
665
Serguei Katkov717a3e42014-11-13 17:19:42 +0600666/**
667 * @brief Used to flush promoted registers if they are used as argument
668 * in an invocation.
669 * @param info the infromation about arguments for invocation.
670 * @param start the first argument we should start to look from.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 */
Serguei Katkov717a3e42014-11-13 17:19:42 +0600672void Mir2Lir::GenDalvikArgsFlushPromoted(CallInfo* info, int start) {
673 if (cu_->disable_opt & (1 << kPromoteRegs)) {
674 // This make sense only if promotion is enabled.
675 return;
676 }
677 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 // Scan the rest of the args - if in phys_reg flush to memory
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000679 for (size_t next_arg = start; next_arg < info->num_arg_words;) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 RegLocation loc = info->args[next_arg];
681 if (loc.wide) {
682 loc = UpdateLocWide(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600683 if (loc.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700684 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 }
686 next_arg += 2;
687 } else {
688 loc = UpdateLoc(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600689 if (loc.location == kLocPhysReg) {
690 if (loc.ref) {
691 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
692 } else {
693 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k32,
694 kNotVolatile);
695 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 }
697 next_arg++;
698 }
699 }
Serguei Katkov717a3e42014-11-13 17:19:42 +0600700}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701
Serguei Katkov717a3e42014-11-13 17:19:42 +0600702/**
703 * @brief Used to optimize the copying of VRs which are arguments of invocation.
704 * Please note that you should flush promoted registers first if you copy.
705 * If implementation does copying it may skip several of the first VRs but must copy
706 * till the end. Implementation must return the number of skipped VRs
707 * (it might be all VRs).
708 * @see GenDalvikArgsFlushPromoted
709 * @param info the information about arguments for invocation.
710 * @param first the first argument we should start to look from.
711 * @param count the number of remaining arguments we can handle.
712 * @return the number of arguments which we did not handle. Unhandled arguments
713 * must be attached to the first one.
714 */
715int Mir2Lir::GenDalvikArgsBulkCopy(CallInfo* info, int first, int count) {
716 // call is pretty expensive, let's use it if count is big.
717 if (count > 16) {
718 GenDalvikArgsFlushPromoted(info, first);
719 int start_offset = SRegOffset(info->args[first].s_reg_low);
720 int outs_offset = StackVisitor::GetOutVROffset(first, cu_->instruction_set);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800721
Andreas Gampeccc60262014-07-04 18:02:38 -0700722 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
723 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -0700724 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +0600725 count * 4, false);
726 count = 0;
727 }
728 return count;
729}
730
731int Mir2Lir::GenDalvikArgs(CallInfo* info, int call_state,
732 LIR** pcrLabel, NextCallInsn next_call_insn,
733 const MethodReference& target_method,
734 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
735 InvokeType type, bool skip_this) {
736 // If no arguments, just return.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000737 if (info->num_arg_words == 0u)
Serguei Katkov717a3e42014-11-13 17:19:42 +0600738 return call_state;
739
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000740 const size_t start_index = skip_this ? 1 : 0;
Serguei Katkov717a3e42014-11-13 17:19:42 +0600741
742 // Get architecture dependent mapping between output VRs and physical registers
743 // basing on shorty of method to call.
744 InToRegStorageMapping in_to_reg_storage_mapping(arena_);
745 {
746 const char* target_shorty = mir_graph_->GetShortyFromMethodReference(target_method);
747 ShortyIterator shorty_iterator(target_shorty, type == kStatic);
748 in_to_reg_storage_mapping.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 }
750
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000751 size_t stack_map_start = std::max(in_to_reg_storage_mapping.GetEndMappedIn(), start_index);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600752 if ((stack_map_start < info->num_arg_words) && info->args[stack_map_start].high_word) {
753 // It is possible that the last mapped reg is 32 bit while arg is 64-bit.
754 // It will be handled together with low part mapped to register.
755 stack_map_start++;
756 }
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000757 size_t regs_left_to_pass_via_stack = info->num_arg_words - stack_map_start;
Serguei Katkov717a3e42014-11-13 17:19:42 +0600758
759 // If it is a range case we can try to copy remaining VRs (not mapped to physical registers)
760 // using more optimal algorithm.
761 if (info->is_range && regs_left_to_pass_via_stack > 1) {
762 regs_left_to_pass_via_stack = GenDalvikArgsBulkCopy(info, stack_map_start,
763 regs_left_to_pass_via_stack);
764 }
765
766 // Now handle any remaining VRs mapped to stack.
767 if (in_to_reg_storage_mapping.HasArgumentsOnStack()) {
768 // Two temps but do not use kArg1, it might be this which we can skip.
769 // Separate single and wide - it can give some advantage.
770 RegStorage regRef = TargetReg(kArg3, kRef);
771 RegStorage regSingle = TargetReg(kArg3, kNotWide);
772 RegStorage regWide = TargetReg(kArg2, kWide);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000773 for (size_t i = start_index; i < stack_map_start + regs_left_to_pass_via_stack; i++) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600774 RegLocation rl_arg = info->args[i];
775 rl_arg = UpdateRawLoc(rl_arg);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000776 RegStorage reg = in_to_reg_storage_mapping.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600777 if (!reg.Valid()) {
778 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
779 {
780 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
781 if (rl_arg.wide) {
782 if (rl_arg.location == kLocPhysReg) {
783 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
784 } else {
785 LoadValueDirectWideFixed(rl_arg, regWide);
786 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regWide, k64, kNotVolatile);
787 }
788 } else {
789 if (rl_arg.location == kLocPhysReg) {
790 if (rl_arg.ref) {
791 StoreRefDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, kNotVolatile);
792 } else {
793 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k32, kNotVolatile);
794 }
795 } else {
796 if (rl_arg.ref) {
797 LoadValueDirectFixed(rl_arg, regRef);
798 StoreRefDisp(TargetPtrReg(kSp), out_offset, regRef, kNotVolatile);
799 } else {
800 LoadValueDirectFixed(rl_arg, regSingle);
801 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regSingle, k32, kNotVolatile);
802 }
803 }
804 }
805 }
806 call_state = next_call_insn(cu_, info, call_state, target_method,
807 vtable_idx, direct_code, direct_method, type);
808 }
809 if (rl_arg.wide) {
810 i++;
811 }
812 }
813 }
814
815 // Finish with VRs mapped to physical registers.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000816 for (size_t i = start_index; i < stack_map_start; i++) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600817 RegLocation rl_arg = info->args[i];
818 rl_arg = UpdateRawLoc(rl_arg);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000819 RegStorage reg = in_to_reg_storage_mapping.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600820 if (reg.Valid()) {
821 if (rl_arg.wide) {
822 // if reg is not 64-bit (it is half of 64-bit) then handle it separately.
823 if (!reg.Is64Bit()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600824 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
825 if (rl_arg.location == kLocPhysReg) {
826 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000827 // Dump it to memory.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600828 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
829 LoadBaseDisp(TargetPtrReg(kSp), out_offset, reg, k32, kNotVolatile);
830 } else {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000831 int high_offset = StackVisitor::GetOutVROffset(i + 1, cu_->instruction_set);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600832 // First, use target reg for high part.
833 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low + 1), reg, k32,
834 kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000835 StoreBaseDisp(TargetPtrReg(kSp), high_offset, reg, k32, kNotVolatile);
836 // Now, use target reg for low part.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600837 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low), reg, k32, kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000838 int low_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
839 // And store it to the expected memory location.
840 StoreBaseDisp(TargetPtrReg(kSp), low_offset, reg, k32, kNotVolatile);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600841 }
842 } else {
843 LoadValueDirectWideFixed(rl_arg, reg);
844 }
845 } else {
846 LoadValueDirectFixed(rl_arg, reg);
847 }
848 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
849 direct_code, direct_method, type);
850 }
851 if (rl_arg.wide) {
852 i++;
853 }
854 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855
856 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
857 direct_code, direct_method, type);
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 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700864 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 }
866 return call_state;
867}
868
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000869void Mir2Lir::EnsureInitializedArgMappingToPhysicalReg() {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600870 if (!in_to_reg_storage_mapping_.IsInitialized()) {
871 ShortyIterator shorty_iterator(cu_->shorty, cu_->invoke_type == kStatic);
872 in_to_reg_storage_mapping_.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
873 }
Serguei Katkov717a3e42014-11-13 17:19:42 +0600874}
875
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700876RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 RegLocation res;
878 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700879 // If result is unused, return a sink target based on type of invoke target.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800880 res = GetReturn(
881 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 } else {
883 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700884 DCHECK_EQ(LocToRegClass(res),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800885 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 }
887 return res;
888}
889
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700890RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 RegLocation res;
892 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700893 // If result is unused, return a sink target based on type of invoke target.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800894 res = GetReturnWide(ShortyToRegClass(
895 mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 } else {
897 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700898 DCHECK_EQ(LocToRegClass(res),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800899 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 }
901 return res;
902}
903
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -0700904bool Mir2Lir::GenInlinedReferenceGetReferent(CallInfo* info) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100905 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
906 // TODO: add Mips and Mips64 implementations.
Fred Shih4ee7a662014-07-11 09:59:27 -0700907 return false;
908 }
909
Fred Shih4ee7a662014-07-11 09:59:27 -0700910 bool use_direct_type_ptr;
911 uintptr_t direct_type_ptr;
Fred Shihe7f82e22014-08-06 10:46:37 -0700912 ClassReference ref;
913 if (!cu_->compiler_driver->CanEmbedReferenceTypeInCode(&ref,
914 &use_direct_type_ptr, &direct_type_ptr)) {
915 return false;
916 }
917
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700918 RegStorage reg_class = TargetReg(kArg1, kRef);
919 Clobber(reg_class);
920 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -0700921 if (use_direct_type_ptr) {
922 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -0700923 } else {
Fred Shihe7f82e22014-08-06 10:46:37 -0700924 uint16_t type_idx = ref.first->GetClassDef(ref.second).class_idx_;
925 LoadClassType(*ref.first, type_idx, kArg1);
Fred Shih4ee7a662014-07-11 09:59:27 -0700926 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700927
Fred Shihe7f82e22014-08-06 10:46:37 -0700928 uint32_t slow_path_flag_offset = cu_->compiler_driver->GetReferenceSlowFlagOffset();
929 uint32_t disable_flag_offset = cu_->compiler_driver->GetReferenceDisableFlagOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700930 CHECK(slow_path_flag_offset && disable_flag_offset &&
931 (slow_path_flag_offset != disable_flag_offset));
932
933 // intrinsic logic start.
934 RegLocation rl_obj = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -0700935 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih4ee7a662014-07-11 09:59:27 -0700936
937 RegStorage reg_slow_path = AllocTemp();
938 RegStorage reg_disabled = AllocTemp();
Andreas Gampef6815702015-01-20 09:53:48 -0800939 LoadBaseDisp(reg_class, slow_path_flag_offset, reg_slow_path, kSignedByte, kNotVolatile);
940 LoadBaseDisp(reg_class, disable_flag_offset, reg_disabled, kSignedByte, kNotVolatile);
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700941 FreeTemp(reg_class);
942 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -0700943 FreeTemp(reg_disabled);
944
945 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700946 LIR* slow_path_branch;
947 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
948 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
949 slow_path_branch = OpCondBranch(kCondNe, nullptr);
950 } else {
951 // Generate compare and branch.
952 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
953 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700954 FreeTemp(reg_slow_path);
955
956 // slow path not enabled, simply load the referent of the reference object
957 RegLocation rl_dest = InlineTarget(info);
958 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
959 GenNullCheck(rl_obj.reg, info->opt_flags);
960 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
961 kNotVolatile);
962 MarkPossibleNullPointerException(info->opt_flags);
963 StoreValue(rl_dest, rl_result);
964
965 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
966 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
Serguei Katkov9863daf2014-09-04 15:21:32 +0700967 ClobberCallerSave(); // We must clobber everything because slow path will return here
Fred Shih4ee7a662014-07-11 09:59:27 -0700968 return true;
969}
970
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700971bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972 // Location of reference to data array
973 int value_offset = mirror::String::ValueOffset().Int32Value();
974 // Location of count
975 int count_offset = mirror::String::CountOffset().Int32Value();
976 // Starting offset within data array
977 int offset_offset = mirror::String::OffsetOffset().Int32Value();
978 // Start of char data with array_
979 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
980
981 RegLocation rl_obj = info->args[0];
982 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -0700983 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -0700984 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800985 RegStorage reg_max;
986 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +0000988 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -0800989 RegStorage reg_off;
990 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -0700991 reg_off = AllocTemp();
992 reg_ptr = AllocTempRef();
993 if (range_check) {
994 reg_max = AllocTemp();
995 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -0800996 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 }
Andreas Gampe98430592014-07-27 19:44:50 -0700998 Load32Disp(rl_obj.reg, offset_offset, reg_off);
999 MarkPossibleNullPointerException(info->opt_flags);
1000 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
1001 if (range_check) {
1002 // Set up a slow path to allow retry in case of bounds violation */
1003 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
1004 FreeTemp(reg_max);
1005 range_check_branch = OpCondBranch(kCondUge, nullptr);
1006 }
1007 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001008 if (rl_idx.is_const) {
1009 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1010 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001011 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001012 }
buzbee2700f7e2014-03-07 09:46:20 -08001013 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001014 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001015 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001016 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 RegLocation rl_dest = InlineTarget(info);
1018 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001019 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 FreeTemp(reg_off);
1021 FreeTemp(reg_ptr);
1022 StoreValue(rl_dest, rl_result);
1023 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001024 DCHECK(range_check_branch != nullptr);
1025 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001026 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 return true;
1029}
1030
1031// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001032bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001033 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1034 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 return false;
1036 }
1037 // dst = src.length();
1038 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001039 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 RegLocation rl_dest = InlineTarget(info);
1041 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001042 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001043 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001044 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 if (is_empty) {
1046 // dst = (dst == 0);
1047 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001048 RegStorage t_reg = AllocTemp();
1049 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1050 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001051 } else if (cu_->instruction_set == kArm64) {
1052 OpRegImm(kOpSub, rl_result.reg, 1);
1053 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001055 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001056 OpRegImm(kOpSub, rl_result.reg, 1);
1057 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 }
1059 }
1060 StoreValue(rl_dest, rl_result);
1061 return true;
1062}
1063
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001064bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001065 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1066 // TODO: add Mips and Mips64 implementations.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001067 return false;
1068 }
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001069 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
1070 if (rl_dest.s_reg_low == INVALID_SREG) {
1071 // Result is unused, the code is dead. Inlining successful, no code generated.
1072 return true;
1073 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001074 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001075 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001076 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001077 if (IsWide(size)) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001078 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001079 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1080 StoreValueWide(rl_dest, rl_result);
1081 return true;
1082 }
buzbee2700f7e2014-03-07 09:46:20 -08001083 RegStorage r_i_low = rl_i.reg.GetLow();
1084 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001085 // 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 +00001086 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001087 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001088 }
buzbee2700f7e2014-03-07 09:46:20 -08001089 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1090 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1091 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001092 FreeTemp(r_i_low);
1093 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001094 StoreValueWide(rl_dest, rl_result);
1095 } else {
buzbee695d13a2014-04-19 13:32:20 -07001096 DCHECK(size == k32 || size == kSignedHalf);
1097 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001098 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001099 StoreValue(rl_dest, rl_result);
1100 }
1101 return true;
1102}
1103
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001104bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001105 RegLocation rl_dest = InlineTarget(info);
1106 if (rl_dest.s_reg_low == INVALID_SREG) {
1107 // Result is unused, the code is dead. Inlining successful, no code generated.
1108 return true;
1109 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 RegLocation rl_src = info->args[0];
1111 rl_src = LoadValue(rl_src, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001113 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001115 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1116 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1117 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 StoreValue(rl_dest, rl_result);
1119 return true;
1120}
1121
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001122bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001123 RegLocation rl_dest = InlineTargetWide(info);
1124 if (rl_dest.s_reg_low == INVALID_SREG) {
1125 // Result is unused, the code is dead. Inlining successful, no code generated.
1126 return true;
1127 }
Vladimir Markob9823312014-03-20 17:38:43 +00001128 RegLocation rl_src = info->args[0];
1129 rl_src = LoadValueWide(rl_src, kCoreReg);
Vladimir Markob9823312014-03-20 17:38:43 +00001130 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1131
1132 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001133 if (cu_->instruction_set != kX86_64 &&
1134 (cu_->instruction_set == kX86 ||
1135 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001136 OpRegCopyWide(rl_result.reg, rl_src.reg);
1137 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1138 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1139 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001140 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1141 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001142 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001143 }
1144 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 }
Vladimir Markob9823312014-03-20 17:38:43 +00001146
1147 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001148 RegStorage sign_reg;
1149 if (cu_->instruction_set == kX86_64) {
1150 sign_reg = AllocTempWide();
1151 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1152 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1153 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1154 } else {
1155 sign_reg = AllocTemp();
1156 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1157 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1158 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1159 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1160 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1161 }
buzbee082833c2014-05-17 23:16:26 -07001162 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001163 StoreValueWide(rl_dest, rl_result);
1164 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165}
1166
Serban Constantinescu23abec92014-07-02 16:13:38 +01001167bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001168 // Currently implemented only for ARM64.
1169 UNUSED(info, size);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001170 return false;
1171}
1172
1173bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001174 // Currently implemented only for ARM64.
1175 UNUSED(info, is_min, is_double);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001176 return false;
1177}
1178
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001179bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001180 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001181 return false;
1182}
1183
1184bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001185 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001186 return false;
1187}
1188
1189bool Mir2Lir::GenInlinedRint(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001190 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001191 return false;
1192}
1193
1194bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001195 UNUSED(info, is_double);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001196 return false;
1197}
1198
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001199bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001200 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1201 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 return false;
1203 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 RegLocation rl_dest = InlineTarget(info);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001205 if (rl_dest.s_reg_low == INVALID_SREG) {
1206 // Result is unused, the code is dead. Inlining successful, no code generated.
1207 return true;
1208 }
1209 RegLocation rl_src = info->args[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 StoreValue(rl_dest, rl_src);
1211 return true;
1212}
1213
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001214bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001215 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1216 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 return false;
1218 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 RegLocation rl_dest = InlineTargetWide(info);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001220 if (rl_dest.s_reg_low == INVALID_SREG) {
1221 // Result is unused, the code is dead. Inlining successful, no code generated.
1222 return true;
1223 }
1224 RegLocation rl_src = info->args[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 StoreValueWide(rl_dest, rl_src);
1226 return true;
1227}
1228
DaniilSokolov70c4f062014-06-24 17:34:00 -07001229bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001230 UNUSED(info);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001231 return false;
1232}
1233
1234
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001236 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 * otherwise bails to standard library code.
1238 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001239bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001240 RegLocation rl_obj = info->args[0];
1241 RegLocation rl_char = info->args[1];
1242 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1243 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1244 return false;
1245 }
1246
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001247 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001249 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1250 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1251 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 LoadValueDirectFixed(rl_obj, reg_ptr);
1254 LoadValueDirectFixed(rl_char, reg_char);
1255 if (zero_based) {
1256 LoadConstant(reg_start, 0);
1257 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001258 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 LoadValueDirectFixed(rl_start, reg_start);
1260 }
Andreas Gampe98430592014-07-27 19:44:50 -07001261 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001262 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001263 LIR* high_code_point_branch =
1264 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001266 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001267 if (!rl_char.is_const) {
1268 // Add the slow path for code points beyond 0xFFFF.
1269 DCHECK(high_code_point_branch != nullptr);
1270 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1271 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001272 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001273 ClobberCallerSave(); // We must clobber everything because slow path will return here
Vladimir Marko3bc86152014-03-13 14:11:28 +00001274 } else {
1275 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1276 DCHECK(high_code_point_branch == nullptr);
1277 }
buzbeea0cd2d72014-06-01 09:33:49 -07001278 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 RegLocation rl_dest = InlineTarget(info);
1280 StoreValue(rl_dest, rl_return);
1281 return true;
1282}
1283
1284/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001285bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001286 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1287 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 return false;
1289 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001290 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001292 RegStorage reg_this = TargetReg(kArg0, kRef);
1293 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294
1295 RegLocation rl_this = info->args[0];
1296 RegLocation rl_cmp = info->args[1];
1297 LoadValueDirectFixed(rl_this, reg_this);
1298 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001299 RegStorage r_tgt;
1300 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001301 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001302 } else {
1303 r_tgt = RegStorage::InvalidReg();
1304 }
Dave Allisonf9439142014-03-27 15:10:22 -07001305 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001306 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001307 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001308 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001309 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001311 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001312 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 RegLocation rl_dest = InlineTarget(info);
1314 StoreValue(rl_dest, rl_return);
1315 return true;
1316}
1317
1318bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1319 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001320
1321 // Early exit if the result is unused.
1322 if (rl_dest.orig_sreg < 0) {
1323 return true;
1324 }
1325
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001326 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001327
Andreas Gamped500b532015-01-16 22:09:55 -08001328 if (Is64BitInstructionSet(cu_->instruction_set)) {
1329 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1330 kNotVolatile);
1331 } else {
1332 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 }
Andreas Gamped500b532015-01-16 22:09:55 -08001334
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 StoreValue(rl_dest, rl_result);
1336 return true;
1337}
1338
1339bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1340 bool is_long, bool is_volatile) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001341 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1342 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 return false;
1344 }
1345 // Unused - RegLocation rl_src_unsafe = info->args[0];
1346 RegLocation rl_src_obj = info->args[1]; // Object
1347 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001348 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001349 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001350
buzbeea0cd2d72014-06-01 09:33:49 -07001351 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001353 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001355 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1356 || cu_->instruction_set == kArm64) {
1357 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001358 } else {
1359 RegStorage rl_temp_offset = AllocTemp();
1360 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001361 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001362 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001363 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001365 if (rl_result.ref) {
1366 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1367 } else {
1368 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1369 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001370 }
1371
1372 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001373 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001374 }
1375
1376 if (is_long) {
1377 StoreValueWide(rl_dest, rl_result);
1378 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001379 StoreValue(rl_dest, rl_result);
1380 }
1381 return true;
1382}
1383
1384bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1385 bool is_object, bool is_volatile, bool is_ordered) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001386 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1387 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 return false;
1389 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390 // Unused - RegLocation rl_src_unsafe = info->args[0];
1391 RegLocation rl_src_obj = info->args[1]; // Object
1392 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001393 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 RegLocation rl_src_value = info->args[4]; // value to store
1395 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001396 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 }
buzbeea0cd2d72014-06-01 09:33:49 -07001398 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1400 RegLocation rl_value;
1401 if (is_long) {
1402 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001403 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1404 || cu_->instruction_set == kArm64) {
1405 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001406 } else {
1407 RegStorage rl_temp_offset = AllocTemp();
1408 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001409 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001410 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001411 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 } else {
buzbee7c02e912014-10-03 13:14:17 -07001413 rl_value = LoadValue(rl_src_value, LocToRegClass(rl_src_value));
Matteo Franchin255e0142014-07-04 13:50:41 +01001414 if (rl_value.ref) {
1415 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1416 } else {
1417 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1418 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001420
1421 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001422 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001423
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001425 // Prevent reordering with a subsequent volatile load.
1426 // May also be needed to address store atomicity issues.
1427 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 }
1429 if (is_object) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001430 MarkGCCard(0, rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431 }
1432 return true;
1433}
1434
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001435void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001436 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001437 const DexFile* dex_file = info->method_ref.dex_file;
1438 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(dex_file)
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001439 ->GenIntrinsic(this, info)) {
1440 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001442 GenInvokeNoInline(info);
1443}
1444
1445void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446 int call_state = 0;
1447 LIR* null_ck;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001448 LIR** p_null_ck = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001449 NextCallInsn next_call_insn;
1450 FlushAllRegs(); /* Everything to home location */
1451 // Explicit register usage
1452 LockCallTemps();
1453
Vladimir Markof096aad2014-01-23 15:51:58 +00001454 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1455 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1456 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
Vladimir Markof4da6752014-08-01 19:04:18 +01001457 info->type = method_info.GetSharpType();
Vladimir Markof096aad2014-01-23 15:51:58 +00001458 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 bool skip_this;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001460
Brian Carlstrom7940e442013-07-12 13:46:57 -07001461 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001462 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001463 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 } else if (info->type == kDirect) {
1465 if (fast_path) {
1466 p_null_ck = &null_ck;
1467 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001468 next_call_insn = fast_path ? GetNextSDCallInsn() : NextDirectCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 skip_this = false;
1470 } else if (info->type == kStatic) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001471 next_call_insn = fast_path ? GetNextSDCallInsn() : NextStaticCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 skip_this = false;
1473 } else if (info->type == kSuper) {
1474 DCHECK(!fast_path); // Fast path is a direct call.
1475 next_call_insn = NextSuperCallInsnSP;
1476 skip_this = false;
1477 } else {
1478 DCHECK_EQ(info->type, kVirtual);
1479 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1480 skip_this = fast_path;
1481 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001482 MethodReference target_method = method_info.GetTargetMethod();
Serguei Katkov717a3e42014-11-13 17:19:42 +06001483 call_state = GenDalvikArgs(info, call_state, p_null_ck,
1484 next_call_insn, target_method, method_info.VTableIndex(),
1485 method_info.DirectCode(), method_info.DirectMethod(),
1486 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 // Finish up any of the call sequence not interleaved in arg loading
1488 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001489 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001490 method_info.DirectCode(), method_info.DirectMethod(),
1491 original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001493 LIR* call_insn = GenCallInsn(method_info);
Vladimir Markof4da6752014-08-01 19:04:18 +01001494 MarkSafepointPC(call_insn);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495
Vladimir Markobfe400b2014-12-19 19:27:26 +00001496 FreeCallTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 if (info->result.location != kLocInvalid) {
1498 // We have a following MOVE_RESULT - do it now.
1499 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001500 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 StoreValueWide(info->result, ret_loc);
1502 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001503 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 StoreValue(info->result, ret_loc);
1505 }
1506 }
1507}
1508
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509} // namespace art