blob: 6b553fd181c80efebaedba864d69ee13644d96c4 [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)
51 : LIRSlowPath(m2l, info_in->offset, branch_in, resume_in), info_(info_in) {
Vladimir Marko3bc86152014-03-13 14:11:28 +000052 }
53
54 void Compile() {
55 m2l_->ResetRegPool();
56 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070057 GenerateTargetLabel(kPseudoIntrinsicRetry);
Vladimir Marko3bc86152014-03-13 14:11:28 +000058 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
59 m2l_->GenInvokeNoInline(info_);
60 if (cont_ != nullptr) {
61 m2l_->OpUnconditionalBranch(cont_);
62 }
63 }
64
65 private:
66 CallInfo* const info_;
67 };
68
Mingyao Yang3a74d152014-04-21 15:39:44 -070069 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000070}
71
Brian Carlstrom7940e442013-07-12 13:46:57 -070072/*
73 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000074 * the helper target address, and the actual call to the helper. Because x86
75 * has a memory call operation, part 1 is a NOP for x86. For other targets,
76 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 */
Andreas Gampe2f244e92014-05-08 03:35:25 -070078// template <size_t pointer_size>
Andreas Gampe98430592014-07-27 19:44:50 -070079RegStorage Mir2Lir::CallHelperSetup(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070080 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
81 return RegStorage::InvalidReg();
82 } else {
Andreas Gampe98430592014-07-27 19:44:50 -070083 return LoadHelper(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070084 }
85}
86
Andreas Gampe98430592014-07-27 19:44:50 -070087LIR* Mir2Lir::CallHelper(RegStorage r_tgt, QuickEntrypointEnum trampoline, bool safepoint_pc,
88 bool use_link) {
89 LIR* call_inst = InvokeTrampoline(use_link ? kOpBlx : kOpBx, r_tgt, trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070090
Andreas Gampe98430592014-07-27 19:44:50 -070091 if (r_tgt.Valid()) {
Dave Allisond6ed6422014-04-09 23:36:15 +000092 FreeTemp(r_tgt);
93 }
Andreas Gampe98430592014-07-27 19:44:50 -070094
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 if (safepoint_pc) {
96 MarkSafepointPC(call_inst);
97 }
98 return call_inst;
99}
100
Andreas Gampe98430592014-07-27 19:44:50 -0700101void Mir2Lir::CallRuntimeHelper(QuickEntrypointEnum trampoline, bool safepoint_pc) {
102 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang42894562014-04-07 12:42:16 -0700103 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700104 CallHelper(r_tgt, trampoline, safepoint_pc);
Mingyao Yang42894562014-04-07 12:42:16 -0700105}
106
Andreas Gampe98430592014-07-27 19:44:50 -0700107void Mir2Lir::CallRuntimeHelperImm(QuickEntrypointEnum trampoline, int arg0, bool safepoint_pc) {
108 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700109 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000110 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700111 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112}
113
Andreas Gampe98430592014-07-27 19:44:50 -0700114void Mir2Lir::CallRuntimeHelperReg(QuickEntrypointEnum trampoline, RegStorage arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700115 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700116 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700117 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000118 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700119 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120}
121
Andreas Gampe98430592014-07-27 19:44:50 -0700122void Mir2Lir::CallRuntimeHelperRegLocation(QuickEntrypointEnum trampoline, RegLocation arg0,
123 bool safepoint_pc) {
124 RegStorage r_tgt = CallHelperSetup(trampoline);
buzbee2700f7e2014-03-07 09:46:20 -0800125 if (arg0.wide == 0) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700126 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, arg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700128 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000130 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700131 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132}
133
Andreas Gampe98430592014-07-27 19:44:50 -0700134void Mir2Lir::CallRuntimeHelperImmImm(QuickEntrypointEnum trampoline, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700136 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700137 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
138 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000139 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700140 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141}
142
Andreas Gampe98430592014-07-27 19:44:50 -0700143void Mir2Lir::CallRuntimeHelperImmRegLocation(QuickEntrypointEnum trampoline, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700145 RegStorage r_tgt = CallHelperSetup(trampoline);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 if (arg1.wide == 0) {
Andreas Gampef9872f02014-07-01 19:00:09 -0700147 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700149 RegStorage r_tmp = TargetReg(cu_->instruction_set == kMips ? kArg2 : kArg1, kWide);
buzbee2700f7e2014-03-07 09:46:20 -0800150 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700152 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000153 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700154 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155}
156
Andreas Gampe98430592014-07-27 19:44:50 -0700157void Mir2Lir::CallRuntimeHelperRegLocationImm(QuickEntrypointEnum trampoline, RegLocation arg0,
158 int arg1, bool safepoint_pc) {
159 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampef9872f02014-07-01 19:00:09 -0700160 DCHECK(!arg0.wide);
161 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700162 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000163 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700164 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165}
166
Andreas Gampe98430592014-07-27 19:44:50 -0700167void Mir2Lir::CallRuntimeHelperImmReg(QuickEntrypointEnum trampoline, int arg0, RegStorage arg1,
168 bool safepoint_pc) {
169 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700170 OpRegCopy(TargetReg(kArg1, arg1.GetWideKind()), arg1);
171 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000172 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700173 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174}
175
Andreas Gampe98430592014-07-27 19:44:50 -0700176void Mir2Lir::CallRuntimeHelperRegImm(QuickEntrypointEnum trampoline, RegStorage arg0, int arg1,
177 bool safepoint_pc) {
178 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700179 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
180 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000181 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700182 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183}
184
Andreas Gampe98430592014-07-27 19:44:50 -0700185void Mir2Lir::CallRuntimeHelperImmMethod(QuickEntrypointEnum trampoline, int arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700186 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700187 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700188 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
189 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000190 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700191 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192}
193
Andreas Gampe98430592014-07-27 19:44:50 -0700194void Mir2Lir::CallRuntimeHelperRegMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800195 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700196 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700197 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
198 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
199 if (r_tmp.NotExactlyEquals(arg0)) {
200 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800201 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700202 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800203 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700204 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800205}
206
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800207void Mir2Lir::CallRuntimeHelperRegRegLocationMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
208 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700209 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800210 DCHECK(!IsSameReg(TargetReg(kArg2, arg0.GetWideKind()), arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700211 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
212 if (r_tmp.NotExactlyEquals(arg0)) {
213 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800214 }
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800215 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
216 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800217 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700218 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800219}
220
Andreas Gampe98430592014-07-27 19:44:50 -0700221void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(QuickEntrypointEnum trampoline,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700222 RegLocation arg0, RegLocation arg1,
223 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700224 RegStorage r_tgt = CallHelperSetup(trampoline);
Maja Gagic6ea651f2015-02-24 16:55:04 +0100225 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kMips64 ||
226 cu_->instruction_set == kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700227 RegStorage arg0_reg = TargetReg((arg0.fp) ? kFArg0 : kArg0, arg0);
228
229 RegStorage arg1_reg;
230 if (arg1.fp == arg0.fp) {
231 arg1_reg = TargetReg((arg1.fp) ? kFArg1 : kArg1, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700233 arg1_reg = TargetReg((arg1.fp) ? kFArg0 : kArg0, arg1);
234 }
235
236 if (arg0.wide == 0) {
237 LoadValueDirectFixed(arg0, arg0_reg);
238 } else {
239 LoadValueDirectWideFixed(arg0, arg0_reg);
240 }
241
242 if (arg1.wide == 0) {
243 LoadValueDirectFixed(arg1, arg1_reg);
244 } else {
245 LoadValueDirectWideFixed(arg1, arg1_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 }
247 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700248 DCHECK(!cu_->target64);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700249 if (arg0.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700250 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700251 if (arg1.wide == 0) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800252 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700253 if (cu_->instruction_set == kMips) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800254 LoadValueDirectFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700255 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800256 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700257 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700258 } else {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800259 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700260 if (cu_->instruction_set == kMips) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800261 LoadValueDirectWideFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700262 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800263 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700264 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700265 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700267 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700268 if (arg1.wide == 0) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800269 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
270 if (cu_->instruction_set == kMips) {
271 LoadValueDirectFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kNotWide));
272 } else {
273 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kNotWide));
274 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700275 } else {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800276 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
277 if (cu_->instruction_set == kMips) {
278 LoadValueDirectWideFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kWide));
279 } else {
280 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
281 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700282 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 }
284 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000285 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700286 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287}
288
Mingyao Yang80365d92014-04-18 12:10:58 -0700289void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700290 WideKind arg0_kind = arg0.GetWideKind();
291 WideKind arg1_kind = arg1.GetWideKind();
292 if (IsSameReg(arg1, TargetReg(kArg0, arg1_kind))) {
293 if (IsSameReg(arg0, TargetReg(kArg1, arg0_kind))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700294 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampeccc60262014-07-04 18:02:38 -0700295 OpRegCopy(TargetReg(kArg2, arg1_kind), arg1);
296 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
297 OpRegCopy(TargetReg(kArg1, arg1_kind), TargetReg(kArg2, arg1_kind));
Mingyao Yang80365d92014-04-18 12:10:58 -0700298 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700299 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
300 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700301 }
302 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700303 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
304 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700305 }
306}
307
Andreas Gampe98430592014-07-27 19:44:50 -0700308void Mir2Lir::CallRuntimeHelperRegReg(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800309 RegStorage arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700310 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700311 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000312 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700313 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314}
315
Andreas Gampe98430592014-07-27 19:44:50 -0700316void Mir2Lir::CallRuntimeHelperRegRegImm(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800317 RegStorage arg1, int arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700318 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700319 CopyToArgumentRegs(arg0, arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700320 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000321 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700322 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323}
324
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800325void Mir2Lir::CallRuntimeHelperImmRegLocationMethod(QuickEntrypointEnum trampoline, int arg0,
326 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700327 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800328 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
329 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
Andreas Gampeccc60262014-07-04 18:02:38 -0700330 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000331 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700332 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333}
334
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800335void Mir2Lir::CallRuntimeHelperImmImmMethod(QuickEntrypointEnum trampoline, int arg0, int arg1,
Andreas Gampe98430592014-07-27 19:44:50 -0700336 bool safepoint_pc) {
337 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800338 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
339 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700340 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000341 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700342 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343}
344
Andreas Gampe98430592014-07-27 19:44:50 -0700345void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(QuickEntrypointEnum trampoline, int arg0,
346 RegLocation arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 RegLocation arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700348 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700349 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
350 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700351 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700353 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700355 LoadValueDirectWideFixed(arg2, TargetReg(kArg2, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700357 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000358 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700359 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360}
361
Andreas Gampeccc60262014-07-04 18:02:38 -0700362void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(
Andreas Gampe98430592014-07-27 19:44:50 -0700363 QuickEntrypointEnum trampoline,
Andreas Gampeccc60262014-07-04 18:02:38 -0700364 RegLocation arg0,
365 RegLocation arg1,
366 RegLocation arg2,
367 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700368 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700369 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
370 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
371 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000372 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700373 CallHelper(r_tgt, trampoline, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700374}
375
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376/*
377 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100378 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 * assignment of promoted arguments.
380 *
381 * ArgLocs is an array of location records describing the incoming arguments
382 * with one location record per word of argument.
383 */
Zheng Xu5667fdb2014-10-23 18:29:55 +0800384// TODO: Support 64-bit argument registers.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700385void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800387 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 * It will attempt to keep kArg0 live (or copy it to home location
389 * if promoted).
390 */
391 RegLocation rl_src = rl_method;
392 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -0700393 rl_src.reg = TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700395 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700396 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 // If Method* has been promoted, explicitly flush
398 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700399 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 }
401
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700402 if (mir_graph_->GetNumOfInVRs() == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800404 }
405
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700406 int start_vreg = mir_graph_->GetFirstInVR();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 /*
408 * Copy incoming arguments to their proper home locations.
409 * NOTE: an older version of dx had an issue in which
410 * it would reuse static method argument registers.
411 * This could result in the same Dalvik virtual register
412 * being promoted to both core and fp regs. To account for this,
413 * we only copy to the corresponding promoted physical register
414 * if it matches the type of the SSA name for the incoming
415 * argument. It is also possible that long and double arguments
416 * end up half-promoted. In those cases, we must flush the promoted
417 * half to memory as well.
418 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100419 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600420 RegLocation* t_loc = nullptr;
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000421 EnsureInitializedArgMappingToPhysicalReg();
Serguei Katkov717a3e42014-11-13 17:19:42 +0600422 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i += t_loc->wide ? 2 : 1) {
423 // get reg corresponding to input
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000424 RegStorage reg = in_to_reg_storage_mapping_.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600425 t_loc = &ArgLocs[i];
426
427 // If the wide input appeared as single, flush it and go
428 // as it comes from memory.
429 if (t_loc->wide && reg.Valid() && !reg.Is64Bit()) {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000430 // The memory already holds the half. Don't do anything.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600431 reg = RegStorage::InvalidReg();
432 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800433
buzbee2700f7e2014-03-07 09:46:20 -0800434 if (reg.Valid()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600435 // If arriving in register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436
Serguei Katkov717a3e42014-11-13 17:19:42 +0600437 // We have already updated the arg location with promoted info
438 // so we can be based on it.
439 if (t_loc->location == kLocPhysReg) {
440 // Just copy it.
441 if (t_loc->wide) {
442 OpRegCopyWide(t_loc->reg, reg);
443 } else {
444 OpRegCopy(t_loc->reg, reg);
445 }
446 } else {
447 // Needs flush.
448 int offset = SRegOffset(start_vreg + i);
449 if (t_loc->ref) {
450 StoreRefDisp(TargetPtrReg(kSp), offset, reg, kNotVolatile);
451 } else {
452 StoreBaseDisp(TargetPtrReg(kSp), offset, reg, t_loc->wide ? k64 : k32, kNotVolatile);
buzbeed0a03b82013-09-14 08:21:05 -0700453 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 } else {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600456 // If arriving in frame & promoted.
457 if (t_loc->location == kLocPhysReg) {
458 int offset = SRegOffset(start_vreg + i);
459 if (t_loc->ref) {
460 LoadRefDisp(TargetPtrReg(kSp), offset, t_loc->reg, kNotVolatile);
461 } else {
462 LoadBaseDisp(TargetPtrReg(kSp), offset, t_loc->reg, t_loc->wide ? k64 : k32,
463 kNotVolatile);
464 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 }
466 }
467 }
468}
469
Andreas Gampeccc60262014-07-04 18:02:38 -0700470static void CommonCallCodeLoadThisIntoArg1(const CallInfo* info, Mir2Lir* cg) {
471 RegLocation rl_arg = info->args[0];
472 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1, kRef));
473}
474
475static void CommonCallCodeLoadClassIntoArg0(const CallInfo* info, Mir2Lir* cg) {
476 cg->GenNullCheck(cg->TargetReg(kArg1, kRef), info->opt_flags);
477 // get this->klass_ [use kArg1, set kArg0]
478 cg->LoadRefDisp(cg->TargetReg(kArg1, kRef), mirror::Object::ClassOffset().Int32Value(),
479 cg->TargetReg(kArg0, kRef),
480 kNotVolatile);
481 cg->MarkPossibleNullPointerException(info->opt_flags);
482}
483
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700484static bool CommonCallCodeLoadCodePointerIntoInvokeTgt(const RegStorage* alt_from,
Andreas Gampeccc60262014-07-04 18:02:38 -0700485 const CompilationUnit* cu, Mir2Lir* cg) {
486 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Mathieu Chartier2d721012014-11-10 11:08:06 -0800487 int32_t offset = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
488 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
Andreas Gampeccc60262014-07-04 18:02:38 -0700489 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
Mathieu Chartier2d721012014-11-10 11:08:06 -0800490 cg->LoadWordDisp(alt_from == nullptr ? cg->TargetReg(kArg0, kRef) : *alt_from, offset,
Andreas Gampeccc60262014-07-04 18:02:38 -0700491 cg->TargetPtrReg(kInvokeTgt));
492 return true;
493 }
494 return false;
495}
496
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497/*
498 * Bit of a hack here - in the absence of a real scheduling pass,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 * emit the next instruction in a virtual invoke sequence.
500 * We can use kLr as a temp prior to target address loading
501 * Note also that we'll load the first argument ("this") into
Serguei Katkov717a3e42014-11-13 17:19:42 +0600502 * kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 */
504static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
505 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700506 uint32_t method_idx, uintptr_t, uintptr_t,
507 InvokeType) {
508 UNUSED(target_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
510 /*
511 * This is the fast path in which the target virtual method is
512 * fully resolved at compile time.
513 */
514 switch (state) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700515 case 0:
516 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700518 case 1:
519 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
520 // Includes a null-check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700522 case 2: {
523 // Get this->klass_.embedded_vtable[method_idx] [usr kArg0, set kArg0]
524 int32_t offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
525 method_idx * sizeof(mirror::Class::VTableEntry);
526 // Load target method from embedded vtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700527 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700529 }
530 case 3:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700531 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700532 break; // kInvokeTgt := kArg0->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700534 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
535 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 default:
537 return -1;
538 }
539 return state + 1;
540}
541
542/*
Jeff Hao88474b42013-10-23 16:24:40 -0700543 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
544 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
545 * 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 +0600546 * argument ("this") into kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 */
548static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
549 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700550 uint32_t method_idx, uintptr_t, uintptr_t, InvokeType) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552
Jeff Hao88474b42013-10-23 16:24:40 -0700553 switch (state) {
554 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700555 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Andreas Gampeccc60262014-07-04 18:02:38 -0700556 cg->LoadConstant(cg->TargetReg(kHiddenArg, kNotWide), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400557 if (cu->instruction_set == kX86) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700558 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, kNotWide), cg->TargetReg(kHiddenArg, kNotWide));
Jeff Hao88474b42013-10-23 16:24:40 -0700559 }
560 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700561 case 1:
562 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Jeff Hao88474b42013-10-23 16:24:40 -0700563 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700564 case 2:
565 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
566 // Includes a null-check.
Jeff Hao88474b42013-10-23 16:24:40 -0700567 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700568 case 3: { // Get target method [use kInvokeTgt, set kArg0]
569 int32_t offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
570 (method_idx % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
571 // Load target method from embedded imtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700572 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700573 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700574 }
575 case 4:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700576 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700577 break; // kInvokeTgt := kArg0->entrypoint
Jeff Hao88474b42013-10-23 16:24:40 -0700578 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700579 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
580 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 default:
582 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 }
584 return state + 1;
585}
586
Andreas Gampeccc60262014-07-04 18:02:38 -0700587static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info,
Andreas Gampe98430592014-07-27 19:44:50 -0700588 QuickEntrypointEnum trampoline, int state,
Andreas Gampeccc60262014-07-04 18:02:38 -0700589 const MethodReference& target_method, uint32_t method_idx) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700590 UNUSED(info, method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Andreas Gampe98430592014-07-27 19:44:50 -0700592
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 /*
594 * This handles the case in which the base method is not fully
595 * resolved at compile time, we bail to a runtime helper.
596 */
597 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700598 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 // Load trampoline target
Andreas Gampe98430592014-07-27 19:44:50 -0700600 int32_t disp;
601 if (cu->target64) {
602 disp = GetThreadOffset<8>(trampoline).Int32Value();
603 } else {
604 disp = GetThreadOffset<4>(trampoline).Int32Value();
605 }
606 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), disp, cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 }
608 // Load kArg0 with method index
609 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampeccc60262014-07-04 18:02:38 -0700610 cg->LoadConstant(cg->TargetReg(kArg0, kNotWide), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 return 1;
612 }
613 return -1;
614}
615
616static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
617 int state,
618 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700619 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700620 return NextInvokeInsnSP(cu, info, kQuickInvokeStaticTrampolineWithAccessCheck, state,
621 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622}
623
624static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
625 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700626 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700627 return NextInvokeInsnSP(cu, info, kQuickInvokeDirectTrampolineWithAccessCheck, state,
628 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629}
630
631static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
632 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700633 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700634 return NextInvokeInsnSP(cu, info, kQuickInvokeSuperTrampolineWithAccessCheck, state,
635 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636}
637
638static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
639 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700640 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700641 return NextInvokeInsnSP(cu, info, kQuickInvokeVirtualTrampolineWithAccessCheck, state,
642 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643}
644
645static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
646 CallInfo* info, int state,
647 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700648 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700649 return NextInvokeInsnSP(cu, info, kQuickInvokeInterfaceTrampolineWithAccessCheck, state,
650 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651}
652
Dave Allison69dfe512014-07-11 17:11:58 +0000653// Default implementation of implicit null pointer check.
654// Overridden by arch specific as necessary.
655void Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
656 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
657 return;
658 }
659 RegStorage tmp = AllocTemp();
660 Load32Disp(reg, 0, tmp);
661 MarkPossibleNullPointerException(opt_flags);
662 FreeTemp(tmp);
663}
664
Serguei Katkov717a3e42014-11-13 17:19:42 +0600665/**
666 * @brief Used to flush promoted registers if they are used as argument
667 * in an invocation.
668 * @param info the infromation about arguments for invocation.
669 * @param start the first argument we should start to look from.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 */
Serguei Katkov717a3e42014-11-13 17:19:42 +0600671void Mir2Lir::GenDalvikArgsFlushPromoted(CallInfo* info, int start) {
672 if (cu_->disable_opt & (1 << kPromoteRegs)) {
673 // This make sense only if promotion is enabled.
674 return;
675 }
676 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 // Scan the rest of the args - if in phys_reg flush to memory
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000678 for (size_t next_arg = start; next_arg < info->num_arg_words;) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 RegLocation loc = info->args[next_arg];
680 if (loc.wide) {
681 loc = UpdateLocWide(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600682 if (loc.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700683 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 }
685 next_arg += 2;
686 } else {
687 loc = UpdateLoc(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600688 if (loc.location == kLocPhysReg) {
689 if (loc.ref) {
690 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
691 } else {
692 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k32,
693 kNotVolatile);
694 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 }
696 next_arg++;
697 }
698 }
Serguei Katkov717a3e42014-11-13 17:19:42 +0600699}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700
Serguei Katkov717a3e42014-11-13 17:19:42 +0600701/**
702 * @brief Used to optimize the copying of VRs which are arguments of invocation.
703 * Please note that you should flush promoted registers first if you copy.
704 * If implementation does copying it may skip several of the first VRs but must copy
705 * till the end. Implementation must return the number of skipped VRs
706 * (it might be all VRs).
707 * @see GenDalvikArgsFlushPromoted
708 * @param info the information about arguments for invocation.
709 * @param first the first argument we should start to look from.
710 * @param count the number of remaining arguments we can handle.
711 * @return the number of arguments which we did not handle. Unhandled arguments
712 * must be attached to the first one.
713 */
714int Mir2Lir::GenDalvikArgsBulkCopy(CallInfo* info, int first, int count) {
715 // call is pretty expensive, let's use it if count is big.
716 if (count > 16) {
717 GenDalvikArgsFlushPromoted(info, first);
718 int start_offset = SRegOffset(info->args[first].s_reg_low);
719 int outs_offset = StackVisitor::GetOutVROffset(first, cu_->instruction_set);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800720
Andreas Gampeccc60262014-07-04 18:02:38 -0700721 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
722 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -0700723 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +0600724 count * 4, false);
725 count = 0;
726 }
727 return count;
728}
729
730int Mir2Lir::GenDalvikArgs(CallInfo* info, int call_state,
731 LIR** pcrLabel, NextCallInsn next_call_insn,
732 const MethodReference& target_method,
733 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
734 InvokeType type, bool skip_this) {
735 // If no arguments, just return.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000736 if (info->num_arg_words == 0u)
Serguei Katkov717a3e42014-11-13 17:19:42 +0600737 return call_state;
738
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000739 const size_t start_index = skip_this ? 1 : 0;
Serguei Katkov717a3e42014-11-13 17:19:42 +0600740
741 // Get architecture dependent mapping between output VRs and physical registers
742 // basing on shorty of method to call.
743 InToRegStorageMapping in_to_reg_storage_mapping(arena_);
744 {
745 const char* target_shorty = mir_graph_->GetShortyFromMethodReference(target_method);
746 ShortyIterator shorty_iterator(target_shorty, type == kStatic);
747 in_to_reg_storage_mapping.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 }
749
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000750 size_t stack_map_start = std::max(in_to_reg_storage_mapping.GetEndMappedIn(), start_index);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600751 if ((stack_map_start < info->num_arg_words) && info->args[stack_map_start].high_word) {
752 // It is possible that the last mapped reg is 32 bit while arg is 64-bit.
753 // It will be handled together with low part mapped to register.
754 stack_map_start++;
755 }
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000756 size_t regs_left_to_pass_via_stack = info->num_arg_words - stack_map_start;
Serguei Katkov717a3e42014-11-13 17:19:42 +0600757
758 // If it is a range case we can try to copy remaining VRs (not mapped to physical registers)
759 // using more optimal algorithm.
760 if (info->is_range && regs_left_to_pass_via_stack > 1) {
761 regs_left_to_pass_via_stack = GenDalvikArgsBulkCopy(info, stack_map_start,
762 regs_left_to_pass_via_stack);
763 }
764
765 // Now handle any remaining VRs mapped to stack.
766 if (in_to_reg_storage_mapping.HasArgumentsOnStack()) {
767 // Two temps but do not use kArg1, it might be this which we can skip.
768 // Separate single and wide - it can give some advantage.
769 RegStorage regRef = TargetReg(kArg3, kRef);
770 RegStorage regSingle = TargetReg(kArg3, kNotWide);
771 RegStorage regWide = TargetReg(kArg2, kWide);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000772 for (size_t i = start_index; i < stack_map_start + regs_left_to_pass_via_stack; i++) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600773 RegLocation rl_arg = info->args[i];
774 rl_arg = UpdateRawLoc(rl_arg);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000775 RegStorage reg = in_to_reg_storage_mapping.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600776 if (!reg.Valid()) {
777 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
778 {
779 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
780 if (rl_arg.wide) {
781 if (rl_arg.location == kLocPhysReg) {
782 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
783 } else {
784 LoadValueDirectWideFixed(rl_arg, regWide);
785 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regWide, k64, kNotVolatile);
786 }
787 } else {
788 if (rl_arg.location == kLocPhysReg) {
789 if (rl_arg.ref) {
790 StoreRefDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, kNotVolatile);
791 } else {
792 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k32, kNotVolatile);
793 }
794 } else {
795 if (rl_arg.ref) {
796 LoadValueDirectFixed(rl_arg, regRef);
797 StoreRefDisp(TargetPtrReg(kSp), out_offset, regRef, kNotVolatile);
798 } else {
799 LoadValueDirectFixed(rl_arg, regSingle);
800 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regSingle, k32, kNotVolatile);
801 }
802 }
803 }
804 }
805 call_state = next_call_insn(cu_, info, call_state, target_method,
806 vtable_idx, direct_code, direct_method, type);
807 }
808 if (rl_arg.wide) {
809 i++;
810 }
811 }
812 }
813
814 // Finish with VRs mapped to physical registers.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000815 for (size_t i = start_index; i < stack_map_start; i++) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600816 RegLocation rl_arg = info->args[i];
817 rl_arg = UpdateRawLoc(rl_arg);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000818 RegStorage reg = in_to_reg_storage_mapping.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600819 if (reg.Valid()) {
820 if (rl_arg.wide) {
821 // if reg is not 64-bit (it is half of 64-bit) then handle it separately.
822 if (!reg.Is64Bit()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600823 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
824 if (rl_arg.location == kLocPhysReg) {
825 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000826 // Dump it to memory.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600827 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
828 LoadBaseDisp(TargetPtrReg(kSp), out_offset, reg, k32, kNotVolatile);
829 } else {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000830 int high_offset = StackVisitor::GetOutVROffset(i + 1, cu_->instruction_set);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600831 // First, use target reg for high part.
832 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low + 1), reg, k32,
833 kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000834 StoreBaseDisp(TargetPtrReg(kSp), high_offset, reg, k32, kNotVolatile);
835 // Now, use target reg for low part.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600836 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low), reg, k32, kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000837 int low_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
838 // And store it to the expected memory location.
839 StoreBaseDisp(TargetPtrReg(kSp), low_offset, reg, k32, kNotVolatile);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600840 }
841 } else {
842 LoadValueDirectWideFixed(rl_arg, reg);
843 }
844 } else {
845 LoadValueDirectFixed(rl_arg, reg);
846 }
847 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
848 direct_code, direct_method, type);
849 }
850 if (rl_arg.wide) {
851 i++;
852 }
853 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854
855 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
856 direct_code, direct_method, type);
857 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +0000858 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700859 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700860 } else {
861 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +0000862 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700863 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 }
865 return call_state;
866}
867
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000868void Mir2Lir::EnsureInitializedArgMappingToPhysicalReg() {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600869 if (!in_to_reg_storage_mapping_.IsInitialized()) {
870 ShortyIterator shorty_iterator(cu_->shorty, cu_->invoke_type == kStatic);
871 in_to_reg_storage_mapping_.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
872 }
Serguei Katkov717a3e42014-11-13 17:19:42 +0600873}
874
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700875RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 RegLocation res;
877 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700878 // If result is unused, return a sink target based on type of invoke target.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800879 res = GetReturn(
880 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 } else {
882 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700883 DCHECK_EQ(LocToRegClass(res),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800884 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 }
886 return res;
887}
888
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700889RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700890 RegLocation res;
891 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700892 // If result is unused, return a sink target based on type of invoke target.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800893 res = GetReturnWide(ShortyToRegClass(
894 mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 } else {
896 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700897 DCHECK_EQ(LocToRegClass(res),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800898 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 }
900 return res;
901}
902
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -0700903bool Mir2Lir::GenInlinedReferenceGetReferent(CallInfo* info) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100904 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
905 // TODO: add Mips and Mips64 implementations.
Fred Shih4ee7a662014-07-11 09:59:27 -0700906 return false;
907 }
908
Fred Shih4ee7a662014-07-11 09:59:27 -0700909 bool use_direct_type_ptr;
910 uintptr_t direct_type_ptr;
Fred Shihe7f82e22014-08-06 10:46:37 -0700911 ClassReference ref;
912 if (!cu_->compiler_driver->CanEmbedReferenceTypeInCode(&ref,
913 &use_direct_type_ptr, &direct_type_ptr)) {
914 return false;
915 }
916
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700917 RegStorage reg_class = TargetReg(kArg1, kRef);
918 Clobber(reg_class);
919 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -0700920 if (use_direct_type_ptr) {
921 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -0700922 } else {
Fred Shihe7f82e22014-08-06 10:46:37 -0700923 uint16_t type_idx = ref.first->GetClassDef(ref.second).class_idx_;
924 LoadClassType(*ref.first, type_idx, kArg1);
Fred Shih4ee7a662014-07-11 09:59:27 -0700925 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700926
Fred Shihe7f82e22014-08-06 10:46:37 -0700927 uint32_t slow_path_flag_offset = cu_->compiler_driver->GetReferenceSlowFlagOffset();
928 uint32_t disable_flag_offset = cu_->compiler_driver->GetReferenceDisableFlagOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700929 CHECK(slow_path_flag_offset && disable_flag_offset &&
930 (slow_path_flag_offset != disable_flag_offset));
931
932 // intrinsic logic start.
933 RegLocation rl_obj = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -0700934 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih4ee7a662014-07-11 09:59:27 -0700935
936 RegStorage reg_slow_path = AllocTemp();
937 RegStorage reg_disabled = AllocTemp();
Andreas Gampef6815702015-01-20 09:53:48 -0800938 LoadBaseDisp(reg_class, slow_path_flag_offset, reg_slow_path, kSignedByte, kNotVolatile);
939 LoadBaseDisp(reg_class, disable_flag_offset, reg_disabled, kSignedByte, kNotVolatile);
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700940 FreeTemp(reg_class);
941 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -0700942 FreeTemp(reg_disabled);
943
944 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700945 LIR* slow_path_branch;
946 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
947 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
948 slow_path_branch = OpCondBranch(kCondNe, nullptr);
949 } else {
950 // Generate compare and branch.
951 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
952 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700953 FreeTemp(reg_slow_path);
954
955 // slow path not enabled, simply load the referent of the reference object
956 RegLocation rl_dest = InlineTarget(info);
957 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
958 GenNullCheck(rl_obj.reg, info->opt_flags);
959 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
960 kNotVolatile);
961 MarkPossibleNullPointerException(info->opt_flags);
962 StoreValue(rl_dest, rl_result);
963
964 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
965 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
Serguei Katkov9863daf2014-09-04 15:21:32 +0700966 ClobberCallerSave(); // We must clobber everything because slow path will return here
Fred Shih4ee7a662014-07-11 09:59:27 -0700967 return true;
968}
969
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700970bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 // Location of reference to data array
972 int value_offset = mirror::String::ValueOffset().Int32Value();
973 // Location of count
974 int count_offset = mirror::String::CountOffset().Int32Value();
975 // Starting offset within data array
976 int offset_offset = mirror::String::OffsetOffset().Int32Value();
977 // Start of char data with array_
978 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
979
980 RegLocation rl_obj = info->args[0];
981 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -0700982 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -0700983 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800984 RegStorage reg_max;
985 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +0000987 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -0800988 RegStorage reg_off;
989 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -0700990 reg_off = AllocTemp();
991 reg_ptr = AllocTempRef();
992 if (range_check) {
993 reg_max = AllocTemp();
994 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -0800995 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 }
Andreas Gampe98430592014-07-27 19:44:50 -0700997 Load32Disp(rl_obj.reg, offset_offset, reg_off);
998 MarkPossibleNullPointerException(info->opt_flags);
999 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
1000 if (range_check) {
1001 // Set up a slow path to allow retry in case of bounds violation */
1002 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
1003 FreeTemp(reg_max);
1004 range_check_branch = OpCondBranch(kCondUge, nullptr);
1005 }
1006 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001007 if (rl_idx.is_const) {
1008 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1009 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001010 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001011 }
buzbee2700f7e2014-03-07 09:46:20 -08001012 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001013 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001014 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001015 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 RegLocation rl_dest = InlineTarget(info);
1017 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001018 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 FreeTemp(reg_off);
1020 FreeTemp(reg_ptr);
1021 StoreValue(rl_dest, rl_result);
1022 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001023 DCHECK(range_check_branch != nullptr);
1024 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001025 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 return true;
1028}
1029
1030// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001031bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001032 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1033 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 return false;
1035 }
1036 // dst = src.length();
1037 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001038 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 RegLocation rl_dest = InlineTarget(info);
1040 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001041 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001042 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001043 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 if (is_empty) {
1045 // dst = (dst == 0);
1046 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001047 RegStorage t_reg = AllocTemp();
1048 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1049 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001050 } else if (cu_->instruction_set == kArm64) {
1051 OpRegImm(kOpSub, rl_result.reg, 1);
1052 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001054 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001055 OpRegImm(kOpSub, rl_result.reg, 1);
1056 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 }
1058 }
1059 StoreValue(rl_dest, rl_result);
1060 return true;
1061}
1062
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001063bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001064 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1065 // TODO: add Mips and Mips64 implementations.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001066 return false;
1067 }
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001068 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
1069 if (rl_dest.s_reg_low == INVALID_SREG) {
1070 // Result is unused, the code is dead. Inlining successful, no code generated.
1071 return true;
1072 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001073 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001074 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001075 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001076 if (IsWide(size)) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001077 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001078 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1079 StoreValueWide(rl_dest, rl_result);
1080 return true;
1081 }
buzbee2700f7e2014-03-07 09:46:20 -08001082 RegStorage r_i_low = rl_i.reg.GetLow();
1083 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001084 // 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 +00001085 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001086 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001087 }
buzbee2700f7e2014-03-07 09:46:20 -08001088 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1089 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1090 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001091 FreeTemp(r_i_low);
1092 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001093 StoreValueWide(rl_dest, rl_result);
1094 } else {
buzbee695d13a2014-04-19 13:32:20 -07001095 DCHECK(size == k32 || size == kSignedHalf);
1096 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001097 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001098 StoreValue(rl_dest, rl_result);
1099 }
1100 return true;
1101}
1102
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001103bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001104 RegLocation rl_dest = InlineTarget(info);
1105 if (rl_dest.s_reg_low == INVALID_SREG) {
1106 // Result is unused, the code is dead. Inlining successful, no code generated.
1107 return true;
1108 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 RegLocation rl_src = info->args[0];
1110 rl_src = LoadValue(rl_src, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001112 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001114 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1115 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1116 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 StoreValue(rl_dest, rl_result);
1118 return true;
1119}
1120
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001121bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001122 RegLocation rl_dest = InlineTargetWide(info);
1123 if (rl_dest.s_reg_low == INVALID_SREG) {
1124 // Result is unused, the code is dead. Inlining successful, no code generated.
1125 return true;
1126 }
Vladimir Markob9823312014-03-20 17:38:43 +00001127 RegLocation rl_src = info->args[0];
1128 rl_src = LoadValueWide(rl_src, kCoreReg);
Vladimir Markob9823312014-03-20 17:38:43 +00001129 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1130
1131 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001132 if (cu_->instruction_set != kX86_64 &&
1133 (cu_->instruction_set == kX86 ||
1134 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001135 OpRegCopyWide(rl_result.reg, rl_src.reg);
1136 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1137 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1138 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001139 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1140 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001141 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001142 }
1143 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 }
Vladimir Markob9823312014-03-20 17:38:43 +00001145
1146 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001147 RegStorage sign_reg;
1148 if (cu_->instruction_set == kX86_64) {
1149 sign_reg = AllocTempWide();
1150 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1151 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1152 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1153 } else {
1154 sign_reg = AllocTemp();
1155 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1156 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1157 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1158 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1159 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1160 }
buzbee082833c2014-05-17 23:16:26 -07001161 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001162 StoreValueWide(rl_dest, rl_result);
1163 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164}
1165
Serban Constantinescu23abec92014-07-02 16:13:38 +01001166bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001167 // Currently implemented only for ARM64.
1168 UNUSED(info, size);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001169 return false;
1170}
1171
1172bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001173 // Currently implemented only for ARM64.
1174 UNUSED(info, is_min, is_double);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001175 return false;
1176}
1177
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001178bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001179 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001180 return false;
1181}
1182
1183bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001184 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001185 return false;
1186}
1187
1188bool Mir2Lir::GenInlinedRint(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001189 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001190 return false;
1191}
1192
1193bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001194 UNUSED(info, is_double);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001195 return false;
1196}
1197
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001198bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001199 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1200 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 return false;
1202 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 RegLocation rl_dest = InlineTarget(info);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001204 if (rl_dest.s_reg_low == INVALID_SREG) {
1205 // Result is unused, the code is dead. Inlining successful, no code generated.
1206 return true;
1207 }
1208 RegLocation rl_src = info->args[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 StoreValue(rl_dest, rl_src);
1210 return true;
1211}
1212
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001213bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001214 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1215 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 return false;
1217 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 RegLocation rl_dest = InlineTargetWide(info);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001219 if (rl_dest.s_reg_low == INVALID_SREG) {
1220 // Result is unused, the code is dead. Inlining successful, no code generated.
1221 return true;
1222 }
1223 RegLocation rl_src = info->args[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 StoreValueWide(rl_dest, rl_src);
1225 return true;
1226}
1227
DaniilSokolov70c4f062014-06-24 17:34:00 -07001228bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001229 UNUSED(info);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001230 return false;
1231}
1232
1233
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001235 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 * otherwise bails to standard library code.
1237 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001238bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001239 RegLocation rl_obj = info->args[0];
1240 RegLocation rl_char = info->args[1];
1241 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1242 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1243 return false;
1244 }
1245
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001246 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001248 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1249 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1250 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252 LoadValueDirectFixed(rl_obj, reg_ptr);
1253 LoadValueDirectFixed(rl_char, reg_char);
1254 if (zero_based) {
1255 LoadConstant(reg_start, 0);
1256 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001257 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 LoadValueDirectFixed(rl_start, reg_start);
1259 }
Andreas Gampe98430592014-07-27 19:44:50 -07001260 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001261 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001262 LIR* high_code_point_branch =
1263 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001265 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001266 if (!rl_char.is_const) {
1267 // Add the slow path for code points beyond 0xFFFF.
1268 DCHECK(high_code_point_branch != nullptr);
1269 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1270 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001271 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001272 ClobberCallerSave(); // We must clobber everything because slow path will return here
Vladimir Marko3bc86152014-03-13 14:11:28 +00001273 } else {
1274 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1275 DCHECK(high_code_point_branch == nullptr);
1276 }
buzbeea0cd2d72014-06-01 09:33:49 -07001277 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 RegLocation rl_dest = InlineTarget(info);
1279 StoreValue(rl_dest, rl_return);
1280 return true;
1281}
1282
1283/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001284bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001285 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1286 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 return false;
1288 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001289 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001291 RegStorage reg_this = TargetReg(kArg0, kRef);
1292 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293
1294 RegLocation rl_this = info->args[0];
1295 RegLocation rl_cmp = info->args[1];
1296 LoadValueDirectFixed(rl_this, reg_this);
1297 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001298 RegStorage r_tgt;
1299 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001300 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001301 } else {
1302 r_tgt = RegStorage::InvalidReg();
1303 }
Dave Allisonf9439142014-03-27 15:10:22 -07001304 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001305 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001306 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001307 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001308 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001309 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001310 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001311 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 RegLocation rl_dest = InlineTarget(info);
1313 StoreValue(rl_dest, rl_return);
1314 return true;
1315}
1316
1317bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1318 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001319
1320 // Early exit if the result is unused.
1321 if (rl_dest.orig_sreg < 0) {
1322 return true;
1323 }
1324
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001325 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001326
Andreas Gamped500b532015-01-16 22:09:55 -08001327 if (Is64BitInstructionSet(cu_->instruction_set)) {
1328 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1329 kNotVolatile);
1330 } else {
1331 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 }
Andreas Gamped500b532015-01-16 22:09:55 -08001333
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 StoreValue(rl_dest, rl_result);
1335 return true;
1336}
1337
1338bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1339 bool is_long, bool is_volatile) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001340 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1341 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 return false;
1343 }
1344 // Unused - RegLocation rl_src_unsafe = info->args[0];
1345 RegLocation rl_src_obj = info->args[1]; // Object
1346 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001347 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001348 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001349
buzbeea0cd2d72014-06-01 09:33:49 -07001350 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001352 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001354 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1355 || cu_->instruction_set == kArm64) {
1356 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001357 } else {
1358 RegStorage rl_temp_offset = AllocTemp();
1359 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001360 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001361 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001362 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001364 if (rl_result.ref) {
1365 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1366 } else {
1367 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1368 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001369 }
1370
1371 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001372 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001373 }
1374
1375 if (is_long) {
1376 StoreValueWide(rl_dest, rl_result);
1377 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 StoreValue(rl_dest, rl_result);
1379 }
1380 return true;
1381}
1382
1383bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1384 bool is_object, bool is_volatile, bool is_ordered) {
Maja Gagic6ea651f2015-02-24 16:55:04 +01001385 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
1386 // TODO: add Mips and Mips64 implementations.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 return false;
1388 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389 // Unused - RegLocation rl_src_unsafe = info->args[0];
1390 RegLocation rl_src_obj = info->args[1]; // Object
1391 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001392 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393 RegLocation rl_src_value = info->args[4]; // value to store
1394 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001395 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001396 }
buzbeea0cd2d72014-06-01 09:33:49 -07001397 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1399 RegLocation rl_value;
1400 if (is_long) {
1401 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001402 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1403 || cu_->instruction_set == kArm64) {
1404 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001405 } else {
1406 RegStorage rl_temp_offset = AllocTemp();
1407 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001408 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001409 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001410 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411 } else {
buzbee7c02e912014-10-03 13:14:17 -07001412 rl_value = LoadValue(rl_src_value, LocToRegClass(rl_src_value));
Matteo Franchin255e0142014-07-04 13:50:41 +01001413 if (rl_value.ref) {
1414 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1415 } else {
1416 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1417 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001418 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001419
1420 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001421 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001422
Brian Carlstrom7940e442013-07-12 13:46:57 -07001423 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001424 // Prevent reordering with a subsequent volatile load.
1425 // May also be needed to address store atomicity issues.
1426 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001427 }
1428 if (is_object) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001429 MarkGCCard(0, rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001430 }
1431 return true;
1432}
1433
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001434void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001435 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001436 const DexFile* dex_file = info->method_ref.dex_file;
1437 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(dex_file)
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001438 ->GenIntrinsic(this, info)) {
1439 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001441 GenInvokeNoInline(info);
1442}
1443
1444void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 int call_state = 0;
1446 LIR* null_ck;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001447 LIR** p_null_ck = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001448 NextCallInsn next_call_insn;
1449 FlushAllRegs(); /* Everything to home location */
1450 // Explicit register usage
1451 LockCallTemps();
1452
Vladimir Markof096aad2014-01-23 15:51:58 +00001453 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1454 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1455 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
Vladimir Markof4da6752014-08-01 19:04:18 +01001456 info->type = method_info.GetSharpType();
Vladimir Markof096aad2014-01-23 15:51:58 +00001457 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 bool skip_this;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001459
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001461 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001462 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 } else if (info->type == kDirect) {
1464 if (fast_path) {
1465 p_null_ck = &null_ck;
1466 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001467 next_call_insn = fast_path ? GetNextSDCallInsn() : NextDirectCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 skip_this = false;
1469 } else if (info->type == kStatic) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001470 next_call_insn = fast_path ? GetNextSDCallInsn() : NextStaticCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 skip_this = false;
1472 } else if (info->type == kSuper) {
1473 DCHECK(!fast_path); // Fast path is a direct call.
1474 next_call_insn = NextSuperCallInsnSP;
1475 skip_this = false;
1476 } else {
1477 DCHECK_EQ(info->type, kVirtual);
1478 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1479 skip_this = fast_path;
1480 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001481 MethodReference target_method = method_info.GetTargetMethod();
Serguei Katkov717a3e42014-11-13 17:19:42 +06001482 call_state = GenDalvikArgs(info, call_state, p_null_ck,
1483 next_call_insn, target_method, method_info.VTableIndex(),
1484 method_info.DirectCode(), method_info.DirectMethod(),
1485 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486 // Finish up any of the call sequence not interleaved in arg loading
1487 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001488 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001489 method_info.DirectCode(), method_info.DirectMethod(),
1490 original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001492 LIR* call_insn = GenCallInsn(method_info);
Vladimir Markof4da6752014-08-01 19:04:18 +01001493 MarkSafepointPC(call_insn);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001494
Vladimir Markobfe400b2014-12-19 19:27:26 +00001495 FreeCallTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 if (info->result.location != kLocInvalid) {
1497 // We have a following MOVE_RESULT - do it now.
1498 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001499 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 StoreValueWide(info->result, ret_loc);
1501 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001502 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 StoreValue(info->result, ret_loc);
1504 }
1505 }
1506}
1507
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508} // namespace art