blob: 01f1d375ed519eaf93c8fa683b2949e7706567b6 [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);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700225 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700226 RegStorage arg0_reg = TargetReg((arg0.fp) ? kFArg0 : kArg0, arg0);
227
228 RegStorage arg1_reg;
229 if (arg1.fp == arg0.fp) {
230 arg1_reg = TargetReg((arg1.fp) ? kFArg1 : kArg1, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700232 arg1_reg = TargetReg((arg1.fp) ? kFArg0 : kArg0, arg1);
233 }
234
235 if (arg0.wide == 0) {
236 LoadValueDirectFixed(arg0, arg0_reg);
237 } else {
238 LoadValueDirectWideFixed(arg0, arg0_reg);
239 }
240
241 if (arg1.wide == 0) {
242 LoadValueDirectFixed(arg1, arg1_reg);
243 } else {
244 LoadValueDirectWideFixed(arg1, arg1_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 }
246 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700247 DCHECK(!cu_->target64);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700248 if (arg0.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700249 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700250 if (arg1.wide == 0) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800251 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700252 if (cu_->instruction_set == kMips) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800253 LoadValueDirectFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700254 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800255 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700256 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700257 } else {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800258 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700259 if (cu_->instruction_set == kMips) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800260 LoadValueDirectWideFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700261 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800262 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700263 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700264 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700266 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700267 if (arg1.wide == 0) {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800268 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
269 if (cu_->instruction_set == kMips) {
270 LoadValueDirectFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kNotWide));
271 } else {
272 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kNotWide));
273 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700274 } else {
Douglas Leungf58c11c2015-02-13 16:53:03 -0800275 // For Mips, when the 1st arg is integral, then remaining arg are passed in core reg.
276 if (cu_->instruction_set == kMips) {
277 LoadValueDirectWideFixed(arg1, TargetReg((arg1.fp && arg0.fp) ? kFArg2 : kArg2, kWide));
278 } else {
279 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
280 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700281 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 }
283 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000284 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700285 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286}
287
Mingyao Yang80365d92014-04-18 12:10:58 -0700288void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700289 WideKind arg0_kind = arg0.GetWideKind();
290 WideKind arg1_kind = arg1.GetWideKind();
291 if (IsSameReg(arg1, TargetReg(kArg0, arg1_kind))) {
292 if (IsSameReg(arg0, TargetReg(kArg1, arg0_kind))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700293 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampeccc60262014-07-04 18:02:38 -0700294 OpRegCopy(TargetReg(kArg2, arg1_kind), arg1);
295 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
296 OpRegCopy(TargetReg(kArg1, arg1_kind), TargetReg(kArg2, arg1_kind));
Mingyao Yang80365d92014-04-18 12:10:58 -0700297 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700298 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
299 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700300 }
301 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700302 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
303 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700304 }
305}
306
Andreas Gampe98430592014-07-27 19:44:50 -0700307void Mir2Lir::CallRuntimeHelperRegReg(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800308 RegStorage arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700309 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700310 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000311 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700312 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313}
314
Andreas Gampe98430592014-07-27 19:44:50 -0700315void Mir2Lir::CallRuntimeHelperRegRegImm(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800316 RegStorage arg1, int arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700317 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700318 CopyToArgumentRegs(arg0, arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700319 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000320 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700321 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322}
323
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800324void Mir2Lir::CallRuntimeHelperImmRegLocationMethod(QuickEntrypointEnum trampoline, int arg0,
325 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700326 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800327 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
328 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
Andreas Gampeccc60262014-07-04 18:02:38 -0700329 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000330 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700331 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332}
333
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800334void Mir2Lir::CallRuntimeHelperImmImmMethod(QuickEntrypointEnum trampoline, int arg0, int arg1,
Andreas Gampe98430592014-07-27 19:44:50 -0700335 bool safepoint_pc) {
336 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800337 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
338 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700339 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000340 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700341 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342}
343
Andreas Gampe98430592014-07-27 19:44:50 -0700344void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(QuickEntrypointEnum trampoline, int arg0,
345 RegLocation arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 RegLocation arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700347 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700348 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
349 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700350 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700352 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700354 LoadValueDirectWideFixed(arg2, TargetReg(kArg2, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700356 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000357 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700358 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359}
360
Andreas Gampeccc60262014-07-04 18:02:38 -0700361void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(
Andreas Gampe98430592014-07-27 19:44:50 -0700362 QuickEntrypointEnum trampoline,
Andreas Gampeccc60262014-07-04 18:02:38 -0700363 RegLocation arg0,
364 RegLocation arg1,
365 RegLocation arg2,
366 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700367 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700368 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
369 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
370 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000371 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700372 CallHelper(r_tgt, trampoline, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700373}
374
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375/*
376 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100377 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 * assignment of promoted arguments.
379 *
380 * ArgLocs is an array of location records describing the incoming arguments
381 * with one location record per word of argument.
382 */
Zheng Xu5667fdb2014-10-23 18:29:55 +0800383// TODO: Support 64-bit argument registers.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700384void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800386 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 * It will attempt to keep kArg0 live (or copy it to home location
388 * if promoted).
389 */
390 RegLocation rl_src = rl_method;
391 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -0700392 rl_src.reg = TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700394 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700395 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 // If Method* has been promoted, explicitly flush
397 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700398 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 }
400
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700401 if (mir_graph_->GetNumOfInVRs() == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800403 }
404
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700405 int start_vreg = mir_graph_->GetFirstInVR();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 /*
407 * Copy incoming arguments to their proper home locations.
408 * NOTE: an older version of dx had an issue in which
409 * it would reuse static method argument registers.
410 * This could result in the same Dalvik virtual register
411 * being promoted to both core and fp regs. To account for this,
412 * we only copy to the corresponding promoted physical register
413 * if it matches the type of the SSA name for the incoming
414 * argument. It is also possible that long and double arguments
415 * end up half-promoted. In those cases, we must flush the promoted
416 * half to memory as well.
417 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100418 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600419 RegLocation* t_loc = nullptr;
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000420 EnsureInitializedArgMappingToPhysicalReg();
Serguei Katkov717a3e42014-11-13 17:19:42 +0600421 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i += t_loc->wide ? 2 : 1) {
422 // get reg corresponding to input
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000423 RegStorage reg = in_to_reg_storage_mapping_.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600424 t_loc = &ArgLocs[i];
425
426 // If the wide input appeared as single, flush it and go
427 // as it comes from memory.
428 if (t_loc->wide && reg.Valid() && !reg.Is64Bit()) {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000429 // The memory already holds the half. Don't do anything.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600430 reg = RegStorage::InvalidReg();
431 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800432
buzbee2700f7e2014-03-07 09:46:20 -0800433 if (reg.Valid()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600434 // If arriving in register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435
Serguei Katkov717a3e42014-11-13 17:19:42 +0600436 // We have already updated the arg location with promoted info
437 // so we can be based on it.
438 if (t_loc->location == kLocPhysReg) {
439 // Just copy it.
440 if (t_loc->wide) {
441 OpRegCopyWide(t_loc->reg, reg);
442 } else {
443 OpRegCopy(t_loc->reg, reg);
444 }
445 } else {
446 // Needs flush.
447 int offset = SRegOffset(start_vreg + i);
448 if (t_loc->ref) {
449 StoreRefDisp(TargetPtrReg(kSp), offset, reg, kNotVolatile);
450 } else {
451 StoreBaseDisp(TargetPtrReg(kSp), offset, reg, t_loc->wide ? k64 : k32, kNotVolatile);
buzbeed0a03b82013-09-14 08:21:05 -0700452 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 } else {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600455 // If arriving in frame & promoted.
456 if (t_loc->location == kLocPhysReg) {
457 int offset = SRegOffset(start_vreg + i);
458 if (t_loc->ref) {
459 LoadRefDisp(TargetPtrReg(kSp), offset, t_loc->reg, kNotVolatile);
460 } else {
461 LoadBaseDisp(TargetPtrReg(kSp), offset, t_loc->reg, t_loc->wide ? k64 : k32,
462 kNotVolatile);
463 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 }
465 }
466 }
467}
468
Andreas Gampeccc60262014-07-04 18:02:38 -0700469static void CommonCallCodeLoadThisIntoArg1(const CallInfo* info, Mir2Lir* cg) {
470 RegLocation rl_arg = info->args[0];
471 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1, kRef));
472}
473
474static void CommonCallCodeLoadClassIntoArg0(const CallInfo* info, Mir2Lir* cg) {
475 cg->GenNullCheck(cg->TargetReg(kArg1, kRef), info->opt_flags);
476 // get this->klass_ [use kArg1, set kArg0]
477 cg->LoadRefDisp(cg->TargetReg(kArg1, kRef), mirror::Object::ClassOffset().Int32Value(),
478 cg->TargetReg(kArg0, kRef),
479 kNotVolatile);
480 cg->MarkPossibleNullPointerException(info->opt_flags);
481}
482
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700483static bool CommonCallCodeLoadCodePointerIntoInvokeTgt(const RegStorage* alt_from,
Andreas Gampeccc60262014-07-04 18:02:38 -0700484 const CompilationUnit* cu, Mir2Lir* cg) {
485 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Mathieu Chartier2d721012014-11-10 11:08:06 -0800486 int32_t offset = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
487 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
Andreas Gampeccc60262014-07-04 18:02:38 -0700488 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
Mathieu Chartier2d721012014-11-10 11:08:06 -0800489 cg->LoadWordDisp(alt_from == nullptr ? cg->TargetReg(kArg0, kRef) : *alt_from, offset,
Andreas Gampeccc60262014-07-04 18:02:38 -0700490 cg->TargetPtrReg(kInvokeTgt));
491 return true;
492 }
493 return false;
494}
495
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496/*
497 * Bit of a hack here - in the absence of a real scheduling pass,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 * emit the next instruction in a virtual invoke sequence.
499 * We can use kLr as a temp prior to target address loading
500 * Note also that we'll load the first argument ("this") into
Serguei Katkov717a3e42014-11-13 17:19:42 +0600501 * kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 */
503static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
504 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700505 uint32_t method_idx, uintptr_t, uintptr_t,
506 InvokeType) {
507 UNUSED(target_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
509 /*
510 * This is the fast path in which the target virtual method is
511 * fully resolved at compile time.
512 */
513 switch (state) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700514 case 0:
515 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700517 case 1:
518 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
519 // Includes a null-check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700521 case 2: {
522 // Get this->klass_.embedded_vtable[method_idx] [usr kArg0, set kArg0]
523 int32_t offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
524 method_idx * sizeof(mirror::Class::VTableEntry);
525 // Load target method from embedded vtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700526 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700528 }
529 case 3:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700530 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700531 break; // kInvokeTgt := kArg0->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700533 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
534 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 default:
536 return -1;
537 }
538 return state + 1;
539}
540
541/*
Jeff Hao88474b42013-10-23 16:24:40 -0700542 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
543 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
544 * 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 +0600545 * argument ("this") into kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 */
547static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
548 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700549 uint32_t method_idx, uintptr_t, uintptr_t, InvokeType) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551
Jeff Hao88474b42013-10-23 16:24:40 -0700552 switch (state) {
553 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700554 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Andreas Gampeccc60262014-07-04 18:02:38 -0700555 cg->LoadConstant(cg->TargetReg(kHiddenArg, kNotWide), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400556 if (cu->instruction_set == kX86) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700557 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, kNotWide), cg->TargetReg(kHiddenArg, kNotWide));
Jeff Hao88474b42013-10-23 16:24:40 -0700558 }
559 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700560 case 1:
561 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Jeff Hao88474b42013-10-23 16:24:40 -0700562 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700563 case 2:
564 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
565 // Includes a null-check.
Jeff Hao88474b42013-10-23 16:24:40 -0700566 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700567 case 3: { // Get target method [use kInvokeTgt, set kArg0]
568 int32_t offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
569 (method_idx % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
570 // Load target method from embedded imtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700571 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700572 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700573 }
574 case 4:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700575 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700576 break; // kInvokeTgt := kArg0->entrypoint
Jeff Hao88474b42013-10-23 16:24:40 -0700577 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700578 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
579 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 default:
581 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 }
583 return state + 1;
584}
585
Andreas Gampeccc60262014-07-04 18:02:38 -0700586static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info,
Andreas Gampe98430592014-07-27 19:44:50 -0700587 QuickEntrypointEnum trampoline, int state,
Andreas Gampeccc60262014-07-04 18:02:38 -0700588 const MethodReference& target_method, uint32_t method_idx) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700589 UNUSED(info, method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Andreas Gampe98430592014-07-27 19:44:50 -0700591
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 /*
593 * This handles the case in which the base method is not fully
594 * resolved at compile time, we bail to a runtime helper.
595 */
596 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700597 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 // Load trampoline target
Andreas Gampe98430592014-07-27 19:44:50 -0700599 int32_t disp;
600 if (cu->target64) {
601 disp = GetThreadOffset<8>(trampoline).Int32Value();
602 } else {
603 disp = GetThreadOffset<4>(trampoline).Int32Value();
604 }
605 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), disp, cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 }
607 // Load kArg0 with method index
608 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampeccc60262014-07-04 18:02:38 -0700609 cg->LoadConstant(cg->TargetReg(kArg0, kNotWide), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 return 1;
611 }
612 return -1;
613}
614
615static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
616 int state,
617 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700618 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700619 return NextInvokeInsnSP(cu, info, kQuickInvokeStaticTrampolineWithAccessCheck, state,
620 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621}
622
623static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
624 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700625 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700626 return NextInvokeInsnSP(cu, info, kQuickInvokeDirectTrampolineWithAccessCheck, state,
627 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628}
629
630static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
631 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700632 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700633 return NextInvokeInsnSP(cu, info, kQuickInvokeSuperTrampolineWithAccessCheck, state,
634 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635}
636
637static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
638 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700639 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700640 return NextInvokeInsnSP(cu, info, kQuickInvokeVirtualTrampolineWithAccessCheck, state,
641 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642}
643
644static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
645 CallInfo* info, int state,
646 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700647 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700648 return NextInvokeInsnSP(cu, info, kQuickInvokeInterfaceTrampolineWithAccessCheck, state,
649 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650}
651
Dave Allison69dfe512014-07-11 17:11:58 +0000652// Default implementation of implicit null pointer check.
653// Overridden by arch specific as necessary.
654void Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
655 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
656 return;
657 }
658 RegStorage tmp = AllocTemp();
659 Load32Disp(reg, 0, tmp);
660 MarkPossibleNullPointerException(opt_flags);
661 FreeTemp(tmp);
662}
663
Serguei Katkov717a3e42014-11-13 17:19:42 +0600664/**
665 * @brief Used to flush promoted registers if they are used as argument
666 * in an invocation.
667 * @param info the infromation about arguments for invocation.
668 * @param start the first argument we should start to look from.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 */
Serguei Katkov717a3e42014-11-13 17:19:42 +0600670void Mir2Lir::GenDalvikArgsFlushPromoted(CallInfo* info, int start) {
671 if (cu_->disable_opt & (1 << kPromoteRegs)) {
672 // This make sense only if promotion is enabled.
673 return;
674 }
675 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 // Scan the rest of the args - if in phys_reg flush to memory
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000677 for (size_t next_arg = start; next_arg < info->num_arg_words;) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 RegLocation loc = info->args[next_arg];
679 if (loc.wide) {
680 loc = UpdateLocWide(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600681 if (loc.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700682 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 }
684 next_arg += 2;
685 } else {
686 loc = UpdateLoc(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600687 if (loc.location == kLocPhysReg) {
688 if (loc.ref) {
689 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
690 } else {
691 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k32,
692 kNotVolatile);
693 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 }
695 next_arg++;
696 }
697 }
Serguei Katkov717a3e42014-11-13 17:19:42 +0600698}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699
Serguei Katkov717a3e42014-11-13 17:19:42 +0600700/**
701 * @brief Used to optimize the copying of VRs which are arguments of invocation.
702 * Please note that you should flush promoted registers first if you copy.
703 * If implementation does copying it may skip several of the first VRs but must copy
704 * till the end. Implementation must return the number of skipped VRs
705 * (it might be all VRs).
706 * @see GenDalvikArgsFlushPromoted
707 * @param info the information about arguments for invocation.
708 * @param first the first argument we should start to look from.
709 * @param count the number of remaining arguments we can handle.
710 * @return the number of arguments which we did not handle. Unhandled arguments
711 * must be attached to the first one.
712 */
713int Mir2Lir::GenDalvikArgsBulkCopy(CallInfo* info, int first, int count) {
714 // call is pretty expensive, let's use it if count is big.
715 if (count > 16) {
716 GenDalvikArgsFlushPromoted(info, first);
717 int start_offset = SRegOffset(info->args[first].s_reg_low);
718 int outs_offset = StackVisitor::GetOutVROffset(first, cu_->instruction_set);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800719
Andreas Gampeccc60262014-07-04 18:02:38 -0700720 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
721 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -0700722 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +0600723 count * 4, false);
724 count = 0;
725 }
726 return count;
727}
728
729int Mir2Lir::GenDalvikArgs(CallInfo* info, int call_state,
730 LIR** pcrLabel, NextCallInsn next_call_insn,
731 const MethodReference& target_method,
732 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
733 InvokeType type, bool skip_this) {
734 // If no arguments, just return.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000735 if (info->num_arg_words == 0u)
Serguei Katkov717a3e42014-11-13 17:19:42 +0600736 return call_state;
737
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000738 const size_t start_index = skip_this ? 1 : 0;
Serguei Katkov717a3e42014-11-13 17:19:42 +0600739
740 // Get architecture dependent mapping between output VRs and physical registers
741 // basing on shorty of method to call.
742 InToRegStorageMapping in_to_reg_storage_mapping(arena_);
743 {
744 const char* target_shorty = mir_graph_->GetShortyFromMethodReference(target_method);
745 ShortyIterator shorty_iterator(target_shorty, type == kStatic);
746 in_to_reg_storage_mapping.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 }
748
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000749 size_t stack_map_start = std::max(in_to_reg_storage_mapping.GetEndMappedIn(), start_index);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600750 if ((stack_map_start < info->num_arg_words) && info->args[stack_map_start].high_word) {
751 // It is possible that the last mapped reg is 32 bit while arg is 64-bit.
752 // It will be handled together with low part mapped to register.
753 stack_map_start++;
754 }
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000755 size_t regs_left_to_pass_via_stack = info->num_arg_words - stack_map_start;
Serguei Katkov717a3e42014-11-13 17:19:42 +0600756
757 // If it is a range case we can try to copy remaining VRs (not mapped to physical registers)
758 // using more optimal algorithm.
759 if (info->is_range && regs_left_to_pass_via_stack > 1) {
760 regs_left_to_pass_via_stack = GenDalvikArgsBulkCopy(info, stack_map_start,
761 regs_left_to_pass_via_stack);
762 }
763
764 // Now handle any remaining VRs mapped to stack.
765 if (in_to_reg_storage_mapping.HasArgumentsOnStack()) {
766 // Two temps but do not use kArg1, it might be this which we can skip.
767 // Separate single and wide - it can give some advantage.
768 RegStorage regRef = TargetReg(kArg3, kRef);
769 RegStorage regSingle = TargetReg(kArg3, kNotWide);
770 RegStorage regWide = TargetReg(kArg2, kWide);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000771 for (size_t i = start_index; i < stack_map_start + regs_left_to_pass_via_stack; i++) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600772 RegLocation rl_arg = info->args[i];
773 rl_arg = UpdateRawLoc(rl_arg);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000774 RegStorage reg = in_to_reg_storage_mapping.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600775 if (!reg.Valid()) {
776 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
777 {
778 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
779 if (rl_arg.wide) {
780 if (rl_arg.location == kLocPhysReg) {
781 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
782 } else {
783 LoadValueDirectWideFixed(rl_arg, regWide);
784 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regWide, k64, kNotVolatile);
785 }
786 } else {
787 if (rl_arg.location == kLocPhysReg) {
788 if (rl_arg.ref) {
789 StoreRefDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, kNotVolatile);
790 } else {
791 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k32, kNotVolatile);
792 }
793 } else {
794 if (rl_arg.ref) {
795 LoadValueDirectFixed(rl_arg, regRef);
796 StoreRefDisp(TargetPtrReg(kSp), out_offset, regRef, kNotVolatile);
797 } else {
798 LoadValueDirectFixed(rl_arg, regSingle);
799 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regSingle, k32, kNotVolatile);
800 }
801 }
802 }
803 }
804 call_state = next_call_insn(cu_, info, call_state, target_method,
805 vtable_idx, direct_code, direct_method, type);
806 }
807 if (rl_arg.wide) {
808 i++;
809 }
810 }
811 }
812
813 // Finish with VRs mapped to physical registers.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000814 for (size_t i = start_index; i < stack_map_start; i++) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600815 RegLocation rl_arg = info->args[i];
816 rl_arg = UpdateRawLoc(rl_arg);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000817 RegStorage reg = in_to_reg_storage_mapping.GetReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600818 if (reg.Valid()) {
819 if (rl_arg.wide) {
820 // if reg is not 64-bit (it is half of 64-bit) then handle it separately.
821 if (!reg.Is64Bit()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600822 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
823 if (rl_arg.location == kLocPhysReg) {
824 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000825 // Dump it to memory.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600826 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
827 LoadBaseDisp(TargetPtrReg(kSp), out_offset, reg, k32, kNotVolatile);
828 } else {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000829 int high_offset = StackVisitor::GetOutVROffset(i + 1, cu_->instruction_set);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600830 // First, use target reg for high part.
831 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low + 1), reg, k32,
832 kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000833 StoreBaseDisp(TargetPtrReg(kSp), high_offset, reg, k32, kNotVolatile);
834 // Now, use target reg for low part.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600835 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low), reg, k32, kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000836 int low_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
837 // And store it to the expected memory location.
838 StoreBaseDisp(TargetPtrReg(kSp), low_offset, reg, k32, kNotVolatile);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600839 }
840 } else {
841 LoadValueDirectWideFixed(rl_arg, reg);
842 }
843 } else {
844 LoadValueDirectFixed(rl_arg, reg);
845 }
846 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
847 direct_code, direct_method, type);
848 }
849 if (rl_arg.wide) {
850 i++;
851 }
852 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853
854 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
855 direct_code, direct_method, type);
856 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +0000857 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700858 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700859 } else {
860 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +0000861 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700862 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 }
864 return call_state;
865}
866
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000867void Mir2Lir::EnsureInitializedArgMappingToPhysicalReg() {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600868 if (!in_to_reg_storage_mapping_.IsInitialized()) {
869 ShortyIterator shorty_iterator(cu_->shorty, cu_->invoke_type == kStatic);
870 in_to_reg_storage_mapping_.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
871 }
Serguei Katkov717a3e42014-11-13 17:19:42 +0600872}
873
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700874RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 RegLocation res;
876 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700877 // If result is unused, return a sink target based on type of invoke target.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800878 res = GetReturn(
879 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 } else {
881 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700882 DCHECK_EQ(LocToRegClass(res),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800883 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 }
885 return res;
886}
887
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700888RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889 RegLocation res;
890 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700891 // If result is unused, return a sink target based on type of invoke target.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800892 res = GetReturnWide(ShortyToRegClass(
893 mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700894 } else {
895 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700896 DCHECK_EQ(LocToRegClass(res),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800897 ShortyToRegClass(mir_graph_->GetShortyFromMethodReference(info->method_ref)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 }
899 return res;
900}
901
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -0700902bool Mir2Lir::GenInlinedReferenceGetReferent(CallInfo* info) {
Fred Shih4ee7a662014-07-11 09:59:27 -0700903 if (cu_->instruction_set == kMips) {
904 // TODO - add Mips implementation
905 return false;
906 }
907
Fred Shih4ee7a662014-07-11 09:59:27 -0700908 bool use_direct_type_ptr;
909 uintptr_t direct_type_ptr;
Fred Shihe7f82e22014-08-06 10:46:37 -0700910 ClassReference ref;
911 if (!cu_->compiler_driver->CanEmbedReferenceTypeInCode(&ref,
912 &use_direct_type_ptr, &direct_type_ptr)) {
913 return false;
914 }
915
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700916 RegStorage reg_class = TargetReg(kArg1, kRef);
917 Clobber(reg_class);
918 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -0700919 if (use_direct_type_ptr) {
920 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -0700921 } else {
Fred Shihe7f82e22014-08-06 10:46:37 -0700922 uint16_t type_idx = ref.first->GetClassDef(ref.second).class_idx_;
923 LoadClassType(*ref.first, type_idx, kArg1);
Fred Shih4ee7a662014-07-11 09:59:27 -0700924 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700925
Fred Shihe7f82e22014-08-06 10:46:37 -0700926 uint32_t slow_path_flag_offset = cu_->compiler_driver->GetReferenceSlowFlagOffset();
927 uint32_t disable_flag_offset = cu_->compiler_driver->GetReferenceDisableFlagOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700928 CHECK(slow_path_flag_offset && disable_flag_offset &&
929 (slow_path_flag_offset != disable_flag_offset));
930
931 // intrinsic logic start.
932 RegLocation rl_obj = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -0700933 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih4ee7a662014-07-11 09:59:27 -0700934
935 RegStorage reg_slow_path = AllocTemp();
936 RegStorage reg_disabled = AllocTemp();
Andreas Gampef6815702015-01-20 09:53:48 -0800937 LoadBaseDisp(reg_class, slow_path_flag_offset, reg_slow_path, kSignedByte, kNotVolatile);
938 LoadBaseDisp(reg_class, disable_flag_offset, reg_disabled, kSignedByte, kNotVolatile);
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700939 FreeTemp(reg_class);
940 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -0700941 FreeTemp(reg_disabled);
942
943 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700944 LIR* slow_path_branch;
945 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
946 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
947 slow_path_branch = OpCondBranch(kCondNe, nullptr);
948 } else {
949 // Generate compare and branch.
950 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
951 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700952 FreeTemp(reg_slow_path);
953
954 // slow path not enabled, simply load the referent of the reference object
955 RegLocation rl_dest = InlineTarget(info);
956 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
957 GenNullCheck(rl_obj.reg, info->opt_flags);
958 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
959 kNotVolatile);
960 MarkPossibleNullPointerException(info->opt_flags);
961 StoreValue(rl_dest, rl_result);
962
963 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
964 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
Serguei Katkov9863daf2014-09-04 15:21:32 +0700965 ClobberCallerSave(); // We must clobber everything because slow path will return here
Fred Shih4ee7a662014-07-11 09:59:27 -0700966 return true;
967}
968
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700969bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 // Location of reference to data array
971 int value_offset = mirror::String::ValueOffset().Int32Value();
972 // Location of count
973 int count_offset = mirror::String::CountOffset().Int32Value();
974 // Starting offset within data array
975 int offset_offset = mirror::String::OffsetOffset().Int32Value();
976 // Start of char data with array_
977 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
978
979 RegLocation rl_obj = info->args[0];
980 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -0700981 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -0700982 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800983 RegStorage reg_max;
984 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +0000986 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -0800987 RegStorage reg_off;
988 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -0700989 reg_off = AllocTemp();
990 reg_ptr = AllocTempRef();
991 if (range_check) {
992 reg_max = AllocTemp();
993 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -0800994 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 }
Andreas Gampe98430592014-07-27 19:44:50 -0700996 Load32Disp(rl_obj.reg, offset_offset, reg_off);
997 MarkPossibleNullPointerException(info->opt_flags);
998 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
999 if (range_check) {
1000 // Set up a slow path to allow retry in case of bounds violation */
1001 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
1002 FreeTemp(reg_max);
1003 range_check_branch = OpCondBranch(kCondUge, nullptr);
1004 }
1005 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001006 if (rl_idx.is_const) {
1007 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1008 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001009 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001010 }
buzbee2700f7e2014-03-07 09:46:20 -08001011 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001012 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001013 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001014 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 RegLocation rl_dest = InlineTarget(info);
1016 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001017 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 FreeTemp(reg_off);
1019 FreeTemp(reg_ptr);
1020 StoreValue(rl_dest, rl_result);
1021 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001022 DCHECK(range_check_branch != nullptr);
1023 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001024 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 return true;
1027}
1028
1029// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001030bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 if (cu_->instruction_set == kMips) {
1032 // TODO - add Mips implementation
1033 return false;
1034 }
1035 // dst = src.length();
1036 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001037 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 RegLocation rl_dest = InlineTarget(info);
1039 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001040 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001041 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001042 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 if (is_empty) {
1044 // dst = (dst == 0);
1045 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001046 RegStorage t_reg = AllocTemp();
1047 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1048 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001049 } else if (cu_->instruction_set == kArm64) {
1050 OpRegImm(kOpSub, rl_result.reg, 1);
1051 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001053 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001054 OpRegImm(kOpSub, rl_result.reg, 1);
1055 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 }
1057 }
1058 StoreValue(rl_dest, rl_result);
1059 return true;
1060}
1061
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001062bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Zheng Xua3fe7422014-07-09 14:03:15 +08001063 if (cu_->instruction_set == kMips) {
1064 // TODO - add Mips implementation.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001065 return false;
1066 }
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001067 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
1068 if (rl_dest.s_reg_low == INVALID_SREG) {
1069 // Result is unused, the code is dead. Inlining successful, no code generated.
1070 return true;
1071 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001072 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001073 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001074 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001075 if (IsWide(size)) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001076 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001077 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1078 StoreValueWide(rl_dest, rl_result);
1079 return true;
1080 }
buzbee2700f7e2014-03-07 09:46:20 -08001081 RegStorage r_i_low = rl_i.reg.GetLow();
1082 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001083 // 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 +00001084 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001085 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001086 }
buzbee2700f7e2014-03-07 09:46:20 -08001087 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1088 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1089 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001090 FreeTemp(r_i_low);
1091 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001092 StoreValueWide(rl_dest, rl_result);
1093 } else {
buzbee695d13a2014-04-19 13:32:20 -07001094 DCHECK(size == k32 || size == kSignedHalf);
1095 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001096 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001097 StoreValue(rl_dest, rl_result);
1098 }
1099 return true;
1100}
1101
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001102bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001103 RegLocation rl_dest = InlineTarget(info);
1104 if (rl_dest.s_reg_low == INVALID_SREG) {
1105 // Result is unused, the code is dead. Inlining successful, no code generated.
1106 return true;
1107 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 RegLocation rl_src = info->args[0];
1109 rl_src = LoadValue(rl_src, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001111 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001113 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1114 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1115 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 StoreValue(rl_dest, rl_result);
1117 return true;
1118}
1119
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001120bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001121 RegLocation rl_dest = InlineTargetWide(info);
1122 if (rl_dest.s_reg_low == INVALID_SREG) {
1123 // Result is unused, the code is dead. Inlining successful, no code generated.
1124 return true;
1125 }
Vladimir Markob9823312014-03-20 17:38:43 +00001126 RegLocation rl_src = info->args[0];
1127 rl_src = LoadValueWide(rl_src, kCoreReg);
Vladimir Markob9823312014-03-20 17:38:43 +00001128 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1129
1130 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001131 if (cu_->instruction_set != kX86_64 &&
1132 (cu_->instruction_set == kX86 ||
1133 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001134 OpRegCopyWide(rl_result.reg, rl_src.reg);
1135 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1136 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1137 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001138 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1139 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001140 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001141 }
1142 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 }
Vladimir Markob9823312014-03-20 17:38:43 +00001144
1145 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001146 RegStorage sign_reg;
1147 if (cu_->instruction_set == kX86_64) {
1148 sign_reg = AllocTempWide();
1149 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1150 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1151 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1152 } else {
1153 sign_reg = AllocTemp();
1154 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1155 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1156 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1157 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1158 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1159 }
buzbee082833c2014-05-17 23:16:26 -07001160 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001161 StoreValueWide(rl_dest, rl_result);
1162 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163}
1164
Serban Constantinescu23abec92014-07-02 16:13:38 +01001165bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001166 // Currently implemented only for ARM64.
1167 UNUSED(info, size);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001168 return false;
1169}
1170
1171bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001172 // Currently implemented only for ARM64.
1173 UNUSED(info, is_min, is_double);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001174 return false;
1175}
1176
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001177bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001178 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001179 return false;
1180}
1181
1182bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001183 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001184 return false;
1185}
1186
1187bool Mir2Lir::GenInlinedRint(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001188 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001189 return false;
1190}
1191
1192bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001193 UNUSED(info, is_double);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001194 return false;
1195}
1196
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001197bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 if (cu_->instruction_set == kMips) {
1199 // TODO - add Mips implementation
1200 return false;
1201 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 RegLocation rl_dest = InlineTarget(info);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001203 if (rl_dest.s_reg_low == INVALID_SREG) {
1204 // Result is unused, the code is dead. Inlining successful, no code generated.
1205 return true;
1206 }
1207 RegLocation rl_src = info->args[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 StoreValue(rl_dest, rl_src);
1209 return true;
1210}
1211
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001212bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 if (cu_->instruction_set == kMips) {
1214 // TODO - add Mips implementation
1215 return false;
1216 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 RegLocation rl_dest = InlineTargetWide(info);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001218 if (rl_dest.s_reg_low == INVALID_SREG) {
1219 // Result is unused, the code is dead. Inlining successful, no code generated.
1220 return true;
1221 }
1222 RegLocation rl_src = info->args[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 StoreValueWide(rl_dest, rl_src);
1224 return true;
1225}
1226
DaniilSokolov70c4f062014-06-24 17:34:00 -07001227bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001228 UNUSED(info);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001229 return false;
1230}
1231
1232
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001234 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 * otherwise bails to standard library code.
1236 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001237bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001238 RegLocation rl_obj = info->args[0];
1239 RegLocation rl_char = info->args[1];
1240 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1241 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1242 return false;
1243 }
1244
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001245 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001247 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1248 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1249 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 LoadValueDirectFixed(rl_obj, reg_ptr);
1252 LoadValueDirectFixed(rl_char, reg_char);
1253 if (zero_based) {
1254 LoadConstant(reg_start, 0);
1255 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001256 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 LoadValueDirectFixed(rl_start, reg_start);
1258 }
Andreas Gampe98430592014-07-27 19:44:50 -07001259 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001260 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001261 LIR* high_code_point_branch =
1262 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001264 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001265 if (!rl_char.is_const) {
1266 // Add the slow path for code points beyond 0xFFFF.
1267 DCHECK(high_code_point_branch != nullptr);
1268 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1269 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001270 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001271 ClobberCallerSave(); // We must clobber everything because slow path will return here
Vladimir Marko3bc86152014-03-13 14:11:28 +00001272 } else {
1273 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1274 DCHECK(high_code_point_branch == nullptr);
1275 }
buzbeea0cd2d72014-06-01 09:33:49 -07001276 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 RegLocation rl_dest = InlineTarget(info);
1278 StoreValue(rl_dest, rl_return);
1279 return true;
1280}
1281
1282/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001283bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 if (cu_->instruction_set == kMips) {
1285 // TODO - add Mips implementation
1286 return false;
1287 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001288 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001290 RegStorage reg_this = TargetReg(kArg0, kRef);
1291 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292
1293 RegLocation rl_this = info->args[0];
1294 RegLocation rl_cmp = info->args[1];
1295 LoadValueDirectFixed(rl_this, reg_this);
1296 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001297 RegStorage r_tgt;
1298 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001299 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001300 } else {
1301 r_tgt = RegStorage::InvalidReg();
1302 }
Dave Allisonf9439142014-03-27 15:10:22 -07001303 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001304 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001305 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001306 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001307 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001309 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001310 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 RegLocation rl_dest = InlineTarget(info);
1312 StoreValue(rl_dest, rl_return);
1313 return true;
1314}
1315
1316bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1317 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001318
1319 // Early exit if the result is unused.
1320 if (rl_dest.orig_sreg < 0) {
1321 return true;
1322 }
1323
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001324 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001325
Andreas Gamped500b532015-01-16 22:09:55 -08001326 if (Is64BitInstructionSet(cu_->instruction_set)) {
1327 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1328 kNotVolatile);
1329 } else {
1330 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 }
Andreas Gamped500b532015-01-16 22:09:55 -08001332
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 StoreValue(rl_dest, rl_result);
1334 return true;
1335}
1336
1337bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1338 bool is_long, bool is_volatile) {
1339 if (cu_->instruction_set == kMips) {
1340 // TODO - add Mips implementation
1341 return false;
1342 }
1343 // Unused - RegLocation rl_src_unsafe = info->args[0];
1344 RegLocation rl_src_obj = info->args[1]; // Object
1345 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001346 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001347 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001348
buzbeea0cd2d72014-06-01 09:33:49 -07001349 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001351 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001353 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1354 || cu_->instruction_set == kArm64) {
1355 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001356 } else {
1357 RegStorage rl_temp_offset = AllocTemp();
1358 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001359 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001360 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001361 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001363 if (rl_result.ref) {
1364 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1365 } else {
1366 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1367 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001368 }
1369
1370 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001371 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001372 }
1373
1374 if (is_long) {
1375 StoreValueWide(rl_dest, rl_result);
1376 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 StoreValue(rl_dest, rl_result);
1378 }
1379 return true;
1380}
1381
1382bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1383 bool is_object, bool is_volatile, bool is_ordered) {
1384 if (cu_->instruction_set == kMips) {
1385 // TODO - add Mips implementation
1386 return false;
1387 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 // Unused - RegLocation rl_src_unsafe = info->args[0];
1389 RegLocation rl_src_obj = info->args[1]; // Object
1390 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001391 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001392 RegLocation rl_src_value = info->args[4]; // value to store
1393 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001394 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 }
buzbeea0cd2d72014-06-01 09:33:49 -07001396 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1398 RegLocation rl_value;
1399 if (is_long) {
1400 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001401 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1402 || cu_->instruction_set == kArm64) {
1403 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001404 } else {
1405 RegStorage rl_temp_offset = AllocTemp();
1406 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001407 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001408 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001409 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410 } else {
buzbee7c02e912014-10-03 13:14:17 -07001411 rl_value = LoadValue(rl_src_value, LocToRegClass(rl_src_value));
Matteo Franchin255e0142014-07-04 13:50:41 +01001412 if (rl_value.ref) {
1413 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1414 } else {
1415 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1416 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001418
1419 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001420 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001421
Brian Carlstrom7940e442013-07-12 13:46:57 -07001422 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001423 // Prevent reordering with a subsequent volatile load.
1424 // May also be needed to address store atomicity issues.
1425 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 }
1427 if (is_object) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001428 MarkGCCard(0, rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 }
1430 return true;
1431}
1432
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001433void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001434 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001435 const DexFile* dex_file = info->method_ref.dex_file;
1436 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(dex_file)
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001437 ->GenIntrinsic(this, info)) {
1438 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001439 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001440 GenInvokeNoInline(info);
1441}
1442
1443void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 int call_state = 0;
1445 LIR* null_ck;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001446 LIR** p_null_ck = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 NextCallInsn next_call_insn;
1448 FlushAllRegs(); /* Everything to home location */
1449 // Explicit register usage
1450 LockCallTemps();
1451
Vladimir Markof096aad2014-01-23 15:51:58 +00001452 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1453 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1454 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
Vladimir Markof4da6752014-08-01 19:04:18 +01001455 info->type = method_info.GetSharpType();
Vladimir Markof096aad2014-01-23 15:51:58 +00001456 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001457 bool skip_this;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001458
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001461 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001462 } else if (info->type == kDirect) {
1463 if (fast_path) {
1464 p_null_ck = &null_ck;
1465 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001466 next_call_insn = fast_path ? GetNextSDCallInsn() : NextDirectCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 skip_this = false;
1468 } else if (info->type == kStatic) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001469 next_call_insn = fast_path ? GetNextSDCallInsn() : NextStaticCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001470 skip_this = false;
1471 } else if (info->type == kSuper) {
1472 DCHECK(!fast_path); // Fast path is a direct call.
1473 next_call_insn = NextSuperCallInsnSP;
1474 skip_this = false;
1475 } else {
1476 DCHECK_EQ(info->type, kVirtual);
1477 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1478 skip_this = fast_path;
1479 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001480 MethodReference target_method = method_info.GetTargetMethod();
Serguei Katkov717a3e42014-11-13 17:19:42 +06001481 call_state = GenDalvikArgs(info, call_state, p_null_ck,
1482 next_call_insn, target_method, method_info.VTableIndex(),
1483 method_info.DirectCode(), method_info.DirectMethod(),
1484 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001485 // Finish up any of the call sequence not interleaved in arg loading
1486 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001487 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001488 method_info.DirectCode(), method_info.DirectMethod(),
1489 original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001490 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001491 LIR* call_insn = GenCallInsn(method_info);
Vladimir Markof4da6752014-08-01 19:04:18 +01001492 MarkSafepointPC(call_insn);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493
Vladimir Markobfe400b2014-12-19 19:27:26 +00001494 FreeCallTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 if (info->result.location != kLocInvalid) {
1496 // We have a following MOVE_RESULT - do it now.
1497 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001498 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 StoreValueWide(info->result, ret_loc);
1500 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001501 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 StoreValue(info->result, ret_loc);
1503 }
1504 }
1505}
1506
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507} // namespace art