blob: bb5b0cdd22e18022cbf326001c70dedbb57fd0d2 [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) {
251 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700252 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700253 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800254 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700255 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700256 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700257 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700258 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700259 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800260 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700261 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700262 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700264 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700265 if (arg1.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700266 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700267 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700268 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700269 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 }
271 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000272 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700273 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274}
275
Mingyao Yang80365d92014-04-18 12:10:58 -0700276void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700277 WideKind arg0_kind = arg0.GetWideKind();
278 WideKind arg1_kind = arg1.GetWideKind();
279 if (IsSameReg(arg1, TargetReg(kArg0, arg1_kind))) {
280 if (IsSameReg(arg0, TargetReg(kArg1, arg0_kind))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700281 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampeccc60262014-07-04 18:02:38 -0700282 OpRegCopy(TargetReg(kArg2, arg1_kind), arg1);
283 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
284 OpRegCopy(TargetReg(kArg1, arg1_kind), TargetReg(kArg2, arg1_kind));
Mingyao Yang80365d92014-04-18 12:10:58 -0700285 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700286 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
287 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700288 }
289 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700290 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
291 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700292 }
293}
294
Andreas Gampe98430592014-07-27 19:44:50 -0700295void Mir2Lir::CallRuntimeHelperRegReg(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800296 RegStorage arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700297 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700298 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000299 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700300 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301}
302
Andreas Gampe98430592014-07-27 19:44:50 -0700303void Mir2Lir::CallRuntimeHelperRegRegImm(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800304 RegStorage arg1, int arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700305 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700306 CopyToArgumentRegs(arg0, arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700307 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000308 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700309 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310}
311
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800312void Mir2Lir::CallRuntimeHelperImmRegLocationMethod(QuickEntrypointEnum trampoline, int arg0,
313 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700314 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800315 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
316 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
Andreas Gampeccc60262014-07-04 18:02:38 -0700317 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000318 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700319 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320}
321
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800322void Mir2Lir::CallRuntimeHelperImmImmMethod(QuickEntrypointEnum trampoline, int arg0, int arg1,
Andreas Gampe98430592014-07-27 19:44:50 -0700323 bool safepoint_pc) {
324 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800325 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
326 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700327 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000328 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700329 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330}
331
Andreas Gampe98430592014-07-27 19:44:50 -0700332void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(QuickEntrypointEnum trampoline, int arg0,
333 RegLocation arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 RegLocation arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700335 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700336 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
337 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700338 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700340 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700342 LoadValueDirectWideFixed(arg2, TargetReg(kArg2, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700344 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000345 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700346 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347}
348
Andreas Gampeccc60262014-07-04 18:02:38 -0700349void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(
Andreas Gampe98430592014-07-27 19:44:50 -0700350 QuickEntrypointEnum trampoline,
Andreas Gampeccc60262014-07-04 18:02:38 -0700351 RegLocation arg0,
352 RegLocation arg1,
353 RegLocation arg2,
354 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700355 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700356 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
357 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
358 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000359 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700360 CallHelper(r_tgt, trampoline, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700361}
362
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363/*
364 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100365 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 * assignment of promoted arguments.
367 *
368 * ArgLocs is an array of location records describing the incoming arguments
369 * with one location record per word of argument.
370 */
Zheng Xu5667fdb2014-10-23 18:29:55 +0800371// TODO: Support 64-bit argument registers.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700372void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800374 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 * It will attempt to keep kArg0 live (or copy it to home location
376 * if promoted).
377 */
378 RegLocation rl_src = rl_method;
379 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -0700380 rl_src.reg = TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700382 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700383 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 // If Method* has been promoted, explicitly flush
385 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700386 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 }
388
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700389 if (mir_graph_->GetNumOfInVRs() == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800391 }
392
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700393 int start_vreg = mir_graph_->GetFirstInVR();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 /*
395 * Copy incoming arguments to their proper home locations.
396 * NOTE: an older version of dx had an issue in which
397 * it would reuse static method argument registers.
398 * This could result in the same Dalvik virtual register
399 * being promoted to both core and fp regs. To account for this,
400 * we only copy to the corresponding promoted physical register
401 * if it matches the type of the SSA name for the incoming
402 * argument. It is also possible that long and double arguments
403 * end up half-promoted. In those cases, we must flush the promoted
404 * half to memory as well.
405 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100406 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600407 RegLocation* t_loc = nullptr;
408 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i += t_loc->wide ? 2 : 1) {
409 // get reg corresponding to input
buzbee2700f7e2014-03-07 09:46:20 -0800410 RegStorage reg = GetArgMappingToPhysicalReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600411 t_loc = &ArgLocs[i];
412
413 // If the wide input appeared as single, flush it and go
414 // as it comes from memory.
415 if (t_loc->wide && reg.Valid() && !reg.Is64Bit()) {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000416 // The memory already holds the half. Don't do anything.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600417 reg = RegStorage::InvalidReg();
418 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800419
buzbee2700f7e2014-03-07 09:46:20 -0800420 if (reg.Valid()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600421 // If arriving in register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422
Serguei Katkov717a3e42014-11-13 17:19:42 +0600423 // We have already updated the arg location with promoted info
424 // so we can be based on it.
425 if (t_loc->location == kLocPhysReg) {
426 // Just copy it.
427 if (t_loc->wide) {
428 OpRegCopyWide(t_loc->reg, reg);
429 } else {
430 OpRegCopy(t_loc->reg, reg);
431 }
432 } else {
433 // Needs flush.
434 int offset = SRegOffset(start_vreg + i);
435 if (t_loc->ref) {
436 StoreRefDisp(TargetPtrReg(kSp), offset, reg, kNotVolatile);
437 } else {
438 StoreBaseDisp(TargetPtrReg(kSp), offset, reg, t_loc->wide ? k64 : k32, kNotVolatile);
buzbeed0a03b82013-09-14 08:21:05 -0700439 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 } else {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600442 // If arriving in frame & promoted.
443 if (t_loc->location == kLocPhysReg) {
444 int offset = SRegOffset(start_vreg + i);
445 if (t_loc->ref) {
446 LoadRefDisp(TargetPtrReg(kSp), offset, t_loc->reg, kNotVolatile);
447 } else {
448 LoadBaseDisp(TargetPtrReg(kSp), offset, t_loc->reg, t_loc->wide ? k64 : k32,
449 kNotVolatile);
450 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 }
452 }
453 }
454}
455
Andreas Gampeccc60262014-07-04 18:02:38 -0700456static void CommonCallCodeLoadThisIntoArg1(const CallInfo* info, Mir2Lir* cg) {
457 RegLocation rl_arg = info->args[0];
458 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1, kRef));
459}
460
461static void CommonCallCodeLoadClassIntoArg0(const CallInfo* info, Mir2Lir* cg) {
462 cg->GenNullCheck(cg->TargetReg(kArg1, kRef), info->opt_flags);
463 // get this->klass_ [use kArg1, set kArg0]
464 cg->LoadRefDisp(cg->TargetReg(kArg1, kRef), mirror::Object::ClassOffset().Int32Value(),
465 cg->TargetReg(kArg0, kRef),
466 kNotVolatile);
467 cg->MarkPossibleNullPointerException(info->opt_flags);
468}
469
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700470static bool CommonCallCodeLoadCodePointerIntoInvokeTgt(const RegStorage* alt_from,
Andreas Gampeccc60262014-07-04 18:02:38 -0700471 const CompilationUnit* cu, Mir2Lir* cg) {
472 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Mathieu Chartier2d721012014-11-10 11:08:06 -0800473 int32_t offset = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
474 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
Andreas Gampeccc60262014-07-04 18:02:38 -0700475 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
Mathieu Chartier2d721012014-11-10 11:08:06 -0800476 cg->LoadWordDisp(alt_from == nullptr ? cg->TargetReg(kArg0, kRef) : *alt_from, offset,
Andreas Gampeccc60262014-07-04 18:02:38 -0700477 cg->TargetPtrReg(kInvokeTgt));
478 return true;
479 }
480 return false;
481}
482
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483/*
484 * Bit of a hack here - in the absence of a real scheduling pass,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 * emit the next instruction in a virtual invoke sequence.
486 * We can use kLr as a temp prior to target address loading
487 * Note also that we'll load the first argument ("this") into
Serguei Katkov717a3e42014-11-13 17:19:42 +0600488 * kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 */
490static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
491 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700492 uint32_t method_idx, uintptr_t, uintptr_t,
493 InvokeType) {
494 UNUSED(target_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
496 /*
497 * This is the fast path in which the target virtual method is
498 * fully resolved at compile time.
499 */
500 switch (state) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700501 case 0:
502 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700504 case 1:
505 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
506 // Includes a null-check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700508 case 2: {
509 // Get this->klass_.embedded_vtable[method_idx] [usr kArg0, set kArg0]
510 int32_t offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
511 method_idx * sizeof(mirror::Class::VTableEntry);
512 // Load target method from embedded vtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700513 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700515 }
516 case 3:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700517 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700518 break; // kInvokeTgt := kArg0->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700520 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
521 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 default:
523 return -1;
524 }
525 return state + 1;
526}
527
528/*
Jeff Hao88474b42013-10-23 16:24:40 -0700529 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
530 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
531 * 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 +0600532 * argument ("this") into kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 */
534static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
535 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700536 uint32_t method_idx, uintptr_t, uintptr_t, InvokeType) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538
Jeff Hao88474b42013-10-23 16:24:40 -0700539 switch (state) {
540 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700541 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Andreas Gampeccc60262014-07-04 18:02:38 -0700542 cg->LoadConstant(cg->TargetReg(kHiddenArg, kNotWide), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400543 if (cu->instruction_set == kX86) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700544 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, kNotWide), cg->TargetReg(kHiddenArg, kNotWide));
Jeff Hao88474b42013-10-23 16:24:40 -0700545 }
546 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700547 case 1:
548 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Jeff Hao88474b42013-10-23 16:24:40 -0700549 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700550 case 2:
551 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
552 // Includes a null-check.
Jeff Hao88474b42013-10-23 16:24:40 -0700553 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700554 case 3: { // Get target method [use kInvokeTgt, set kArg0]
555 int32_t offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
556 (method_idx % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
557 // Load target method from embedded imtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700558 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700559 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700560 }
561 case 4:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700562 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700563 break; // kInvokeTgt := kArg0->entrypoint
Jeff Hao88474b42013-10-23 16:24:40 -0700564 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700565 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
566 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 default:
568 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 }
570 return state + 1;
571}
572
Andreas Gampeccc60262014-07-04 18:02:38 -0700573static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info,
Andreas Gampe98430592014-07-27 19:44:50 -0700574 QuickEntrypointEnum trampoline, int state,
Andreas Gampeccc60262014-07-04 18:02:38 -0700575 const MethodReference& target_method, uint32_t method_idx) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700576 UNUSED(info, method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Andreas Gampe98430592014-07-27 19:44:50 -0700578
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 /*
580 * This handles the case in which the base method is not fully
581 * resolved at compile time, we bail to a runtime helper.
582 */
583 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700584 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 // Load trampoline target
Andreas Gampe98430592014-07-27 19:44:50 -0700586 int32_t disp;
587 if (cu->target64) {
588 disp = GetThreadOffset<8>(trampoline).Int32Value();
589 } else {
590 disp = GetThreadOffset<4>(trampoline).Int32Value();
591 }
592 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), disp, cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 }
594 // Load kArg0 with method index
595 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampeccc60262014-07-04 18:02:38 -0700596 cg->LoadConstant(cg->TargetReg(kArg0, kNotWide), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 return 1;
598 }
599 return -1;
600}
601
602static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
603 int state,
604 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700605 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700606 return NextInvokeInsnSP(cu, info, kQuickInvokeStaticTrampolineWithAccessCheck, state,
607 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608}
609
610static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
611 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700612 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700613 return NextInvokeInsnSP(cu, info, kQuickInvokeDirectTrampolineWithAccessCheck, state,
614 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615}
616
617static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, 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, kQuickInvokeSuperTrampolineWithAccessCheck, state,
621 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622}
623
624static int NextVCallInsnSP(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, kQuickInvokeVirtualTrampolineWithAccessCheck, state,
628 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629}
630
631static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
632 CallInfo* info, int state,
633 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700634 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700635 return NextInvokeInsnSP(cu, info, kQuickInvokeInterfaceTrampolineWithAccessCheck, state,
636 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637}
638
Dave Allison69dfe512014-07-11 17:11:58 +0000639// Default implementation of implicit null pointer check.
640// Overridden by arch specific as necessary.
641void Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
642 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
643 return;
644 }
645 RegStorage tmp = AllocTemp();
646 Load32Disp(reg, 0, tmp);
647 MarkPossibleNullPointerException(opt_flags);
648 FreeTemp(tmp);
649}
650
Serguei Katkov717a3e42014-11-13 17:19:42 +0600651/**
652 * @brief Used to flush promoted registers if they are used as argument
653 * in an invocation.
654 * @param info the infromation about arguments for invocation.
655 * @param start the first argument we should start to look from.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 */
Serguei Katkov717a3e42014-11-13 17:19:42 +0600657void Mir2Lir::GenDalvikArgsFlushPromoted(CallInfo* info, int start) {
658 if (cu_->disable_opt & (1 << kPromoteRegs)) {
659 // This make sense only if promotion is enabled.
660 return;
661 }
662 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 // Scan the rest of the args - if in phys_reg flush to memory
Serguei Katkov717a3e42014-11-13 17:19:42 +0600664 for (int next_arg = start; next_arg < info->num_arg_words;) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 RegLocation loc = info->args[next_arg];
666 if (loc.wide) {
667 loc = UpdateLocWide(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600668 if (loc.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700669 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 }
671 next_arg += 2;
672 } else {
673 loc = UpdateLoc(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600674 if (loc.location == kLocPhysReg) {
675 if (loc.ref) {
676 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
677 } else {
678 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k32,
679 kNotVolatile);
680 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 }
682 next_arg++;
683 }
684 }
Serguei Katkov717a3e42014-11-13 17:19:42 +0600685}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686
Serguei Katkov717a3e42014-11-13 17:19:42 +0600687/**
688 * @brief Used to optimize the copying of VRs which are arguments of invocation.
689 * Please note that you should flush promoted registers first if you copy.
690 * If implementation does copying it may skip several of the first VRs but must copy
691 * till the end. Implementation must return the number of skipped VRs
692 * (it might be all VRs).
693 * @see GenDalvikArgsFlushPromoted
694 * @param info the information about arguments for invocation.
695 * @param first the first argument we should start to look from.
696 * @param count the number of remaining arguments we can handle.
697 * @return the number of arguments which we did not handle. Unhandled arguments
698 * must be attached to the first one.
699 */
700int Mir2Lir::GenDalvikArgsBulkCopy(CallInfo* info, int first, int count) {
701 // call is pretty expensive, let's use it if count is big.
702 if (count > 16) {
703 GenDalvikArgsFlushPromoted(info, first);
704 int start_offset = SRegOffset(info->args[first].s_reg_low);
705 int outs_offset = StackVisitor::GetOutVROffset(first, cu_->instruction_set);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800706
Andreas Gampeccc60262014-07-04 18:02:38 -0700707 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
708 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -0700709 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +0600710 count * 4, false);
711 count = 0;
712 }
713 return count;
714}
715
716int Mir2Lir::GenDalvikArgs(CallInfo* info, int call_state,
717 LIR** pcrLabel, NextCallInsn next_call_insn,
718 const MethodReference& target_method,
719 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
720 InvokeType type, bool skip_this) {
721 // If no arguments, just return.
722 if (info->num_arg_words == 0)
723 return call_state;
724
725 const int start_index = skip_this ? 1 : 0;
726
727 // Get architecture dependent mapping between output VRs and physical registers
728 // basing on shorty of method to call.
729 InToRegStorageMapping in_to_reg_storage_mapping(arena_);
730 {
731 const char* target_shorty = mir_graph_->GetShortyFromMethodReference(target_method);
732 ShortyIterator shorty_iterator(target_shorty, type == kStatic);
733 in_to_reg_storage_mapping.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 }
735
Serguei Katkov717a3e42014-11-13 17:19:42 +0600736 int stack_map_start = std::max(in_to_reg_storage_mapping.GetMaxMappedIn() + 1, start_index);
737 if ((stack_map_start < info->num_arg_words) && info->args[stack_map_start].high_word) {
738 // It is possible that the last mapped reg is 32 bit while arg is 64-bit.
739 // It will be handled together with low part mapped to register.
740 stack_map_start++;
741 }
742 int regs_left_to_pass_via_stack = info->num_arg_words - stack_map_start;
743
744 // If it is a range case we can try to copy remaining VRs (not mapped to physical registers)
745 // using more optimal algorithm.
746 if (info->is_range && regs_left_to_pass_via_stack > 1) {
747 regs_left_to_pass_via_stack = GenDalvikArgsBulkCopy(info, stack_map_start,
748 regs_left_to_pass_via_stack);
749 }
750
751 // Now handle any remaining VRs mapped to stack.
752 if (in_to_reg_storage_mapping.HasArgumentsOnStack()) {
753 // Two temps but do not use kArg1, it might be this which we can skip.
754 // Separate single and wide - it can give some advantage.
755 RegStorage regRef = TargetReg(kArg3, kRef);
756 RegStorage regSingle = TargetReg(kArg3, kNotWide);
757 RegStorage regWide = TargetReg(kArg2, kWide);
758 for (int i = start_index;
759 i < stack_map_start + regs_left_to_pass_via_stack; i++) {
760 RegLocation rl_arg = info->args[i];
761 rl_arg = UpdateRawLoc(rl_arg);
762 RegStorage reg = in_to_reg_storage_mapping.Get(i);
763 if (!reg.Valid()) {
764 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
765 {
766 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
767 if (rl_arg.wide) {
768 if (rl_arg.location == kLocPhysReg) {
769 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
770 } else {
771 LoadValueDirectWideFixed(rl_arg, regWide);
772 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regWide, k64, kNotVolatile);
773 }
774 } else {
775 if (rl_arg.location == kLocPhysReg) {
776 if (rl_arg.ref) {
777 StoreRefDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, kNotVolatile);
778 } else {
779 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k32, kNotVolatile);
780 }
781 } else {
782 if (rl_arg.ref) {
783 LoadValueDirectFixed(rl_arg, regRef);
784 StoreRefDisp(TargetPtrReg(kSp), out_offset, regRef, kNotVolatile);
785 } else {
786 LoadValueDirectFixed(rl_arg, regSingle);
787 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regSingle, k32, kNotVolatile);
788 }
789 }
790 }
791 }
792 call_state = next_call_insn(cu_, info, call_state, target_method,
793 vtable_idx, direct_code, direct_method, type);
794 }
795 if (rl_arg.wide) {
796 i++;
797 }
798 }
799 }
800
801 // Finish with VRs mapped to physical registers.
802 for (int i = start_index; i < stack_map_start; i++) {
803 RegLocation rl_arg = info->args[i];
804 rl_arg = UpdateRawLoc(rl_arg);
805 RegStorage reg = in_to_reg_storage_mapping.Get(i);
806 if (reg.Valid()) {
807 if (rl_arg.wide) {
808 // if reg is not 64-bit (it is half of 64-bit) then handle it separately.
809 if (!reg.Is64Bit()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600810 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
811 if (rl_arg.location == kLocPhysReg) {
812 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000813 // Dump it to memory.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600814 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
815 LoadBaseDisp(TargetPtrReg(kSp), out_offset, reg, k32, kNotVolatile);
816 } else {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000817 int high_offset = StackVisitor::GetOutVROffset(i + 1, cu_->instruction_set);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600818 // First, use target reg for high part.
819 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low + 1), reg, k32,
820 kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000821 StoreBaseDisp(TargetPtrReg(kSp), high_offset, reg, k32, kNotVolatile);
822 // Now, use target reg for low part.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600823 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low), reg, k32, kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000824 int low_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
825 // And store it to the expected memory location.
826 StoreBaseDisp(TargetPtrReg(kSp), low_offset, reg, k32, kNotVolatile);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600827 }
828 } else {
829 LoadValueDirectWideFixed(rl_arg, reg);
830 }
831 } else {
832 LoadValueDirectFixed(rl_arg, reg);
833 }
834 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
835 direct_code, direct_method, type);
836 }
837 if (rl_arg.wide) {
838 i++;
839 }
840 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841
842 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
843 direct_code, direct_method, type);
844 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +0000845 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700846 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700847 } else {
848 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +0000849 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700850 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 }
852 return call_state;
853}
854
Serguei Katkov717a3e42014-11-13 17:19:42 +0600855RegStorage Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
856 if (!in_to_reg_storage_mapping_.IsInitialized()) {
857 ShortyIterator shorty_iterator(cu_->shorty, cu_->invoke_type == kStatic);
858 in_to_reg_storage_mapping_.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
859 }
860 return in_to_reg_storage_mapping_.Get(arg_num);
861}
862
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700863RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 RegLocation res;
865 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700866 // If result is unused, return a sink target based on type of invoke target.
867 res = GetReturn(ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 } else {
869 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700870 DCHECK_EQ(LocToRegClass(res),
871 ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 }
873 return res;
874}
875
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700876RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 RegLocation res;
878 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700879 // If result is unused, return a sink target based on type of invoke target.
880 res = GetReturnWide(ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[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),
884 ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 }
886 return res;
887}
888
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -0700889bool Mir2Lir::GenInlinedReferenceGetReferent(CallInfo* info) {
Fred Shih4ee7a662014-07-11 09:59:27 -0700890 if (cu_->instruction_set == kMips) {
891 // TODO - add Mips implementation
892 return false;
893 }
894
Fred Shih4ee7a662014-07-11 09:59:27 -0700895 bool use_direct_type_ptr;
896 uintptr_t direct_type_ptr;
Fred Shihe7f82e22014-08-06 10:46:37 -0700897 ClassReference ref;
898 if (!cu_->compiler_driver->CanEmbedReferenceTypeInCode(&ref,
899 &use_direct_type_ptr, &direct_type_ptr)) {
900 return false;
901 }
902
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700903 RegStorage reg_class = TargetReg(kArg1, kRef);
904 Clobber(reg_class);
905 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -0700906 if (use_direct_type_ptr) {
907 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -0700908 } else {
Fred Shihe7f82e22014-08-06 10:46:37 -0700909 uint16_t type_idx = ref.first->GetClassDef(ref.second).class_idx_;
910 LoadClassType(*ref.first, type_idx, kArg1);
Fred Shih4ee7a662014-07-11 09:59:27 -0700911 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700912
Fred Shihe7f82e22014-08-06 10:46:37 -0700913 uint32_t slow_path_flag_offset = cu_->compiler_driver->GetReferenceSlowFlagOffset();
914 uint32_t disable_flag_offset = cu_->compiler_driver->GetReferenceDisableFlagOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700915 CHECK(slow_path_flag_offset && disable_flag_offset &&
916 (slow_path_flag_offset != disable_flag_offset));
917
918 // intrinsic logic start.
919 RegLocation rl_obj = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -0700920 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih4ee7a662014-07-11 09:59:27 -0700921
922 RegStorage reg_slow_path = AllocTemp();
923 RegStorage reg_disabled = AllocTemp();
Andreas Gampef6815702015-01-20 09:53:48 -0800924 LoadBaseDisp(reg_class, slow_path_flag_offset, reg_slow_path, kSignedByte, kNotVolatile);
925 LoadBaseDisp(reg_class, disable_flag_offset, reg_disabled, kSignedByte, kNotVolatile);
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700926 FreeTemp(reg_class);
927 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -0700928 FreeTemp(reg_disabled);
929
930 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700931 LIR* slow_path_branch;
932 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
933 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
934 slow_path_branch = OpCondBranch(kCondNe, nullptr);
935 } else {
936 // Generate compare and branch.
937 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
938 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700939 FreeTemp(reg_slow_path);
940
941 // slow path not enabled, simply load the referent of the reference object
942 RegLocation rl_dest = InlineTarget(info);
943 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
944 GenNullCheck(rl_obj.reg, info->opt_flags);
945 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
946 kNotVolatile);
947 MarkPossibleNullPointerException(info->opt_flags);
948 StoreValue(rl_dest, rl_result);
949
950 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
951 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
Serguei Katkov9863daf2014-09-04 15:21:32 +0700952 ClobberCallerSave(); // We must clobber everything because slow path will return here
Fred Shih4ee7a662014-07-11 09:59:27 -0700953 return true;
954}
955
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700956bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 // Location of reference to data array
958 int value_offset = mirror::String::ValueOffset().Int32Value();
959 // Location of count
960 int count_offset = mirror::String::CountOffset().Int32Value();
961 // Starting offset within data array
962 int offset_offset = mirror::String::OffsetOffset().Int32Value();
963 // Start of char data with array_
964 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
965
966 RegLocation rl_obj = info->args[0];
967 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -0700968 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -0700969 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800970 RegStorage reg_max;
971 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +0000973 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -0800974 RegStorage reg_off;
975 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -0700976 reg_off = AllocTemp();
977 reg_ptr = AllocTempRef();
978 if (range_check) {
979 reg_max = AllocTemp();
980 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -0800981 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 }
Andreas Gampe98430592014-07-27 19:44:50 -0700983 Load32Disp(rl_obj.reg, offset_offset, reg_off);
984 MarkPossibleNullPointerException(info->opt_flags);
985 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
986 if (range_check) {
987 // Set up a slow path to allow retry in case of bounds violation */
988 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
989 FreeTemp(reg_max);
990 range_check_branch = OpCondBranch(kCondUge, nullptr);
991 }
992 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -0800993 if (rl_idx.is_const) {
994 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
995 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800996 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -0800997 }
buzbee2700f7e2014-03-07 09:46:20 -0800998 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000999 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001000 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001001 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 RegLocation rl_dest = InlineTarget(info);
1003 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001004 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005 FreeTemp(reg_off);
1006 FreeTemp(reg_ptr);
1007 StoreValue(rl_dest, rl_result);
1008 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001009 DCHECK(range_check_branch != nullptr);
1010 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001011 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 return true;
1014}
1015
1016// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001017bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 if (cu_->instruction_set == kMips) {
1019 // TODO - add Mips implementation
1020 return false;
1021 }
1022 // dst = src.length();
1023 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001024 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 RegLocation rl_dest = InlineTarget(info);
1026 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001027 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001028 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001029 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 if (is_empty) {
1031 // dst = (dst == 0);
1032 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001033 RegStorage t_reg = AllocTemp();
1034 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1035 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001036 } else if (cu_->instruction_set == kArm64) {
1037 OpRegImm(kOpSub, rl_result.reg, 1);
1038 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001040 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001041 OpRegImm(kOpSub, rl_result.reg, 1);
1042 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 }
1044 }
1045 StoreValue(rl_dest, rl_result);
1046 return true;
1047}
1048
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001049bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Zheng Xua3fe7422014-07-09 14:03:15 +08001050 if (cu_->instruction_set == kMips) {
1051 // TODO - add Mips implementation.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001052 return false;
1053 }
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001054 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
1055 if (rl_dest.s_reg_low == INVALID_SREG) {
1056 // Result is unused, the code is dead. Inlining successful, no code generated.
1057 return true;
1058 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001059 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001060 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001061 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001062 if (IsWide(size)) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001063 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001064 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1065 StoreValueWide(rl_dest, rl_result);
1066 return true;
1067 }
buzbee2700f7e2014-03-07 09:46:20 -08001068 RegStorage r_i_low = rl_i.reg.GetLow();
1069 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001070 // 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 +00001071 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001072 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001073 }
buzbee2700f7e2014-03-07 09:46:20 -08001074 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1075 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1076 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001077 FreeTemp(r_i_low);
1078 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001079 StoreValueWide(rl_dest, rl_result);
1080 } else {
buzbee695d13a2014-04-19 13:32:20 -07001081 DCHECK(size == k32 || size == kSignedHalf);
1082 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001083 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001084 StoreValue(rl_dest, rl_result);
1085 }
1086 return true;
1087}
1088
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001089bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001090 RegLocation rl_dest = InlineTarget(info);
1091 if (rl_dest.s_reg_low == INVALID_SREG) {
1092 // Result is unused, the code is dead. Inlining successful, no code generated.
1093 return true;
1094 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 RegLocation rl_src = info->args[0];
1096 rl_src = LoadValue(rl_src, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001098 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001100 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1101 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1102 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 StoreValue(rl_dest, rl_result);
1104 return true;
1105}
1106
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001107bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001108 RegLocation rl_dest = InlineTargetWide(info);
1109 if (rl_dest.s_reg_low == INVALID_SREG) {
1110 // Result is unused, the code is dead. Inlining successful, no code generated.
1111 return true;
1112 }
Vladimir Markob9823312014-03-20 17:38:43 +00001113 RegLocation rl_src = info->args[0];
1114 rl_src = LoadValueWide(rl_src, kCoreReg);
Vladimir Markob9823312014-03-20 17:38:43 +00001115 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1116
1117 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001118 if (cu_->instruction_set != kX86_64 &&
1119 (cu_->instruction_set == kX86 ||
1120 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001121 OpRegCopyWide(rl_result.reg, rl_src.reg);
1122 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1123 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1124 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001125 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1126 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001127 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001128 }
1129 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 }
Vladimir Markob9823312014-03-20 17:38:43 +00001131
1132 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001133 RegStorage sign_reg;
1134 if (cu_->instruction_set == kX86_64) {
1135 sign_reg = AllocTempWide();
1136 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1137 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1138 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1139 } else {
1140 sign_reg = AllocTemp();
1141 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1142 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1143 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1144 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1145 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1146 }
buzbee082833c2014-05-17 23:16:26 -07001147 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001148 StoreValueWide(rl_dest, rl_result);
1149 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150}
1151
Serban Constantinescu23abec92014-07-02 16:13:38 +01001152bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001153 // Currently implemented only for ARM64.
1154 UNUSED(info, size);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001155 return false;
1156}
1157
1158bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001159 // Currently implemented only for ARM64.
1160 UNUSED(info, is_min, is_double);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001161 return false;
1162}
1163
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001164bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001165 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001166 return false;
1167}
1168
1169bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001170 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001171 return false;
1172}
1173
1174bool Mir2Lir::GenInlinedRint(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001175 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001176 return false;
1177}
1178
1179bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001180 UNUSED(info, is_double);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001181 return false;
1182}
1183
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001184bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 if (cu_->instruction_set == kMips) {
1186 // TODO - add Mips implementation
1187 return false;
1188 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 RegLocation rl_dest = InlineTarget(info);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001190 if (rl_dest.s_reg_low == INVALID_SREG) {
1191 // Result is unused, the code is dead. Inlining successful, no code generated.
1192 return true;
1193 }
1194 RegLocation rl_src = info->args[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 StoreValue(rl_dest, rl_src);
1196 return true;
1197}
1198
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001199bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 if (cu_->instruction_set == kMips) {
1201 // TODO - add Mips implementation
1202 return false;
1203 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 RegLocation rl_dest = InlineTargetWide(info);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001205 if (rl_dest.s_reg_low == INVALID_SREG) {
1206 // Result is unused, the code is dead. Inlining successful, no code generated.
1207 return true;
1208 }
1209 RegLocation rl_src = info->args[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 StoreValueWide(rl_dest, rl_src);
1211 return true;
1212}
1213
DaniilSokolov70c4f062014-06-24 17:34:00 -07001214bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001215 UNUSED(info);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001216 return false;
1217}
1218
1219
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001221 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 * otherwise bails to standard library code.
1223 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001224bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001225 RegLocation rl_obj = info->args[0];
1226 RegLocation rl_char = info->args[1];
1227 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1228 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1229 return false;
1230 }
1231
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001232 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001234 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1235 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1236 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 LoadValueDirectFixed(rl_obj, reg_ptr);
1239 LoadValueDirectFixed(rl_char, reg_char);
1240 if (zero_based) {
1241 LoadConstant(reg_start, 0);
1242 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001243 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 LoadValueDirectFixed(rl_start, reg_start);
1245 }
Andreas Gampe98430592014-07-27 19:44:50 -07001246 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001247 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001248 LIR* high_code_point_branch =
1249 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001251 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001252 if (!rl_char.is_const) {
1253 // Add the slow path for code points beyond 0xFFFF.
1254 DCHECK(high_code_point_branch != nullptr);
1255 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1256 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001257 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001258 ClobberCallerSave(); // We must clobber everything because slow path will return here
Vladimir Marko3bc86152014-03-13 14:11:28 +00001259 } else {
1260 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1261 DCHECK(high_code_point_branch == nullptr);
1262 }
buzbeea0cd2d72014-06-01 09:33:49 -07001263 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 RegLocation rl_dest = InlineTarget(info);
1265 StoreValue(rl_dest, rl_return);
1266 return true;
1267}
1268
1269/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001270bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 if (cu_->instruction_set == kMips) {
1272 // TODO - add Mips implementation
1273 return false;
1274 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001275 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001277 RegStorage reg_this = TargetReg(kArg0, kRef);
1278 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279
1280 RegLocation rl_this = info->args[0];
1281 RegLocation rl_cmp = info->args[1];
1282 LoadValueDirectFixed(rl_this, reg_this);
1283 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001284 RegStorage r_tgt;
1285 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001286 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001287 } else {
1288 r_tgt = RegStorage::InvalidReg();
1289 }
Dave Allisonf9439142014-03-27 15:10:22 -07001290 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001291 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001292 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001293 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001294 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001296 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001297 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 RegLocation rl_dest = InlineTarget(info);
1299 StoreValue(rl_dest, rl_return);
1300 return true;
1301}
1302
1303bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1304 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001305
1306 // Early exit if the result is unused.
1307 if (rl_dest.orig_sreg < 0) {
1308 return true;
1309 }
1310
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001311 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001312
Andreas Gamped500b532015-01-16 22:09:55 -08001313 if (Is64BitInstructionSet(cu_->instruction_set)) {
1314 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1315 kNotVolatile);
1316 } else {
1317 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 }
Andreas Gamped500b532015-01-16 22:09:55 -08001319
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 StoreValue(rl_dest, rl_result);
1321 return true;
1322}
1323
1324bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1325 bool is_long, bool is_volatile) {
1326 if (cu_->instruction_set == kMips) {
1327 // TODO - add Mips implementation
1328 return false;
1329 }
1330 // Unused - RegLocation rl_src_unsafe = info->args[0];
1331 RegLocation rl_src_obj = info->args[1]; // Object
1332 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001333 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001334 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001335
buzbeea0cd2d72014-06-01 09:33:49 -07001336 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001338 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001340 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1341 || cu_->instruction_set == kArm64) {
1342 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001343 } else {
1344 RegStorage rl_temp_offset = AllocTemp();
1345 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001346 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001347 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001348 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001350 if (rl_result.ref) {
1351 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1352 } else {
1353 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1354 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001355 }
1356
1357 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001358 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001359 }
1360
1361 if (is_long) {
1362 StoreValueWide(rl_dest, rl_result);
1363 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 StoreValue(rl_dest, rl_result);
1365 }
1366 return true;
1367}
1368
1369bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1370 bool is_object, bool is_volatile, bool is_ordered) {
1371 if (cu_->instruction_set == kMips) {
1372 // TODO - add Mips implementation
1373 return false;
1374 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 // Unused - RegLocation rl_src_unsafe = info->args[0];
1376 RegLocation rl_src_obj = info->args[1]; // Object
1377 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001378 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001379 RegLocation rl_src_value = info->args[4]; // value to store
1380 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001381 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 }
buzbeea0cd2d72014-06-01 09:33:49 -07001383 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1385 RegLocation rl_value;
1386 if (is_long) {
1387 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001388 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1389 || cu_->instruction_set == kArm64) {
1390 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001391 } else {
1392 RegStorage rl_temp_offset = AllocTemp();
1393 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001394 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001395 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001396 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 } else {
buzbee7c02e912014-10-03 13:14:17 -07001398 rl_value = LoadValue(rl_src_value, LocToRegClass(rl_src_value));
Matteo Franchin255e0142014-07-04 13:50:41 +01001399 if (rl_value.ref) {
1400 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1401 } else {
1402 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1403 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001405
1406 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001407 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001408
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001410 // Prevent reordering with a subsequent volatile load.
1411 // May also be needed to address store atomicity issues.
1412 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 }
1414 if (is_object) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001415 MarkGCCard(0, rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 }
1417 return true;
1418}
1419
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001420void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001421 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001422 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1423 ->GenIntrinsic(this, info)) {
1424 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001425 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001426 GenInvokeNoInline(info);
1427}
1428
1429void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001430 int call_state = 0;
1431 LIR* null_ck;
1432 LIR** p_null_ck = NULL;
1433 NextCallInsn next_call_insn;
1434 FlushAllRegs(); /* Everything to home location */
1435 // Explicit register usage
1436 LockCallTemps();
1437
Vladimir Markof096aad2014-01-23 15:51:58 +00001438 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1439 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1440 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
Vladimir Markof4da6752014-08-01 19:04:18 +01001441 info->type = method_info.GetSharpType();
Vladimir Markof096aad2014-01-23 15:51:58 +00001442 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001446 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 } else if (info->type == kDirect) {
1448 if (fast_path) {
1449 p_null_ck = &null_ck;
1450 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001451 next_call_insn = fast_path ? GetNextSDCallInsn() : NextDirectCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 skip_this = false;
1453 } else if (info->type == kStatic) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001454 next_call_insn = fast_path ? GetNextSDCallInsn() : NextStaticCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001455 skip_this = false;
1456 } else if (info->type == kSuper) {
1457 DCHECK(!fast_path); // Fast path is a direct call.
1458 next_call_insn = NextSuperCallInsnSP;
1459 skip_this = false;
1460 } else {
1461 DCHECK_EQ(info->type, kVirtual);
1462 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1463 skip_this = fast_path;
1464 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001465 MethodReference target_method = method_info.GetTargetMethod();
Serguei Katkov717a3e42014-11-13 17:19:42 +06001466 call_state = GenDalvikArgs(info, call_state, p_null_ck,
1467 next_call_insn, target_method, method_info.VTableIndex(),
1468 method_info.DirectCode(), method_info.DirectMethod(),
1469 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001470 // Finish up any of the call sequence not interleaved in arg loading
1471 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001472 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1473 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001475 LIR* call_insn = GenCallInsn(method_info);
Vladimir Markof4da6752014-08-01 19:04:18 +01001476 MarkSafepointPC(call_insn);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477
Vladimir Markobfe400b2014-12-19 19:27:26 +00001478 FreeCallTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 if (info->result.location != kLocInvalid) {
1480 // We have a following MOVE_RESULT - do it now.
1481 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001482 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483 StoreValueWide(info->result, ret_loc);
1484 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001485 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486 StoreValue(info->result, ret_loc);
1487 }
1488 }
1489}
1490
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491} // namespace art