blob: 2a6dfefe221f0c66d7eb58867b59171f50fa9256 [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
Vladimir Markof4da6752014-08-01 19:04:18 +010017#include "arm/codegen_arm.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070018#include "dex/compiler_ir.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000019#include "dex/frontend.h"
20#include "dex/quick/dex_file_method_inliner.h"
21#include "dex/quick/dex_file_to_method_inliner_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "dex_file-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070023#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "invoke_type.h"
25#include "mirror/array.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070026#include "mirror/class-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070027#include "mirror/dex_cache.h"
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070028#include "mirror/object_array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "mirror/string.h"
30#include "mir_to_lir-inl.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010031#include "scoped_thread_state_change.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
33namespace art {
34
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070035// Shortcuts to repeatedly used long types.
36typedef mirror::ObjectArray<mirror::Object> ObjArray;
37
Brian Carlstrom7940e442013-07-12 13:46:57 -070038/*
39 * This source files contains "gen" codegen routines that should
40 * be applicable to most targets. Only mid-level support utilities
41 * and "op" calls may be used here.
42 */
43
Mingyao Yang3a74d152014-04-21 15:39:44 -070044void Mir2Lir::AddIntrinsicSlowPath(CallInfo* info, LIR* branch, LIR* resume) {
45 class IntrinsicSlowPathPath : public Mir2Lir::LIRSlowPath {
Vladimir Marko3bc86152014-03-13 14:11:28 +000046 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080047 IntrinsicSlowPathPath(Mir2Lir* m2l, CallInfo* info_in, LIR* branch_in, LIR* resume_in)
48 : LIRSlowPath(m2l, info_in->offset, branch_in, resume_in), info_(info_in) {
Vladimir Marko3bc86152014-03-13 14:11:28 +000049 }
50
51 void Compile() {
52 m2l_->ResetRegPool();
53 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070054 GenerateTargetLabel(kPseudoIntrinsicRetry);
Vladimir Marko3bc86152014-03-13 14:11:28 +000055 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
56 m2l_->GenInvokeNoInline(info_);
57 if (cont_ != nullptr) {
58 m2l_->OpUnconditionalBranch(cont_);
59 }
60 }
61
62 private:
63 CallInfo* const info_;
64 };
65
Mingyao Yang3a74d152014-04-21 15:39:44 -070066 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000067}
68
Brian Carlstrom7940e442013-07-12 13:46:57 -070069/*
70 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000071 * the helper target address, and the actual call to the helper. Because x86
72 * has a memory call operation, part 1 is a NOP for x86. For other targets,
73 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 */
Andreas Gampe2f244e92014-05-08 03:35:25 -070075// template <size_t pointer_size>
Andreas Gampe98430592014-07-27 19:44:50 -070076RegStorage Mir2Lir::CallHelperSetup(QuickEntrypointEnum trampoline) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070077 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
78 return RegStorage::InvalidReg();
79 } else {
Andreas Gampe98430592014-07-27 19:44:50 -070080 return LoadHelper(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070081 }
82}
83
Andreas Gampe98430592014-07-27 19:44:50 -070084LIR* Mir2Lir::CallHelper(RegStorage r_tgt, QuickEntrypointEnum trampoline, bool safepoint_pc,
85 bool use_link) {
86 LIR* call_inst = InvokeTrampoline(use_link ? kOpBlx : kOpBx, r_tgt, trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -070087
Andreas Gampe98430592014-07-27 19:44:50 -070088 if (r_tgt.Valid()) {
Dave Allisond6ed6422014-04-09 23:36:15 +000089 FreeTemp(r_tgt);
90 }
Andreas Gampe98430592014-07-27 19:44:50 -070091
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 if (safepoint_pc) {
93 MarkSafepointPC(call_inst);
94 }
95 return call_inst;
96}
97
Andreas Gampe98430592014-07-27 19:44:50 -070098void Mir2Lir::CallRuntimeHelper(QuickEntrypointEnum trampoline, bool safepoint_pc) {
99 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang42894562014-04-07 12:42:16 -0700100 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700101 CallHelper(r_tgt, trampoline, safepoint_pc);
Mingyao Yang42894562014-04-07 12:42:16 -0700102}
103
Andreas Gampe98430592014-07-27 19:44:50 -0700104void Mir2Lir::CallRuntimeHelperImm(QuickEntrypointEnum trampoline, int arg0, bool safepoint_pc) {
105 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700106 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000107 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700108 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109}
110
Andreas Gampe98430592014-07-27 19:44:50 -0700111void Mir2Lir::CallRuntimeHelperReg(QuickEntrypointEnum trampoline, RegStorage arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700112 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700113 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700114 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000115 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700116 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117}
118
Andreas Gampe98430592014-07-27 19:44:50 -0700119void Mir2Lir::CallRuntimeHelperRegLocation(QuickEntrypointEnum trampoline, RegLocation arg0,
120 bool safepoint_pc) {
121 RegStorage r_tgt = CallHelperSetup(trampoline);
buzbee2700f7e2014-03-07 09:46:20 -0800122 if (arg0.wide == 0) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700123 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, arg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700125 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000127 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700128 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129}
130
Andreas Gampe98430592014-07-27 19:44:50 -0700131void Mir2Lir::CallRuntimeHelperImmImm(QuickEntrypointEnum trampoline, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700133 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700134 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
135 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000136 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700137 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138}
139
Andreas Gampe98430592014-07-27 19:44:50 -0700140void Mir2Lir::CallRuntimeHelperImmRegLocation(QuickEntrypointEnum trampoline, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700142 RegStorage r_tgt = CallHelperSetup(trampoline);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 if (arg1.wide == 0) {
Andreas Gampef9872f02014-07-01 19:00:09 -0700144 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700146 RegStorage r_tmp = TargetReg(cu_->instruction_set == kMips ? kArg2 : kArg1, kWide);
buzbee2700f7e2014-03-07 09:46:20 -0800147 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700149 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000150 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700151 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152}
153
Andreas Gampe98430592014-07-27 19:44:50 -0700154void Mir2Lir::CallRuntimeHelperRegLocationImm(QuickEntrypointEnum trampoline, RegLocation arg0,
155 int arg1, bool safepoint_pc) {
156 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampef9872f02014-07-01 19:00:09 -0700157 DCHECK(!arg0.wide);
158 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700159 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000160 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700161 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162}
163
Andreas Gampe98430592014-07-27 19:44:50 -0700164void Mir2Lir::CallRuntimeHelperImmReg(QuickEntrypointEnum trampoline, int arg0, RegStorage arg1,
165 bool safepoint_pc) {
166 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700167 OpRegCopy(TargetReg(kArg1, arg1.GetWideKind()), arg1);
168 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000169 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700170 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171}
172
Andreas Gampe98430592014-07-27 19:44:50 -0700173void Mir2Lir::CallRuntimeHelperRegImm(QuickEntrypointEnum trampoline, RegStorage arg0, int arg1,
174 bool safepoint_pc) {
175 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700176 OpRegCopy(TargetReg(kArg0, arg0.GetWideKind()), arg0);
177 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000178 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700179 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180}
181
Andreas Gampe98430592014-07-27 19:44:50 -0700182void Mir2Lir::CallRuntimeHelperImmMethod(QuickEntrypointEnum trampoline, int arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700183 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700184 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700185 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
186 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000187 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700188 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189}
190
Andreas Gampe98430592014-07-27 19:44:50 -0700191void Mir2Lir::CallRuntimeHelperRegMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800192 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700193 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampeccc60262014-07-04 18:02:38 -0700194 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.GetWideKind()), arg0));
195 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
196 if (r_tmp.NotExactlyEquals(arg0)) {
197 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800198 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700199 LoadCurrMethodDirect(TargetReg(kArg1, kRef));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800200 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700201 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800202}
203
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800204void Mir2Lir::CallRuntimeHelperRegRegLocationMethod(QuickEntrypointEnum trampoline, RegStorage arg0,
205 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700206 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800207 DCHECK(!IsSameReg(TargetReg(kArg2, arg0.GetWideKind()), arg0));
Andreas Gampeccc60262014-07-04 18:02:38 -0700208 RegStorage r_tmp = TargetReg(kArg0, arg0.GetWideKind());
209 if (r_tmp.NotExactlyEquals(arg0)) {
210 OpRegCopy(r_tmp, arg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800211 }
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
213 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800214 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700215 CallHelper(r_tgt, trampoline, safepoint_pc);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800216}
217
Andreas Gampe98430592014-07-27 19:44:50 -0700218void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(QuickEntrypointEnum trampoline,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700219 RegLocation arg0, RegLocation arg1,
220 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700221 RegStorage r_tgt = CallHelperSetup(trampoline);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700222 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700223 RegStorage arg0_reg = TargetReg((arg0.fp) ? kFArg0 : kArg0, arg0);
224
225 RegStorage arg1_reg;
226 if (arg1.fp == arg0.fp) {
227 arg1_reg = TargetReg((arg1.fp) ? kFArg1 : kArg1, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700229 arg1_reg = TargetReg((arg1.fp) ? kFArg0 : kArg0, arg1);
230 }
231
232 if (arg0.wide == 0) {
233 LoadValueDirectFixed(arg0, arg0_reg);
234 } else {
235 LoadValueDirectWideFixed(arg0, arg0_reg);
236 }
237
238 if (arg1.wide == 0) {
239 LoadValueDirectFixed(arg1, arg1_reg);
240 } else {
241 LoadValueDirectWideFixed(arg1, arg1_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 }
243 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700244 DCHECK(!cu_->target64);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700245 if (arg0.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700246 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700247 if (arg1.wide == 0) {
248 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700249 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700250 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800251 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700252 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700253 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700254 if (cu_->instruction_set == kMips) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700255 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700256 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800257 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg1 : kArg1, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700258 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700259 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700261 LoadValueDirectWideFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700262 if (arg1.wide == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700263 LoadValueDirectFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kNotWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700264 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700265 LoadValueDirectWideFixed(arg1, TargetReg(arg1.fp ? kFArg2 : kArg2, kWide));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700266 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 }
268 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000269 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700270 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271}
272
Mingyao Yang80365d92014-04-18 12:10:58 -0700273void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700274 WideKind arg0_kind = arg0.GetWideKind();
275 WideKind arg1_kind = arg1.GetWideKind();
276 if (IsSameReg(arg1, TargetReg(kArg0, arg1_kind))) {
277 if (IsSameReg(arg0, TargetReg(kArg1, arg0_kind))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700278 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampeccc60262014-07-04 18:02:38 -0700279 OpRegCopy(TargetReg(kArg2, arg1_kind), arg1);
280 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
281 OpRegCopy(TargetReg(kArg1, arg1_kind), TargetReg(kArg2, arg1_kind));
Mingyao Yang80365d92014-04-18 12:10:58 -0700282 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700283 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
284 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700285 }
286 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700287 OpRegCopy(TargetReg(kArg0, arg0_kind), arg0);
288 OpRegCopy(TargetReg(kArg1, arg1_kind), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700289 }
290}
291
Andreas Gampe98430592014-07-27 19:44:50 -0700292void Mir2Lir::CallRuntimeHelperRegReg(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800293 RegStorage arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700294 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700295 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000296 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700297 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298}
299
Andreas Gampe98430592014-07-27 19:44:50 -0700300void Mir2Lir::CallRuntimeHelperRegRegImm(QuickEntrypointEnum trampoline, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800301 RegStorage arg1, int arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700302 RegStorage r_tgt = CallHelperSetup(trampoline);
Mingyao Yang80365d92014-04-18 12:10:58 -0700303 CopyToArgumentRegs(arg0, arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700304 LoadConstant(TargetReg(kArg2, kNotWide), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000305 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700306 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307}
308
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800309void Mir2Lir::CallRuntimeHelperImmRegLocationMethod(QuickEntrypointEnum trampoline, int arg0,
310 RegLocation arg1, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700311 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800312 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
313 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
Andreas Gampeccc60262014-07-04 18:02:38 -0700314 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000315 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700316 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317}
318
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800319void Mir2Lir::CallRuntimeHelperImmImmMethod(QuickEntrypointEnum trampoline, int arg0, int arg1,
Andreas Gampe98430592014-07-27 19:44:50 -0700320 bool safepoint_pc) {
321 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800322 LoadCurrMethodDirect(TargetReg(kArg2, kRef));
323 LoadConstant(TargetReg(kArg1, kNotWide), arg1);
Andreas Gampeccc60262014-07-04 18:02:38 -0700324 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000325 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700326 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327}
328
Andreas Gampe98430592014-07-27 19:44:50 -0700329void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(QuickEntrypointEnum trampoline, int arg0,
330 RegLocation arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 RegLocation arg2, bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700332 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700333 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
334 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700335 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700337 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700339 LoadValueDirectWideFixed(arg2, TargetReg(kArg2, kWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700341 LoadConstant(TargetReg(kArg0, kNotWide), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000342 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700343 CallHelper(r_tgt, trampoline, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344}
345
Andreas Gampeccc60262014-07-04 18:02:38 -0700346void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(
Andreas Gampe98430592014-07-27 19:44:50 -0700347 QuickEntrypointEnum trampoline,
Andreas Gampeccc60262014-07-04 18:02:38 -0700348 RegLocation arg0,
349 RegLocation arg1,
350 RegLocation arg2,
351 bool safepoint_pc) {
Andreas Gampe98430592014-07-27 19:44:50 -0700352 RegStorage r_tgt = CallHelperSetup(trampoline);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700353 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
354 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
355 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000356 ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700357 CallHelper(r_tgt, trampoline, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700358}
359
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360/*
361 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100362 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 * assignment of promoted arguments.
364 *
365 * ArgLocs is an array of location records describing the incoming arguments
366 * with one location record per word of argument.
367 */
Zheng Xu5667fdb2014-10-23 18:29:55 +0800368// TODO: Support 64-bit argument registers.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700369void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800371 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 * It will attempt to keep kArg0 live (or copy it to home location
373 * if promoted).
374 */
375 RegLocation rl_src = rl_method;
376 rl_src.location = kLocPhysReg;
Andreas Gampeccc60262014-07-04 18:02:38 -0700377 rl_src.reg = TargetReg(kArg0, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700379 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700380 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 // If Method* has been promoted, explicitly flush
382 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700383 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 }
385
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700386 if (mir_graph_->GetNumOfInVRs() == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800388 }
389
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700390 int start_vreg = mir_graph_->GetFirstInVR();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 /*
392 * Copy incoming arguments to their proper home locations.
393 * NOTE: an older version of dx had an issue in which
394 * it would reuse static method argument registers.
395 * This could result in the same Dalvik virtual register
396 * being promoted to both core and fp regs. To account for this,
397 * we only copy to the corresponding promoted physical register
398 * if it matches the type of the SSA name for the incoming
399 * argument. It is also possible that long and double arguments
400 * end up half-promoted. In those cases, we must flush the promoted
401 * half to memory as well.
402 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100403 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600404 RegLocation* t_loc = nullptr;
405 for (uint32_t i = 0; i < mir_graph_->GetNumOfInVRs(); i += t_loc->wide ? 2 : 1) {
406 // get reg corresponding to input
buzbee2700f7e2014-03-07 09:46:20 -0800407 RegStorage reg = GetArgMappingToPhysicalReg(i);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600408 t_loc = &ArgLocs[i];
409
410 // If the wide input appeared as single, flush it and go
411 // as it comes from memory.
412 if (t_loc->wide && reg.Valid() && !reg.Is64Bit()) {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000413 // The memory already holds the half. Don't do anything.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600414 reg = RegStorage::InvalidReg();
415 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800416
buzbee2700f7e2014-03-07 09:46:20 -0800417 if (reg.Valid()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600418 // If arriving in register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419
Serguei Katkov717a3e42014-11-13 17:19:42 +0600420 // We have already updated the arg location with promoted info
421 // so we can be based on it.
422 if (t_loc->location == kLocPhysReg) {
423 // Just copy it.
424 if (t_loc->wide) {
425 OpRegCopyWide(t_loc->reg, reg);
426 } else {
427 OpRegCopy(t_loc->reg, reg);
428 }
429 } else {
430 // Needs flush.
431 int offset = SRegOffset(start_vreg + i);
432 if (t_loc->ref) {
433 StoreRefDisp(TargetPtrReg(kSp), offset, reg, kNotVolatile);
434 } else {
435 StoreBaseDisp(TargetPtrReg(kSp), offset, reg, t_loc->wide ? k64 : k32, kNotVolatile);
buzbeed0a03b82013-09-14 08:21:05 -0700436 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 } else {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600439 // If arriving in frame & promoted.
440 if (t_loc->location == kLocPhysReg) {
441 int offset = SRegOffset(start_vreg + i);
442 if (t_loc->ref) {
443 LoadRefDisp(TargetPtrReg(kSp), offset, t_loc->reg, kNotVolatile);
444 } else {
445 LoadBaseDisp(TargetPtrReg(kSp), offset, t_loc->reg, t_loc->wide ? k64 : k32,
446 kNotVolatile);
447 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 }
449 }
450 }
451}
452
Andreas Gampeccc60262014-07-04 18:02:38 -0700453static void CommonCallCodeLoadThisIntoArg1(const CallInfo* info, Mir2Lir* cg) {
454 RegLocation rl_arg = info->args[0];
455 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1, kRef));
456}
457
458static void CommonCallCodeLoadClassIntoArg0(const CallInfo* info, Mir2Lir* cg) {
459 cg->GenNullCheck(cg->TargetReg(kArg1, kRef), info->opt_flags);
460 // get this->klass_ [use kArg1, set kArg0]
461 cg->LoadRefDisp(cg->TargetReg(kArg1, kRef), mirror::Object::ClassOffset().Int32Value(),
462 cg->TargetReg(kArg0, kRef),
463 kNotVolatile);
464 cg->MarkPossibleNullPointerException(info->opt_flags);
465}
466
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700467static bool CommonCallCodeLoadCodePointerIntoInvokeTgt(const RegStorage* alt_from,
Andreas Gampeccc60262014-07-04 18:02:38 -0700468 const CompilationUnit* cu, Mir2Lir* cg) {
469 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Mathieu Chartier2d721012014-11-10 11:08:06 -0800470 int32_t offset = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
471 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
Andreas Gampeccc60262014-07-04 18:02:38 -0700472 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
Mathieu Chartier2d721012014-11-10 11:08:06 -0800473 cg->LoadWordDisp(alt_from == nullptr ? cg->TargetReg(kArg0, kRef) : *alt_from, offset,
Andreas Gampeccc60262014-07-04 18:02:38 -0700474 cg->TargetPtrReg(kInvokeTgt));
475 return true;
476 }
477 return false;
478}
479
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480/*
481 * Bit of a hack here - in the absence of a real scheduling pass,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 * emit the next instruction in a virtual invoke sequence.
483 * We can use kLr as a temp prior to target address loading
484 * Note also that we'll load the first argument ("this") into
Serguei Katkov717a3e42014-11-13 17:19:42 +0600485 * kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 */
487static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
488 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700489 uint32_t method_idx, uintptr_t, uintptr_t,
490 InvokeType) {
491 UNUSED(target_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
493 /*
494 * This is the fast path in which the target virtual method is
495 * fully resolved at compile time.
496 */
497 switch (state) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700498 case 0:
499 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700501 case 1:
502 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
503 // Includes a null-check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700505 case 2: {
506 // Get this->klass_.embedded_vtable[method_idx] [usr kArg0, set kArg0]
507 int32_t offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
508 method_idx * sizeof(mirror::Class::VTableEntry);
509 // Load target method from embedded vtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700510 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700512 }
513 case 3:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700514 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700515 break; // kInvokeTgt := kArg0->entrypoint
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700517 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
518 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 default:
520 return -1;
521 }
522 return state + 1;
523}
524
525/*
Jeff Hao88474b42013-10-23 16:24:40 -0700526 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
527 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
528 * 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 +0600529 * argument ("this") into kArg1 here rather than the standard GenDalvikArgs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 */
531static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
532 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700533 uint32_t method_idx, uintptr_t, uintptr_t, InvokeType) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535
Jeff Hao88474b42013-10-23 16:24:40 -0700536 switch (state) {
537 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700538 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Andreas Gampeccc60262014-07-04 18:02:38 -0700539 cg->LoadConstant(cg->TargetReg(kHiddenArg, kNotWide), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400540 if (cu->instruction_set == kX86) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700541 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, kNotWide), cg->TargetReg(kHiddenArg, kNotWide));
Jeff Hao88474b42013-10-23 16:24:40 -0700542 }
543 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700544 case 1:
545 CommonCallCodeLoadThisIntoArg1(info, cg); // kArg1 := this
Jeff Hao88474b42013-10-23 16:24:40 -0700546 break;
Andreas Gampeccc60262014-07-04 18:02:38 -0700547 case 2:
548 CommonCallCodeLoadClassIntoArg0(info, cg); // kArg0 := kArg1->class
549 // Includes a null-check.
Jeff Hao88474b42013-10-23 16:24:40 -0700550 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700551 case 3: { // Get target method [use kInvokeTgt, set kArg0]
552 int32_t offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
553 (method_idx % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
554 // Load target method from embedded imtable to kArg0 [use kArg0, set kArg0]
Andreas Gampeccc60262014-07-04 18:02:38 -0700555 cg->LoadRefDisp(cg->TargetReg(kArg0, kRef), offset, cg->TargetReg(kArg0, kRef), kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700556 break;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700557 }
558 case 4:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700559 if (CommonCallCodeLoadCodePointerIntoInvokeTgt(nullptr, cu, cg)) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700560 break; // kInvokeTgt := kArg0->entrypoint
Jeff Hao88474b42013-10-23 16:24:40 -0700561 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700562 DCHECK(cu->instruction_set == kX86 || cu->instruction_set == kX86_64);
563 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 default:
565 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 }
567 return state + 1;
568}
569
Andreas Gampeccc60262014-07-04 18:02:38 -0700570static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info,
Andreas Gampe98430592014-07-27 19:44:50 -0700571 QuickEntrypointEnum trampoline, int state,
Andreas Gampeccc60262014-07-04 18:02:38 -0700572 const MethodReference& target_method, uint32_t method_idx) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700573 UNUSED(info, method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Andreas Gampe98430592014-07-27 19:44:50 -0700575
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 /*
577 * This handles the case in which the base method is not fully
578 * resolved at compile time, we bail to a runtime helper.
579 */
580 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700581 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 // Load trampoline target
Andreas Gampe98430592014-07-27 19:44:50 -0700583 int32_t disp;
584 if (cu->target64) {
585 disp = GetThreadOffset<8>(trampoline).Int32Value();
586 } else {
587 disp = GetThreadOffset<4>(trampoline).Int32Value();
588 }
589 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), disp, cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 }
591 // Load kArg0 with method index
592 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampeccc60262014-07-04 18:02:38 -0700593 cg->LoadConstant(cg->TargetReg(kArg0, kNotWide), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 return 1;
595 }
596 return -1;
597}
598
599static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
600 int state,
601 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700602 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700603 return NextInvokeInsnSP(cu, info, kQuickInvokeStaticTrampolineWithAccessCheck, state,
604 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605}
606
607static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
608 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700609 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700610 return NextInvokeInsnSP(cu, info, kQuickInvokeDirectTrampolineWithAccessCheck, state,
611 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612}
613
614static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
615 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700616 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700617 return NextInvokeInsnSP(cu, info, kQuickInvokeSuperTrampolineWithAccessCheck, state,
618 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619}
620
621static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
622 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700623 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700624 return NextInvokeInsnSP(cu, info, kQuickInvokeVirtualTrampolineWithAccessCheck, state,
625 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626}
627
628static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
629 CallInfo* info, int state,
630 const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700631 uint32_t, uintptr_t, uintptr_t, InvokeType) {
Andreas Gampe98430592014-07-27 19:44:50 -0700632 return NextInvokeInsnSP(cu, info, kQuickInvokeInterfaceTrampolineWithAccessCheck, state,
633 target_method, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634}
635
Dave Allison69dfe512014-07-11 17:11:58 +0000636// Default implementation of implicit null pointer check.
637// Overridden by arch specific as necessary.
638void Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) {
639 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
640 return;
641 }
642 RegStorage tmp = AllocTemp();
643 Load32Disp(reg, 0, tmp);
644 MarkPossibleNullPointerException(opt_flags);
645 FreeTemp(tmp);
646}
647
Serguei Katkov717a3e42014-11-13 17:19:42 +0600648/**
649 * @brief Used to flush promoted registers if they are used as argument
650 * in an invocation.
651 * @param info the infromation about arguments for invocation.
652 * @param start the first argument we should start to look from.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 */
Serguei Katkov717a3e42014-11-13 17:19:42 +0600654void Mir2Lir::GenDalvikArgsFlushPromoted(CallInfo* info, int start) {
655 if (cu_->disable_opt & (1 << kPromoteRegs)) {
656 // This make sense only if promotion is enabled.
657 return;
658 }
659 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 // Scan the rest of the args - if in phys_reg flush to memory
Serguei Katkov717a3e42014-11-13 17:19:42 +0600661 for (int next_arg = start; next_arg < info->num_arg_words;) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 RegLocation loc = info->args[next_arg];
663 if (loc.wide) {
664 loc = UpdateLocWide(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600665 if (loc.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700666 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 }
668 next_arg += 2;
669 } else {
670 loc = UpdateLoc(loc);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600671 if (loc.location == kLocPhysReg) {
672 if (loc.ref) {
673 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
674 } else {
675 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k32,
676 kNotVolatile);
677 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 }
679 next_arg++;
680 }
681 }
Serguei Katkov717a3e42014-11-13 17:19:42 +0600682}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683
Serguei Katkov717a3e42014-11-13 17:19:42 +0600684/**
685 * @brief Used to optimize the copying of VRs which are arguments of invocation.
686 * Please note that you should flush promoted registers first if you copy.
687 * If implementation does copying it may skip several of the first VRs but must copy
688 * till the end. Implementation must return the number of skipped VRs
689 * (it might be all VRs).
690 * @see GenDalvikArgsFlushPromoted
691 * @param info the information about arguments for invocation.
692 * @param first the first argument we should start to look from.
693 * @param count the number of remaining arguments we can handle.
694 * @return the number of arguments which we did not handle. Unhandled arguments
695 * must be attached to the first one.
696 */
697int Mir2Lir::GenDalvikArgsBulkCopy(CallInfo* info, int first, int count) {
698 // call is pretty expensive, let's use it if count is big.
699 if (count > 16) {
700 GenDalvikArgsFlushPromoted(info, first);
701 int start_offset = SRegOffset(info->args[first].s_reg_low);
702 int outs_offset = StackVisitor::GetOutVROffset(first, cu_->instruction_set);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800703
Andreas Gampeccc60262014-07-04 18:02:38 -0700704 OpRegRegImm(kOpAdd, TargetReg(kArg0, kRef), TargetPtrReg(kSp), outs_offset);
705 OpRegRegImm(kOpAdd, TargetReg(kArg1, kRef), TargetPtrReg(kSp), start_offset);
Andreas Gampe98430592014-07-27 19:44:50 -0700706 CallRuntimeHelperRegRegImm(kQuickMemcpy, TargetReg(kArg0, kRef), TargetReg(kArg1, kRef),
Serguei Katkov717a3e42014-11-13 17:19:42 +0600707 count * 4, false);
708 count = 0;
709 }
710 return count;
711}
712
713int Mir2Lir::GenDalvikArgs(CallInfo* info, int call_state,
714 LIR** pcrLabel, NextCallInsn next_call_insn,
715 const MethodReference& target_method,
716 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
717 InvokeType type, bool skip_this) {
718 // If no arguments, just return.
719 if (info->num_arg_words == 0)
720 return call_state;
721
722 const int start_index = skip_this ? 1 : 0;
723
724 // Get architecture dependent mapping between output VRs and physical registers
725 // basing on shorty of method to call.
726 InToRegStorageMapping in_to_reg_storage_mapping(arena_);
727 {
728 const char* target_shorty = mir_graph_->GetShortyFromMethodReference(target_method);
729 ShortyIterator shorty_iterator(target_shorty, type == kStatic);
730 in_to_reg_storage_mapping.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 }
732
Serguei Katkov717a3e42014-11-13 17:19:42 +0600733 int stack_map_start = std::max(in_to_reg_storage_mapping.GetMaxMappedIn() + 1, start_index);
734 if ((stack_map_start < info->num_arg_words) && info->args[stack_map_start].high_word) {
735 // It is possible that the last mapped reg is 32 bit while arg is 64-bit.
736 // It will be handled together with low part mapped to register.
737 stack_map_start++;
738 }
739 int regs_left_to_pass_via_stack = info->num_arg_words - stack_map_start;
740
741 // If it is a range case we can try to copy remaining VRs (not mapped to physical registers)
742 // using more optimal algorithm.
743 if (info->is_range && regs_left_to_pass_via_stack > 1) {
744 regs_left_to_pass_via_stack = GenDalvikArgsBulkCopy(info, stack_map_start,
745 regs_left_to_pass_via_stack);
746 }
747
748 // Now handle any remaining VRs mapped to stack.
749 if (in_to_reg_storage_mapping.HasArgumentsOnStack()) {
750 // Two temps but do not use kArg1, it might be this which we can skip.
751 // Separate single and wide - it can give some advantage.
752 RegStorage regRef = TargetReg(kArg3, kRef);
753 RegStorage regSingle = TargetReg(kArg3, kNotWide);
754 RegStorage regWide = TargetReg(kArg2, kWide);
755 for (int i = start_index;
756 i < stack_map_start + regs_left_to_pass_via_stack; i++) {
757 RegLocation rl_arg = info->args[i];
758 rl_arg = UpdateRawLoc(rl_arg);
759 RegStorage reg = in_to_reg_storage_mapping.Get(i);
760 if (!reg.Valid()) {
761 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
762 {
763 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
764 if (rl_arg.wide) {
765 if (rl_arg.location == kLocPhysReg) {
766 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
767 } else {
768 LoadValueDirectWideFixed(rl_arg, regWide);
769 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regWide, k64, kNotVolatile);
770 }
771 } else {
772 if (rl_arg.location == kLocPhysReg) {
773 if (rl_arg.ref) {
774 StoreRefDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, kNotVolatile);
775 } else {
776 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k32, kNotVolatile);
777 }
778 } else {
779 if (rl_arg.ref) {
780 LoadValueDirectFixed(rl_arg, regRef);
781 StoreRefDisp(TargetPtrReg(kSp), out_offset, regRef, kNotVolatile);
782 } else {
783 LoadValueDirectFixed(rl_arg, regSingle);
784 StoreBaseDisp(TargetPtrReg(kSp), out_offset, regSingle, k32, kNotVolatile);
785 }
786 }
787 }
788 }
789 call_state = next_call_insn(cu_, info, call_state, target_method,
790 vtable_idx, direct_code, direct_method, type);
791 }
792 if (rl_arg.wide) {
793 i++;
794 }
795 }
796 }
797
798 // Finish with VRs mapped to physical registers.
799 for (int i = start_index; i < stack_map_start; i++) {
800 RegLocation rl_arg = info->args[i];
801 rl_arg = UpdateRawLoc(rl_arg);
802 RegStorage reg = in_to_reg_storage_mapping.Get(i);
803 if (reg.Valid()) {
804 if (rl_arg.wide) {
805 // if reg is not 64-bit (it is half of 64-bit) then handle it separately.
806 if (!reg.Is64Bit()) {
Serguei Katkov717a3e42014-11-13 17:19:42 +0600807 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
808 if (rl_arg.location == kLocPhysReg) {
809 int out_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000810 // Dump it to memory.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600811 StoreBaseDisp(TargetPtrReg(kSp), out_offset, rl_arg.reg, k64, kNotVolatile);
812 LoadBaseDisp(TargetPtrReg(kSp), out_offset, reg, k32, kNotVolatile);
813 } else {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000814 int high_offset = StackVisitor::GetOutVROffset(i + 1, cu_->instruction_set);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600815 // First, use target reg for high part.
816 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low + 1), reg, k32,
817 kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000818 StoreBaseDisp(TargetPtrReg(kSp), high_offset, reg, k32, kNotVolatile);
819 // Now, use target reg for low part.
Serguei Katkov717a3e42014-11-13 17:19:42 +0600820 LoadBaseDisp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low), reg, k32, kNotVolatile);
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000821 int low_offset = StackVisitor::GetOutVROffset(i, cu_->instruction_set);
822 // And store it to the expected memory location.
823 StoreBaseDisp(TargetPtrReg(kSp), low_offset, reg, k32, kNotVolatile);
Serguei Katkov717a3e42014-11-13 17:19:42 +0600824 }
825 } else {
826 LoadValueDirectWideFixed(rl_arg, reg);
827 }
828 } else {
829 LoadValueDirectFixed(rl_arg, reg);
830 }
831 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
832 direct_code, direct_method, type);
833 }
834 if (rl_arg.wide) {
835 i++;
836 }
837 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838
839 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
840 direct_code, direct_method, type);
841 if (pcrLabel) {
Dave Allison69dfe512014-07-11 17:11:58 +0000842 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampeccc60262014-07-04 18:02:38 -0700843 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700844 } else {
845 *pcrLabel = nullptr;
Dave Allison69dfe512014-07-11 17:11:58 +0000846 GenImplicitNullCheck(TargetReg(kArg1, kRef), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700847 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 }
849 return call_state;
850}
851
Serguei Katkov717a3e42014-11-13 17:19:42 +0600852RegStorage Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
853 if (!in_to_reg_storage_mapping_.IsInitialized()) {
854 ShortyIterator shorty_iterator(cu_->shorty, cu_->invoke_type == kStatic);
855 in_to_reg_storage_mapping_.Initialize(&shorty_iterator, GetResetedInToRegStorageMapper());
856 }
857 return in_to_reg_storage_mapping_.Get(arg_num);
858}
859
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700860RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 RegLocation res;
862 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700863 // If result is unused, return a sink target based on type of invoke target.
864 res = GetReturn(ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 } else {
866 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700867 DCHECK_EQ(LocToRegClass(res),
868 ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869 }
870 return res;
871}
872
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700873RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 RegLocation res;
875 if (info->result.location == kLocInvalid) {
buzbee90a21f82014-09-07 11:37:51 -0700876 // If result is unused, return a sink target based on type of invoke target.
877 res = GetReturnWide(ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 } else {
879 res = info->result;
buzbee90a21f82014-09-07 11:37:51 -0700880 DCHECK_EQ(LocToRegClass(res),
881 ShortyToRegClass(mir_graph_->GetShortyFromTargetIdx(info->index)[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 }
883 return res;
884}
885
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -0700886bool Mir2Lir::GenInlinedReferenceGetReferent(CallInfo* info) {
Fred Shih4ee7a662014-07-11 09:59:27 -0700887 if (cu_->instruction_set == kMips) {
888 // TODO - add Mips implementation
889 return false;
890 }
891
Fred Shih4ee7a662014-07-11 09:59:27 -0700892 bool use_direct_type_ptr;
893 uintptr_t direct_type_ptr;
Fred Shihe7f82e22014-08-06 10:46:37 -0700894 ClassReference ref;
895 if (!cu_->compiler_driver->CanEmbedReferenceTypeInCode(&ref,
896 &use_direct_type_ptr, &direct_type_ptr)) {
897 return false;
898 }
899
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700900 RegStorage reg_class = TargetReg(kArg1, kRef);
901 Clobber(reg_class);
902 LockTemp(reg_class);
Fred Shih4ee7a662014-07-11 09:59:27 -0700903 if (use_direct_type_ptr) {
904 LoadConstant(reg_class, direct_type_ptr);
Alex Lighteb76e112014-07-29 15:22:40 -0700905 } else {
Fred Shihe7f82e22014-08-06 10:46:37 -0700906 uint16_t type_idx = ref.first->GetClassDef(ref.second).class_idx_;
907 LoadClassType(*ref.first, type_idx, kArg1);
Fred Shih4ee7a662014-07-11 09:59:27 -0700908 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700909
Fred Shihe7f82e22014-08-06 10:46:37 -0700910 uint32_t slow_path_flag_offset = cu_->compiler_driver->GetReferenceSlowFlagOffset();
911 uint32_t disable_flag_offset = cu_->compiler_driver->GetReferenceDisableFlagOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700912 CHECK(slow_path_flag_offset && disable_flag_offset &&
913 (slow_path_flag_offset != disable_flag_offset));
914
915 // intrinsic logic start.
916 RegLocation rl_obj = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -0700917 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih4ee7a662014-07-11 09:59:27 -0700918
919 RegStorage reg_slow_path = AllocTemp();
920 RegStorage reg_disabled = AllocTemp();
Andreas Gampef6815702015-01-20 09:53:48 -0800921 LoadBaseDisp(reg_class, slow_path_flag_offset, reg_slow_path, kSignedByte, kNotVolatile);
922 LoadBaseDisp(reg_class, disable_flag_offset, reg_disabled, kSignedByte, kNotVolatile);
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700923 FreeTemp(reg_class);
924 LIR* or_inst = OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
Fred Shih4ee7a662014-07-11 09:59:27 -0700925 FreeTemp(reg_disabled);
926
927 // if slow path, jump to JNI path target
Andreas Gampe30ab8a82014-07-17 00:12:32 -0700928 LIR* slow_path_branch;
929 if (or_inst->u.m.def_mask->HasBit(ResourceMask::kCCode)) {
930 // Generate conditional branch only, as the OR set a condition state (we are interested in a 'Z' flag).
931 slow_path_branch = OpCondBranch(kCondNe, nullptr);
932 } else {
933 // Generate compare and branch.
934 slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
935 }
Fred Shih4ee7a662014-07-11 09:59:27 -0700936 FreeTemp(reg_slow_path);
937
938 // slow path not enabled, simply load the referent of the reference object
939 RegLocation rl_dest = InlineTarget(info);
940 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
941 GenNullCheck(rl_obj.reg, info->opt_flags);
942 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
943 kNotVolatile);
944 MarkPossibleNullPointerException(info->opt_flags);
945 StoreValue(rl_dest, rl_result);
946
947 LIR* intrinsic_finish = NewLIR0(kPseudoTargetLabel);
948 AddIntrinsicSlowPath(info, slow_path_branch, intrinsic_finish);
Serguei Katkov9863daf2014-09-04 15:21:32 +0700949 ClobberCallerSave(); // We must clobber everything because slow path will return here
Fred Shih4ee7a662014-07-11 09:59:27 -0700950 return true;
951}
952
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700953bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 // Location of reference to data array
955 int value_offset = mirror::String::ValueOffset().Int32Value();
956 // Location of count
957 int count_offset = mirror::String::CountOffset().Int32Value();
958 // Starting offset within data array
959 int offset_offset = mirror::String::OffsetOffset().Int32Value();
960 // Start of char data with array_
961 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
962
963 RegLocation rl_obj = info->args[0];
964 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -0700965 rl_obj = LoadValue(rl_obj, kRefReg);
Andreas Gampe98430592014-07-27 19:44:50 -0700966 rl_idx = LoadValue(rl_idx, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800967 RegStorage reg_max;
968 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +0000970 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -0800971 RegStorage reg_off;
972 RegStorage reg_ptr;
Andreas Gampe98430592014-07-27 19:44:50 -0700973 reg_off = AllocTemp();
974 reg_ptr = AllocTempRef();
975 if (range_check) {
976 reg_max = AllocTemp();
977 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -0800978 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 }
Andreas Gampe98430592014-07-27 19:44:50 -0700980 Load32Disp(rl_obj.reg, offset_offset, reg_off);
981 MarkPossibleNullPointerException(info->opt_flags);
982 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
983 if (range_check) {
984 // Set up a slow path to allow retry in case of bounds violation */
985 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
986 FreeTemp(reg_max);
987 range_check_branch = OpCondBranch(kCondUge, nullptr);
988 }
989 OpRegImm(kOpAdd, reg_ptr, data_offset);
Mark Mendell2b724cb2014-02-06 05:24:20 -0800990 if (rl_idx.is_const) {
991 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
992 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800993 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -0800994 }
buzbee2700f7e2014-03-07 09:46:20 -0800995 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000996 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800997 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -0800998 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 RegLocation rl_dest = InlineTarget(info);
1000 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Andreas Gampe98430592014-07-27 19:44:50 -07001001 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 FreeTemp(reg_off);
1003 FreeTemp(reg_ptr);
1004 StoreValue(rl_dest, rl_result);
1005 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001006 DCHECK(range_check_branch != nullptr);
1007 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001008 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 return true;
1011}
1012
1013// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001014bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 if (cu_->instruction_set == kMips) {
1016 // TODO - add Mips implementation
1017 return false;
1018 }
1019 // dst = src.length();
1020 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001021 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 RegLocation rl_dest = InlineTarget(info);
1023 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001024 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001025 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001026 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 if (is_empty) {
1028 // dst = (dst == 0);
1029 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001030 RegStorage t_reg = AllocTemp();
1031 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1032 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001033 } else if (cu_->instruction_set == kArm64) {
1034 OpRegImm(kOpSub, rl_result.reg, 1);
1035 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001037 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001038 OpRegImm(kOpSub, rl_result.reg, 1);
1039 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 }
1041 }
1042 StoreValue(rl_dest, rl_result);
1043 return true;
1044}
1045
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001046bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Zheng Xua3fe7422014-07-09 14:03:15 +08001047 if (cu_->instruction_set == kMips) {
1048 // TODO - add Mips implementation.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001049 return false;
1050 }
1051 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001052 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
1053 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001054 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001055 if (IsWide(size)) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001056 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001057 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1058 StoreValueWide(rl_dest, rl_result);
1059 return true;
1060 }
buzbee2700f7e2014-03-07 09:46:20 -08001061 RegStorage r_i_low = rl_i.reg.GetLow();
1062 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001063 // 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 +00001064 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001065 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001066 }
buzbee2700f7e2014-03-07 09:46:20 -08001067 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1068 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1069 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001070 FreeTemp(r_i_low);
1071 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001072 StoreValueWide(rl_dest, rl_result);
1073 } else {
buzbee695d13a2014-04-19 13:32:20 -07001074 DCHECK(size == k32 || size == kSignedHalf);
1075 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001076 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001077 StoreValue(rl_dest, rl_result);
1078 }
1079 return true;
1080}
1081
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001082bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 RegLocation rl_src = info->args[0];
1084 rl_src = LoadValue(rl_src, kCoreReg);
1085 RegLocation rl_dest = InlineTarget(info);
1086 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001087 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001089 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1090 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1091 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 StoreValue(rl_dest, rl_result);
1093 return true;
1094}
1095
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001096bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Vladimir Markob9823312014-03-20 17:38:43 +00001097 RegLocation rl_src = info->args[0];
1098 rl_src = LoadValueWide(rl_src, kCoreReg);
1099 RegLocation rl_dest = InlineTargetWide(info);
1100 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1101
1102 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001103 if (cu_->instruction_set != kX86_64 &&
1104 (cu_->instruction_set == kX86 ||
1105 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001106 OpRegCopyWide(rl_result.reg, rl_src.reg);
1107 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1108 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1109 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001110 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1111 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001112 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001113 }
1114 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 }
Vladimir Markob9823312014-03-20 17:38:43 +00001116
1117 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001118 RegStorage sign_reg;
1119 if (cu_->instruction_set == kX86_64) {
1120 sign_reg = AllocTempWide();
1121 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1122 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1123 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1124 } else {
1125 sign_reg = AllocTemp();
1126 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1127 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1128 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1129 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1130 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1131 }
buzbee082833c2014-05-17 23:16:26 -07001132 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001133 StoreValueWide(rl_dest, rl_result);
1134 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135}
1136
Serban Constantinescu23abec92014-07-02 16:13:38 +01001137bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 // Currently implemented only for ARM64.
1139 UNUSED(info, size);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001140 return false;
1141}
1142
1143bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001144 // Currently implemented only for ARM64.
1145 UNUSED(info, is_min, is_double);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001146 return false;
1147}
1148
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001149bool Mir2Lir::GenInlinedCeil(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001150 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001151 return false;
1152}
1153
1154bool Mir2Lir::GenInlinedFloor(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001155 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001156 return false;
1157}
1158
1159bool Mir2Lir::GenInlinedRint(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001160 UNUSED(info);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001161 return false;
1162}
1163
1164bool Mir2Lir::GenInlinedRound(CallInfo* info, bool is_double) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001165 UNUSED(info, is_double);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001166 return false;
1167}
1168
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001169bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 if (cu_->instruction_set == kMips) {
1171 // TODO - add Mips implementation
1172 return false;
1173 }
1174 RegLocation rl_src = info->args[0];
1175 RegLocation rl_dest = InlineTarget(info);
1176 StoreValue(rl_dest, rl_src);
1177 return true;
1178}
1179
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001180bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 if (cu_->instruction_set == kMips) {
1182 // TODO - add Mips implementation
1183 return false;
1184 }
1185 RegLocation rl_src = info->args[0];
1186 RegLocation rl_dest = InlineTargetWide(info);
1187 StoreValueWide(rl_dest, rl_src);
1188 return true;
1189}
1190
DaniilSokolov70c4f062014-06-24 17:34:00 -07001191bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001192 UNUSED(info);
DaniilSokolov70c4f062014-06-24 17:34:00 -07001193 return false;
1194}
1195
1196
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001198 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199 * otherwise bails to standard library code.
1200 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001201bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001202 RegLocation rl_obj = info->args[0];
1203 RegLocation rl_char = info->args[1];
1204 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1205 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1206 return false;
1207 }
1208
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001209 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001211 RegStorage reg_ptr = TargetReg(kArg0, kRef);
1212 RegStorage reg_char = TargetReg(kArg1, kNotWide);
1213 RegStorage reg_start = TargetReg(kArg2, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 LoadValueDirectFixed(rl_obj, reg_ptr);
1216 LoadValueDirectFixed(rl_char, reg_char);
1217 if (zero_based) {
1218 LoadConstant(reg_start, 0);
1219 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001220 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 LoadValueDirectFixed(rl_start, reg_start);
1222 }
Andreas Gampe98430592014-07-27 19:44:50 -07001223 RegStorage r_tgt = LoadHelper(kQuickIndexOf);
Dave Allisonf9439142014-03-27 15:10:22 -07001224 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001225 LIR* high_code_point_branch =
1226 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001228 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001229 if (!rl_char.is_const) {
1230 // Add the slow path for code points beyond 0xFFFF.
1231 DCHECK(high_code_point_branch != nullptr);
1232 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1233 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001234 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001235 ClobberCallerSave(); // We must clobber everything because slow path will return here
Vladimir Marko3bc86152014-03-13 14:11:28 +00001236 } else {
1237 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1238 DCHECK(high_code_point_branch == nullptr);
1239 }
buzbeea0cd2d72014-06-01 09:33:49 -07001240 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 RegLocation rl_dest = InlineTarget(info);
1242 StoreValue(rl_dest, rl_return);
1243 return true;
1244}
1245
1246/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001247bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 if (cu_->instruction_set == kMips) {
1249 // TODO - add Mips implementation
1250 return false;
1251 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001252 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 LockCallTemps(); // Using fixed registers
Andreas Gampeccc60262014-07-04 18:02:38 -07001254 RegStorage reg_this = TargetReg(kArg0, kRef);
1255 RegStorage reg_cmp = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256
1257 RegLocation rl_this = info->args[0];
1258 RegLocation rl_cmp = info->args[1];
1259 LoadValueDirectFixed(rl_this, reg_this);
1260 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001261 RegStorage r_tgt;
1262 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Andreas Gampe98430592014-07-27 19:44:50 -07001263 r_tgt = LoadHelper(kQuickStringCompareTo);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001264 } else {
1265 r_tgt = RegStorage::InvalidReg();
1266 }
Dave Allisonf9439142014-03-27 15:10:22 -07001267 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001268 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001269 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001270 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001271 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 // NOTE: not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001273 CallHelper(r_tgt, kQuickStringCompareTo, false, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001274 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 RegLocation rl_dest = InlineTarget(info);
1276 StoreValue(rl_dest, rl_return);
1277 return true;
1278}
1279
1280bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1281 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001282
1283 // Early exit if the result is unused.
1284 if (rl_dest.orig_sreg < 0) {
1285 return true;
1286 }
1287
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001288 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001289
Andreas Gamped500b532015-01-16 22:09:55 -08001290 if (Is64BitInstructionSet(cu_->instruction_set)) {
1291 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1292 kNotVolatile);
1293 } else {
1294 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 }
Andreas Gamped500b532015-01-16 22:09:55 -08001296
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 StoreValue(rl_dest, rl_result);
1298 return true;
1299}
1300
1301bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1302 bool is_long, bool is_volatile) {
1303 if (cu_->instruction_set == kMips) {
1304 // TODO - add Mips implementation
1305 return false;
1306 }
1307 // Unused - RegLocation rl_src_unsafe = info->args[0];
1308 RegLocation rl_src_obj = info->args[1]; // Object
1309 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001310 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001311 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001312
buzbeea0cd2d72014-06-01 09:33:49 -07001313 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001315 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001317 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1318 || cu_->instruction_set == kArm64) {
1319 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001320 } else {
1321 RegStorage rl_temp_offset = AllocTemp();
1322 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001323 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001324 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001325 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001326 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001327 if (rl_result.ref) {
1328 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1329 } else {
1330 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1331 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001332 }
1333
1334 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001335 GenMemBarrier(kLoadAny);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001336 }
1337
1338 if (is_long) {
1339 StoreValueWide(rl_dest, rl_result);
1340 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 StoreValue(rl_dest, rl_result);
1342 }
1343 return true;
1344}
1345
1346bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1347 bool is_object, bool is_volatile, bool is_ordered) {
1348 if (cu_->instruction_set == kMips) {
1349 // TODO - add Mips implementation
1350 return false;
1351 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 // Unused - RegLocation rl_src_unsafe = info->args[0];
1353 RegLocation rl_src_obj = info->args[1]; // Object
1354 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001355 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 RegLocation rl_src_value = info->args[4]; // value to store
1357 if (is_volatile || is_ordered) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001358 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 }
buzbeea0cd2d72014-06-01 09:33:49 -07001360 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1362 RegLocation rl_value;
1363 if (is_long) {
1364 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001365 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1366 || cu_->instruction_set == kArm64) {
1367 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001368 } else {
1369 RegStorage rl_temp_offset = AllocTemp();
1370 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001371 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001372 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001373 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 } else {
buzbee7c02e912014-10-03 13:14:17 -07001375 rl_value = LoadValue(rl_src_value, LocToRegClass(rl_src_value));
Matteo Franchin255e0142014-07-04 13:50:41 +01001376 if (rl_value.ref) {
1377 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1378 } else {
1379 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1380 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001382
1383 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001384 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001385
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 if (is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001387 // Prevent reordering with a subsequent volatile load.
1388 // May also be needed to address store atomicity issues.
1389 GenMemBarrier(kAnyAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390 }
1391 if (is_object) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001392 MarkGCCard(0, rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393 }
1394 return true;
1395}
1396
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001397void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001398 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001399 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1400 ->GenIntrinsic(this, info)) {
1401 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001403 GenInvokeNoInline(info);
1404}
1405
1406void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407 int call_state = 0;
1408 LIR* null_ck;
1409 LIR** p_null_ck = NULL;
1410 NextCallInsn next_call_insn;
1411 FlushAllRegs(); /* Everything to home location */
1412 // Explicit register usage
1413 LockCallTemps();
1414
Vladimir Markof096aad2014-01-23 15:51:58 +00001415 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1416 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1417 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
Vladimir Markof4da6752014-08-01 19:04:18 +01001418 info->type = method_info.GetSharpType();
Vladimir Markof096aad2014-01-23 15:51:58 +00001419 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001420 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001421 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001422 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001423 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 } else if (info->type == kDirect) {
1425 if (fast_path) {
1426 p_null_ck = &null_ck;
1427 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001428 next_call_insn = fast_path ? GetNextSDCallInsn() : NextDirectCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 skip_this = false;
1430 } else if (info->type == kStatic) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001431 next_call_insn = fast_path ? GetNextSDCallInsn() : NextStaticCallInsnSP;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432 skip_this = false;
1433 } else if (info->type == kSuper) {
1434 DCHECK(!fast_path); // Fast path is a direct call.
1435 next_call_insn = NextSuperCallInsnSP;
1436 skip_this = false;
1437 } else {
1438 DCHECK_EQ(info->type, kVirtual);
1439 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1440 skip_this = fast_path;
1441 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001442 MethodReference target_method = method_info.GetTargetMethod();
Serguei Katkov717a3e42014-11-13 17:19:42 +06001443 call_state = GenDalvikArgs(info, call_state, p_null_ck,
1444 next_call_insn, target_method, method_info.VTableIndex(),
1445 method_info.DirectCode(), method_info.DirectMethod(),
1446 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 // Finish up any of the call sequence not interleaved in arg loading
1448 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001449 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1450 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001452 LIR* call_insn = GenCallInsn(method_info);
Vladimir Markof4da6752014-08-01 19:04:18 +01001453 MarkSafepointPC(call_insn);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454
Vladimir Markobfe400b2014-12-19 19:27:26 +00001455 FreeCallTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 if (info->result.location != kLocInvalid) {
1457 // We have a following MOVE_RESULT - do it now.
1458 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001459 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 StoreValueWide(info->result, ret_loc);
1461 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001462 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 StoreValue(info->result, ret_loc);
1464 }
1465 }
1466}
1467
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468} // namespace art