blob: bd662b71c74a3b2562310819f76c065f8762fbb1 [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 */
Brian Carlstrom7940e442013-07-12 13:46:57 -070016#include "dex/compiler_ir.h"
17#include "dex/compiler_internals.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070018#include "dex/quick/arm/arm_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070020#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "mirror/array.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080022#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080024#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025
26namespace art {
27
28/*
29 * This source files contains "gen" codegen routines that should
30 * be applicable to most targets. Only mid-level support utilities
31 * and "op" calls may be used here.
32 */
33
34/*
buzbeeb48819d2013-09-14 16:15:25 -070035 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 * blocks.
37 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070038void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 LIR* barrier = NewLIR0(kPseudoBarrier);
40 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070041 DCHECK(!barrier->flags.use_def_invalid);
42 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070043}
44
Mingyao Yange643a172014-04-08 11:02:52 -070045void Mir2Lir::GenDivZeroException() {
46 LIR* branch = OpUnconditionalBranch(nullptr);
47 AddDivZeroCheckSlowPath(branch);
48}
49
50void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070051 LIR* branch = OpCondBranch(c_code, nullptr);
52 AddDivZeroCheckSlowPath(branch);
53}
54
Mingyao Yange643a172014-04-08 11:02:52 -070055void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
56 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070057 AddDivZeroCheckSlowPath(branch);
58}
59
60void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
61 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
62 public:
63 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
64 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
65 }
66
Mingyao Yange643a172014-04-08 11:02:52 -070067 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070068 m2l_->ResetRegPool();
69 m2l_->ResetDefTracking();
70 GenerateTargetLabel();
71 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
72 }
73 };
74
75 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
76}
Dave Allisonb373e092014-02-20 16:06:36 -080077
Mingyao Yang80365d92014-04-18 12:10:58 -070078void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
79 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
80 public:
81 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, RegStorage index, RegStorage length)
82 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
83 index_(index), length_(length) {
84 }
85
86 void Compile() OVERRIDE {
87 m2l_->ResetRegPool();
88 m2l_->ResetDefTracking();
89 GenerateTargetLabel();
90 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
91 index_, length_, true);
92 }
93
94 private:
95 const RegStorage index_;
96 const RegStorage length_;
97 };
98
99 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
100 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
101}
102
103void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
104 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
105 public:
106 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, int index, RegStorage length)
107 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
108 index_(index), length_(length) {
109 }
110
111 void Compile() OVERRIDE {
112 m2l_->ResetRegPool();
113 m2l_->ResetDefTracking();
114 GenerateTargetLabel();
115
116 m2l_->OpRegCopy(m2l_->TargetReg(kArg1), length_);
117 m2l_->LoadConstant(m2l_->TargetReg(kArg0), index_);
118 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
119 m2l_->TargetReg(kArg0), m2l_->TargetReg(kArg1), true);
120 }
121
122 private:
123 const int32_t index_;
124 const RegStorage length_;
125 };
126
127 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
128 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
129}
130
Mingyao Yange643a172014-04-08 11:02:52 -0700131LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
132 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
133 public:
134 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
135 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
136 }
137
138 void Compile() OVERRIDE {
139 m2l_->ResetRegPool();
140 m2l_->ResetDefTracking();
141 GenerateTargetLabel();
142 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
143 }
144 };
145
146 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
147 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
148 return branch;
149}
150
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800152LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800153 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700154 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 }
Dave Allisonb373e092014-02-20 16:06:36 -0800156 return nullptr;
157}
158
Dave Allisonf9439142014-03-27 15:10:22 -0700159/* Perform an explicit null-check on a register. */
160LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
161 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
162 return NULL;
163 }
Mingyao Yange643a172014-04-08 11:02:52 -0700164 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700165}
166
Dave Allisonb373e092014-02-20 16:06:36 -0800167void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
168 if (!Runtime::Current()->ExplicitNullChecks()) {
169 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
170 return;
171 }
172 MarkSafepointPC(last_lir_insn_);
173 }
174}
175
176void Mir2Lir::MarkPossibleStackOverflowException() {
177 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
178 MarkSafepointPC(last_lir_insn_);
179 }
180}
181
buzbee2700f7e2014-03-07 09:46:20 -0800182void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800183 if (!Runtime::Current()->ExplicitNullChecks()) {
184 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
185 return;
186 }
187 // Force an implicit null check by performing a memory operation (load) from the given
188 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800189 RegStorage tmp = AllocTemp();
190 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700191 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800192 FreeTemp(tmp);
193 MarkSafepointPC(load);
194 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195}
196
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
198 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700199 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 ConditionCode cond;
201 switch (opcode) {
202 case Instruction::IF_EQ:
203 cond = kCondEq;
204 break;
205 case Instruction::IF_NE:
206 cond = kCondNe;
207 break;
208 case Instruction::IF_LT:
209 cond = kCondLt;
210 break;
211 case Instruction::IF_GE:
212 cond = kCondGe;
213 break;
214 case Instruction::IF_GT:
215 cond = kCondGt;
216 break;
217 case Instruction::IF_LE:
218 cond = kCondLe;
219 break;
220 default:
221 cond = static_cast<ConditionCode>(0);
222 LOG(FATAL) << "Unexpected opcode " << opcode;
223 }
224
225 // Normalize such that if either operand is constant, src2 will be constant
226 if (rl_src1.is_const) {
227 RegLocation rl_temp = rl_src1;
228 rl_src1 = rl_src2;
229 rl_src2 = rl_temp;
230 cond = FlipComparisonOrder(cond);
231 }
232
233 rl_src1 = LoadValue(rl_src1, kCoreReg);
234 // Is this really an immediate comparison?
235 if (rl_src2.is_const) {
236 // If it's already live in a register or not easily materialized, just keep going
237 RegLocation rl_temp = UpdateLoc(rl_src2);
238 if ((rl_temp.location == kLocDalvikFrame) &&
239 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
240 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800241 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 return;
243 }
244 }
245 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800246 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247}
248
249void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700250 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 ConditionCode cond;
252 rl_src = LoadValue(rl_src, kCoreReg);
253 switch (opcode) {
254 case Instruction::IF_EQZ:
255 cond = kCondEq;
256 break;
257 case Instruction::IF_NEZ:
258 cond = kCondNe;
259 break;
260 case Instruction::IF_LTZ:
261 cond = kCondLt;
262 break;
263 case Instruction::IF_GEZ:
264 cond = kCondGe;
265 break;
266 case Instruction::IF_GTZ:
267 cond = kCondGt;
268 break;
269 case Instruction::IF_LEZ:
270 cond = kCondLe;
271 break;
272 default:
273 cond = static_cast<ConditionCode>(0);
274 LOG(FATAL) << "Unexpected opcode " << opcode;
275 }
buzbee2700f7e2014-03-07 09:46:20 -0800276 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277}
278
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700279void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
281 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800282 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800284 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 }
buzbee2700f7e2014-03-07 09:46:20 -0800286 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 StoreValueWide(rl_dest, rl_result);
288}
289
290void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700291 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700292 rl_src = LoadValue(rl_src, kCoreReg);
293 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
294 OpKind op = kOpInvalid;
295 switch (opcode) {
296 case Instruction::INT_TO_BYTE:
297 op = kOp2Byte;
298 break;
299 case Instruction::INT_TO_SHORT:
300 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700302 case Instruction::INT_TO_CHAR:
303 op = kOp2Char;
304 break;
305 default:
306 LOG(ERROR) << "Bad int conversion type";
307 }
buzbee2700f7e2014-03-07 09:46:20 -0800308 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700309 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310}
311
312/*
313 * Let helper function take care of everything. Will call
314 * Array::AllocFromCode(type_idx, method, count);
315 * Note: AllocFromCode will handle checks for errNegativeArraySize.
316 */
317void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700318 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700320 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800321 const DexFile* dex_file = cu_->dex_file;
322 CompilerDriver* driver = cu_->compiler_driver;
323 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800325 bool is_type_initialized; // Ignored as an array does not have an initializer.
326 bool use_direct_type_ptr;
327 uintptr_t direct_type_ptr;
328 if (kEmbedClassInCode &&
329 driver->CanEmbedTypeInCode(*dex_file, type_idx,
330 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
331 // The fast path.
332 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800333 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700334 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800335 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
336 } else {
337 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700338 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800339 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
340 }
341 } else {
342 // The slow path.
343 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700344 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800345 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
346 }
347 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700349 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800350 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 RegLocation rl_result = GetReturn(false);
353 StoreValue(rl_dest, rl_result);
354}
355
356/*
357 * Similar to GenNewArray, but with post-allocation initialization.
358 * Verifier guarantees we're dealing with an array class. Current
359 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
360 * Current code also throws internal unimp if not 'L', '[' or 'I'.
361 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700362void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 int elems = info->num_arg_words;
364 int type_idx = info->index;
365 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700366 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
368 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700369 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700371 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 }
373 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
374 FreeTemp(TargetReg(kArg2));
375 FreeTemp(TargetReg(kArg1));
376 /*
377 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
378 * return region. Because AllocFromCode placed the new array
379 * in kRet0, we'll just lock it into place. When debugger support is
380 * added, it may be necessary to additionally copy all return
381 * values to a home location in thread-local storage
382 */
383 LockTemp(TargetReg(kRet0));
384
385 // TODO: use the correct component size, currently all supported types
386 // share array alignment with ints (see comment at head of function)
387 size_t component_size = sizeof(int32_t);
388
389 // Having a range of 0 is legal
390 if (info->is_range && (elems > 0)) {
391 /*
392 * Bit of ugliness here. We're going generate a mem copy loop
393 * on the register range, but it is possible that some regs
394 * in the range have been promoted. This is unlikely, but
395 * before generating the copy, we'll just force a flush
396 * of any regs in the source range that have been promoted to
397 * home location.
398 */
399 for (int i = 0; i < elems; i++) {
400 RegLocation loc = UpdateLoc(info->args[i]);
401 if (loc.location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700402 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 }
404 }
405 /*
406 * TUNING note: generated code here could be much improved, but
407 * this is an uncommon operation and isn't especially performance
408 * critical.
409 */
buzbee2700f7e2014-03-07 09:46:20 -0800410 RegStorage r_src = AllocTemp();
411 RegStorage r_dst = AllocTemp();
412 RegStorage r_idx = AllocTemp();
413 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700414 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 case kThumb2:
416 r_val = TargetReg(kLr);
417 break;
418 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700419 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 FreeTemp(TargetReg(kRet0));
421 r_val = AllocTemp();
422 break;
423 case kMips:
424 r_val = AllocTemp();
425 break;
426 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
427 }
428 // Set up source pointer
429 RegLocation rl_first = info->args[0];
430 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
431 // Set up the target pointer
432 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
433 mirror::Array::DataOffset(component_size).Int32Value());
434 // Set up the loop counter (known to be > 0)
435 LoadConstant(r_idx, elems - 1);
436 // Generate the copy loop. Going backwards for convenience
437 LIR* target = NewLIR0(kPseudoTargetLabel);
438 // Copy next element
buzbee695d13a2014-04-19 13:32:20 -0700439 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
440 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 FreeTemp(r_val);
442 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700443 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 // Restore the target pointer
445 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
446 -mirror::Array::DataOffset(component_size).Int32Value());
447 }
448 } else if (!info->is_range) {
449 // TUNING: interleave
450 for (int i = 0; i < elems; i++) {
451 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700452 Store32Disp(TargetReg(kRet0),
453 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800455 if (IsTemp(rl_arg.reg)) {
456 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 }
458 }
459 }
460 if (info->result.location != kLocInvalid) {
461 StoreValue(info->result, GetReturn(false /* not fp */));
462 }
463}
464
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800465//
466// Slow path to ensure a class is initialized for sget/sput.
467//
468class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
469 public:
buzbee2700f7e2014-03-07 09:46:20 -0800470 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
471 RegStorage r_base) :
472 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
473 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800474 }
475
476 void Compile() {
477 LIR* unresolved_target = GenerateTargetLabel();
478 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700479 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800480 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800481 // Copy helper's result into r_base, a no-op on all but MIPS.
482 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
483
484 m2l_->OpUnconditionalBranch(cont_);
485 }
486
487 private:
488 LIR* const uninit_;
489 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800490 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800491};
492
Vladimir Markobe0e5462014-02-26 11:24:15 +0000493void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700494 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000495 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
496 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
497 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
498 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800499 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000500 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 // Fast path, static storage base is this method's class
502 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800503 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700504 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800505 if (IsTemp(rl_method.reg)) {
506 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 }
508 } else {
509 // Medium path, static storage base in a different class which requires checks that the other
510 // class is initialized.
511 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000512 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 // May do runtime call so everything to home locations.
514 FlushAllRegs();
515 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800516 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 LockTemp(r_method);
518 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800519 r_base = TargetReg(kArg0);
520 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700521 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Dmitry Petrochenko973cc952014-04-18 14:53:09 +0700522 LoadRefDisp(r_base, mirror::Array::DataOffsetOfType<mirror::Object>(field_info.StorageIndex()), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800523 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000524 if (!field_info.IsInitialized() &&
525 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800526 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800527
528 // The slow path is invoked if the r_base is NULL or the class pointed
529 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800530 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800531 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800532 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800533 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800534 mirror::Class::StatusOffset().Int32Value(),
535 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800536 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800537
buzbee2700f7e2014-03-07 09:46:20 -0800538 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000539 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800540
541 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 FreeTemp(r_method);
544 }
545 // rBase now holds static storage base
546 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700547 RegisterClass register_kind = kAnyReg;
548 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
549 // Force long/double volatile stores into SSE registers to avoid tearing.
550 register_kind = kFPReg;
551 }
552 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 } else {
554 rl_src = LoadValue(rl_src, kAnyReg);
555 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000556 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800557 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 GenMemBarrier(kStoreStore);
559 }
560 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800561 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
buzbee695d13a2014-04-19 13:32:20 -0700562 } else if (rl_src.ref) {
563 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 } else {
buzbee695d13a2014-04-19 13:32:20 -0700565 Store32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000567 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800568 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 GenMemBarrier(kStoreLoad);
570 }
571 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800572 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800574 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 } else {
576 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700577 ThreadOffset<4> setter_offset =
578 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
579 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
580 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000581 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 }
583}
584
Vladimir Markobe0e5462014-02-26 11:24:15 +0000585void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700586 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000587 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
588 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
589 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
590 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800591 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000592 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 // Fast path, static storage base is this method's class
594 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800595 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700596 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 } else {
598 // Medium path, static storage base in a different class which requires checks that the other
599 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000600 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 // May do runtime call so everything to home locations.
602 FlushAllRegs();
603 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800604 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 LockTemp(r_method);
606 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800607 r_base = TargetReg(kArg0);
608 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700609 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Dmitry Petrochenko973cc952014-04-18 14:53:09 +0700610 LoadRefDisp(r_base, mirror::Array::DataOffsetOfType<mirror::Object>(field_info.StorageIndex()), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800611 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000612 if (!field_info.IsInitialized() &&
613 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800615
616 // The slow path is invoked if the r_base is NULL or the class pointed
617 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800618 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800619 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800620 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800621 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800622 mirror::Class::StatusOffset().Int32Value(),
623 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800624 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800625
buzbee2700f7e2014-03-07 09:46:20 -0800626 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000627 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800628
629 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 FreeTemp(r_method);
632 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800633 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700634 RegisterClass result_reg_kind = kAnyReg;
635 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
636 // Force long/double volatile loads into SSE registers to avoid tearing.
637 result_reg_kind = kFPReg;
638 }
639 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800640
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800642 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
buzbee695d13a2014-04-19 13:32:20 -0700643 } else if (rl_result.ref) {
644 LoadRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 } else {
buzbee695d13a2014-04-19 13:32:20 -0700646 Load32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800648 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800649
650 if (field_info.IsVolatile()) {
651 // Without context sensitive analysis, we must issue the most conservative barriers.
652 // In this case, either a load or store may follow so we issue both barriers.
653 GenMemBarrier(kLoadLoad);
654 GenMemBarrier(kLoadStore);
655 }
656
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 if (is_long_or_double) {
658 StoreValueWide(rl_dest, rl_result);
659 } else {
660 StoreValue(rl_dest, rl_result);
661 }
662 } else {
663 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700664 ThreadOffset<4> getterOffset =
665 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
666 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
667 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000668 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 if (is_long_or_double) {
670 RegLocation rl_result = GetReturnWide(rl_dest.fp);
671 StoreValueWide(rl_dest, rl_result);
672 } else {
673 RegLocation rl_result = GetReturn(rl_dest.fp);
674 StoreValue(rl_dest, rl_result);
675 }
676 }
677}
678
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800679// Generate code for all slow paths.
680void Mir2Lir::HandleSlowPaths() {
681 int n = slow_paths_.Size();
682 for (int i = 0; i < n; ++i) {
683 LIRSlowPath* slowpath = slow_paths_.Get(i);
684 slowpath->Compile();
685 }
686 slow_paths_.Reset();
687}
688
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700689void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700691 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 for (int i = 0; i < num_elems; i++) {
693 ResetRegPool();
694 ResetDefTracking();
695 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700696 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 current_dalvik_offset_ = lab->operands[1];
698 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800699 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
701 OpUnconditionalBranch(resume_lab);
702 }
703}
704
Vladimir Markobe0e5462014-02-26 11:24:15 +0000705void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700707 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000708 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
709 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
710 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 RegLocation rl_result;
712 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000713 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 rl_obj = LoadValue(rl_obj, kCoreReg);
715 if (is_long_or_double) {
716 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800717 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700718 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700719 RegisterClass result_reg_kind = kAnyReg;
720 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
721 // Force long/double volatile loads into SSE registers to avoid tearing.
722 result_reg_kind = kFPReg;
723 }
724 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800725 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
726 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800727 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000728 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800729 // Without context sensitive analysis, we must issue the most conservative barriers.
730 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800732 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 }
734 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800735 RegStorage reg_ptr = AllocTemp();
736 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800738 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700739 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000740 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800741 // Without context sensitive analysis, we must issue the most conservative barriers.
742 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800744 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 }
746 FreeTemp(reg_ptr);
747 }
748 StoreValueWide(rl_dest, rl_result);
749 } else {
750 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800751 GenNullCheck(rl_obj.reg, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700752 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, k32,
buzbee2700f7e2014-03-07 09:46:20 -0800753 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800754 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000755 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800756 // Without context sensitive analysis, we must issue the most conservative barriers.
757 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800759 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 }
761 StoreValue(rl_dest, rl_result);
762 }
763 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700764 ThreadOffset<4> getterOffset =
765 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
766 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
767 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000768 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 if (is_long_or_double) {
770 RegLocation rl_result = GetReturnWide(rl_dest.fp);
771 StoreValueWide(rl_dest, rl_result);
772 } else {
773 RegLocation rl_result = GetReturn(rl_dest.fp);
774 StoreValue(rl_dest, rl_result);
775 }
776 }
777}
778
Vladimir Markobe0e5462014-02-26 11:24:15 +0000779void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700781 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000782 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
783 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
784 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000786 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 rl_obj = LoadValue(rl_obj, kCoreReg);
788 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700789 RegisterClass src_reg_kind = kAnyReg;
790 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
791 // Force long/double volatile stores into SSE registers to avoid tearing.
792 src_reg_kind = kFPReg;
793 }
794 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800795 GenNullCheck(rl_obj.reg, opt_flags);
796 RegStorage reg_ptr = AllocTemp();
797 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000798 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800799 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 GenMemBarrier(kStoreStore);
801 }
buzbee2700f7e2014-03-07 09:46:20 -0800802 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800803 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000804 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800805 // A load might follow the volatile store so insert a StoreLoad barrier.
806 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 }
808 FreeTemp(reg_ptr);
809 } else {
810 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800811 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000812 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800813 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 GenMemBarrier(kStoreStore);
815 }
buzbee695d13a2014-04-19 13:32:20 -0700816 Store32Disp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800817 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000818 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800819 // A load might follow the volatile store so insert a StoreLoad barrier.
820 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 }
822 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800823 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824 }
825 }
826 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700827 ThreadOffset<4> setter_offset =
828 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
829 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
830 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000831 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
832 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 }
834}
835
Ian Rogersa9a82542013-10-04 11:17:26 -0700836void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
837 RegLocation rl_src) {
838 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
839 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
840 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700841 ThreadOffset<4> helper = needs_range_check
842 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
843 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
844 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700845 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
846}
847
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700848void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800850 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
852 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
853 *cu_->dex_file,
854 type_idx)) {
855 // Call out to helper which resolves type and verifies access.
856 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700857 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800858 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700859 RegLocation rl_result = GetReturn(false);
860 StoreValue(rl_dest, rl_result);
861 } else {
862 // We're don't need access checks, load type from dex cache
863 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700864 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700865 Load32Disp(rl_method.reg, dex_cache_offset, res_reg);
Dmitry Petrochenko973cc952014-04-18 14:53:09 +0700866 int32_t offset_of_type = mirror::Array::DataOffsetOfType<mirror::Class>(type_idx);
buzbee695d13a2014-04-19 13:32:20 -0700867 Load32Disp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
869 type_idx) || SLOW_TYPE_PATH) {
870 // Slow path, at runtime test if type is null and if so initialize
871 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800872 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800873 LIR* cont = NewLIR0(kPseudoTargetLabel);
874
875 // Object to generate the slow path for class resolution.
876 class SlowPath : public LIRSlowPath {
877 public:
878 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
879 const RegLocation& rl_method, const RegLocation& rl_result) :
880 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
881 rl_method_(rl_method), rl_result_(rl_result) {
882 }
883
884 void Compile() {
885 GenerateTargetLabel();
886
Ian Rogersdd7624d2014-03-14 17:43:00 -0700887 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800888 rl_method_.reg, true);
889 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800890
891 m2l_->OpUnconditionalBranch(cont_);
892 }
893
894 private:
895 const int type_idx_;
896 const RegLocation rl_method_;
897 const RegLocation rl_result_;
898 };
899
900 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800901 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800902
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800904 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 // Fast path, we're done - just store result
906 StoreValue(rl_dest, rl_result);
907 }
908 }
909}
910
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700911void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 /* NOTE: Most strings should be available at compile time */
Dmitry Petrochenko973cc952014-04-18 14:53:09 +0700913 int32_t offset_of_string = mirror::Array::DataOffsetOfType<mirror::String>(string_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
915 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
916 // slow path, resolve string if not in dex cache
917 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700918 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800919
920 // If the Method* is already in a register, we can save a copy.
921 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800922 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800923 if (rl_method.location == kLocPhysReg) {
924 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800925 DCHECK(!IsTemp(rl_method.reg));
926 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800927 } else {
928 r_method = TargetReg(kArg2);
929 LoadCurrMethodDirect(r_method);
930 }
buzbee695d13a2014-04-19 13:32:20 -0700931 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
932 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800933
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 // Might call out to helper, which will return resolved string in kRet0
buzbee695d13a2014-04-19 13:32:20 -0700935 Load32Disp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800936 if (cu_->instruction_set == kThumb2 ||
937 cu_->instruction_set == kMips) {
938 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800939 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800940 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
941 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800943
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800944 // Object to generate the slow path for string resolution.
945 class SlowPath : public LIRSlowPath {
946 public:
buzbee2700f7e2014-03-07 09:46:20 -0800947 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800948 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
949 }
950
951 void Compile() {
952 GenerateTargetLabel();
953
Dave Allisond6ed6422014-04-09 23:36:15 +0000954 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800955
Dave Allisond6ed6422014-04-09 23:36:15 +0000956 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
957 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
958 m2l_->MarkSafepointPC(call_inst);
959 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800960
961 m2l_->OpUnconditionalBranch(cont_);
962 }
963
964 private:
buzbee2700f7e2014-03-07 09:46:20 -0800965 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800966 };
967
968 // Add to list for future.
969 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700971 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -0800972 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
973 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700974 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -0800975 true);
Mark Mendell766e9292014-01-27 07:55:47 -0800976 LIR* target = NewLIR0(kPseudoTargetLabel);
977 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 }
979 GenBarrier();
980 StoreValue(rl_dest, GetReturn(false));
981 } else {
982 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800983 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700985 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
986 Load32Disp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 StoreValue(rl_dest, rl_result);
988 }
989}
990
991/*
992 * Let helper function take care of everything. Will
993 * call Class::NewInstanceFromCode(type_idx, method);
994 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700995void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 FlushAllRegs(); /* Everything to home location */
997 // alloc will always check for resolution, do we also need to verify
998 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -0700999 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001000 const DexFile* dex_file = cu_->dex_file;
1001 CompilerDriver* driver = cu_->compiler_driver;
1002 if (driver->CanAccessInstantiableTypeWithoutChecks(
1003 cu_->method_idx, *dex_file, type_idx)) {
1004 bool is_type_initialized;
1005 bool use_direct_type_ptr;
1006 uintptr_t direct_type_ptr;
1007 if (kEmbedClassInCode &&
1008 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1009 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1010 // The fast path.
1011 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001012 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001013 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001014 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001015 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1016 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001017 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001018 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1019 }
1020 } else {
1021 // Use the direct pointer.
1022 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001023 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001024 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1025 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001026 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001027 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1028 }
1029 }
1030 } else {
1031 // The slow path.
1032 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001033 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001034 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1035 }
1036 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001038 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001039 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 RegLocation rl_result = GetReturn(false);
1042 StoreValue(rl_dest, rl_result);
1043}
1044
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001045void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001047 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048}
1049
1050// For final classes there are no sub-classes to check and so we can answer the instance-of
1051// question with simple comparisons.
1052void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1053 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001054 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001055 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001056
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 RegLocation object = LoadValue(rl_src, kCoreReg);
1058 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001059 RegStorage result_reg = rl_result.reg;
1060 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 result_reg = AllocTypedTemp(false, kCoreReg);
1062 }
1063 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001064 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065
buzbee2700f7e2014-03-07 09:46:20 -08001066 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1067 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068
1069 LoadCurrMethodDirect(check_class);
1070 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001071 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1072 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 } else {
buzbee695d13a2014-04-19 13:32:20 -07001074 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1075 check_class);
1076 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Dmitry Petrochenko973cc952014-04-18 14:53:09 +07001077 int32_t offset_of_type = mirror::Array::DataOffsetOfType<mirror::Class>(type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001078 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
1080
1081 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001082 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 if (cu_->instruction_set == kThumb2) {
1084 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001085 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001087 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 } else {
1089 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1090 LoadConstant(result_reg, 1); // eq case - load true
1091 }
1092 LIR* target = NewLIR0(kPseudoTargetLabel);
1093 null_branchover->target = target;
1094 if (ne_branchover != NULL) {
1095 ne_branchover->target = target;
1096 }
1097 FreeTemp(object_class);
1098 FreeTemp(check_class);
1099 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001100 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 FreeTemp(result_reg);
1102 }
1103 StoreValue(rl_dest, rl_result);
1104}
1105
1106void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1107 bool type_known_abstract, bool use_declaring_class,
1108 bool can_assume_type_is_in_dex_cache,
1109 uint32_t type_idx, RegLocation rl_dest,
1110 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001111 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001112 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001113
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 FlushAllRegs();
1115 // May generate a call - use explicit registers
1116 LockCallTemps();
1117 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001118 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 if (needs_access_check) {
1120 // Check we have access to type_idx and if not throw IllegalAccessError,
1121 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001122 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 type_idx, true);
1124 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1125 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1126 } else if (use_declaring_class) {
1127 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001128 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001129 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 } else {
1131 // Load dex cache entry into class_reg (kArg2)
1132 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001133 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1134 class_reg);
Dmitry Petrochenko973cc952014-04-18 14:53:09 +07001135 int32_t offset_of_type = mirror::Array::DataOffsetOfType<mirror::Class>(type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001136 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 if (!can_assume_type_is_in_dex_cache) {
1138 // Need to test presence of type in dex cache at runtime
1139 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1140 // Not resolved
1141 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001142 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001143 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1145 // Rejoin code paths
1146 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1147 hop_branch->target = hop_target;
1148 }
1149 }
1150 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1151 RegLocation rl_result = GetReturn(false);
1152 if (cu_->instruction_set == kMips) {
1153 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001154 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 }
1156 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1157
1158 /* load object->klass_ */
1159 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001160 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1162 LIR* branchover = NULL;
1163 if (type_known_final) {
1164 // rl_result == ref == null == 0.
1165 if (cu_->instruction_set == kThumb2) {
1166 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001167 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001168 LoadConstant(rl_result.reg, 1); // .eq case - load true
1169 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001170 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001172 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001174 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 }
1176 } else {
1177 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001178 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001179 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 if (!type_known_abstract) {
1181 /* Uses conditional nullification */
1182 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001183 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1185 }
1186 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1187 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001188 if (it != nullptr) {
1189 OpEndIT(it);
1190 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 FreeTemp(r_tgt);
1192 } else {
1193 if (!type_known_abstract) {
1194 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001195 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1197 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001198 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001199 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1200 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1201 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 }
1203 }
1204 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001205 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 /* branch targets here */
1207 LIR* target = NewLIR0(kPseudoTargetLabel);
1208 StoreValue(rl_dest, rl_result);
1209 branch1->target = target;
1210 if (branchover != NULL) {
1211 branchover->target = target;
1212 }
1213}
1214
1215void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1216 bool type_known_final, type_known_abstract, use_declaring_class;
1217 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1218 *cu_->dex_file,
1219 type_idx,
1220 &type_known_final,
1221 &type_known_abstract,
1222 &use_declaring_class);
1223 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1224 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1225
1226 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1227 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1228 } else {
1229 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1230 use_declaring_class, can_assume_type_is_in_dex_cache,
1231 type_idx, rl_dest, rl_src);
1232 }
1233}
1234
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001235void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 bool type_known_final, type_known_abstract, use_declaring_class;
1237 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1238 *cu_->dex_file,
1239 type_idx,
1240 &type_known_final,
1241 &type_known_abstract,
1242 &use_declaring_class);
1243 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1244 // of the exception throw path.
1245 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001246 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 // Verifier type analysis proved this check cast would never cause an exception.
1248 return;
1249 }
1250 FlushAllRegs();
1251 // May generate a call - use explicit registers
1252 LockCallTemps();
1253 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001254 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 if (needs_access_check) {
1256 // Check we have access to type_idx and if not throw IllegalAccessError,
1257 // returns Class* in kRet0
1258 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001259 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 type_idx, TargetReg(kArg1), true);
1261 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1262 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001263 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1264 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 } else {
1266 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001267 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1268 class_reg);
Dmitry Petrochenko973cc952014-04-18 14:53:09 +07001269 int32_t offset_of_type = mirror::Array::DataOffsetOfType<mirror::Class>(type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001270 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1272 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001273 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1274 LIR* cont = NewLIR0(kPseudoTargetLabel);
1275
1276 // Slow path to initialize the type. Executed if the type is NULL.
1277 class SlowPath : public LIRSlowPath {
1278 public:
1279 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001280 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001281 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1282 class_reg_(class_reg) {
1283 }
1284
1285 void Compile() {
1286 GenerateTargetLabel();
1287
1288 // Call out to helper, which will return resolved type in kArg0
1289 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001290 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001291 m2l_->TargetReg(kArg1), true);
1292 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1293 m2l_->OpUnconditionalBranch(cont_);
1294 }
1295 public:
1296 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001297 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001298 };
1299
buzbee2700f7e2014-03-07 09:46:20 -08001300 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 }
1302 }
1303 // At this point, class_reg (kArg2) has class
1304 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001305
1306 // Slow path for the case where the classes are not equal. In this case we need
1307 // to call a helper function to do the check.
1308 class SlowPath : public LIRSlowPath {
1309 public:
1310 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1311 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1312 }
1313
1314 void Compile() {
1315 GenerateTargetLabel();
1316
1317 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001318 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1319 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001320 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001321 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001322 m2l_->TargetReg(kArg1), true);
1323
1324 m2l_->OpUnconditionalBranch(cont_);
1325 }
1326
1327 private:
1328 bool load_;
1329 };
1330
1331 if (type_known_abstract) {
1332 // Easier case, run slow path if target is non-null (slow path will load from target)
1333 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1334 LIR* cont = NewLIR0(kPseudoTargetLabel);
1335 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1336 } else {
1337 // Harder, more common case. We need to generate a forward branch over the load
1338 // if the target is null. If it's non-null we perform the load and branch to the
1339 // slow path if the classes are not equal.
1340
1341 /* Null is OK - continue */
1342 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1343 /* load object->klass_ */
1344 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001345 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001346
1347 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1348 LIR* cont = NewLIR0(kPseudoTargetLabel);
1349
1350 // Add the slow path that will not perform load since this is already done.
1351 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1352
1353 // Set the null check to branch to the continuation.
1354 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 }
1356}
1357
1358void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001359 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 RegLocation rl_result;
1361 if (cu_->instruction_set == kThumb2) {
1362 /*
1363 * NOTE: This is the one place in the code in which we might have
1364 * as many as six live temporary registers. There are 5 in the normal
1365 * set for Arm. Until we have spill capabilities, temporarily add
1366 * lr to the temp set. It is safe to do this locally, but note that
1367 * lr is used explicitly elsewhere in the code generator and cannot
1368 * normally be used as a general temp register.
1369 */
1370 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1371 FreeTemp(TargetReg(kLr)); // and make it available
1372 }
1373 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1374 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1375 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1376 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001377 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1378 RegStorage t_reg = AllocTemp();
1379 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1380 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1381 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 FreeTemp(t_reg);
1383 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001384 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1385 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 }
1387 /*
1388 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1389 * following StoreValueWide might need to allocate a temp register.
1390 * To further work around the lack of a spill capability, explicitly
1391 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1392 * Remove when spill is functional.
1393 */
1394 FreeRegLocTemps(rl_result, rl_src1);
1395 FreeRegLocTemps(rl_result, rl_src2);
1396 StoreValueWide(rl_dest, rl_result);
1397 if (cu_->instruction_set == kThumb2) {
1398 Clobber(TargetReg(kLr));
1399 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1400 }
1401}
1402
1403
1404void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001405 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001406 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407
1408 switch (opcode) {
1409 case Instruction::SHL_LONG:
1410 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001411 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 break;
1413 case Instruction::SHR_LONG:
1414 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001415 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 break;
1417 case Instruction::USHR_LONG:
1418 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001419 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001420 break;
1421 default:
1422 LOG(FATAL) << "Unexpected case";
1423 }
1424 FlushAllRegs(); /* Send everything to home location */
1425 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1426 RegLocation rl_result = GetReturnWide(false);
1427 StoreValueWide(rl_dest, rl_result);
1428}
1429
1430
1431void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001432 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001433 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 OpKind op = kOpBkpt;
1435 bool is_div_rem = false;
1436 bool check_zero = false;
1437 bool unary = false;
1438 RegLocation rl_result;
1439 bool shift_op = false;
1440 switch (opcode) {
1441 case Instruction::NEG_INT:
1442 op = kOpNeg;
1443 unary = true;
1444 break;
1445 case Instruction::NOT_INT:
1446 op = kOpMvn;
1447 unary = true;
1448 break;
1449 case Instruction::ADD_INT:
1450 case Instruction::ADD_INT_2ADDR:
1451 op = kOpAdd;
1452 break;
1453 case Instruction::SUB_INT:
1454 case Instruction::SUB_INT_2ADDR:
1455 op = kOpSub;
1456 break;
1457 case Instruction::MUL_INT:
1458 case Instruction::MUL_INT_2ADDR:
1459 op = kOpMul;
1460 break;
1461 case Instruction::DIV_INT:
1462 case Instruction::DIV_INT_2ADDR:
1463 check_zero = true;
1464 op = kOpDiv;
1465 is_div_rem = true;
1466 break;
1467 /* NOTE: returns in kArg1 */
1468 case Instruction::REM_INT:
1469 case Instruction::REM_INT_2ADDR:
1470 check_zero = true;
1471 op = kOpRem;
1472 is_div_rem = true;
1473 break;
1474 case Instruction::AND_INT:
1475 case Instruction::AND_INT_2ADDR:
1476 op = kOpAnd;
1477 break;
1478 case Instruction::OR_INT:
1479 case Instruction::OR_INT_2ADDR:
1480 op = kOpOr;
1481 break;
1482 case Instruction::XOR_INT:
1483 case Instruction::XOR_INT_2ADDR:
1484 op = kOpXor;
1485 break;
1486 case Instruction::SHL_INT:
1487 case Instruction::SHL_INT_2ADDR:
1488 shift_op = true;
1489 op = kOpLsl;
1490 break;
1491 case Instruction::SHR_INT:
1492 case Instruction::SHR_INT_2ADDR:
1493 shift_op = true;
1494 op = kOpAsr;
1495 break;
1496 case Instruction::USHR_INT:
1497 case Instruction::USHR_INT_2ADDR:
1498 shift_op = true;
1499 op = kOpLsr;
1500 break;
1501 default:
1502 LOG(FATAL) << "Invalid word arith op: " << opcode;
1503 }
1504 if (!is_div_rem) {
1505 if (unary) {
1506 rl_src1 = LoadValue(rl_src1, kCoreReg);
1507 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001508 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 } else {
1510 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001511 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001512 RegStorage t_reg = AllocTemp();
1513 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 rl_src1 = LoadValue(rl_src1, kCoreReg);
1515 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001516 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 FreeTemp(t_reg);
1518 } else {
1519 rl_src1 = LoadValue(rl_src1, kCoreReg);
1520 rl_src2 = LoadValue(rl_src2, kCoreReg);
1521 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001522 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001523 }
1524 }
1525 StoreValue(rl_dest, rl_result);
1526 } else {
Dave Allison70202782013-10-22 17:52:19 -07001527 bool done = false; // Set to true if we happen to find a way to use a real instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 if (cu_->instruction_set == kMips) {
1529 rl_src1 = LoadValue(rl_src1, kCoreReg);
1530 rl_src2 = LoadValue(rl_src2, kCoreReg);
1531 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001532 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533 }
buzbee2700f7e2014-03-07 09:46:20 -08001534 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001535 done = true;
1536 } else if (cu_->instruction_set == kThumb2) {
1537 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1538 // Use ARM SDIV instruction for division. For remainder we also need to
1539 // calculate using a MUL and subtract.
1540 rl_src1 = LoadValue(rl_src1, kCoreReg);
1541 rl_src2 = LoadValue(rl_src2, kCoreReg);
1542 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001543 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001544 }
buzbee2700f7e2014-03-07 09:46:20 -08001545 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001546 done = true;
1547 }
1548 }
1549
1550 // If we haven't already generated the code use the callout function.
1551 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001552 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 FlushAllRegs(); /* Send everything to home location */
1554 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001555 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1557 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001558 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 }
Dave Allison70202782013-10-22 17:52:19 -07001560 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001561 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 if (op == kOpDiv)
1563 rl_result = GetReturn(false);
1564 else
1565 rl_result = GetReturnAlt();
1566 }
1567 StoreValue(rl_dest, rl_result);
1568 }
1569}
1570
1571/*
1572 * The following are the first-level codegen routines that analyze the format
1573 * of each bytecode then either dispatch special purpose codegen routines
1574 * or produce corresponding Thumb instructions directly.
1575 */
1576
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001578static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 x &= x - 1;
1580 return (x & (x - 1)) == 0;
1581}
1582
Brian Carlstrom7940e442013-07-12 13:46:57 -07001583// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1584// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001585bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001586 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1588 return false;
1589 }
1590 // No divide instruction for Arm, so check for more special cases
1591 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001592 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 }
1594 int k = LowestSetBit(lit);
1595 if (k >= 30) {
1596 // Avoid special cases.
1597 return false;
1598 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 rl_src = LoadValue(rl_src, kCoreReg);
1600 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001601 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001602 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 if (lit == 2) {
1604 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001605 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1606 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1607 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001609 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001611 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1612 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 }
1614 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001615 RegStorage t_reg1 = AllocTemp();
1616 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001618 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1619 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001621 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001623 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001625 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001626 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001627 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 }
1629 }
1630 StoreValue(rl_dest, rl_result);
1631 return true;
1632}
1633
1634// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1635// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001636bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001637 if (lit < 0) {
1638 return false;
1639 }
1640 if (lit == 0) {
1641 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1642 LoadConstant(rl_result.reg, 0);
1643 StoreValue(rl_dest, rl_result);
1644 return true;
1645 }
1646 if (lit == 1) {
1647 rl_src = LoadValue(rl_src, kCoreReg);
1648 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1649 OpRegCopy(rl_result.reg, rl_src.reg);
1650 StoreValue(rl_dest, rl_result);
1651 return true;
1652 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001653 // There is RegRegRegShift on Arm, so check for more special cases
1654 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001655 return EasyMultiply(rl_src, rl_dest, lit);
1656 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001657 // Can we simplify this multiplication?
1658 bool power_of_two = false;
1659 bool pop_count_le2 = false;
1660 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001661 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 power_of_two = true;
1663 } else if (IsPopCountLE2(lit)) {
1664 pop_count_le2 = true;
1665 } else if (IsPowerOfTwo(lit + 1)) {
1666 power_of_two_minus_one = true;
1667 } else {
1668 return false;
1669 }
1670 rl_src = LoadValue(rl_src, kCoreReg);
1671 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1672 if (power_of_two) {
1673 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001674 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 } else if (pop_count_le2) {
1676 // Shift and add and shift.
1677 int first_bit = LowestSetBit(lit);
1678 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1679 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1680 } else {
1681 // Reverse subtract: (src << (shift + 1)) - src.
1682 DCHECK(power_of_two_minus_one);
1683 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001684 RegStorage t_reg = AllocTemp();
1685 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1686 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 }
1688 StoreValue(rl_dest, rl_result);
1689 return true;
1690}
1691
1692void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001693 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001694 RegLocation rl_result;
1695 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1696 int shift_op = false;
1697 bool is_div = false;
1698
1699 switch (opcode) {
1700 case Instruction::RSUB_INT_LIT8:
1701 case Instruction::RSUB_INT: {
1702 rl_src = LoadValue(rl_src, kCoreReg);
1703 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1704 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001705 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001707 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1708 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 }
1710 StoreValue(rl_dest, rl_result);
1711 return;
1712 }
1713
1714 case Instruction::SUB_INT:
1715 case Instruction::SUB_INT_2ADDR:
1716 lit = -lit;
1717 // Intended fallthrough
1718 case Instruction::ADD_INT:
1719 case Instruction::ADD_INT_2ADDR:
1720 case Instruction::ADD_INT_LIT8:
1721 case Instruction::ADD_INT_LIT16:
1722 op = kOpAdd;
1723 break;
1724 case Instruction::MUL_INT:
1725 case Instruction::MUL_INT_2ADDR:
1726 case Instruction::MUL_INT_LIT8:
1727 case Instruction::MUL_INT_LIT16: {
1728 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1729 return;
1730 }
1731 op = kOpMul;
1732 break;
1733 }
1734 case Instruction::AND_INT:
1735 case Instruction::AND_INT_2ADDR:
1736 case Instruction::AND_INT_LIT8:
1737 case Instruction::AND_INT_LIT16:
1738 op = kOpAnd;
1739 break;
1740 case Instruction::OR_INT:
1741 case Instruction::OR_INT_2ADDR:
1742 case Instruction::OR_INT_LIT8:
1743 case Instruction::OR_INT_LIT16:
1744 op = kOpOr;
1745 break;
1746 case Instruction::XOR_INT:
1747 case Instruction::XOR_INT_2ADDR:
1748 case Instruction::XOR_INT_LIT8:
1749 case Instruction::XOR_INT_LIT16:
1750 op = kOpXor;
1751 break;
1752 case Instruction::SHL_INT_LIT8:
1753 case Instruction::SHL_INT:
1754 case Instruction::SHL_INT_2ADDR:
1755 lit &= 31;
1756 shift_op = true;
1757 op = kOpLsl;
1758 break;
1759 case Instruction::SHR_INT_LIT8:
1760 case Instruction::SHR_INT:
1761 case Instruction::SHR_INT_2ADDR:
1762 lit &= 31;
1763 shift_op = true;
1764 op = kOpAsr;
1765 break;
1766 case Instruction::USHR_INT_LIT8:
1767 case Instruction::USHR_INT:
1768 case Instruction::USHR_INT_2ADDR:
1769 lit &= 31;
1770 shift_op = true;
1771 op = kOpLsr;
1772 break;
1773
1774 case Instruction::DIV_INT:
1775 case Instruction::DIV_INT_2ADDR:
1776 case Instruction::DIV_INT_LIT8:
1777 case Instruction::DIV_INT_LIT16:
1778 case Instruction::REM_INT:
1779 case Instruction::REM_INT_2ADDR:
1780 case Instruction::REM_INT_LIT8:
1781 case Instruction::REM_INT_LIT16: {
1782 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001783 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001784 return;
1785 }
buzbee11b63d12013-08-27 07:34:17 -07001786 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001788 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001789 (opcode == Instruction::DIV_INT_LIT16)) {
1790 is_div = true;
1791 } else {
1792 is_div = false;
1793 }
buzbee11b63d12013-08-27 07:34:17 -07001794 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1795 return;
1796 }
Dave Allison70202782013-10-22 17:52:19 -07001797
1798 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001799 if (cu_->instruction_set == kMips) {
1800 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001801 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001802 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001803 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001804 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1805 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001806 } else if (cu_->instruction_set == kThumb2) {
1807 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1808 // Use ARM SDIV instruction for division. For remainder we also need to
1809 // calculate using a MUL and subtract.
1810 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001811 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001812 done = true;
1813 }
1814 }
1815
1816 if (!done) {
1817 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001818 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1819 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001820 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001821 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1822 if (is_div)
1823 rl_result = GetReturn(false);
1824 else
1825 rl_result = GetReturnAlt();
1826 }
1827 StoreValue(rl_dest, rl_result);
1828 return;
1829 }
1830 default:
1831 LOG(FATAL) << "Unexpected opcode " << opcode;
1832 }
1833 rl_src = LoadValue(rl_src, kCoreReg);
1834 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001835 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001836 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001837 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001838 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001839 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 }
1841 StoreValue(rl_dest, rl_result);
1842}
1843
1844void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001845 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 RegLocation rl_result;
1847 OpKind first_op = kOpBkpt;
1848 OpKind second_op = kOpBkpt;
1849 bool call_out = false;
1850 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001851 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001852 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001853
1854 switch (opcode) {
1855 case Instruction::NOT_LONG:
1856 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1857 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1858 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001859 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1860 RegStorage t_reg = AllocTemp();
1861 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1862 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1863 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001864 FreeTemp(t_reg);
1865 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001866 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1867 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001868 }
1869 StoreValueWide(rl_dest, rl_result);
1870 return;
1871 case Instruction::ADD_LONG:
1872 case Instruction::ADD_LONG_2ADDR:
1873 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001874 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001875 return;
1876 }
1877 first_op = kOpAdd;
1878 second_op = kOpAdc;
1879 break;
1880 case Instruction::SUB_LONG:
1881 case Instruction::SUB_LONG_2ADDR:
1882 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001883 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001884 return;
1885 }
1886 first_op = kOpSub;
1887 second_op = kOpSbc;
1888 break;
1889 case Instruction::MUL_LONG:
1890 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001891 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001892 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893 return;
1894 } else {
1895 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001896 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001897 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001898 }
1899 break;
1900 case Instruction::DIV_LONG:
1901 case Instruction::DIV_LONG_2ADDR:
1902 call_out = true;
1903 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001904 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001905 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001906 break;
1907 case Instruction::REM_LONG:
1908 case Instruction::REM_LONG_2ADDR:
1909 call_out = true;
1910 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001911 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001912 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001913 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001914 break;
1915 case Instruction::AND_LONG_2ADDR:
1916 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001917 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001918 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001919 }
1920 first_op = kOpAnd;
1921 second_op = kOpAnd;
1922 break;
1923 case Instruction::OR_LONG:
1924 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001925 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001926 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001927 return;
1928 }
1929 first_op = kOpOr;
1930 second_op = kOpOr;
1931 break;
1932 case Instruction::XOR_LONG:
1933 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001934 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001935 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 return;
1937 }
1938 first_op = kOpXor;
1939 second_op = kOpXor;
1940 break;
1941 case Instruction::NEG_LONG: {
1942 GenNegLong(rl_dest, rl_src2);
1943 return;
1944 }
1945 default:
1946 LOG(FATAL) << "Invalid long arith op";
1947 }
1948 if (!call_out) {
1949 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1950 } else {
1951 FlushAllRegs(); /* Send everything to home location */
1952 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001953 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1954 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1955 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1956 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07001957 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08001958 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 // NOTE: callout here is not a safepoint
1960 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1961 } else {
1962 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1963 }
1964 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001965 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001966 rl_result = GetReturnWide(false);
1967 else
1968 rl_result = GetReturnWideAlt();
1969 StoreValueWide(rl_dest, rl_result);
1970 }
1971}
1972
Ian Rogersdd7624d2014-03-14 17:43:00 -07001973void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001974 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975 /*
1976 * Don't optimize the register usage since it calls out to support
1977 * functions
1978 */
1979 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1981 if (rl_dest.wide) {
1982 RegLocation rl_result;
1983 rl_result = GetReturnWide(rl_dest.fp);
1984 StoreValueWide(rl_dest, rl_result);
1985 } else {
1986 RegLocation rl_result;
1987 rl_result = GetReturn(rl_dest.fp);
1988 StoreValue(rl_dest, rl_result);
1989 }
1990}
1991
1992/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001993void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001994 if (Runtime::Current()->ExplicitSuspendChecks()) {
1995 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1996 return;
1997 }
1998 FlushAllRegs();
1999 LIR* branch = OpTestSuspend(NULL);
2000 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2001 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2002 current_dalvik_offset_);
2003 branch->target = target;
2004 suspend_launchpads_.Insert(target);
2005 } else {
2006 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2007 return;
2008 }
2009 FlushAllRegs(); // TODO: needed?
2010 LIR* inst = CheckSuspendUsingLoad();
2011 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013}
2014
2015/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002016void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002017 if (Runtime::Current()->ExplicitSuspendChecks()) {
2018 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2019 OpUnconditionalBranch(target);
2020 return;
2021 }
2022 OpTestSuspend(target);
2023 LIR* launch_pad =
2024 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2025 current_dalvik_offset_);
2026 FlushAllRegs();
2027 OpUnconditionalBranch(launch_pad);
2028 suspend_launchpads_.Insert(launch_pad);
2029 } else {
2030 // For the implicit suspend check, just perform the trigger
2031 // load and branch to the target.
2032 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2033 OpUnconditionalBranch(target);
2034 return;
2035 }
2036 FlushAllRegs();
2037 LIR* inst = CheckSuspendUsingLoad();
2038 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002039 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041}
2042
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002043/* Call out to helper assembly routine that will null check obj and then lock it. */
2044void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2045 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002046 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002047}
2048
2049/* Call out to helper assembly routine that will null check obj and then unlock it. */
2050void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2051 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002052 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002053}
2054
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002055/* Generic code for generating a wide constant into a VR. */
2056void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2057 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002058 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002059 StoreValueWide(rl_dest, rl_result);
2060}
2061
Brian Carlstrom7940e442013-07-12 13:46:57 -07002062} // namespace art