blob: fee15d7b3a87e9ce9f3cc35e669f2868fcdd575a [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"
Dave Allison754ddad2014-02-19 14:05:39 -080022#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070023#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "invoke_type.h"
25#include "mirror/array.h"
26#include "mirror/string.h"
27#include "mir_to_lir-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "x86/codegen_x86.h"
29
30namespace art {
31
32/*
33 * This source files contains "gen" codegen routines that should
34 * be applicable to most targets. Only mid-level support utilities
35 * and "op" calls may be used here.
36 */
37
Vladimir Marko3bc86152014-03-13 14:11:28 +000038void Mir2Lir::AddIntrinsicLaunchpad(CallInfo* info, LIR* branch, LIR* resume) {
39 class IntrinsicLaunchpadPath : public Mir2Lir::LIRSlowPath {
40 public:
41 IntrinsicLaunchpadPath(Mir2Lir* m2l, CallInfo* info, LIR* branch, LIR* resume = nullptr)
42 : LIRSlowPath(m2l, info->offset, branch, resume), info_(info) {
43 }
44
45 void Compile() {
46 m2l_->ResetRegPool();
47 m2l_->ResetDefTracking();
48 LIR* label = GenerateTargetLabel();
49 label->opcode = kPseudoIntrinsicRetry;
50 // NOTE: GenInvokeNoInline() handles MarkSafepointPC.
51 m2l_->GenInvokeNoInline(info_);
52 if (cont_ != nullptr) {
53 m2l_->OpUnconditionalBranch(cont_);
54 }
55 }
56
57 private:
58 CallInfo* const info_;
59 };
60
61 AddSlowPath(new (arena_) IntrinsicLaunchpadPath(this, info, branch, resume));
62}
63
Brian Carlstrom7940e442013-07-12 13:46:57 -070064/*
65 * To save scheduling time, helper calls are broken into two parts: generation of
Dave Allison754ddad2014-02-19 14:05:39 -080066 * the helper target address, and the actual call to the helper.
67 * These functions can be overridden by architecture specific codegen.
Brian Carlstrom7940e442013-07-12 13:46:57 -070068 */
Ian Rogersdd7624d2014-03-14 17:43:00 -070069RegStorage Mir2Lir::CallHelperSetup(ThreadOffset<4> helper_offset) {
Dave Allison754ddad2014-02-19 14:05:39 -080070 return LoadHelper(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070071}
72
73/* NOTE: if r_tgt is a temp, it will be freed following use */
Ian Rogersdd7624d2014-03-14 17:43:00 -070074LIR* Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<4> helper_offset, bool safepoint_pc,
buzbee2700f7e2014-03-07 09:46:20 -080075 bool use_link) {
Brian Carlstrom60d7a652014-03-13 18:10:08 -070076 OpKind op = use_link ? kOpBlx : kOpBx;
Dave Allison754ddad2014-02-19 14:05:39 -080077 LIR* call_inst = OpReg(op, r_tgt);
78 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 if (safepoint_pc) {
80 MarkSafepointPC(call_inst);
81 }
82 return call_inst;
83}
84
Ian Rogersdd7624d2014-03-14 17:43:00 -070085void Mir2Lir::CallRuntimeHelperImm(ThreadOffset<4> helper_offset, int arg0, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -080086 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +000088 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 CallHelper(r_tgt, helper_offset, safepoint_pc);
90}
91
Ian Rogersdd7624d2014-03-14 17:43:00 -070092void Mir2Lir::CallRuntimeHelperReg(ThreadOffset<4> helper_offset, RegStorage arg0,
93 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -080094 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 OpRegCopy(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +000096 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 CallHelper(r_tgt, helper_offset, safepoint_pc);
98}
99
Ian Rogersdd7624d2014-03-14 17:43:00 -0700100void Mir2Lir::CallRuntimeHelperRegLocation(ThreadOffset<4> helper_offset, RegLocation arg0,
Ian Rogers848871b2013-08-05 10:56:33 -0700101 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800102 RegStorage r_tgt = CallHelperSetup(helper_offset);
103 if (arg0.wide == 0) {
104 LoadValueDirectFixed(arg0, TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800106 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
107 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000109 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 CallHelper(r_tgt, helper_offset, safepoint_pc);
111}
112
Ian Rogersdd7624d2014-03-14 17:43:00 -0700113void Mir2Lir::CallRuntimeHelperImmImm(ThreadOffset<4> helper_offset, int arg0, int arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800115 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 LoadConstant(TargetReg(kArg0), arg0);
117 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000118 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 CallHelper(r_tgt, helper_offset, safepoint_pc);
120}
121
Ian Rogersdd7624d2014-03-14 17:43:00 -0700122void Mir2Lir::CallRuntimeHelperImmRegLocation(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 RegLocation arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800124 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 if (arg1.wide == 0) {
126 LoadValueDirectFixed(arg1, TargetReg(kArg1));
127 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800128 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
129 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 }
131 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000132 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 CallHelper(r_tgt, helper_offset, safepoint_pc);
134}
135
Ian Rogersdd7624d2014-03-14 17:43:00 -0700136void Mir2Lir::CallRuntimeHelperRegLocationImm(ThreadOffset<4> helper_offset, RegLocation arg0,
137 int arg1, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800138 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 LoadValueDirectFixed(arg0, TargetReg(kArg0));
140 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000141 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 CallHelper(r_tgt, helper_offset, safepoint_pc);
143}
144
Ian Rogersdd7624d2014-03-14 17:43:00 -0700145void Mir2Lir::CallRuntimeHelperImmReg(ThreadOffset<4> helper_offset, int arg0, RegStorage arg1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800147 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 OpRegCopy(TargetReg(kArg1), arg1);
149 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000150 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 CallHelper(r_tgt, helper_offset, safepoint_pc);
152}
153
Ian Rogersdd7624d2014-03-14 17:43:00 -0700154void Mir2Lir::CallRuntimeHelperRegImm(ThreadOffset<4> helper_offset, RegStorage arg0, int arg1,
Ian Rogers848871b2013-08-05 10:56:33 -0700155 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800156 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 OpRegCopy(TargetReg(kArg0), arg0);
158 LoadConstant(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000159 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 CallHelper(r_tgt, helper_offset, safepoint_pc);
161}
162
Ian Rogersdd7624d2014-03-14 17:43:00 -0700163void Mir2Lir::CallRuntimeHelperImmMethod(ThreadOffset<4> helper_offset, int arg0,
164 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800165 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 LoadCurrMethodDirect(TargetReg(kArg1));
167 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000168 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 CallHelper(r_tgt, helper_offset, safepoint_pc);
170}
171
Ian Rogersdd7624d2014-03-14 17:43:00 -0700172void Mir2Lir::CallRuntimeHelperRegMethod(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800173 bool safepoint_pc) {
174 RegStorage r_tgt = CallHelperSetup(helper_offset);
175 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800176 if (TargetReg(kArg0) != arg0) {
177 OpRegCopy(TargetReg(kArg0), arg0);
178 }
179 LoadCurrMethodDirect(TargetReg(kArg1));
180 ClobberCallerSave();
181 CallHelper(r_tgt, helper_offset, safepoint_pc);
182}
183
Ian Rogersdd7624d2014-03-14 17:43:00 -0700184void Mir2Lir::CallRuntimeHelperRegMethodRegLocation(ThreadOffset<4> helper_offset, RegStorage arg0,
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800185 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800186 RegStorage r_tgt = CallHelperSetup(helper_offset);
187 DCHECK_NE(TargetReg(kArg1).GetReg(), arg0.GetReg());
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800188 if (TargetReg(kArg0) != arg0) {
189 OpRegCopy(TargetReg(kArg0), arg0);
190 }
191 LoadCurrMethodDirect(TargetReg(kArg1));
192 LoadValueDirectFixed(arg2, TargetReg(kArg2));
193 ClobberCallerSave();
194 CallHelper(r_tgt, helper_offset, safepoint_pc);
195}
196
Ian Rogersdd7624d2014-03-14 17:43:00 -0700197void Mir2Lir::CallRuntimeHelperRegLocationRegLocation(ThreadOffset<4> helper_offset,
198 RegLocation arg0, RegLocation arg1,
199 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800200 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 if (arg0.wide == 0) {
202 LoadValueDirectFixed(arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
203 if (arg1.wide == 0) {
204 if (cu_->instruction_set == kMips) {
205 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1));
206 } else {
207 LoadValueDirectFixed(arg1, TargetReg(kArg1));
208 }
209 } else {
210 if (cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -0800211 RegStorage r_tmp;
212 if (arg1.fp) {
213 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
214 } else {
215 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
216 }
217 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800219 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg1), TargetReg(kArg2));
220 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 }
222 }
223 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800224 RegStorage r_tmp;
225 if (arg0.fp) {
226 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg0), TargetReg(kFArg1));
227 } else {
228 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
229 }
230 LoadValueDirectWideFixed(arg0, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 if (arg1.wide == 0) {
232 LoadValueDirectFixed(arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2));
233 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800234 RegStorage r_tmp;
235 if (arg1.fp) {
236 r_tmp = RegStorage::MakeRegPair(TargetReg(kFArg2), TargetReg(kFArg3));
237 } else {
238 r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
239 }
240 LoadValueDirectWideFixed(arg1, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 }
242 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000243 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 CallHelper(r_tgt, helper_offset, safepoint_pc);
245}
246
Ian Rogersdd7624d2014-03-14 17:43:00 -0700247void Mir2Lir::CallRuntimeHelperRegReg(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800248 RegStorage arg1, bool safepoint_pc) {
249 RegStorage r_tgt = CallHelperSetup(helper_offset);
250 DCHECK_NE(TargetReg(kArg0).GetReg(), arg1.GetReg()); // check copy into arg0 won't clobber arg1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 OpRegCopy(TargetReg(kArg0), arg0);
252 OpRegCopy(TargetReg(kArg1), arg1);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000253 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 CallHelper(r_tgt, helper_offset, safepoint_pc);
255}
256
Ian Rogersdd7624d2014-03-14 17:43:00 -0700257void Mir2Lir::CallRuntimeHelperRegRegImm(ThreadOffset<4> helper_offset, RegStorage arg0,
buzbee2700f7e2014-03-07 09:46:20 -0800258 RegStorage arg1, int arg2, bool safepoint_pc) {
259 RegStorage r_tgt = CallHelperSetup(helper_offset);
260 DCHECK_NE(TargetReg(kArg0).GetReg(), arg1.GetReg()); // check copy into arg0 won't clobber arg1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 OpRegCopy(TargetReg(kArg0), arg0);
262 OpRegCopy(TargetReg(kArg1), arg1);
263 LoadConstant(TargetReg(kArg2), arg2);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000264 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 CallHelper(r_tgt, helper_offset, safepoint_pc);
266}
267
Ian Rogersdd7624d2014-03-14 17:43:00 -0700268void Mir2Lir::CallRuntimeHelperImmMethodRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 int arg0, RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800270 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 LoadValueDirectFixed(arg2, TargetReg(kArg2));
272 LoadCurrMethodDirect(TargetReg(kArg1));
273 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000274 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 CallHelper(r_tgt, helper_offset, safepoint_pc);
276}
277
Ian Rogersdd7624d2014-03-14 17:43:00 -0700278void Mir2Lir::CallRuntimeHelperImmMethodImm(ThreadOffset<4> helper_offset, int arg0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 int arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800280 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 LoadCurrMethodDirect(TargetReg(kArg1));
282 LoadConstant(TargetReg(kArg2), arg2);
283 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000284 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 CallHelper(r_tgt, helper_offset, safepoint_pc);
286}
287
Ian Rogersdd7624d2014-03-14 17:43:00 -0700288void Mir2Lir::CallRuntimeHelperImmRegLocationRegLocation(ThreadOffset<4> helper_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 int arg0, RegLocation arg1,
290 RegLocation arg2, bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800291 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700292 DCHECK_EQ(arg1.wide, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 LoadValueDirectFixed(arg1, TargetReg(kArg1));
294 if (arg2.wide == 0) {
295 LoadValueDirectFixed(arg2, TargetReg(kArg2));
296 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800297 RegStorage r_tmp = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
298 LoadValueDirectWideFixed(arg2, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 }
300 LoadConstant(TargetReg(kArg0), arg0);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000301 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 CallHelper(r_tgt, helper_offset, safepoint_pc);
303}
304
Ian Rogersdd7624d2014-03-14 17:43:00 -0700305void Mir2Lir::CallRuntimeHelperRegLocationRegLocationRegLocation(ThreadOffset<4> helper_offset,
Ian Rogersa9a82542013-10-04 11:17:26 -0700306 RegLocation arg0, RegLocation arg1,
307 RegLocation arg2,
308 bool safepoint_pc) {
buzbee2700f7e2014-03-07 09:46:20 -0800309 RegStorage r_tgt = CallHelperSetup(helper_offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700310 DCHECK_EQ(arg0.wide, 0U);
311 LoadValueDirectFixed(arg0, TargetReg(kArg0));
312 DCHECK_EQ(arg1.wide, 0U);
313 LoadValueDirectFixed(arg1, TargetReg(kArg1));
314 DCHECK_EQ(arg1.wide, 0U);
315 LoadValueDirectFixed(arg2, TargetReg(kArg2));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000316 ClobberCallerSave();
Ian Rogersa9a82542013-10-04 11:17:26 -0700317 CallHelper(r_tgt, helper_offset, safepoint_pc);
318}
319
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320/*
321 * If there are any ins passed in registers that have not been promoted
322 * to a callee-save register, flush them to the frame. Perform intial
323 * assignment of promoted arguments.
324 *
325 * ArgLocs is an array of location records describing the incoming arguments
326 * with one location record per word of argument.
327 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700328void Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 /*
330 * Dummy up a RegLocation for the incoming Method*
331 * It will attempt to keep kArg0 live (or copy it to home location
332 * if promoted).
333 */
334 RegLocation rl_src = rl_method;
335 rl_src.location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800336 rl_src.reg = TargetReg(kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 rl_src.home = false;
buzbee2700f7e2014-03-07 09:46:20 -0800338 MarkLive(rl_src.reg, rl_src.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 StoreValue(rl_method, rl_src);
340 // If Method* has been promoted, explicitly flush
341 if (rl_method.location == kLocPhysReg) {
342 StoreWordDisp(TargetReg(kSp), 0, TargetReg(kArg0));
343 }
344
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800345 if (cu_->num_ins == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 return;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800347 }
348
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
350 /*
351 * Copy incoming arguments to their proper home locations.
352 * NOTE: an older version of dx had an issue in which
353 * it would reuse static method argument registers.
354 * This could result in the same Dalvik virtual register
355 * being promoted to both core and fp regs. To account for this,
356 * we only copy to the corresponding promoted physical register
357 * if it matches the type of the SSA name for the incoming
358 * argument. It is also possible that long and double arguments
359 * end up half-promoted. In those cases, we must flush the promoted
360 * half to memory as well.
361 */
362 for (int i = 0; i < cu_->num_ins; i++) {
363 PromotionMap* v_map = &promotion_map_[start_vreg + i];
buzbee2700f7e2014-03-07 09:46:20 -0800364 RegStorage reg = GetArgMappingToPhysicalReg(i);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800365
buzbee2700f7e2014-03-07 09:46:20 -0800366 if (reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 // If arriving in register
368 bool need_flush = true;
369 RegLocation* t_loc = &ArgLocs[i];
370 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800371 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 need_flush = false;
373 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800374 OpRegCopy(RegStorage::Solo32(v_map->FpReg), reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 need_flush = false;
376 } else {
377 need_flush = true;
378 }
379
buzbeed0a03b82013-09-14 08:21:05 -0700380 // For wide args, force flush if not fully promoted
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 if (t_loc->wide) {
382 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
buzbeed0a03b82013-09-14 08:21:05 -0700383 // Is only half promoted?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 need_flush |= (p_map->core_location != v_map->core_location) ||
385 (p_map->fp_location != v_map->fp_location);
buzbeed0a03b82013-09-14 08:21:05 -0700386 if ((cu_->instruction_set == kThumb2) && t_loc->fp && !need_flush) {
387 /*
388 * In Arm, a double is represented as a pair of consecutive single float
389 * registers starting at an even number. It's possible that both Dalvik vRegs
390 * representing the incoming double were independently promoted as singles - but
391 * not in a form usable as a double. If so, we need to flush - even though the
392 * incoming arg appears fully in register. At this point in the code, both
393 * halves of the double are promoted. Make sure they are in a usable form.
394 */
395 int lowreg_index = start_vreg + i + (t_loc->high_word ? -1 : 0);
396 int low_reg = promotion_map_[lowreg_index].FpReg;
397 int high_reg = promotion_map_[lowreg_index + 1].FpReg;
398 if (((low_reg & 0x1) != 0) || (high_reg != (low_reg + 1))) {
399 need_flush = true;
400 }
401 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 }
403 if (need_flush) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800404 StoreBaseDisp(TargetReg(kSp), SRegOffset(start_vreg + i), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 }
406 } else {
407 // If arriving in frame & promoted
408 if (v_map->core_location == kLocPhysReg) {
409 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i),
buzbee2700f7e2014-03-07 09:46:20 -0800410 RegStorage::Solo32(v_map->core_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 }
412 if (v_map->fp_location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800413 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->FpReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 }
415 }
416 }
417}
418
419/*
420 * Bit of a hack here - in the absence of a real scheduling pass,
421 * emit the next instruction in static & direct invoke sequences.
422 */
423static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
424 int state, const MethodReference& target_method,
425 uint32_t unused,
426 uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700427 InvokeType type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 if (direct_code != 0 && direct_method != 0) {
430 switch (state) {
431 case 0: // Get the current Method* [sets kArg0]
432 if (direct_code != static_cast<unsigned int>(-1)) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700433 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700434 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
435 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700436 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700437 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 }
439 if (direct_method != static_cast<unsigned int>(-1)) {
440 cg->LoadConstant(cg->TargetReg(kArg0), direct_method);
441 } else {
Jeff Hao49161ce2014-03-12 11:05:25 -0700442 cg->LoadMethodAddress(target_method, type, kArg0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 }
444 break;
445 default:
446 return -1;
447 }
448 } else {
449 switch (state) {
450 case 0: // Get the current Method* [sets kArg0]
451 // TUNING: we can save a reg copy if Method* has been promoted.
452 cg->LoadCurrMethodDirect(cg->TargetReg(kArg0));
453 break;
454 case 1: // Get method->dex_cache_resolved_methods_
455 cg->LoadWordDisp(cg->TargetReg(kArg0),
buzbee2700f7e2014-03-07 09:46:20 -0800456 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
457 cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 // Set up direct code if known.
459 if (direct_code != 0) {
460 if (direct_code != static_cast<unsigned int>(-1)) {
461 cg->LoadConstant(cg->TargetReg(kInvokeTgt), direct_code);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700462 } else if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Ian Rogers83883d72013-10-21 21:07:24 -0700463 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
Jeff Hao49161ce2014-03-12 11:05:25 -0700464 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 }
466 }
467 break;
468 case 2: // Grab target method*
469 CHECK_EQ(cu->dex_file, target_method.dex_file);
470 cg->LoadWordDisp(cg->TargetReg(kArg0),
471 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbee2700f7e2014-03-07 09:46:20 -0800472 (target_method.dex_method_index * 4), cg->TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 break;
474 case 3: // Grab the code from the method*
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700475 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 if (direct_code == 0) {
477 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800478 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 cg->TargetReg(kInvokeTgt));
480 }
481 break;
482 }
483 // Intentional fallthrough for x86
484 default:
485 return -1;
486 }
487 }
488 return state + 1;
489}
490
491/*
492 * Bit of a hack here - in the absence of a real scheduling pass,
493 * emit the next instruction in a virtual invoke sequence.
494 * We can use kLr as a temp prior to target address loading
495 * Note also that we'll load the first argument ("this") into
496 * kArg1 here rather than the standard LoadArgRegs.
497 */
498static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
499 int state, const MethodReference& target_method,
500 uint32_t method_idx, uintptr_t unused, uintptr_t unused2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700501 InvokeType unused3) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
503 /*
504 * This is the fast path in which the target virtual method is
505 * fully resolved at compile time.
506 */
507 switch (state) {
508 case 0: { // Get "this" [set kArg1]
509 RegLocation rl_arg = info->args[0];
510 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
511 break;
512 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700513 case 1: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800514 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 // get this->klass_ [use kArg1, set kInvokeTgt]
516 cg->LoadWordDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
517 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800518 cg->MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700520 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
522 cg->TargetReg(kInvokeTgt));
523 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700524 case 3: // Get target method [use kInvokeTgt, set kArg0]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), (method_idx * 4) +
526 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
527 cg->TargetReg(kArg0));
528 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700529 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700530 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800532 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 cg->TargetReg(kInvokeTgt));
534 break;
535 }
536 // Intentional fallthrough for X86
537 default:
538 return -1;
539 }
540 return state + 1;
541}
542
543/*
Jeff Hao88474b42013-10-23 16:24:40 -0700544 * Emit the next instruction in an invoke interface sequence. This will do a lookup in the
545 * class's IMT, calling either the actual method or art_quick_imt_conflict_trampoline if
546 * more than one interface method map to the same index. Note also that we'll load the first
547 * argument ("this") into kArg1 here rather than the standard LoadArgRegs.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 */
549static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
550 const MethodReference& target_method,
Jeff Hao88474b42013-10-23 16:24:40 -0700551 uint32_t method_idx, uintptr_t unused,
552 uintptr_t direct_method, InvokeType unused2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554
Jeff Hao88474b42013-10-23 16:24:40 -0700555 switch (state) {
556 case 0: // Set target method index in case of conflict [set kHiddenArg, kHiddenFpArg (x86)]
Jeff Hao88474b42013-10-23 16:24:40 -0700557 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
558 cg->LoadConstant(cg->TargetReg(kHiddenArg), target_method.dex_method_index);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700559 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700560 cg->OpRegCopy(cg->TargetReg(kHiddenFpArg), cg->TargetReg(kHiddenArg));
561 }
562 break;
563 case 1: { // Get "this" [set kArg1]
564 RegLocation rl_arg = info->args[0];
565 cg->LoadValueDirectFixed(rl_arg, cg->TargetReg(kArg1));
566 break;
567 }
568 case 2: // Is "this" null? [use kArg1]
Dave Allisonb373e092014-02-20 16:06:36 -0800569 cg->GenNullCheck(cg->TargetReg(kArg1), info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700570 // Get this->klass_ [use kArg1, set kInvokeTgt]
571 cg->LoadWordDisp(cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
572 cg->TargetReg(kInvokeTgt));
Dave Allisonb373e092014-02-20 16:06:36 -0800573 cg->MarkPossibleNullPointerException(info->opt_flags);
Jeff Hao88474b42013-10-23 16:24:40 -0700574 break;
575 case 3: // Get this->klass_->imtable [use kInvokeTgt, set kInvokeTgt]
576 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), mirror::Class::ImTableOffset().Int32Value(),
577 cg->TargetReg(kInvokeTgt));
578 break;
579 case 4: // Get target method [use kInvokeTgt, set kArg0]
580 cg->LoadWordDisp(cg->TargetReg(kInvokeTgt), ((method_idx % ClassLinker::kImtSize) * 4) +
581 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 cg->TargetReg(kArg0));
583 break;
Jeff Hao88474b42013-10-23 16:24:40 -0700584 case 5: // Get the compiled code address [use kArg0, set kInvokeTgt]
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700585 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Jeff Hao88474b42013-10-23 16:24:40 -0700586 cg->LoadWordDisp(cg->TargetReg(kArg0),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800587 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
Jeff Hao88474b42013-10-23 16:24:40 -0700588 cg->TargetReg(kInvokeTgt));
589 break;
590 }
591 // Intentional fallthrough for X86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 default:
593 return -1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 }
595 return state + 1;
596}
597
Ian Rogersdd7624d2014-03-14 17:43:00 -0700598static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, ThreadOffset<4> trampoline,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 int state, const MethodReference& target_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700600 uint32_t method_idx) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
602 /*
603 * This handles the case in which the base method is not fully
604 * resolved at compile time, we bail to a runtime helper.
605 */
606 if (state == 0) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700607 if (cu->instruction_set != kX86 && cu->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 // Load trampoline target
Ian Rogers848871b2013-08-05 10:56:33 -0700609 cg->LoadWordDisp(cg->TargetReg(kSelf), trampoline.Int32Value(), cg->TargetReg(kInvokeTgt));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 }
611 // Load kArg0 with method index
612 CHECK_EQ(cu->dex_file, target_method.dex_file);
613 cg->LoadConstant(cg->TargetReg(kArg0), target_method.dex_method_index);
614 return 1;
615 }
616 return -1;
617}
618
619static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
620 int state,
621 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000622 uint32_t unused, uintptr_t unused2,
623 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700624 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
626}
627
628static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
629 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000630 uint32_t unused, uintptr_t unused2,
631 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700632 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
634}
635
636static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
637 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000638 uint32_t unused, uintptr_t unused2,
639 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700640 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
642}
643
644static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
645 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000646 uint32_t unused, uintptr_t unused2,
647 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700648 ThreadOffset<4> trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
650}
651
652static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
653 CallInfo* info, int state,
654 const MethodReference& target_method,
Vladimir Markof096aad2014-01-23 15:51:58 +0000655 uint32_t unused, uintptr_t unused2,
656 uintptr_t unused3, InvokeType unused4) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700657 ThreadOffset<4> trampoline =
658 QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 return NextInvokeInsnSP(cu, info, trampoline, state, target_method, 0);
660}
661
662int Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
663 NextCallInsn next_call_insn,
664 const MethodReference& target_method,
665 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700666 uintptr_t direct_method, InvokeType type, bool skip_this) {
buzbee2700f7e2014-03-07 09:46:20 -0800667 int last_arg_reg = TargetReg(kArg3).GetReg();
668 int next_reg = TargetReg(kArg1).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 int next_arg = 0;
670 if (skip_this) {
671 next_reg++;
672 next_arg++;
673 }
674 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
675 RegLocation rl_arg = info->args[next_arg++];
676 rl_arg = UpdateRawLoc(rl_arg);
buzbee2700f7e2014-03-07 09:46:20 -0800677 if (rl_arg.wide && (next_reg <= TargetReg(kArg2).GetReg())) {
678 RegStorage r_tmp(RegStorage::k64BitPair, next_reg, next_reg + 1);
679 LoadValueDirectWideFixed(rl_arg, r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 next_reg++;
681 next_arg++;
682 } else {
683 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800684 rl_arg = NarrowRegLoc(rl_arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 rl_arg.is_const = false;
686 }
buzbee2700f7e2014-03-07 09:46:20 -0800687 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(next_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 }
689 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
690 direct_code, direct_method, type);
691 }
692 return call_state;
693}
694
695/*
696 * Load up to 5 arguments, the first three of which will be in
697 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
698 * and as part of the load sequence, it must be replaced with
699 * the target method pointer. Note, this may also be called
700 * for "range" variants if the number of arguments is 5 or fewer.
701 */
702int Mir2Lir::GenDalvikArgsNoRange(CallInfo* info,
703 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
704 const MethodReference& target_method,
705 uint32_t vtable_idx, uintptr_t direct_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700706 uintptr_t direct_method, InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 RegLocation rl_arg;
708
709 /* If no arguments, just return */
710 if (info->num_arg_words == 0)
711 return call_state;
712
713 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
714 direct_code, direct_method, type);
715
716 DCHECK_LE(info->num_arg_words, 5);
717 if (info->num_arg_words > 3) {
718 int32_t next_use = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700719 // Detect special case of wide arg spanning arg3/arg4
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 RegLocation rl_use0 = info->args[0];
721 RegLocation rl_use1 = info->args[1];
722 RegLocation rl_use2 = info->args[2];
buzbee2700f7e2014-03-07 09:46:20 -0800723 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) && rl_use2.wide) {
724 RegStorage reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 // Wide spans, we need the 2nd half of uses[2].
726 rl_arg = UpdateLocWide(rl_use2);
727 if (rl_arg.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800728 reg = rl_arg.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 } else {
730 // kArg2 & rArg3 can safely be used here
731 reg = TargetReg(kArg3);
732 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_arg.s_reg_low) + 4, reg);
733 call_state = next_call_insn(cu_, info, call_state, target_method,
734 vtable_idx, direct_code, direct_method, type);
735 }
736 StoreBaseDisp(TargetReg(kSp), (next_use + 1) * 4, reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
738 direct_code, direct_method, type);
739 next_use++;
740 }
741 // Loop through the rest
742 while (next_use < info->num_arg_words) {
buzbee2700f7e2014-03-07 09:46:20 -0800743 RegStorage low_reg;
744 RegStorage high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 rl_arg = info->args[next_use];
746 rl_arg = UpdateRawLoc(rl_arg);
747 if (rl_arg.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000748 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800749 low_reg = rl_arg.reg.GetLow();
750 high_reg = rl_arg.reg.GetHigh();
751 } else {
752 low_reg = rl_arg.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000753 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 } else {
755 low_reg = TargetReg(kArg2);
756 if (rl_arg.wide) {
757 high_reg = TargetReg(kArg3);
buzbee2700f7e2014-03-07 09:46:20 -0800758 LoadValueDirectWideFixed(rl_arg, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 } else {
760 LoadValueDirectFixed(rl_arg, low_reg);
761 }
762 call_state = next_call_insn(cu_, info, call_state, target_method,
763 vtable_idx, direct_code, direct_method, type);
764 }
765 int outs_offset = (next_use + 1) * 4;
766 if (rl_arg.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800767 StoreBaseDispWide(TargetReg(kSp), outs_offset, RegStorage::MakeRegPair(low_reg, high_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 next_use += 2;
769 } else {
770 StoreWordDisp(TargetReg(kSp), outs_offset, low_reg);
771 next_use++;
772 }
773 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
774 direct_code, direct_method, type);
775 }
776 }
777
778 call_state = LoadArgRegs(info, call_state, next_call_insn,
779 target_method, vtable_idx, direct_code, direct_method,
780 type, skip_this);
781
782 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700783 if (Runtime::Current()->ExplicitNullChecks()) {
784 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
785 } else {
786 *pcrLabel = nullptr;
787 // In lieu of generating a check for kArg1 being null, we need to
788 // perform a load when doing implicit checks.
789 RegStorage tmp = AllocTemp();
790 LoadWordDisp(TargetReg(kArg1), 0, tmp);
791 MarkPossibleNullPointerException(info->opt_flags);
792 FreeTemp(tmp);
793 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 }
795 return call_state;
796}
797
798/*
799 * May have 0+ arguments (also used for jumbo). Note that
800 * source virtual registers may be in physical registers, so may
801 * need to be flushed to home location before copying. This
802 * applies to arg3 and above (see below).
803 *
804 * Two general strategies:
805 * If < 20 arguments
806 * Pass args 3-18 using vldm/vstm block copy
807 * Pass arg0, arg1 & arg2 in kArg1-kArg3
808 * If 20+ arguments
809 * Pass args arg19+ using memcpy block copy
810 * Pass arg0, arg1 & arg2 in kArg1-kArg3
811 *
812 */
813int Mir2Lir::GenDalvikArgsRange(CallInfo* info, int call_state,
814 LIR** pcrLabel, NextCallInsn next_call_insn,
815 const MethodReference& target_method,
816 uint32_t vtable_idx, uintptr_t direct_code, uintptr_t direct_method,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700817 InvokeType type, bool skip_this) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 // If we can treat it as non-range (Jumbo ops will use range form)
819 if (info->num_arg_words <= 5)
820 return GenDalvikArgsNoRange(info, call_state, pcrLabel,
821 next_call_insn, target_method, vtable_idx,
822 direct_code, direct_method, type, skip_this);
823 /*
824 * First load the non-register arguments. Both forms expect all
825 * of the source arguments to be in their home frame location, so
826 * scan the s_reg names and flush any that have been promoted to
827 * frame backing storage.
828 */
829 // Scan the rest of the args - if in phys_reg flush to memory
830 for (int next_arg = 0; next_arg < info->num_arg_words;) {
831 RegLocation loc = info->args[next_arg];
832 if (loc.wide) {
833 loc = UpdateLocWide(loc);
834 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800835 StoreBaseDispWide(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 }
837 next_arg += 2;
838 } else {
839 loc = UpdateLoc(loc);
840 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
buzbee2700f7e2014-03-07 09:46:20 -0800841 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700842 }
843 next_arg++;
844 }
845 }
846
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800847 // Logic below assumes that Method pointer is at offset zero from SP.
848 DCHECK_EQ(VRegOffset(static_cast<int>(kVRegMethodPtrBaseReg)), 0);
849
850 // The first 3 arguments are passed via registers.
851 // TODO: For 64-bit, instead of hardcoding 4 for Method* size, we should either
852 // get size of uintptr_t or size of object reference according to model being used.
853 int outs_offset = 4 /* Method* */ + (3 * sizeof(uint32_t));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 int start_offset = SRegOffset(info->args[3].s_reg_low);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800855 int regs_left_to_pass_via_stack = info->num_arg_words - 3;
856 DCHECK_GT(regs_left_to_pass_via_stack, 0);
857
858 if (cu_->instruction_set == kThumb2 && regs_left_to_pass_via_stack <= 16) {
859 // Use vldm/vstm pair using kArg3 as a temp
860 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
861 direct_code, direct_method, type);
862 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
863 LIR* ld = OpVldm(TargetReg(kArg3), regs_left_to_pass_via_stack);
864 // TUNING: loosen barrier
865 ld->u.m.def_mask = ENCODE_ALL;
866 SetMemRefType(ld, true /* is_load */, kDalvikReg);
867 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
868 direct_code, direct_method, type);
869 OpRegRegImm(kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
870 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
871 direct_code, direct_method, type);
872 LIR* st = OpVstm(TargetReg(kArg3), regs_left_to_pass_via_stack);
873 SetMemRefType(st, false /* is_load */, kDalvikReg);
874 st->u.m.def_mask = ENCODE_ALL;
875 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
876 direct_code, direct_method, type);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700877 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800878 int current_src_offset = start_offset;
879 int current_dest_offset = outs_offset;
880
881 while (regs_left_to_pass_via_stack > 0) {
882 // This is based on the knowledge that the stack itself is 16-byte aligned.
883 bool src_is_16b_aligned = (current_src_offset & 0xF) == 0;
884 bool dest_is_16b_aligned = (current_dest_offset & 0xF) == 0;
885 size_t bytes_to_move;
886
887 /*
888 * The amount to move defaults to 32-bit. If there are 4 registers left to move, then do a
889 * a 128-bit move because we won't get the chance to try to aligned. If there are more than
890 * 4 registers left to move, consider doing a 128-bit only if either src or dest are aligned.
891 * We do this because we could potentially do a smaller move to align.
892 */
893 if (regs_left_to_pass_via_stack == 4 ||
894 (regs_left_to_pass_via_stack > 4 && (src_is_16b_aligned || dest_is_16b_aligned))) {
895 // Moving 128-bits via xmm register.
896 bytes_to_move = sizeof(uint32_t) * 4;
897
898 // Allocate a free xmm temp. Since we are working through the calling sequence,
899 // we expect to have an xmm temporary available.
buzbee2700f7e2014-03-07 09:46:20 -0800900 RegStorage temp = AllocTempDouble();
901 CHECK_GT(temp.GetLowReg(), 0);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800902
903 LIR* ld1 = nullptr;
904 LIR* ld2 = nullptr;
905 LIR* st1 = nullptr;
906 LIR* st2 = nullptr;
907
908 /*
909 * The logic is similar for both loads and stores. If we have 16-byte alignment,
910 * do an aligned move. If we have 8-byte alignment, then do the move in two
911 * parts. This approach prevents possible cache line splits. Finally, fall back
912 * to doing an unaligned move. In most cases we likely won't split the cache
913 * line but we cannot prove it and thus take a conservative approach.
914 */
915 bool src_is_8b_aligned = (current_src_offset & 0x7) == 0;
916 bool dest_is_8b_aligned = (current_dest_offset & 0x7) == 0;
917
918 if (src_is_16b_aligned) {
919 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovA128FP);
920 } else if (src_is_8b_aligned) {
921 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800922 ld2 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset + (bytes_to_move >> 1),
923 kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800924 } else {
925 ld1 = OpMovRegMem(temp, TargetReg(kSp), current_src_offset, kMovU128FP);
926 }
927
928 if (dest_is_16b_aligned) {
929 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovA128FP);
930 } else if (dest_is_8b_aligned) {
931 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovLo128FP);
buzbee2700f7e2014-03-07 09:46:20 -0800932 st2 = OpMovMemReg(TargetReg(kSp), current_dest_offset + (bytes_to_move >> 1),
933 temp, kMovHi128FP);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800934 } else {
935 st1 = OpMovMemReg(TargetReg(kSp), current_dest_offset, temp, kMovU128FP);
936 }
937
938 // TODO If we could keep track of aliasing information for memory accesses that are wider
939 // than 64-bit, we wouldn't need to set up a barrier.
940 if (ld1 != nullptr) {
941 if (ld2 != nullptr) {
942 // For 64-bit load we can actually set up the aliasing information.
943 AnnotateDalvikRegAccess(ld1, current_src_offset >> 2, true, true);
944 AnnotateDalvikRegAccess(ld2, (current_src_offset + (bytes_to_move >> 1)) >> 2, true, true);
945 } else {
946 // Set barrier for 128-bit load.
947 SetMemRefType(ld1, true /* is_load */, kDalvikReg);
948 ld1->u.m.def_mask = ENCODE_ALL;
949 }
950 }
951 if (st1 != nullptr) {
952 if (st2 != nullptr) {
953 // For 64-bit store we can actually set up the aliasing information.
954 AnnotateDalvikRegAccess(st1, current_dest_offset >> 2, false, true);
955 AnnotateDalvikRegAccess(st2, (current_dest_offset + (bytes_to_move >> 1)) >> 2, false, true);
956 } else {
957 // Set barrier for 128-bit store.
958 SetMemRefType(st1, false /* is_load */, kDalvikReg);
959 st1->u.m.def_mask = ENCODE_ALL;
960 }
961 }
962
963 // Free the temporary used for the data movement.
buzbee2700f7e2014-03-07 09:46:20 -0800964 // CLEANUP: temp is currently a bogus pair, elmiminate extra free when updated.
965 FreeTemp(temp.GetLow());
966 FreeTemp(temp.GetHigh());
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800967 } else {
968 // Moving 32-bits via general purpose register.
969 bytes_to_move = sizeof(uint32_t);
970
971 // Instead of allocating a new temp, simply reuse one of the registers being used
972 // for argument passing.
buzbee2700f7e2014-03-07 09:46:20 -0800973 RegStorage temp = TargetReg(kArg3);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800974
975 // Now load the argument VR and store to the outs.
976 LoadWordDisp(TargetReg(kSp), current_src_offset, temp);
977 StoreWordDisp(TargetReg(kSp), current_dest_offset, temp);
978 }
979
980 current_src_offset += bytes_to_move;
981 current_dest_offset += bytes_to_move;
982 regs_left_to_pass_via_stack -= (bytes_to_move >> 2);
983 }
984 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985 // Generate memcpy
986 OpRegRegImm(kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
987 OpRegRegImm(kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700988 CallRuntimeHelperRegRegImm(QUICK_ENTRYPOINT_OFFSET(4, pMemcpy), TargetReg(kArg0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 }
991
992 call_state = LoadArgRegs(info, call_state, next_call_insn,
993 target_method, vtable_idx, direct_code, direct_method,
994 type, skip_this);
995
996 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
997 direct_code, direct_method, type);
998 if (pcrLabel) {
Dave Allisonf9439142014-03-27 15:10:22 -0700999 if (Runtime::Current()->ExplicitNullChecks()) {
1000 *pcrLabel = GenExplicitNullCheck(TargetReg(kArg1), info->opt_flags);
1001 } else {
1002 *pcrLabel = nullptr;
1003 // In lieu of generating a check for kArg1 being null, we need to
1004 // perform a load when doing implicit checks.
1005 RegStorage tmp = AllocTemp();
1006 LoadWordDisp(TargetReg(kArg1), 0, tmp);
1007 MarkPossibleNullPointerException(info->opt_flags);
1008 FreeTemp(tmp);
1009 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 }
1011 return call_state;
1012}
1013
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001014RegLocation Mir2Lir::InlineTarget(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 RegLocation res;
1016 if (info->result.location == kLocInvalid) {
1017 res = GetReturn(false);
1018 } else {
1019 res = info->result;
1020 }
1021 return res;
1022}
1023
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001024RegLocation Mir2Lir::InlineTargetWide(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 RegLocation res;
1026 if (info->result.location == kLocInvalid) {
1027 res = GetReturnWide(false);
1028 } else {
1029 res = info->result;
1030 }
1031 return res;
1032}
1033
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001034bool Mir2Lir::GenInlinedCharAt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 if (cu_->instruction_set == kMips) {
1036 // TODO - add Mips implementation
1037 return false;
1038 }
1039 // Location of reference to data array
1040 int value_offset = mirror::String::ValueOffset().Int32Value();
1041 // Location of count
1042 int count_offset = mirror::String::CountOffset().Int32Value();
1043 // Starting offset within data array
1044 int offset_offset = mirror::String::OffsetOffset().Int32Value();
1045 // Start of char data with array_
1046 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
1047
1048 RegLocation rl_obj = info->args[0];
1049 RegLocation rl_idx = info->args[1];
1050 rl_obj = LoadValue(rl_obj, kCoreReg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001051 // X86 wants to avoid putting a constant index into a register.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001052 if (!((cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64)&& rl_idx.is_const)) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001053 rl_idx = LoadValue(rl_idx, kCoreReg);
1054 }
buzbee2700f7e2014-03-07 09:46:20 -08001055 RegStorage reg_max;
1056 GenNullCheck(rl_obj.reg, info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
Vladimir Marko3bc86152014-03-13 14:11:28 +00001058 LIR* range_check_branch = nullptr;
buzbee2700f7e2014-03-07 09:46:20 -08001059 RegStorage reg_off;
1060 RegStorage reg_ptr;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001061 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 reg_off = AllocTemp();
1063 reg_ptr = AllocTemp();
1064 if (range_check) {
1065 reg_max = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001066 LoadWordDisp(rl_obj.reg, count_offset, reg_max);
Dave Allisonb373e092014-02-20 16:06:36 -08001067 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 }
buzbee2700f7e2014-03-07 09:46:20 -08001069 LoadWordDisp(rl_obj.reg, offset_offset, reg_off);
Dave Allisonb373e092014-02-20 16:06:36 -08001070 MarkPossibleNullPointerException(info->opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -08001071 LoadWordDisp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 if (range_check) {
1073 // Set up a launch pad to allow retry in case of bounds violation */
buzbee2700f7e2014-03-07 09:46:20 -08001074 OpRegReg(kOpCmp, rl_idx.reg, reg_max);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 FreeTemp(reg_max);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001076 range_check_branch = OpCondBranch(kCondUge, nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -07001077 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001078 OpRegImm(kOpAdd, reg_ptr, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 } else {
1080 if (range_check) {
Mark Mendell2b724cb2014-02-06 05:24:20 -08001081 // On x86, we can compare to memory directly
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 // Set up a launch pad to allow retry in case of bounds violation */
Mark Mendell2b724cb2014-02-06 05:24:20 -08001083 if (rl_idx.is_const) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001084 range_check_branch = OpCmpMemImmBranch(
buzbee2700f7e2014-03-07 09:46:20 -08001085 kCondUlt, RegStorage::InvalidReg(), rl_obj.reg, count_offset,
Vladimir Marko3bc86152014-03-13 14:11:28 +00001086 mir_graph_->ConstantValue(rl_idx.orig_sreg), nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001087 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001088 OpRegMem(kOpCmp, rl_idx.reg, rl_obj.reg, count_offset);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001089 range_check_branch = OpCondBranch(kCondUge, nullptr);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001090 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 }
1092 reg_off = AllocTemp();
1093 reg_ptr = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001094 LoadWordDisp(rl_obj.reg, offset_offset, reg_off);
1095 LoadWordDisp(rl_obj.reg, value_offset, reg_ptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 }
Mark Mendell2b724cb2014-02-06 05:24:20 -08001097 if (rl_idx.is_const) {
1098 OpRegImm(kOpAdd, reg_off, mir_graph_->ConstantValue(rl_idx.orig_sreg));
1099 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001100 OpRegReg(kOpAdd, reg_off, rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001101 }
buzbee2700f7e2014-03-07 09:46:20 -08001102 FreeTemp(rl_obj.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001103 if (rl_idx.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001104 FreeTemp(rl_idx.reg);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001105 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 RegLocation rl_dest = InlineTarget(info);
1107 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001108 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
buzbee2700f7e2014-03-07 09:46:20 -08001109 LoadBaseIndexed(reg_ptr, reg_off, rl_result.reg, 1, kUnsignedHalf);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001110 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001111 LoadBaseIndexedDisp(reg_ptr, reg_off, 1, data_offset, rl_result.reg,
1112 RegStorage::InvalidReg(), kUnsignedHalf, INVALID_SREG);
Mark Mendell2b724cb2014-02-06 05:24:20 -08001113 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 FreeTemp(reg_off);
1115 FreeTemp(reg_ptr);
1116 StoreValue(rl_dest, rl_result);
1117 if (range_check) {
Vladimir Marko3bc86152014-03-13 14:11:28 +00001118 DCHECK(range_check_branch != nullptr);
1119 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've already null checked.
1120 AddIntrinsicLaunchpad(info, range_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 return true;
1123}
1124
1125// Generates an inlined String.is_empty or String.length.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001126bool Mir2Lir::GenInlinedStringIsEmptyOrLength(CallInfo* info, bool is_empty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 if (cu_->instruction_set == kMips) {
1128 // TODO - add Mips implementation
1129 return false;
1130 }
1131 // dst = src.length();
1132 RegLocation rl_obj = info->args[0];
1133 rl_obj = LoadValue(rl_obj, kCoreReg);
1134 RegLocation rl_dest = InlineTarget(info);
1135 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001136 GenNullCheck(rl_obj.reg, info->opt_flags);
1137 LoadWordDisp(rl_obj.reg, mirror::String::CountOffset().Int32Value(), rl_result.reg);
Dave Allisonb373e092014-02-20 16:06:36 -08001138 MarkPossibleNullPointerException(info->opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 if (is_empty) {
1140 // dst = (dst == 0);
1141 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001142 RegStorage t_reg = AllocTemp();
1143 OpRegReg(kOpNeg, t_reg, rl_result.reg);
1144 OpRegRegReg(kOpAdc, rl_result.reg, rl_result.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001146 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbee2700f7e2014-03-07 09:46:20 -08001147 OpRegImm(kOpSub, rl_result.reg, 1);
1148 OpRegImm(kOpLsr, rl_result.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 }
1150 }
1151 StoreValue(rl_dest, rl_result);
1152 return true;
1153}
1154
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001155bool Mir2Lir::GenInlinedReverseBytes(CallInfo* info, OpSize size) {
1156 if (cu_->instruction_set == kMips) {
1157 // TODO - add Mips implementation
1158 return false;
1159 }
1160 RegLocation rl_src_i = info->args[0];
Mark Mendell55d0eac2014-02-06 11:02:52 -08001161 RegLocation rl_dest = (size == kLong) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001162 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1163 if (size == kLong) {
1164 RegLocation rl_i = LoadValueWide(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001165 RegStorage r_i_low = rl_i.reg.GetLow();
1166 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001167 // 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 +00001168 r_i_low = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -08001169 OpRegCopy(r_i_low, rl_i.reg);
Vladimir Markof246af22013-11-27 12:30:15 +00001170 }
buzbee2700f7e2014-03-07 09:46:20 -08001171 OpRegReg(kOpRev, rl_result.reg.GetLow(), rl_i.reg.GetHigh());
1172 OpRegReg(kOpRev, rl_result.reg.GetHigh(), r_i_low);
1173 if (rl_i.reg.GetLowReg() == rl_result.reg.GetLowReg()) {
Vladimir Markof246af22013-11-27 12:30:15 +00001174 FreeTemp(r_i_low);
1175 }
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001176 StoreValueWide(rl_dest, rl_result);
1177 } else {
1178 DCHECK(size == kWord || size == kSignedHalf);
1179 OpKind op = (size == kWord) ? kOpRev : kOpRevsh;
1180 RegLocation rl_i = LoadValue(rl_src_i, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001181 OpRegReg(op, rl_result.reg, rl_i.reg);
Vladimir Marko6bdf1ff2013-10-29 17:40:46 +00001182 StoreValue(rl_dest, rl_result);
1183 }
1184 return true;
1185}
1186
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001187bool Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188 if (cu_->instruction_set == kMips) {
1189 // TODO - add Mips implementation
1190 return false;
1191 }
1192 RegLocation rl_src = info->args[0];
1193 rl_src = LoadValue(rl_src, kCoreReg);
1194 RegLocation rl_dest = InlineTarget(info);
1195 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001196 RegStorage sign_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001198 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 31);
1199 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
1200 OpRegReg(kOpXor, rl_result.reg, sign_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 StoreValue(rl_dest, rl_result);
1202 return true;
1203}
1204
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001205bool Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 if (cu_->instruction_set == kMips) {
1207 // TODO - add Mips implementation
1208 return false;
1209 }
Vladimir Markob9823312014-03-20 17:38:43 +00001210 RegLocation rl_src = info->args[0];
1211 rl_src = LoadValueWide(rl_src, kCoreReg);
1212 RegLocation rl_dest = InlineTargetWide(info);
1213 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1214
1215 // If on x86 or if we would clobber a register needed later, just copy the source first.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001216 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 || rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
buzbee2700f7e2014-03-07 09:46:20 -08001217 OpRegCopyWide(rl_result.reg, rl_src.reg);
1218 if (rl_result.reg.GetLowReg() != rl_src.reg.GetLowReg() &&
1219 rl_result.reg.GetLowReg() != rl_src.reg.GetHighReg() &&
1220 rl_result.reg.GetHighReg() != rl_src.reg.GetLowReg() &&
Vladimir Markob9823312014-03-20 17:38:43 +00001221 rl_result.reg.GetHighReg() != rl_src.reg.GetHighReg()) {
1222 // Reuse source registers to avoid running out of temps.
buzbee2700f7e2014-03-07 09:46:20 -08001223 FreeTemp(rl_src.reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001224 }
1225 rl_src = rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 }
Vladimir Markob9823312014-03-20 17:38:43 +00001227
1228 // abs(x) = y<=x>>31, (x+y)^y.
buzbee2700f7e2014-03-07 09:46:20 -08001229 RegStorage sign_reg = AllocTemp();
1230 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg.GetHigh(), 31);
1231 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), sign_reg);
1232 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), sign_reg);
1233 OpRegReg(kOpXor, rl_result.reg.GetLow(), sign_reg);
1234 OpRegReg(kOpXor, rl_result.reg.GetHigh(), sign_reg);
Vladimir Markob9823312014-03-20 17:38:43 +00001235 StoreValueWide(rl_dest, rl_result);
1236 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237}
1238
Yixin Shoudbb17e32014-02-07 05:09:30 -08001239bool Mir2Lir::GenInlinedAbsFloat(CallInfo* info) {
1240 if (cu_->instruction_set == kMips) {
1241 // TODO - add Mips implementation
1242 return false;
1243 }
1244 RegLocation rl_src = info->args[0];
1245 rl_src = LoadValue(rl_src, kCoreReg);
1246 RegLocation rl_dest = InlineTarget(info);
1247 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001248 OpRegRegImm(kOpAnd, rl_result.reg, rl_src.reg, 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001249 StoreValue(rl_dest, rl_result);
1250 return true;
1251}
1252
1253bool Mir2Lir::GenInlinedAbsDouble(CallInfo* info) {
1254 if (cu_->instruction_set == kMips) {
1255 // TODO - add Mips implementation
1256 return false;
1257 }
1258 RegLocation rl_src = info->args[0];
1259 rl_src = LoadValueWide(rl_src, kCoreReg);
1260 RegLocation rl_dest = InlineTargetWide(info);
1261 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001262 OpRegCopyWide(rl_result.reg, rl_src.reg);
1263 OpRegImm(kOpAnd, rl_result.reg.GetHigh(), 0x7fffffff);
Yixin Shoudbb17e32014-02-07 05:09:30 -08001264 StoreValueWide(rl_dest, rl_result);
1265 return true;
1266}
1267
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001268bool Mir2Lir::GenInlinedFloatCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 if (cu_->instruction_set == kMips) {
1270 // TODO - add Mips implementation
1271 return false;
1272 }
1273 RegLocation rl_src = info->args[0];
1274 RegLocation rl_dest = InlineTarget(info);
1275 StoreValue(rl_dest, rl_src);
1276 return true;
1277}
1278
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001279bool Mir2Lir::GenInlinedDoubleCvt(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 if (cu_->instruction_set == kMips) {
1281 // TODO - add Mips implementation
1282 return false;
1283 }
1284 RegLocation rl_src = info->args[0];
1285 RegLocation rl_dest = InlineTargetWide(info);
1286 StoreValueWide(rl_dest, rl_src);
1287 return true;
1288}
1289
1290/*
Vladimir Marko3bc86152014-03-13 14:11:28 +00001291 * Fast String.indexOf(I) & (II). Tests for simple case of char <= 0xFFFF,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 * otherwise bails to standard library code.
1293 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001294bool Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 if (cu_->instruction_set == kMips) {
1296 // TODO - add Mips implementation
1297 return false;
1298 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001299 RegLocation rl_obj = info->args[0];
1300 RegLocation rl_char = info->args[1];
1301 if (rl_char.is_const && (mir_graph_->ConstantValue(rl_char) & ~0xFFFF) != 0) {
1302 // Code point beyond 0xFFFF. Punt to the real String.indexOf().
1303 return false;
1304 }
1305
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001306 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001308 RegStorage reg_ptr = TargetReg(kArg0);
1309 RegStorage reg_char = TargetReg(kArg1);
1310 RegStorage reg_start = TargetReg(kArg2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 LoadValueDirectFixed(rl_obj, reg_ptr);
1313 LoadValueDirectFixed(rl_char, reg_char);
1314 if (zero_based) {
1315 LoadConstant(reg_start, 0);
1316 } else {
buzbeea44d4f52014-03-05 11:26:39 -08001317 RegLocation rl_start = info->args[2]; // 3rd arg only present in III flavor of IndexOf.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 LoadValueDirectFixed(rl_start, reg_start);
1319 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001320 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pIndexOf));
Dave Allisonf9439142014-03-27 15:10:22 -07001321 GenExplicitNullCheck(reg_ptr, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001322 LIR* high_code_point_branch =
1323 rl_char.is_const ? nullptr : OpCmpImmBranch(kCondGt, reg_char, 0xFFFF, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 // NOTE: not a safepoint
Mark Mendell4028a6c2014-02-19 20:06:20 -08001325 OpReg(kOpBlx, r_tgt);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001326 if (!rl_char.is_const) {
1327 // Add the slow path for code points beyond 0xFFFF.
1328 DCHECK(high_code_point_branch != nullptr);
1329 LIR* resume_tgt = NewLIR0(kPseudoTargetLabel);
1330 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
1331 AddIntrinsicLaunchpad(info, high_code_point_branch, resume_tgt);
1332 } else {
1333 DCHECK_EQ(mir_graph_->ConstantValue(rl_char) & ~0xFFFF, 0);
1334 DCHECK(high_code_point_branch == nullptr);
1335 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 RegLocation rl_return = GetReturn(false);
1337 RegLocation rl_dest = InlineTarget(info);
1338 StoreValue(rl_dest, rl_return);
1339 return true;
1340}
1341
1342/* Fast string.compareTo(Ljava/lang/string;)I. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001343bool Mir2Lir::GenInlinedStringCompareTo(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 if (cu_->instruction_set == kMips) {
1345 // TODO - add Mips implementation
1346 return false;
1347 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001348 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 LockCallTemps(); // Using fixed registers
buzbee2700f7e2014-03-07 09:46:20 -08001350 RegStorage reg_this = TargetReg(kArg0);
1351 RegStorage reg_cmp = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352
1353 RegLocation rl_this = info->args[0];
1354 RegLocation rl_cmp = info->args[1];
1355 LoadValueDirectFixed(rl_this, reg_this);
1356 LoadValueDirectFixed(rl_cmp, reg_cmp);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001357 RegStorage r_tgt = (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) ?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001358 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo)) : RegStorage::InvalidReg();
Dave Allisonf9439142014-03-27 15:10:22 -07001359 GenExplicitNullCheck(reg_this, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001360 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001361 // TUNING: check if rl_cmp.s_reg_low is already null checked
Vladimir Marko3bc86152014-03-13 14:11:28 +00001362 LIR* cmp_null_check_branch = OpCmpImmBranch(kCondEq, reg_cmp, 0, nullptr);
1363 AddIntrinsicLaunchpad(info, cmp_null_check_branch);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 // NOTE: not a safepoint
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001365 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 OpReg(kOpBlx, r_tgt);
1367 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001368 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(4, pStringCompareTo));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001369 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 RegLocation rl_return = GetReturn(false);
1371 RegLocation rl_dest = InlineTarget(info);
1372 StoreValue(rl_dest, rl_return);
1373 return true;
1374}
1375
1376bool Mir2Lir::GenInlinedCurrentThread(CallInfo* info) {
1377 RegLocation rl_dest = InlineTarget(info);
1378 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001379 ThreadOffset<4> offset = Thread::PeerOffset<4>();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001380 if (cu_->instruction_set == kThumb2 || cu_->instruction_set == kMips) {
buzbee2700f7e2014-03-07 09:46:20 -08001381 LoadWordDisp(TargetReg(kSelf), offset.Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001383 CHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001384 reinterpret_cast<X86Mir2Lir*>(this)->OpRegThreadMem(kOpMov, rl_result.reg.GetReg(), offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 }
1386 StoreValue(rl_dest, rl_result);
1387 return true;
1388}
1389
1390bool Mir2Lir::GenInlinedUnsafeGet(CallInfo* info,
1391 bool is_long, bool is_volatile) {
1392 if (cu_->instruction_set == kMips) {
1393 // TODO - add Mips implementation
1394 return false;
1395 }
1396 // Unused - RegLocation rl_src_unsafe = info->args[0];
1397 RegLocation rl_src_obj = info->args[1]; // Object
1398 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001399 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Mark Mendell55d0eac2014-02-06 11:02:52 -08001400 RegLocation rl_dest = is_long ? InlineTargetWide(info) : InlineTarget(info); // result reg
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001401
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1403 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1404 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1405 if (is_long) {
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001406 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001407 LoadBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_result.reg.GetLow(),
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001408 rl_result.reg.GetHigh(), kLong, INVALID_SREG);
1409 } else {
1410 RegStorage rl_temp_offset = AllocTemp();
1411 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1412 LoadBaseDispWide(rl_temp_offset, 0, rl_result.reg, INVALID_SREG);
1413 FreeTemp(rl_temp_offset.GetReg());
1414 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001415 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001416 LoadBaseIndexed(rl_object.reg, rl_offset.reg, rl_result.reg, 0, kWord);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001417 }
1418
1419 if (is_volatile) {
1420 // Without context sensitive analysis, we must issue the most conservative barriers.
1421 // In this case, either a load or store may follow so we issue both barriers.
1422 GenMemBarrier(kLoadLoad);
1423 GenMemBarrier(kLoadStore);
1424 }
1425
1426 if (is_long) {
1427 StoreValueWide(rl_dest, rl_result);
1428 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 StoreValue(rl_dest, rl_result);
1430 }
1431 return true;
1432}
1433
1434bool Mir2Lir::GenInlinedUnsafePut(CallInfo* info, bool is_long,
1435 bool is_object, bool is_volatile, bool is_ordered) {
1436 if (cu_->instruction_set == kMips) {
1437 // TODO - add Mips implementation
1438 return false;
1439 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 // Unused - RegLocation rl_src_unsafe = info->args[0];
1441 RegLocation rl_src_obj = info->args[1]; // Object
1442 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -08001443 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 RegLocation rl_src_value = info->args[4]; // value to store
1445 if (is_volatile || is_ordered) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001446 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 GenMemBarrier(kStoreStore);
1448 }
1449 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
1450 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
1451 RegLocation rl_value;
1452 if (is_long) {
1453 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001454 if (cu_->instruction_set == kX86) {
Vladimir Marko99f391e2014-04-03 12:56:06 +01001455 StoreBaseIndexedDisp(rl_object.reg, rl_offset.reg, 0, 0, rl_value.reg.GetLow(),
Mathieu Chartier7c95cef2014-04-02 17:09:17 -07001456 rl_value.reg.GetHigh(), kLong, INVALID_SREG);
1457 } else {
1458 RegStorage rl_temp_offset = AllocTemp();
1459 OpRegRegReg(kOpAdd, rl_temp_offset, rl_object.reg, rl_offset.reg);
1460 StoreBaseDispWide(rl_temp_offset, 0, rl_value.reg);
1461 FreeTemp(rl_temp_offset.GetReg());
1462 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 } else {
1464 rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001465 StoreBaseIndexed(rl_object.reg, rl_offset.reg, rl_value.reg, 0, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 }
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001467
1468 // Free up the temp early, to ensure x86 doesn't run out of temporaries in MarkGCCard.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001469 FreeTemp(rl_offset.reg.GetReg());
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001470
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 if (is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001472 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 GenMemBarrier(kStoreLoad);
1474 }
1475 if (is_object) {
buzbee2700f7e2014-03-07 09:46:20 -08001476 MarkGCCard(rl_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 }
1478 return true;
1479}
1480
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001481void Mir2Lir::GenInvoke(CallInfo* info) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001482 if ((info->opt_flags & MIR_INLINED) != 0) {
1483 // Already inlined but we may still need the null check.
1484 if (info->type != kStatic &&
1485 ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 ||
1486 (info->opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) {
1487 RegLocation rl_obj = LoadValue(info->args[0], kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001488 GenImmedCheck(kCondEq, rl_obj.reg, 0, kThrowNullPointer);
Vladimir Marko9820b7c2014-01-02 16:40:37 +00001489 }
1490 return;
1491 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001492 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
1493 if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
1494 ->GenIntrinsic(this, info)) {
1495 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 }
Vladimir Marko3bc86152014-03-13 14:11:28 +00001497 GenInvokeNoInline(info);
1498}
1499
1500void Mir2Lir::GenInvokeNoInline(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 int call_state = 0;
1502 LIR* null_ck;
1503 LIR** p_null_ck = NULL;
1504 NextCallInsn next_call_insn;
1505 FlushAllRegs(); /* Everything to home location */
1506 // Explicit register usage
1507 LockCallTemps();
1508
Vladimir Markof096aad2014-01-23 15:51:58 +00001509 const MirMethodLoweringInfo& method_info = mir_graph_->GetMethodLoweringInfo(info->mir);
1510 cu_->compiler_driver->ProcessedInvoke(method_info.GetInvokeType(), method_info.StatsFlags());
1511 InvokeType original_type = static_cast<InvokeType>(method_info.GetInvokeType());
1512 info->type = static_cast<InvokeType>(method_info.GetSharpType());
1513 bool fast_path = method_info.FastPath();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 bool skip_this;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 if (info->type == kInterface) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001516 next_call_insn = fast_path ? NextInterfaceCallInsn : NextInterfaceCallInsnWithAccessCheck;
Jeff Hao88474b42013-10-23 16:24:40 -07001517 skip_this = fast_path;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 } else if (info->type == kDirect) {
1519 if (fast_path) {
1520 p_null_ck = &null_ck;
1521 }
1522 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1523 skip_this = false;
1524 } else if (info->type == kStatic) {
1525 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1526 skip_this = false;
1527 } else if (info->type == kSuper) {
1528 DCHECK(!fast_path); // Fast path is a direct call.
1529 next_call_insn = NextSuperCallInsnSP;
1530 skip_this = false;
1531 } else {
1532 DCHECK_EQ(info->type, kVirtual);
1533 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1534 skip_this = fast_path;
1535 }
Vladimir Markof096aad2014-01-23 15:51:58 +00001536 MethodReference target_method = method_info.GetTargetMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 if (!info->is_range) {
1538 call_state = GenDalvikArgsNoRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001539 next_call_insn, target_method, method_info.VTableIndex(),
1540 method_info.DirectCode(), method_info.DirectMethod(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 original_type, skip_this);
1542 } else {
1543 call_state = GenDalvikArgsRange(info, call_state, p_null_ck,
Vladimir Markof096aad2014-01-23 15:51:58 +00001544 next_call_insn, target_method, method_info.VTableIndex(),
1545 method_info.DirectCode(), method_info.DirectMethod(),
1546 original_type, skip_this);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001547 }
1548 // Finish up any of the call sequence not interleaved in arg loading
1549 while (call_state >= 0) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001550 call_state = next_call_insn(cu_, info, call_state, target_method, method_info.VTableIndex(),
1551 method_info.DirectCode(), method_info.DirectMethod(), original_type);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001552 }
1553 LIR* call_inst;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001554 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 call_inst = OpReg(kOpBlx, TargetReg(kInvokeTgt));
1556 } else {
Jeff Hao88474b42013-10-23 16:24:40 -07001557 if (fast_path) {
Vladimir Markof096aad2014-01-23 15:51:58 +00001558 if (method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001559 // We can have the linker fixup a call relative.
1560 call_inst =
Jeff Hao49161ce2014-03-12 11:05:25 -07001561 reinterpret_cast<X86Mir2Lir*>(this)->CallWithLinkerFixup(target_method, info->type);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001562 } else {
1563 call_inst = OpMem(kOpBlx, TargetReg(kArg0),
1564 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value());
1565 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001567 ThreadOffset<4> trampoline(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 switch (info->type) {
1569 case kInterface:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001570 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeInterfaceTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 break;
1572 case kDirect:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001573 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeDirectTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 break;
1575 case kStatic:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001576 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeStaticTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 break;
1578 case kSuper:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001579 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeSuperTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 break;
1581 case kVirtual:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001582 trampoline = QUICK_ENTRYPOINT_OFFSET(4, pInvokeVirtualTrampolineWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001583 break;
1584 default:
1585 LOG(FATAL) << "Unexpected invoke type";
1586 }
1587 call_inst = OpThreadMem(kOpBlx, trampoline);
1588 }
1589 }
1590 MarkSafepointPC(call_inst);
1591
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001592 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 if (info->result.location != kLocInvalid) {
1594 // We have a following MOVE_RESULT - do it now.
1595 if (info->result.wide) {
1596 RegLocation ret_loc = GetReturnWide(info->result.fp);
1597 StoreValueWide(info->result, ret_loc);
1598 } else {
1599 RegLocation ret_loc = GetReturn(info->result.fp);
1600 StoreValue(info->result, ret_loc);
1601 }
1602 }
1603}
1604
1605} // namespace art