blob: c537d2dae5bdd67e058639a90595ca1b60638b7d [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
17#include "dex/compiler_ir.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000018#include "dex/frontend.h"
19#include "dex/quick/dex_file_method_inliner.h"
20#include "dex/quick/dex_file_to_method_inliner_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "dex_file-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070022#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "invoke_type.h"
24#include "mirror/array.h"
Fred Shih460503b2014-06-18 11:26:11 -070025#include "mirror/class-inl.h"
26#include "mirror/dex_cache.h"
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070027#include "mirror/object_array-inl.h"
Fred Shih460503b2014-06-18 11:26:11 -070028#include "mirror/reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "mirror/string.h"
30#include "mir_to_lir-inl.h"
Fred Shih460503b2014-06-18 11:26:11 -070031#include "scoped_thread_state_change.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "x86/codegen_x86.h"
33
34namespace art {
35
Dmitry Petrochenko37498b62014-05-05 20:33:38 +070036// Shortcuts to repeatedly used long types.
37typedef mirror::ObjectArray<mirror::Object> ObjArray;
38
Brian Carlstrom7940e442013-07-12 13:46:57 -070039/*
40 * This source files contains "gen" codegen routines that should
41 * be applicable to most targets. Only mid-level support utilities
42 * and "op" calls may be used here.
43 */
44
Mingyao Yang3a74d152014-04-21 15:39:44 -070045void Mir2Lir::AddIntrinsicSlowPath(CallInfo* info, LIR* branch, LIR* resume) {
46 class IntrinsicSlowPathPath : public Mir2Lir::LIRSlowPath {
Vladimir Marko3bc86152014-03-13 14:11:28 +000047 public:
Mingyao Yang3a74d152014-04-21 15:39:44 -070048 IntrinsicSlowPathPath(Mir2Lir* m2l, CallInfo* info, LIR* branch, LIR* resume = nullptr)
Vladimir Marko3bc86152014-03-13 14:11:28 +000049 : LIRSlowPath(m2l, info->offset, branch, resume), info_(info) {
50 }
51
52 void Compile() {
53 m2l_->ResetRegPool();
54 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070055 GenerateTargetLabel(kPseudoIntrinsicRetry);
Vladimir Marko3bc86152014-03-13 14:11:28 +000056 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
57 m2l_->GenInvokeNoInline(info_);
58 if (cont_ != nullptr) {
59 m2l_->OpUnconditionalBranch(cont_);
60 }
61 }
62
63 private:
64 CallInfo* const info_;
65 };
66
Mingyao Yang3a74d152014-04-21 15:39:44 -070067 AddSlowPath(new (arena_) IntrinsicSlowPathPath(this, info, branch, resume));
Vladimir Marko3bc86152014-03-13 14:11:28 +000068}
69
Andreas Gampe2f244e92014-05-08 03:35:25 -070070// Macro to help instantiate.
71// TODO: This might be used to only instantiate <4> on pure 32b systems.
72#define INSTANTIATE(sig_part1, ...) \
73 template sig_part1(ThreadOffset<4>, __VA_ARGS__); \
74 template sig_part1(ThreadOffset<8>, __VA_ARGS__); \
75
76
Brian Carlstrom7940e442013-07-12 13:46:57 -070077/*
78 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allisond6ed6422014-04-09 23:36:15 +000079 * the helper target address, and the actual call to the helper. Because x86
80 * has a memory call operation, part 1 is a NOP for x86. For other targets,
81 * load arguments between the two parts.
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 */
Andreas Gampe2f244e92014-05-08 03:35:25 -070083// template <size_t pointer_size>
Ian Rogersdd7624d2014-03-14 17:43:00 -070084RegStorage Mir2Lir::CallHelperSetup(ThreadOffset<4> helper_offset) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070085 // All CallRuntimeHelperXXX call this first. So make a central check here.
86 DCHECK_EQ(4U, GetInstructionSetPointerSize(cu_->instruction_set));
87
88 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
89 return RegStorage::InvalidReg();
90 } else {
91 return LoadHelper(helper_offset);
92 }
93}
94
95RegStorage Mir2Lir::CallHelperSetup(ThreadOffset<8> helper_offset) {
96 // All CallRuntimeHelperXXX call this first. So make a central check here.
97 DCHECK_EQ(8U, GetInstructionSetPointerSize(cu_->instruction_set));
98
99 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
100 return RegStorage::InvalidReg();
101 } else {
102 return LoadHelper(helper_offset);
103 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104}
105
106/* NOTE: if r_tgt is a temp, it will be freed following use */
Andreas Gampe2f244e92014-05-08 03:35:25 -0700107template <size_t pointer_size>
108LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<pointer_size> helper_offset,
109 bool safepoint_pc, bool use_link) {
Dave Allisond6ed6422014-04-09 23:36:15 +0000110 LIR* call_inst;
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700111 OpKind op = use_link ? kOpBlx : kOpBx;
Dave Allisond6ed6422014-04-09 23:36:15 +0000112 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
113 call_inst = OpThreadMem(op, helper_offset);
114 } else {
115 call_inst = OpReg(op, r_tgt);
116 FreeTemp(r_tgt);
117 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 if (safepoint_pc) {
119 MarkSafepointPC(call_inst);
120 }
121 return call_inst;
122}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700123template LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<4> helper_offset,
124 bool safepoint_pc, bool use_link);
125template LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<8> helper_offset,
126 bool safepoint_pc, bool use_link);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127
Andreas Gampe2f244e92014-05-08 03:35:25 -0700128template <size_t pointer_size>
129void Mir2Lir::CallRuntimeHelper(ThreadOffset<pointer_size> helper_offset, bool safepoint_pc) {
Mingyao Yang42894562014-04-07 12:42:16 -0700130 RegStorage r_tgt = CallHelperSetup(helper_offset);
131 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700132 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Mingyao Yang42894562014-04-07 12:42:16 -0700133}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700134INSTANTIATE(void Mir2Lir::CallRuntimeHelper, bool safepoint_pc)
Mingyao Yang42894562014-04-07 12:42:16 -0700135
Andreas Gampe2f244e92014-05-08 03:35:25 -0700136template <size_t pointer_size>
137void Mir2Lir::CallRuntimeHelperImm(ThreadOffset<pointer_size> helper_offset, int arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800138 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700139 LoadConstant(TargetReg(kArg0, false), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000140 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700141 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700143INSTANTIATE(void Mir2Lir::CallRuntimeHelperImm, int arg0, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144
Andreas Gampe2f244e92014-05-08 03:35:25 -0700145template <size_t pointer_size>
146void Mir2Lir::CallRuntimeHelperReg(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700147 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800148 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700149 OpRegCopy(TargetReg(kArg0, arg0.Is64Bit()), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000150 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700151 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700153INSTANTIATE(void Mir2Lir::CallRuntimeHelperReg, RegStorage arg0, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154
Andreas Gampe2f244e92014-05-08 03:35:25 -0700155template <size_t pointer_size>
156void Mir2Lir::CallRuntimeHelperRegLocation(ThreadOffset<pointer_size> helper_offset,
157 RegLocation arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800158 RegStorage r_tgt = CallHelperSetup(helper_offset);
159 if (arg0.wide == 0) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700160 LoadValueDirectFixed(arg0, TargetReg(arg0.fp ? kFArg0 : kArg0, arg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 } else {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700162 RegStorage r_tmp;
buzbee33ae5582014-06-12 14:56:32 -0700163 if (cu_->target64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700164 r_tmp = TargetReg(kArg0, true);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700165 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700166 r_tmp = TargetReg(arg0.fp ? kFArg0 : kArg0, arg0.fp ? kFArg1 : kArg1);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700167 }
buzbee2700f7e2014-03-07 09:46:20 -0800168 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000170 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700171 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700173INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegLocation, RegLocation arg0, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174
Andreas Gampe2f244e92014-05-08 03:35:25 -0700175template <size_t pointer_size>
176void Mir2Lir::CallRuntimeHelperImmImm(ThreadOffset<pointer_size> helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800178 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700179 LoadConstant(TargetReg(kArg0, false), arg0);
180 LoadConstant(TargetReg(kArg1, false), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000181 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700182 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700184INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmImm, int arg0, int arg1, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185
Andreas Gampe2f244e92014-05-08 03:35:25 -0700186template <size_t pointer_size>
187void Mir2Lir::CallRuntimeHelperImmRegLocation(ThreadOffset<pointer_size> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 RegLocation arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800189 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 if (arg1.wide == 0) {
Andreas Gampef9872f02014-07-01 19:00:09 -0700191 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 } else {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700193 RegStorage r_tmp;
buzbee33ae5582014-06-12 14:56:32 -0700194 if (cu_->target64) {
Andreas Gampef9872f02014-07-01 19:00:09 -0700195 r_tmp = TargetReg(kArg1, true);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700196 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700197 if (cu_->instruction_set == kMips) {
198 // skip kArg1 for stack alignment.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700199 r_tmp = TargetReg(kArg2, kArg3);
Douglas Leung2db3e262014-06-25 16:02:55 -0700200 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700201 r_tmp = TargetReg(kArg1, kArg2);
Douglas Leung2db3e262014-06-25 16:02:55 -0700202 }
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700203 }
buzbee2700f7e2014-03-07 09:46:20 -0800204 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 }
Chao-ying Fua77ee512014-07-01 17:43:41 -0700206 LoadConstant(TargetReg(kArg0, false), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000207 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700208 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700210INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmRegLocation, int arg0, RegLocation arg1,
211 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212
Andreas Gampe2f244e92014-05-08 03:35:25 -0700213template <size_t pointer_size>
214void Mir2Lir::CallRuntimeHelperRegLocationImm(ThreadOffset<pointer_size> helper_offset,
215 RegLocation arg0, int arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800216 RegStorage r_tgt = CallHelperSetup(helper_offset);
Andreas Gampef9872f02014-07-01 19:00:09 -0700217 DCHECK(!arg0.wide);
218 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
Chao-ying Fua77ee512014-07-01 17:43:41 -0700219 LoadConstant(TargetReg(kArg1, false), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000220 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700221 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700223INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegLocationImm, RegLocation arg0, int arg1,
224 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225
Andreas Gampe2f244e92014-05-08 03:35:25 -0700226template <size_t pointer_size>
227void Mir2Lir::CallRuntimeHelperImmReg(ThreadOffset<pointer_size> helper_offset, int arg0,
228 RegStorage arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800229 RegStorage r_tgt = CallHelperSetup(helper_offset);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700230 OpRegCopy(TargetReg(kArg1, arg1.Is64Bit()), arg1);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700231 LoadConstant(TargetReg(kArg0, false), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000232 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700233 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700235INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmReg, int arg0, RegStorage arg1, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236
Andreas Gampe2f244e92014-05-08 03:35:25 -0700237template <size_t pointer_size>
238void Mir2Lir::CallRuntimeHelperRegImm(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
239 int arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800240 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700241 OpRegCopy(TargetReg(kArg0, arg0.Is64Bit()), arg0);
242 LoadConstant(TargetReg(kArg1, false), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000243 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700244 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700246INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegImm, RegStorage arg0, int arg1, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247
Andreas Gampe2f244e92014-05-08 03:35:25 -0700248template <size_t pointer_size>
249void Mir2Lir::CallRuntimeHelperImmMethod(ThreadOffset<pointer_size> helper_offset, int arg0,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700250 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800251 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700252 LoadCurrMethodDirect(TargetRefReg(kArg1));
253 LoadConstant(TargetReg(kArg0, false), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000254 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700255 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700257INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmMethod, int arg0, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258
Andreas Gampe2f244e92014-05-08 03:35:25 -0700259template <size_t pointer_size>
260void Mir2Lir::CallRuntimeHelperRegMethod(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800261 bool safepoint_pc) {
262 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700263 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.Is64Bit()), arg0));
buzbeeb5860fb2014-06-21 15:31:01 -0700264 if (TargetReg(kArg0, arg0.Is64Bit()).NotExactlyEquals(arg0)) {
265 OpRegCopy(TargetReg(kArg0, arg0.Is64Bit()), arg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800266 }
buzbeeb5860fb2014-06-21 15:31:01 -0700267 LoadCurrMethodDirect(TargetRefReg(kArg1));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800268 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700269 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800270}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700271INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegMethod, RegStorage arg0, bool safepoint_pc)
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800272
Andreas Gampe2f244e92014-05-08 03:35:25 -0700273template <size_t pointer_size>
274void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(ThreadOffset<pointer_size> helper_offset,
275 RegStorage arg0, RegLocation arg2,
276 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800277 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700278 DCHECK(!IsSameReg(TargetReg(kArg1, arg0.Is64Bit()), arg0));
buzbeeb5860fb2014-06-21 15:31:01 -0700279 if (TargetReg(kArg0, arg0.Is64Bit()).NotExactlyEquals(arg0)) {
280 OpRegCopy(TargetReg(kArg0, arg0.Is64Bit()), arg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800281 }
buzbeeb5860fb2014-06-21 15:31:01 -0700282 LoadCurrMethodDirect(TargetRefReg(kArg1));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700283 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800284 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700285 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800286}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700287INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegMethodRegLocation, RegStorage arg0, RegLocation arg2,
288 bool safepoint_pc)
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800289
Andreas Gampe2f244e92014-05-08 03:35:25 -0700290template <size_t pointer_size>
291void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(ThreadOffset<pointer_size> helper_offset,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700292 RegLocation arg0, RegLocation arg1,
293 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800294 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700295 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700296 RegStorage arg0_reg = TargetReg((arg0.fp) ? kFArg0 : kArg0, arg0);
297
298 RegStorage arg1_reg;
299 if (arg1.fp == arg0.fp) {
300 arg1_reg = TargetReg((arg1.fp) ? kFArg1 : kArg1, arg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700302 arg1_reg = TargetReg((arg1.fp) ? kFArg0 : kArg0, arg1);
303 }
304
305 if (arg0.wide == 0) {
306 LoadValueDirectFixed(arg0, arg0_reg);
307 } else {
308 LoadValueDirectWideFixed(arg0, arg0_reg);
309 }
310
311 if (arg1.wide == 0) {
312 LoadValueDirectFixed(arg1, arg1_reg);
313 } else {
314 LoadValueDirectWideFixed(arg1, arg1_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 }
316 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700317 DCHECK(!cu_->target64);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700318 if (arg0.wide == 0) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700319 LoadValueDirectFixed(arg0, arg0.fp ? TargetReg(kFArg0, false) : TargetReg(kArg0, false));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700320 if (arg1.wide == 0) {
321 if (cu_->instruction_set == kMips) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700322 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2, false) : TargetReg(kArg1, false));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700323 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700324 LoadValueDirectFixed(arg1, TargetReg(kArg1, false));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700325 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700326 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700327 if (cu_->instruction_set == kMips) {
328 RegStorage r_tmp;
329 if (arg1.fp) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700330 r_tmp = TargetReg(kFArg2, kFArg3);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700331 } else {
332 // skip kArg1 for stack alignment.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700333 r_tmp = TargetReg(kArg2, kArg3);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700334 }
335 LoadValueDirectWideFixed(arg1, r_tmp);
336 } else {
337 RegStorage r_tmp;
Chao-ying Fua77ee512014-07-01 17:43:41 -0700338 r_tmp = TargetReg(kArg1, kArg2);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700339 LoadValueDirectWideFixed(arg1, r_tmp);
340 }
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700341 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800343 RegStorage r_tmp;
Andreas Gampe4b537a82014-06-30 22:24:53 -0700344 if (arg0.fp) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700345 r_tmp = TargetReg(kFArg0, kFArg1);
buzbee2700f7e2014-03-07 09:46:20 -0800346 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700347 r_tmp = TargetReg(kArg0, kArg1);
buzbee2700f7e2014-03-07 09:46:20 -0800348 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700349 LoadValueDirectWideFixed(arg0, r_tmp);
350 if (arg1.wide == 0) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700351 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2, false) : TargetReg(kArg2, false));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700352 } else {
353 RegStorage r_tmp;
354 if (arg1.fp) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700355 r_tmp = TargetReg(kFArg2, kFArg3);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700356 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700357 r_tmp = TargetReg(kArg2, kArg3);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700358 }
359 LoadValueDirectWideFixed(arg1, r_tmp);
360 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 }
362 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000363 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700364 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700366INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegLocationRegLocation, RegLocation arg0,
367 RegLocation arg1, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368
Mingyao Yang80365d92014-04-18 12:10:58 -0700369void Mir2Lir::CopyToArgumentRegs(RegStorage arg0, RegStorage arg1) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700370 if (IsSameReg(arg1, TargetReg(kArg0, arg1.Is64Bit()))) {
371 if (IsSameReg(arg0, TargetReg(kArg1, arg0.Is64Bit()))) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700372 // Swap kArg0 and kArg1 with kArg2 as temp.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700373 OpRegCopy(TargetReg(kArg2, arg1.Is64Bit()), arg1);
374 OpRegCopy(TargetReg(kArg0, arg0.Is64Bit()), arg0);
375 OpRegCopy(TargetReg(kArg1, arg1.Is64Bit()), TargetReg(kArg2, arg1.Is64Bit()));
Mingyao Yang80365d92014-04-18 12:10:58 -0700376 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700377 OpRegCopy(TargetReg(kArg1, arg1.Is64Bit()), arg1);
378 OpRegCopy(TargetReg(kArg0, arg0.Is64Bit()), arg0);
Mingyao Yang80365d92014-04-18 12:10:58 -0700379 }
380 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700381 OpRegCopy(TargetReg(kArg0, arg0.Is64Bit()), arg0);
382 OpRegCopy(TargetReg(kArg1, arg1.Is64Bit()), arg1);
Mingyao Yang80365d92014-04-18 12:10:58 -0700383 }
384}
385
Andreas Gampe2f244e92014-05-08 03:35:25 -0700386template <size_t pointer_size>
387void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800388 RegStorage arg1, bool safepoint_pc) {
389 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700390 CopyToArgumentRegs(arg0, arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000391 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700392 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700394INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegReg, RegStorage arg0, RegStorage arg1,
395 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396
Andreas Gampe2f244e92014-05-08 03:35:25 -0700397template <size_t pointer_size>
398void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset<pointer_size> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800399 RegStorage arg1, int arg2, bool safepoint_pc) {
400 RegStorage r_tgt = CallHelperSetup(helper_offset);
Mingyao Yang80365d92014-04-18 12:10:58 -0700401 CopyToArgumentRegs(arg0, arg1);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700402 LoadConstant(TargetReg(kArg2, false), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000403 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700404 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700406INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegRegImm, RegStorage arg0, RegStorage arg1, int arg2,
407 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408
Andreas Gampe2f244e92014-05-08 03:35:25 -0700409template <size_t pointer_size>
410void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset<pointer_size> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 int arg0, RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800412 RegStorage r_tgt = CallHelperSetup(helper_offset);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700413 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Chao-ying Fua77ee512014-07-01 17:43:41 -0700414 LoadCurrMethodDirect(TargetRefReg(kArg1));
415 LoadConstant(TargetReg(kArg0, false), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000416 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700417 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700419INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmMethodRegLocation, int arg0, RegLocation arg2,
420 bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421
Andreas Gampe2f244e92014-05-08 03:35:25 -0700422template <size_t pointer_size>
423void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset<pointer_size> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 int arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800425 RegStorage r_tgt = CallHelperSetup(helper_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700426 LoadCurrMethodDirect(TargetRefReg(kArg1));
427 LoadConstant(TargetReg(kArg2, false), arg2);
428 LoadConstant(TargetReg(kArg0, false), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000429 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700430 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700432INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmMethodImm, int arg0, int arg2, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433
Andreas Gampe2f244e92014-05-08 03:35:25 -0700434template <size_t pointer_size>
435void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset<pointer_size> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436 int arg0, RegLocation arg1,
437 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800438 RegStorage r_tgt = CallHelperSetup(helper_offset);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700439 DCHECK_EQ(static_cast<unsigned int>(arg1.wide), 0U); // The static_cast works around an
440 // instantiation bug in GCC.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700441 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 if (arg2.wide == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700443 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 } else {
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700445 RegStorage r_tmp;
buzbee33ae5582014-06-12 14:56:32 -0700446 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700447 r_tmp = TargetReg(kArg2, true);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700448 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700449 r_tmp = TargetReg(kArg2, kArg3);
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700450 }
buzbee2700f7e2014-03-07 09:46:20 -0800451 LoadValueDirectWideFixed(arg2, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 }
Chao-ying Fua77ee512014-07-01 17:43:41 -0700453 LoadConstant(TargetReg(kArg0, false), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000454 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700455 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700457INSTANTIATE(void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation, int arg0, RegLocation arg1,
458 RegLocation arg2, bool safepoint_pc)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459
Andreas Gampe2f244e92014-05-08 03:35:25 -0700460template <size_t pointer_size>
461void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset<pointer_size> helper_offset,
Ian Rogersa9a82542013-10-04 11:17:26 -0700462 RegLocation arg0, RegLocation arg1,
463 RegLocation arg2,
464 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800465 RegStorage r_tgt = CallHelperSetup(helper_offset);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700466 LoadValueDirectFixed(arg0, TargetReg(kArg0, arg0));
467 LoadValueDirectFixed(arg1, TargetReg(kArg1, arg1));
468 LoadValueDirectFixed(arg2, TargetReg(kArg2, arg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000469 ClobberCallerSave();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700470 CallHelper<pointer_size>(r_tgt, helper_offset, safepoint_pc);
Ian Rogersa9a82542013-10-04 11:17:26 -0700471}
Andreas Gampe2f244e92014-05-08 03:35:25 -0700472INSTANTIATE(void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation, RegLocation arg0,
473 RegLocation arg1, RegLocation arg2, bool safepoint_pc)
Ian Rogersa9a82542013-10-04 11:17:26 -0700474
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475/*
476 * If there are any ins passed in registers that have not been promoted
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100477 * to a callee-save register, flush them to the frame. Perform initial
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 * assignment of promoted arguments.
479 *
480 * ArgLocs is an array of location records describing the incoming arguments
481 * with one location record per word of argument.
482 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700483void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800485 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 * It will attempt to keep kArg0 live (or copy it to home location
487 * if promoted).
488 */
489 RegLocation rl_src = rl_method;
490 rl_src.location = kLocPhysReg;
Andreas Gampe4b537a82014-06-30 22:24:53 -0700491 rl_src.reg = TargetRefReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 rl_src.home = false;
buzbee091cc402014-03-31 10:14:40 -0700493 MarkLive(rl_src);
buzbeef2c3e562014-05-29 12:37:25 -0700494 StoreValue(rl_method, rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 // If Method* has been promoted, explicitly flush
496 if (rl_method.location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700497 StoreRefDisp(TargetPtrReg(kSp), 0, rl_src.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 }
499
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800500 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800502 }
503
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
505 /*
506 * Copy incoming arguments to their proper home locations.
507 * NOTE: an older version of dx had an issue in which
508 * it would reuse static method argument registers.
509 * This could result in the same Dalvik virtual register
510 * being promoted to both core and fp regs. To account for this,
511 * we only copy to the corresponding promoted physical register
512 * if it matches the type of the SSA name for the incoming
513 * argument. It is also possible that long and double arguments
514 * end up half-promoted. In those cases, we must flush the promoted
515 * half to memory as well.
516 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100517 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 for (int i = 0; i < cu_->num_ins; i++) {
519 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800520 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800521
buzbee2700f7e2014-03-07 09:46:20 -0800522 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 // If arriving in register
524 bool need_flush = true;
525 RegLocation* t_loc = &ArgLocs[i];
526 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800527 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 need_flush = false;
529 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbeeb5860fb2014-06-21 15:31:01 -0700530 OpRegCopy(RegStorage::Solo32(v_map->fp_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 need_flush = false;
532 } else {
533 need_flush = true;
534 }
535
buzbeed0a03b82013-09-14 08:21:05 -0700536 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 if (t_loc->wide) {
538 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700539 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 need_flush |= (p_map->core_location != v_map->core_location) ||
541 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700542 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
543 /*
544 * In Arm, a double is represented as a pair of consecutive single float
545 * registers starting at an even number. It's possible that both Dalvik vRegs
546 * representing the incoming double were independently promoted as singles - but
547 * not in a form usable as a double. If so, we need to flush - even though the
548 * incoming arg appears fully in register. At this point in the code, both
549 * halves of the double are promoted. Make sure they are in a usable form.
550 */
551 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
buzbeeb5860fb2014-06-21 15:31:01 -0700552 int low_reg = promotion_map_[lowreg_index].fp_reg;
553 int high_reg = promotion_map_[lowreg_index + 1].fp_reg;
buzbeed0a03b82013-09-14 08:21:05 -0700554 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
555 need_flush = true;
556 }
557 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 }
559 if (need_flush) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700560 Store32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 }
562 } else {
563 // If arriving in frame & promoted
564 if (v_map->core_location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700565 Load32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 }
567 if (v_map->fp_location == kLocPhysReg) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700568 Load32Disp(TargetPtrReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->fp_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 }
570 }
571 }
572}
573
574/*
575 * Bit of a hack here - in the absence of a real scheduling pass,
576 * emit the next instruction in static & direct invoke sequences.
577 */
578static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
579 int state, const MethodReference& target_method,
580 uint32_t unused,
581 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700582 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 if (direct_code != 0 && direct_method != 0) {
585 switch (state) {
586 case 0: // Get the current Method* [sets kArg0]
Ian Rogersff093b32014-04-30 19:04:27 -0700587 if (direct_code != static_cast<uintptr_t>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700588 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700589 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
Ian Rogers83883d72013-10-21 21:07:24 -0700590 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700591 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700592 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 }
Ian Rogersff093b32014-04-30 19:04:27 -0700594 if (direct_method != static_cast<uintptr_t>(-1)) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700595 cg->LoadConstant(cg->TargetRefReg(kArg0), direct_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700597 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 }
599 break;
600 default:
601 return -1;
602 }
603 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700604 RegStorage arg0_ref = cg->TargetRefReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 switch (state) {
606 case 0: // Get the current Method* [sets kArg0]
607 // TUNING: we can save a reg copy if Method* has been promoted.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700608 cg->LoadCurrMethodDirect(arg0_ref);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 break;
610 case 1: // Get method->dex_cache_resolved_methods_
Andreas Gampe4b537a82014-06-30 22:24:53 -0700611 cg->LoadRefDisp(arg0_ref,
buzbee695d13a2014-04-19 13:32:20 -0700612 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700613 arg0_ref,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000614 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 // Set up direct code if known.
616 if (direct_code != 0) {
Ian Rogersff093b32014-04-30 19:04:27 -0700617 if (direct_code != static_cast<uintptr_t>(-1)) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700618 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700619 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700620 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700621 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 }
623 }
624 break;
625 case 2: // Grab target method*
626 CHECK_EQ(cu->dex_file, target_method.dex_file);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700627 cg->LoadRefDisp(arg0_ref,
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700628 ObjArray::OffsetOfElement(target_method.dex_method_index).Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700629 arg0_ref,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000630 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 break;
632 case 3: // Grab the code from the method*
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700633 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 if (direct_code == 0) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700635 cg->LoadWordDisp(arg0_ref,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800636 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Chao-ying Fua77ee512014-07-01 17:43:41 -0700637 cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 }
639 break;
640 }
641 // Intentional fallthrough for x86
642 default:
643 return -1;
644 }
645 }
646 return state + 1;
647}
648
649/*
650 * Bit of a hack here - in the absence of a real scheduling pass,
651 * emit the next instruction in a virtual invoke sequence.
652 * We can use kLr as a temp prior to target address loading
653 * Note also that we'll load the first argument ("this") into
654 * kArg1 here rather than the standard LoadArgRegs.
655 */
656static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
657 int state, const MethodReference& target_method,
658 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700659 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
661 /*
662 * This is the fast path in which the target virtual method is
663 * fully resolved at compile time.
664 */
665 switch (state) {
666 case 0: { // Get "this" [set kArg1]
667 RegLocation rl_arg = info->args[0];
Andreas Gampe4b537a82014-06-30 22:24:53 -0700668 cg->LoadValueDirectFixed(rl_arg, cg->TargetRefReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 break;
670 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700671 case 1: // Is "this" null? [use kArg1]
Andreas Gampe4b537a82014-06-30 22:24:53 -0700672 cg->GenNullCheck(cg->TargetRefReg(kArg1), info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 // get this->klass_ [use kArg1, set kInvokeTgt]
Andreas Gampe4b537a82014-06-30 22:24:53 -0700674 cg->LoadRefDisp(cg->TargetRefReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
Chao-ying Fua77ee512014-07-01 17:43:41 -0700675 cg->TargetPtrReg(kInvokeTgt),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000676 kNotVolatile);
Dave Allisonb373e092014-02-20 16:06:36 -0800677 cg->MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700679 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
Chao-ying Fua77ee512014-07-01 17:43:41 -0700680 cg->LoadRefDisp(cg->TargetPtrReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
681 cg->TargetPtrReg(kInvokeTgt),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000682 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700684 case 3: // Get target method [use kInvokeTgt, set kArg0]
Chao-ying Fua77ee512014-07-01 17:43:41 -0700685 cg->LoadRefDisp(cg->TargetPtrReg(kInvokeTgt),
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700686 ObjArray::OffsetOfElement(method_idx).Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700687 cg->TargetRefReg(kArg0),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000688 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700690 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700691 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700692 cg->LoadWordDisp(cg->TargetRefReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800693 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Chao-ying Fua77ee512014-07-01 17:43:41 -0700694 cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 break;
696 }
697 // Intentional fallthrough for X86
698 default:
699 return -1;
700 }
701 return state + 1;
702}
703
704/*
Jeff Hao88474b42013-10-23 16:24:40 -0700705 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
706 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
707 * more than one interface method map to the same index. Note also that we'll load the first
708 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 */
710static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
711 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700712 uint32_t method_idx, uintptr_t unused,
713 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715
Jeff Hao88474b42013-10-23 16:24:40 -0700716 switch (state) {
717 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700718 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Chao-ying Fua77ee512014-07-01 17:43:41 -0700719 cg->LoadConstant(cg->TargetReg(kHiddenArg, false), target_method.dex_method_index);
Mark Mendelld3703d82014-06-09 15:10:50 -0400720 if (cu->instruction_set == kX86) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700721 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg, false), cg->TargetReg(kHiddenArg, false));
Jeff Hao88474b42013-10-23 16:24:40 -0700722 }
723 break;
724 case 1: { // Get "this" [set kArg1]
725 RegLocation rl_arg = info->args[0];
Andreas Gampe4b537a82014-06-30 22:24:53 -0700726 cg->LoadValueDirectFixed(rl_arg, cg->TargetRefReg(kArg1));
Jeff Hao88474b42013-10-23 16:24:40 -0700727 break;
728 }
729 case 2: // Is "this" null? [use kArg1]
Andreas Gampe4b537a82014-06-30 22:24:53 -0700730 cg->GenNullCheck(cg->TargetRefReg(kArg1), info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700731 // Get this->klass_ [use kArg1, set kInvokeTgt]
Andreas Gampe4b537a82014-06-30 22:24:53 -0700732 cg->LoadRefDisp(cg->TargetRefReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
Chao-ying Fua77ee512014-07-01 17:43:41 -0700733 cg->TargetPtrReg(kInvokeTgt),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000734 kNotVolatile);
Dave Allisonb373e092014-02-20 16:06:36 -0800735 cg->MarkPossibleNullPointerException(info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700736 break;
737 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
buzbee695d13a2014-04-19 13:32:20 -0700738 // NOTE: native pointer.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700739 cg->LoadRefDisp(cg->TargetPtrReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
740 cg->TargetPtrReg(kInvokeTgt),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000741 kNotVolatile);
Jeff Hao88474b42013-10-23 16:24:40 -0700742 break;
743 case 4: // Get target method [use kInvokeTgt, set kArg0]
buzbee695d13a2014-04-19 13:32:20 -0700744 // NOTE: native pointer.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700745 cg->LoadRefDisp(cg->TargetPtrReg(kInvokeTgt),
Dmitry Petrochenko37498b62014-05-05 20:33:38 +0700746 ObjArray::OffsetOfElement(method_idx % ClassLinker::kImtSize).Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700747 cg->TargetRefReg(kArg0),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000748 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700750 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700751 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700752 cg->LoadWordDisp(cg->TargetRefReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800753 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Chao-ying Fua77ee512014-07-01 17:43:41 -0700754 cg->TargetPtrReg(kInvokeTgt));
Jeff Hao88474b42013-10-23 16:24:40 -0700755 break;
756 }
757 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 default:
759 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 }
761 return state + 1;
762}
763
Andreas Gampe2f244e92014-05-08 03:35:25 -0700764template <size_t pointer_size>
765static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset<pointer_size> trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700767 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
769 /*
770 * This handles the case in which the base method is not fully
771 * resolved at compile time, we bail to a runtime helper.
772 */
773 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700774 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 // Load trampoline target
Chao-ying Fua77ee512014-07-01 17:43:41 -0700776 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), trampoline.Int32Value(), cg->TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 }
778 // Load kArg0 with method index
779 CHECK_EQ(cu->dex_file, target_method.dex_file);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700780 cg->LoadConstant(cg->TargetReg(kArg0, false), target_method.dex_method_index);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 return 1;
782 }
783 return -1;
784}
785
786static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
787 int state,
788 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000789 uint32_t unused, uintptr_t unused2,
790 uintptr_t unused3, InvokeType unused4) {
buzbee33ae5582014-06-12 14:56:32 -0700791 if (cu->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700792 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeStaticTrampolineWithAccessCheck);
793 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
794 } else {
795 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
796 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
797 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798}
799
800static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
801 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000802 uint32_t unused, uintptr_t unused2,
803 uintptr_t unused3, InvokeType unused4) {
buzbee33ae5582014-06-12 14:56:32 -0700804 if (cu->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700805 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeDirectTrampolineWithAccessCheck);
806 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
807 } else {
808 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
809 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
810 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811}
812
813static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
814 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000815 uint32_t unused, uintptr_t unused2,
816 uintptr_t unused3, InvokeType unused4) {
buzbee33ae5582014-06-12 14:56:32 -0700817 if (cu->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700818 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeSuperTrampolineWithAccessCheck);
819 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
820 } else {
821 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
822 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
823 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824}
825
826static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
827 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000828 uint32_t unused, uintptr_t unused2,
829 uintptr_t unused3, InvokeType unused4) {
buzbee33ae5582014-06-12 14:56:32 -0700830 if (cu->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700831 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeVirtualTrampolineWithAccessCheck);
832 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
833 } else {
834 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
835 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
836 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837}
838
839static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
840 CallInfo* info, int state,
841 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000842 uint32_t unused, uintptr_t unused2,
843 uintptr_t unused3, InvokeType unused4) {
buzbee33ae5582014-06-12 14:56:32 -0700844 if (cu->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700845 ThreadOffset<8> trampoline = QUICK_ENTRYPOINT_OFFSET(8, pInvokeInterfaceTrampolineWithAccessCheck);
846 return NextInvokeInsnSP<8>(cu, info, trampoline, state, target_method, 0);
847 } else {
848 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
849 return NextInvokeInsnSP<4>(cu, info, trampoline, state, target_method, 0);
850 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851}
852
853int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
854 NextCallInsn next_call_insn,
855 const MethodReference& target_method,
856 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700857 uintptr_t direct_method, InvokeType type, bool skip_this) {
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700858 int last_arg_reg = 3 - 1;
Chao-ying Fua77ee512014-07-01 17:43:41 -0700859 int arg_regs[3] = {TargetReg(kArg1, false).GetReg(), TargetReg(kArg2, false).GetReg(), TargetReg(kArg3, false).GetReg()};
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700860
861 int next_reg = 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 int next_arg = 0;
863 if (skip_this) {
864 next_reg++;
865 next_arg++;
866 }
867 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
868 RegLocation rl_arg = info->args[next_arg++];
869 rl_arg = UpdateRawLoc(rl_arg);
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700870 if (rl_arg.wide && (next_reg <= last_arg_reg - 1)) {
871 RegStorage r_tmp(RegStorage::k64BitPair, arg_regs[next_reg], arg_regs[next_reg + 1]);
buzbee2700f7e2014-03-07 09:46:20 -0800872 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 next_reg++;
874 next_arg++;
875 } else {
876 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800877 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 rl_arg.is_const = false;
879 }
Dmitry Petrochenko26ee07a2014-05-13 12:58:19 +0700880 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(arg_regs[next_reg]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 }
882 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
883 direct_code, direct_method, type);
884 }
885 return call_state;
886}
887
888/*
889 * Load up to 5 arguments, the first three of which will be in
890 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
891 * and as part of the load sequence, it must be replaced with
892 * the target method pointer. Note, this may also be called
893 * for "range" variants if the number of arguments is 5 or fewer.
894 */
895int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
896 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
897 const MethodReference& target_method,
898 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700899 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 RegLocation rl_arg;
901
902 /* If no arguments, just return */
903 if (info->num_arg_words == 0)
904 return call_state;
905
906 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
907 direct_code, direct_method, type);
908
909 DCHECK_LE(info->num_arg_words, 5);
910 if (info->num_arg_words > 3) {
911 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700912 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 RegLocation rl_use0 = info->args[0];
914 RegLocation rl_use1 = info->args[1];
915 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800916 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
917 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 // Wide spans, we need the 2nd half of uses[2].
919 rl_arg = UpdateLocWide(rl_use2);
920 if (rl_arg.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -0700921 if (rl_arg.reg.IsPair()) {
922 reg = rl_arg.reg.GetHigh();
923 } else {
924 RegisterInfo* info = GetRegInfo(rl_arg.reg);
925 info = info->FindMatchingView(RegisterInfo::kHighSingleStorageMask);
926 if (info == nullptr) {
927 // NOTE: For hard float convention we won't split arguments across reg/mem.
928 UNIMPLEMENTED(FATAL) << "Needs hard float api.";
929 }
930 reg = info->GetReg();
931 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 } else {
933 // kArg2 & rArg3 can safely be used here
Chao-ying Fua77ee512014-07-01 17:43:41 -0700934 reg = TargetReg(kArg3, false);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100935 {
936 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700937 Load32Disp(TargetPtrReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100938 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 call_state = next_call_insn(cu_, info, call_state, target_method,
940 vtable_idx, direct_code, direct_method, type);
941 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100942 {
943 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700944 Store32Disp(TargetPtrReg(kSp), (next_use + 1) * 4, reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100945 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
947 direct_code, direct_method, type);
948 next_use++;
949 }
950 // Loop through the rest
951 while (next_use < info->num_arg_words) {
buzbee091cc402014-03-31 10:14:40 -0700952 RegStorage arg_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 rl_arg = info->args[next_use];
954 rl_arg = UpdateRawLoc(rl_arg);
955 if (rl_arg.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700956 arg_reg = rl_arg.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700958 arg_reg = rl_arg.wide ? TargetReg(kArg2, kArg3) : TargetReg(kArg2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 if (rl_arg.wide) {
buzbee091cc402014-03-31 10:14:40 -0700960 LoadValueDirectWideFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 } else {
buzbee091cc402014-03-31 10:14:40 -0700962 LoadValueDirectFixed(rl_arg, arg_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 }
964 call_state = next_call_insn(cu_, info, call_state, target_method,
965 vtable_idx, direct_code, direct_method, type);
966 }
967 int outs_offset = (next_use + 1) * 4;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100968 {
969 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
970 if (rl_arg.wide) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700971 StoreBaseDisp(TargetPtrReg(kSp), outs_offset, arg_reg, k64, kNotVolatile);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100972 next_use += 2;
973 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700974 Store32Disp(TargetPtrReg(kSp), outs_offset, arg_reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100975 next_use++;
976 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 }
978 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
979 direct_code, direct_method, type);
980 }
981 }
982
983 call_state = LoadArgRegs(info, call_state, next_call_insn,
984 target_method, vtable_idx, direct_code, direct_method,
985 type, skip_this);
986
987 if (pcrLabel) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700988 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Chao-ying Fua77ee512014-07-01 17:43:41 -0700989 *pcrLabel = GenExplicitNullCheck(TargetRefReg(kArg1), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -0700990 } else {
991 *pcrLabel = nullptr;
992 // In lieu of generating a check for kArg1 being null, we need to
993 // perform a load when doing implicit checks.
Dave Allison3d14eb62014-07-10 01:54:57 +0000994 RegStorage tmp = AllocTemp();
995 Load32Disp(TargetRefReg(kArg1), 0, tmp);
996 MarkPossibleNullPointerException(info->opt_flags);
997 FreeTemp(tmp);
Dave Allisonf9439142014-03-27 15:10:22 -0700998 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 }
1000 return call_state;
1001}
1002
1003/*
1004 * May have 0+ arguments (also used for jumbo). Note that
1005 * source virtual registers may be in physical registers, so may
1006 * need to be flushed to home location before copying. This
1007 * applies to arg3 and above (see below).
1008 *
1009 * Two general strategies:
1010 * If < 20 arguments
1011 * Pass args 3-18 using vldm/vstm block copy
1012 * Pass arg0, arg1 & arg2 in kArg1-kArg3
1013 * If 20+ arguments
1014 * Pass args arg19+ using memcpy block copy
1015 * Pass arg0, arg1 & arg2 in kArg1-kArg3
1016 *
1017 */
1018int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
1019 LIR** pcrLabel, NextCallInsn next_call_insn,
1020 const MethodReference& target_method,
1021 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001022 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 // If we can treat it as non-range (Jumbo ops will use range form)
1024 if (info->num_arg_words <= 5)
1025 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
1026 next_call_insn, target_method, vtable_idx,
1027 direct_code, direct_method, type, skip_this);
1028 /*
1029 * First load the non-register arguments. Both forms expect all
1030 * of the source arguments to be in their home frame location, so
1031 * scan the s_reg names and flush any that have been promoted to
1032 * frame backing storage.
1033 */
1034 // Scan the rest of the args - if in phys_reg flush to memory
1035 for (int next_arg = 0; next_arg < info->num_arg_words;) {
1036 RegLocation loc = info->args[next_arg];
1037 if (loc.wide) {
1038 loc = UpdateLocWide(loc);
1039 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001040 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001041 StoreBaseDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, k64, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 }
1043 next_arg += 2;
1044 } else {
1045 loc = UpdateLoc(loc);
1046 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001047 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001048 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049 }
1050 next_arg++;
1051 }
1052 }
1053
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001054 // Logic below assumes that Method pointer is at offset zero from SP.
1055 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
1056
1057 // The first 3 arguments are passed via registers.
1058 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
1059 // get size of uintptr_t or size of object reference according to model being used.
1060 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001062 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
1063 DCHECK_GT(regs_left_to_pass_via_stack, 0);
1064
1065 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
1066 // Use vldm/vstm pair using kArg3 as a temp
1067 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1068 direct_code, direct_method, type);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001069 OpRegRegImm(kOpAdd, TargetRefReg(kArg3), TargetPtrReg(kSp), start_offset);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001070 LIR* ld = nullptr;
1071 {
1072 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001073 ld = OpVldm(TargetRefReg(kArg3), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001074 }
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001075 // TUNING: loosen barrier
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001076 ld->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001077 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1078 direct_code, direct_method, type);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001079 OpRegRegImm(kOpAdd, TargetRefReg(kArg3), TargetPtrReg(kSp), 4 /* Method* */ + (3 * 4));
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001080 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1081 direct_code, direct_method, type);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001082 LIR* st = nullptr;
1083 {
1084 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001085 st = OpVstm(TargetRefReg(kArg3), regs_left_to_pass_via_stack);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001086 }
1087 st->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001088 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1089 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001090 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001091 int current_src_offset = start_offset;
1092 int current_dest_offset = outs_offset;
1093
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001094 // Only davik regs are accessed in this loop; no next_call_insn() calls.
1095 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001096 while (regs_left_to_pass_via_stack > 0) {
1097 // This is based on the knowledge that the stack itself is 16-byte aligned.
1098 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
1099 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
1100 size_t bytes_to_move;
1101
1102 /*
1103 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
1104 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
1105 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
1106 * We do this because we could potentially do a smaller move to align.
1107 */
1108 if (regs_left_to_pass_via_stack == 4 ||
1109 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
1110 // Moving 128-bits via xmm register.
1111 bytes_to_move = sizeof(uint32_t) * 4;
1112
1113 // Allocate a free xmm temp. Since we are working through the calling sequence,
Mark Mendelle87f9b52014-04-30 14:13:18 -04001114 // we expect to have an xmm temporary available. AllocTempDouble will abort if
1115 // there are no free registers.
buzbee2700f7e2014-03-07 09:46:20 -08001116 RegStorage temp = AllocTempDouble();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001117
1118 LIR* ld1 = nullptr;
1119 LIR* ld2 = nullptr;
1120 LIR* st1 = nullptr;
1121 LIR* st2 = nullptr;
1122
1123 /*
1124 * The logic is similar for both loads and stores. If we have 16-byte alignment,
1125 * do an aligned move. If we have 8-byte alignment, then do the move in two
1126 * parts. This approach prevents possible cache line splits. Finally, fall back
1127 * to doing an unaligned move. In most cases we likely won't split the cache
1128 * line but we cannot prove it and thus take a conservative approach.
1129 */
1130 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
1131 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
1132
1133 if (src_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001134 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001135 } else if (src_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001136 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovLo128FP);
1137 ld2 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001138 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001139 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001140 ld1 = OpMovRegMem(temp, TargetPtrReg(kSp), current_src_offset, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001141 }
1142
1143 if (dest_is_16b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001144 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovA128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001145 } else if (dest_is_8b_aligned) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001146 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovLo128FP);
1147 st2 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset + (bytes_to_move >> 1),
buzbee2700f7e2014-03-07 09:46:20 -08001148 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001149 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001150 st1 = OpMovMemReg(TargetPtrReg(kSp), current_dest_offset, temp, kMovU128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001151 }
1152
1153 // TODO If we could keep track of aliasing information for memory accesses that are wider
1154 // than 64-bit, we wouldn't need to set up a barrier.
1155 if (ld1 != nullptr) {
1156 if (ld2 != nullptr) {
1157 // For 64-bit load we can actually set up the aliasing information.
1158 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
1159 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
1160 } else {
1161 // Set barrier for 128-bit load.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001162 ld1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001163 }
1164 }
1165 if (st1 != nullptr) {
1166 if (st2 != nullptr) {
1167 // For 64-bit store we can actually set up the aliasing information.
1168 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
1169 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
1170 } else {
1171 // Set barrier for 128-bit store.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001172 st1->u.m.def_mask = &kEncodeAll;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001173 }
1174 }
1175
1176 // Free the temporary used for the data movement.
buzbee091cc402014-03-31 10:14:40 -07001177 FreeTemp(temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001178 } else {
1179 // Moving 32-bits via general purpose register.
1180 bytes_to_move = sizeof(uint32_t);
1181
1182 // Instead of allocating a new temp, simply reuse one of the registers being used
1183 // for argument passing.
Chao-ying Fua77ee512014-07-01 17:43:41 -07001184 RegStorage temp = TargetReg(kArg3, false);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001185
1186 // Now load the argument VR and store to the outs.
Chao-ying Fua77ee512014-07-01 17:43:41 -07001187 Load32Disp(TargetPtrReg(kSp), current_src_offset, temp);
1188 Store32Disp(TargetPtrReg(kSp), current_dest_offset, temp);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -08001189 }
1190
1191 current_src_offset += bytes_to_move;
1192 current_dest_offset += bytes_to_move;
1193 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
1194 }
1195 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 // Generate memcpy
Chao-ying Fua77ee512014-07-01 17:43:41 -07001197 OpRegRegImm(kOpAdd, TargetRefReg(kArg0), TargetPtrReg(kSp), outs_offset);
1198 OpRegRegImm(kOpAdd, TargetRefReg(kArg1), TargetPtrReg(kSp), start_offset);
buzbee33ae5582014-06-12 14:56:32 -07001199 if (cu_->target64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001200 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(8, pMemcpy), TargetRefReg(kArg0),
1201 TargetRefReg(kArg1), (info->num_arg_words - 3) * 4, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001202 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001203 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(4, pMemcpy), TargetRefReg(kArg0),
1204 TargetRefReg(kArg1), (info->num_arg_words - 3) * 4, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001205 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 }
1207
1208 call_state = LoadArgRegs(info, call_state, next_call_insn,
1209 target_method, vtable_idx, direct_code, direct_method,
1210 type, skip_this);
1211
1212 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
1213 direct_code, direct_method, type);
1214 if (pcrLabel) {
Andreas Gampe5655e842014-06-17 16:36:07 -07001215 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001216 *pcrLabel = GenExplicitNullCheck(TargetRefReg(kArg1), info->opt_flags);
Dave Allisonf9439142014-03-27 15:10:22 -07001217 } else {
1218 *pcrLabel = nullptr;
1219 // In lieu of generating a check for kArg1 being null, we need to
1220 // perform a load when doing implicit checks.
Dave Allison3d14eb62014-07-10 01:54:57 +00001221 RegStorage tmp = AllocTemp();
1222 Load32Disp(TargetRefReg(kArg1), 0, tmp);
1223 MarkPossibleNullPointerException(info->opt_flags);
1224 FreeTemp(tmp);
Dave Allisonf9439142014-03-27 15:10:22 -07001225 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 }
1227 return call_state;
1228}
1229
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001230RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 RegLocation res;
1232 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001233 res = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 } else {
1235 res = info->result;
1236 }
1237 return res;
1238}
1239
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001240RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 RegLocation res;
1242 if (info->result.location == kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -07001243 res = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 } else {
1245 res = info->result;
1246 }
1247 return res;
1248}
1249
Fred Shih460503b2014-06-18 11:26:11 -07001250bool Mir2Lir::GenInlinedGet(CallInfo* info) {
1251 if (cu_->instruction_set == kMips) {
1252 // TODO - add Mips implementation
1253 return false;
1254 }
1255
1256 // the refrence class is stored in the image dex file which might not be the same as the cu's
1257 // dex file. Query the reference class for the image dex file then reset to starting dex file
1258 // in after loading class type.
1259 uint16_t type_idx = 0;
1260 const DexFile* ref_dex_file = nullptr;
1261 {
1262 ScopedObjectAccess soa(Thread::Current());
1263 type_idx = mirror::Reference::GetJavaLangRefReference()->GetDexTypeIndex();
1264 ref_dex_file = mirror::Reference::GetJavaLangRefReference()->GetDexCache()->GetDexFile();
1265 }
1266 CHECK(LIKELY(ref_dex_file != nullptr));
1267
1268 // address is either static within the image file, or needs to be patched up after compilation.
1269 bool unused_type_initialized;
1270 bool use_direct_type_ptr;
1271 uintptr_t direct_type_ptr;
1272 bool is_finalizable;
1273 const DexFile* old_dex = cu_->dex_file;
1274 cu_->dex_file = ref_dex_file;
1275 if (!cu_->compiler_driver->CanEmbedTypeInCode(*ref_dex_file, type_idx, &unused_type_initialized,
1276 &use_direct_type_ptr, &direct_type_ptr,
1277 &is_finalizable) || is_finalizable) {
1278 cu_->dex_file = old_dex;
1279 // address is not known and post-compile patch is not possible, cannot insert intrinsic.
1280 return false;
1281 }
1282 if (use_direct_type_ptr) {
1283 LoadConstant(TargetReg(kArg1), direct_type_ptr);
1284 } else {
1285 LoadClassType(type_idx, kArg1);
1286 }
1287 cu_->dex_file = old_dex;
1288
1289 // intrinsic logic start.
1290 RegLocation rl_obj = info->args[0];
1291 rl_obj = LoadValue(rl_obj);
1292
1293 RegStorage reg_class = TargetReg(kArg1, cu_->target64);
1294 RegStorage reg_slow_path = AllocTemp();
1295 RegStorage reg_disabled = AllocTemp();
1296 Load32Disp(reg_class, mirror::ReferenceClass::SlowPathEnabledOffset().Int32Value(),
1297 reg_slow_path);
1298 Load32Disp(reg_class, mirror::ReferenceClass::DisableIntrinsicOffset().Int32Value(),
1299 reg_disabled);
1300 OpRegRegReg(kOpOr, reg_slow_path, reg_slow_path, reg_disabled);
1301 FreeTemp(reg_disabled);
1302
1303 // if slow path, jump to JNI path target
1304 LIR* slow_path_branch = OpCmpImmBranch(kCondNe, reg_slow_path, 0, nullptr);
1305 FreeTemp(reg_slow_path);
1306
1307 // slow path not enabled, simply load the referent of the reference object
1308 RegLocation rl_dest = InlineTarget(info);
1309 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
1310 GenNullCheck(rl_obj.reg, info->opt_flags);
1311 LoadRefDisp(rl_obj.reg, mirror::Reference::ReferentOffset().Int32Value(), rl_result.reg,
1312 kNotVolatile);
1313 MarkPossibleNullPointerException(info->opt_flags);
1314 StoreValue(rl_dest, rl_result);
1315 LIR* jump_finished = OpUnconditionalBranch(nullptr);
1316
1317 // JNI target
1318 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1319 slow_path_branch->target = slow_path_target;
1320 ResetRegPool();
1321 GenInvokeNoInline(info);
1322
1323 LIR* finished_target = NewLIR0(kPseudoTargetLabel);
1324 jump_finished->target = finished_target;
1325
1326 return true;
1327}
1328
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001329bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 if (cu_->instruction_set == kMips) {
1331 // TODO - add Mips implementation
1332 return false;
1333 }
1334 // Location of reference to data array
1335 int value_offset = mirror::String::ValueOffset().Int32Value();
1336 // Location of count
1337 int count_offset = mirror::String::CountOffset().Int32Value();
1338 // Starting offset within data array
1339 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1340 // Start of char data with array_
1341 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1342
1343 RegLocation rl_obj = info->args[0];
1344 RegLocation rl_idx = info->args[1];
buzbeea0cd2d72014-06-01 09:33:49 -07001345 rl_obj = LoadValue(rl_obj, kRefReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001346 // X86 wants to avoid putting a constant index into a register.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001347 if (!((cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64)&& rl_idx.is_const)) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001348 rl_idx = LoadValue(rl_idx, kCoreReg);
1349 }
buzbee2700f7e2014-03-07 09:46:20 -08001350 RegStorage reg_max;
1351 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001353 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001354 RegStorage reg_off;
1355 RegStorage reg_ptr;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001356 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 reg_off = AllocTemp();
buzbeea0cd2d72014-06-01 09:33:49 -07001358 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 if (range_check) {
1360 reg_max = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -07001361 Load32Disp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001362 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 }
buzbee695d13a2014-04-19 13:32:20 -07001364 Load32Disp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001365 MarkPossibleNullPointerException(info->opt_flags);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001366 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367 if (range_check) {
Mingyao Yang3a74d152014-04-21 15:39:44 -07001368 // Set up a slow path to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001369 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001371 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001372 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001373 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 } else {
1375 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001376 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001378 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001379 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001380 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Dave Allison3d14eb62014-07-10 01:54:57 +00001381 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
1382 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001383 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001384 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001385 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 }
1387 reg_off = AllocTemp();
buzbeea0cd2d72014-06-01 09:33:49 -07001388 reg_ptr = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -07001389 Load32Disp(rl_obj.reg, offset_offset, reg_off);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001390 LoadRefDisp(rl_obj.reg, value_offset, reg_ptr, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001392 if (rl_idx.is_const) {
1393 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1394 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001395 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001396 }
buzbee2700f7e2014-03-07 09:46:20 -08001397 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001398 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001399 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001400 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 RegLocation rl_dest = InlineTarget(info);
1402 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001403 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee2700f7e2014-03-07 09:46:20 -08001404 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001405 } else {
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001406 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001407 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408 FreeTemp(reg_off);
1409 FreeTemp(reg_ptr);
1410 StoreValue(rl_dest, rl_result);
1411 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001412 DCHECK(range_check_branch != nullptr);
1413 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001414 AddIntrinsicSlowPath(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001415 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 return true;
1417}
1418
1419// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001420bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001421 if (cu_->instruction_set == kMips) {
1422 // TODO - add Mips implementation
1423 return false;
1424 }
1425 // dst = src.length();
1426 RegLocation rl_obj = info->args[0];
buzbeea0cd2d72014-06-01 09:33:49 -07001427 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 RegLocation rl_dest = InlineTarget(info);
1429 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001430 GenNullCheck(rl_obj.reg, info->opt_flags);
buzbee695d13a2014-04-19 13:32:20 -07001431 Load32Disp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001432 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001433 if (is_empty) {
1434 // dst = (dst == 0);
1435 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001436 RegStorage t_reg = AllocTemp();
1437 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1438 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001439 } else if (cu_->instruction_set == kArm64) {
1440 OpRegImm(kOpSub, rl_result.reg, 1);
1441 OpRegRegImm(kOpLsr, rl_result.reg, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001443 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001444 OpRegImm(kOpSub, rl_result.reg, 1);
1445 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446 }
1447 }
1448 StoreValue(rl_dest, rl_result);
1449 return true;
1450}
1451
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001452bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001453 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
1454 // TODO - add Mips implementation; Enable Arm64.
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001455 return false;
1456 }
1457 RegLocation rl_src_i = info->args[0];
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001458 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -07001459 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001460 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001461 if (size == k64) {
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001462 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +01001463 OpRegReg(kOpRev, rl_result.reg, rl_i.reg);
1464 StoreValueWide(rl_dest, rl_result);
1465 return true;
1466 }
buzbee2700f7e2014-03-07 09:46:20 -08001467 RegStorage r_i_low = rl_i.reg.GetLow();
1468 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001469 // 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 +00001470 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001471 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001472 }
buzbee2700f7e2014-03-07 09:46:20 -08001473 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1474 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1475 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001476 FreeTemp(r_i_low);
1477 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001478 StoreValueWide(rl_dest, rl_result);
1479 } else {
buzbee695d13a2014-04-19 13:32:20 -07001480 DCHECK(size == k32 || size == kSignedHalf);
1481 OpKind op = (size == k32) ? kOpRev : kOpRevsh;
buzbee2700f7e2014-03-07 09:46:20 -08001482 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001483 StoreValue(rl_dest, rl_result);
1484 }
1485 return true;
1486}
1487
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001488bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001489 if (cu_->instruction_set == kMips) {
1490 // TODO - add Mips implementation
1491 return false;
1492 }
1493 RegLocation rl_src = info->args[0];
1494 rl_src = LoadValue(rl_src, kCoreReg);
1495 RegLocation rl_dest = InlineTarget(info);
1496 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001497 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001499 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1500 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1501 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 StoreValue(rl_dest, rl_result);
1503 return true;
1504}
1505
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001506bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507 if (cu_->instruction_set == kMips) {
1508 // TODO - add Mips implementation
1509 return false;
1510 }
Vladimir Markob9823312014-03-20 17:38:43 +00001511 RegLocation rl_src = info->args[0];
1512 rl_src = LoadValueWide(rl_src, kCoreReg);
1513 RegLocation rl_dest = InlineTargetWide(info);
1514 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1515
1516 // If on x86 or if we would clobber a register needed later, just copy the source first.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001517 if (cu_->instruction_set != kX86_64 &&
1518 (cu_->instruction_set == kX86 ||
1519 rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001520 OpRegCopyWide(rl_result.reg, rl_src.reg);
1521 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1522 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1523 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001524 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1525 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001526 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001527 }
1528 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 }
Vladimir Markob9823312014-03-20 17:38:43 +00001530
1531 // abs(x) = y<=x>>31, (x+y)^y.
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001532 RegStorage sign_reg;
1533 if (cu_->instruction_set == kX86_64) {
1534 sign_reg = AllocTempWide();
1535 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
1536 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1537 OpRegReg(kOpXor, rl_result.reg, sign_reg);
1538 } else {
1539 sign_reg = AllocTemp();
1540 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1541 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1542 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1543 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1544 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
1545 }
buzbee082833c2014-05-17 23:16:26 -07001546 FreeTemp(sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001547 StoreValueWide(rl_dest, rl_result);
1548 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549}
1550
Yixin Shoudbb17e32014-02-07 05:09:30 -08001551bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1552 if (cu_->instruction_set == kMips) {
1553 // TODO - add Mips implementation
1554 return false;
1555 }
1556 RegLocation rl_src = info->args[0];
1557 rl_src = LoadValue(rl_src, kCoreReg);
1558 RegLocation rl_dest = InlineTarget(info);
1559 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001560 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001561 StoreValue(rl_dest, rl_result);
1562 return true;
1563}
1564
Serban Constantinescu23abec92014-07-02 16:13:38 +01001565bool Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
1566 // Currently implemented only for ARM64
1567 return false;
1568}
1569
1570bool Mir2Lir::GenInlinedMinMaxFP(CallInfo* info, bool is_min, bool is_double) {
1571 // Currently implemented only for ARM64
1572 return false;
1573}
1574
Yixin Shoudbb17e32014-02-07 05:09:30 -08001575bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1576 if (cu_->instruction_set == kMips) {
1577 // TODO - add Mips implementation
1578 return false;
1579 }
1580 RegLocation rl_src = info->args[0];
1581 rl_src = LoadValueWide(rl_src, kCoreReg);
1582 RegLocation rl_dest = InlineTargetWide(info);
1583 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu169489b2014-06-11 16:43:35 +01001584
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001585 OpRegCopyWide(rl_result.reg, rl_src.reg);
1586 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001587 StoreValueWide(rl_dest, rl_result);
1588 return true;
1589}
1590
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001591bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001592 if (cu_->instruction_set == kMips) {
1593 // TODO - add Mips implementation
1594 return false;
1595 }
1596 RegLocation rl_src = info->args[0];
1597 RegLocation rl_dest = InlineTarget(info);
1598 StoreValue(rl_dest, rl_src);
1599 return true;
1600}
1601
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001602bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 if (cu_->instruction_set == kMips) {
1604 // TODO - add Mips implementation
1605 return false;
1606 }
1607 RegLocation rl_src = info->args[0];
1608 RegLocation rl_dest = InlineTargetWide(info);
1609 StoreValueWide(rl_dest, rl_src);
1610 return true;
1611}
1612
DaniilSokolov70c4f062014-06-24 17:34:00 -07001613bool Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
1614 return false;
1615}
1616
1617
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001619 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 * otherwise bails to standard library code.
1621 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001622bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001623 if (cu_->instruction_set == kMips) {
1624 // TODO - add Mips implementation
1625 return false;
1626 }
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001627 if (cu_->instruction_set == kX86_64) {
1628 // TODO - add kX86_64 implementation
1629 return false;
1630 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001631 RegLocation rl_obj = info->args[0];
1632 RegLocation rl_char = info->args[1];
1633 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1634 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1635 return false;
1636 }
1637
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001638 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 LockCallTemps(); // Using fixed registers
Chao-ying Fua77ee512014-07-01 17:43:41 -07001640 RegStorage reg_ptr = TargetRefReg(kArg0);
1641 RegStorage reg_char = TargetReg(kArg1, false);
1642 RegStorage reg_start = TargetReg(kArg2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 LoadValueDirectFixed(rl_obj, reg_ptr);
1645 LoadValueDirectFixed(rl_char, reg_char);
1646 if (zero_based) {
1647 LoadConstant(reg_start, 0);
1648 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001649 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 LoadValueDirectFixed(rl_start, reg_start);
1651 }
buzbee33ae5582014-06-12 14:56:32 -07001652 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001653 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pIndexOf)) :
1654 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001655 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001656 LIR* high_code_point_branch =
1657 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001659 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001660 if (!rl_char.is_const) {
1661 // Add the slow path for code points beyond 0xFFFF.
1662 DCHECK(high_code_point_branch != nullptr);
1663 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1664 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001665 AddIntrinsicSlowPath(info, high_code_point_branch, resume_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001666 } else {
1667 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1668 DCHECK(high_code_point_branch == nullptr);
1669 }
buzbeea0cd2d72014-06-01 09:33:49 -07001670 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001671 RegLocation rl_dest = InlineTarget(info);
1672 StoreValue(rl_dest, rl_return);
1673 return true;
1674}
1675
1676/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001677bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 if (cu_->instruction_set == kMips) {
1679 // TODO - add Mips implementation
1680 return false;
1681 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001682 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 LockCallTemps(); // Using fixed registers
Chao-ying Fua77ee512014-07-01 17:43:41 -07001684 RegStorage reg_this = TargetRefReg(kArg0);
1685 RegStorage reg_cmp = TargetRefReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686
1687 RegLocation rl_this = info->args[0];
1688 RegLocation rl_cmp = info->args[1];
1689 LoadValueDirectFixed(rl_this, reg_this);
1690 LoadValueDirectFixed(rl_cmp, reg_cmp);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001691 RegStorage r_tgt;
1692 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee33ae5582014-06-12 14:56:32 -07001693 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001694 r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pStringCompareTo));
1695 } else {
1696 r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
1697 }
1698 } else {
1699 r_tgt = RegStorage::InvalidReg();
1700 }
Dave Allisonf9439142014-03-27 15:10:22 -07001701 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001702 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001703 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001704 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001705 AddIntrinsicSlowPath(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 // NOTE: not a safepoint
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001707 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 OpReg(kOpBlx, r_tgt);
1709 } else {
buzbee33ae5582014-06-12 14:56:32 -07001710 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001711 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(8, pStringCompareTo));
1712 } else {
1713 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
1714 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001715 }
buzbeea0cd2d72014-06-01 09:33:49 -07001716 RegLocation rl_return = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 RegLocation rl_dest = InlineTarget(info);
1718 StoreValue(rl_dest, rl_return);
1719 return true;
1720}
1721
1722bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1723 RegLocation rl_dest = InlineTarget(info);
Andreas Gampe7a949612014-07-08 11:03:59 -07001724
1725 // Early exit if the result is unused.
1726 if (rl_dest.orig_sreg < 0) {
1727 return true;
1728 }
1729
nikolay serdjukc5e4ce12014-06-10 17:07:10 +07001730 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001731
1732 switch (cu_->instruction_set) {
1733 case kArm:
1734 // Fall-through.
1735 case kThumb2:
1736 // Fall-through.
1737 case kMips:
Chao-ying Fua77ee512014-07-01 17:43:41 -07001738 Load32Disp(TargetPtrReg(kSelf), Thread::PeerOffset<4>().Int32Value(), rl_result.reg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001739 break;
1740
1741 case kArm64:
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001742 LoadRefDisp(TargetPtrReg(kSelf), Thread::PeerOffset<8>().Int32Value(), rl_result.reg,
1743 kNotVolatile);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001744 break;
1745
1746 case kX86:
1747 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg,
1748 Thread::PeerOffset<4>());
1749 break;
1750
1751 case kX86_64:
1752 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg,
1753 Thread::PeerOffset<8>());
1754 break;
1755
1756 default:
1757 LOG(FATAL) << "Unexpected isa " << cu_->instruction_set;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001758 }
1759 StoreValue(rl_dest, rl_result);
1760 return true;
1761}
1762
1763bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1764 bool is_long, bool is_volatile) {
1765 if (cu_->instruction_set == kMips) {
1766 // TODO - add Mips implementation
1767 return false;
1768 }
1769 // Unused - RegLocation rl_src_unsafe = info->args[0];
1770 RegLocation rl_src_obj = info->args[1]; // Object
1771 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001772 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001773 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001774
buzbeea0cd2d72014-06-01 09:33:49 -07001775 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001776 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001777 RegLocation rl_result = EvalLoc(rl_dest, LocToRegClass(rl_dest), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001778 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001779 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1780 || cu_->instruction_set == kArm64) {
1781 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001782 } else {
1783 RegStorage rl_temp_offset = AllocTemp();
1784 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001785 LoadBaseDisp(rl_temp_offset, 0, rl_result.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001786 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001787 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 } else {
Matteo Franchin255e0142014-07-04 13:50:41 +01001789 if (rl_result.ref) {
1790 LoadRefIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0);
1791 } else {
1792 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, k32);
1793 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001794 }
1795
1796 if (is_volatile) {
1797 // Without context sensitive analysis, we must issue the most conservative barriers.
1798 // In this case, either a load or store may follow so we issue both barriers.
1799 GenMemBarrier(kLoadLoad);
1800 GenMemBarrier(kLoadStore);
1801 }
1802
1803 if (is_long) {
1804 StoreValueWide(rl_dest, rl_result);
1805 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 StoreValue(rl_dest, rl_result);
1807 }
1808 return true;
1809}
1810
1811bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1812 bool is_object, bool is_volatile, bool is_ordered) {
1813 if (cu_->instruction_set == kMips) {
1814 // TODO - add Mips implementation
1815 return false;
1816 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001817 // Unused - RegLocation rl_src_unsafe = info->args[0];
1818 RegLocation rl_src_obj = info->args[1]; // Object
1819 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001820 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001821 RegLocation rl_src_value = info->args[4]; // value to store
1822 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001823 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001824 GenMemBarrier(kStoreStore);
1825 }
buzbeea0cd2d72014-06-01 09:33:49 -07001826 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001827 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1828 RegLocation rl_value;
1829 if (is_long) {
1830 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001831 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64
1832 || cu_->instruction_set == kArm64) {
1833 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k64);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001834 } else {
1835 RegStorage rl_temp_offset = AllocTemp();
1836 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001837 StoreBaseDisp(rl_temp_offset, 0, rl_value.reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -07001838 FreeTemp(rl_temp_offset);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001839 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001841 rl_value = LoadValue(rl_src_value);
Matteo Franchin255e0142014-07-04 13:50:41 +01001842 if (rl_value.ref) {
1843 StoreRefIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0);
1844 } else {
1845 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, k32);
1846 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001847 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001848
1849 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
buzbee091cc402014-03-31 10:14:40 -07001850 FreeTemp(rl_offset.reg);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001851
Brian Carlstrom7940e442013-07-12 13:46:57 -07001852 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001853 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001854 GenMemBarrier(kStoreLoad);
1855 }
1856 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001857 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 }
1859 return true;
1860}
1861
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001862void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001863 if ((info->opt_flags & MIR_INLINED) != 0) {
1864 // Already inlined but we may still need the null check.
1865 if (info->type != kStatic &&
1866 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1867 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
buzbeea0cd2d72014-06-01 09:33:49 -07001868 RegLocation rl_obj = LoadValue(info->args[0], kRefReg);
Mingyao Yange643a172014-04-08 11:02:52 -07001869 GenNullCheck(rl_obj.reg);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001870 }
1871 return;
1872 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001873 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001874 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1875 ->GenIntrinsic(this, info)) {
1876 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001878 GenInvokeNoInline(info);
1879}
1880
Andreas Gampe2f244e92014-05-08 03:35:25 -07001881template <size_t pointer_size>
1882static LIR* GenInvokeNoInlineCall(Mir2Lir* mir_to_lir, InvokeType type) {
1883 ThreadOffset<pointer_size> trampoline(-1);
1884 switch (type) {
1885 case kInterface:
1886 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeInterfaceTrampolineWithAccessCheck);
1887 break;
1888 case kDirect:
1889 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeDirectTrampolineWithAccessCheck);
1890 break;
1891 case kStatic:
1892 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeStaticTrampolineWithAccessCheck);
1893 break;
1894 case kSuper:
1895 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeSuperTrampolineWithAccessCheck);
1896 break;
1897 case kVirtual:
1898 trampoline = QUICK_ENTRYPOINT_OFFSET(pointer_size, pInvokeVirtualTrampolineWithAccessCheck);
1899 break;
1900 default:
1901 LOG(FATAL) << "Unexpected invoke type";
1902 }
1903 return mir_to_lir->OpThreadMem(kOpBlx, trampoline);
1904}
1905
Vladimir Marko3bc86152014-03-13 14:11:28 +00001906void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 int call_state = 0;
1908 LIR* null_ck;
1909 LIR** p_null_ck = NULL;
1910 NextCallInsn next_call_insn;
1911 FlushAllRegs(); /* Everything to home location */
1912 // Explicit register usage
1913 LockCallTemps();
1914
Vladimir Markof096aad2014-01-23 15:51:58 +00001915 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1916 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
Mark Mendelle87f9b52014-04-30 14:13:18 -04001917 BeginInvoke(info);
Vladimir Markof096aad2014-01-23 15:51:58 +00001918 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1919 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1920 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001921 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001922 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001924 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001925 } else if (info->type == kDirect) {
1926 if (fast_path) {
1927 p_null_ck = &null_ck;
1928 }
1929 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1930 skip_this = false;
1931 } else if (info->type == kStatic) {
1932 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1933 skip_this = false;
1934 } else if (info->type == kSuper) {
1935 DCHECK(!fast_path); // Fast path is a direct call.
1936 next_call_insn = NextSuperCallInsnSP;
1937 skip_this = false;
1938 } else {
1939 DCHECK_EQ(info->type, kVirtual);
1940 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1941 skip_this = fast_path;
1942 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001943 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001944 if (!info->is_range) {
1945 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001946 next_call_insn, target_method, method_info.VTableIndex(),
1947 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001948 original_type, skip_this);
1949 } else {
1950 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001951 next_call_insn, target_method, method_info.VTableIndex(),
1952 method_info.DirectCode(), method_info.DirectMethod(),
1953 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 }
1955 // Finish up any of the call sequence not interleaved in arg loading
1956 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001957 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1958 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 }
1960 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001961 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001962 call_inst = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001963 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001964 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001965 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001966 // We can have the linker fixup a call relative.
1967 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001968 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001969 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001970 call_inst = OpMem(kOpBlx, TargetRefReg(kArg0),
Mark Mendell55d0eac2014-02-06 11:02:52 -08001971 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1972 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001974 // TODO: Extract?
buzbee33ae5582014-06-12 14:56:32 -07001975 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001976 call_inst = GenInvokeNoInlineCall<8>(this, info->type);
1977 } else {
Andreas Gampe3ec5da22014-05-12 18:43:28 -07001978 call_inst = GenInvokeNoInlineCall<4>(this, info->type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001979 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 }
1981 }
Mark Mendelle87f9b52014-04-30 14:13:18 -04001982 EndInvoke(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 MarkSafepointPC(call_inst);
1984
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001985 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001986 if (info->result.location != kLocInvalid) {
1987 // We have a following MOVE_RESULT - do it now.
1988 if (info->result.wide) {
buzbeea0cd2d72014-06-01 09:33:49 -07001989 RegLocation ret_loc = GetReturnWide(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 StoreValueWide(info->result, ret_loc);
1991 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001992 RegLocation ret_loc = GetReturn(LocToRegClass(info->result));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001993 StoreValue(info->result, ret_loc);
1994 }
1995 }
1996}
1997
1998} // namespace art