blob: aa9b2a491a8f332dac656ab82d7e250083b696e1 [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);
Andreas Gampeb5a14d22014-04-24 17:30:35 +0000522 LoadRefDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
523 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800524 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000525 if (!field_info.IsInitialized() &&
526 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800527 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800528
529 // The slow path is invoked if the r_base is NULL or the class pointed
530 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800531 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800532 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800533 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800534 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800535 mirror::Class::StatusOffset().Int32Value(),
536 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800537 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800538
buzbee2700f7e2014-03-07 09:46:20 -0800539 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000540 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800541
542 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 FreeTemp(r_method);
545 }
546 // rBase now holds static storage base
547 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700548 RegisterClass register_kind = kAnyReg;
549 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
550 // Force long/double volatile stores into SSE registers to avoid tearing.
551 register_kind = kFPReg;
552 }
553 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 } else {
555 rl_src = LoadValue(rl_src, kAnyReg);
556 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000557 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800558 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 GenMemBarrier(kStoreStore);
560 }
561 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800562 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
buzbee695d13a2014-04-19 13:32:20 -0700563 } else if (rl_src.ref) {
564 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 } else {
buzbee695d13a2014-04-19 13:32:20 -0700566 Store32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000568 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800569 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 GenMemBarrier(kStoreLoad);
571 }
572 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800573 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800575 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 } else {
577 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700578 ThreadOffset<4> setter_offset =
579 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
580 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
581 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000582 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 }
584}
585
Vladimir Markobe0e5462014-02-26 11:24:15 +0000586void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700587 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000588 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
589 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
590 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
591 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800592 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000593 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 // Fast path, static storage base is this method's class
595 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800596 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700597 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 } else {
599 // Medium path, static storage base in a different class which requires checks that the other
600 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000601 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 // May do runtime call so everything to home locations.
603 FlushAllRegs();
604 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800605 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 LockTemp(r_method);
607 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800608 r_base = TargetReg(kArg0);
609 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700610 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampeb5a14d22014-04-24 17:30:35 +0000611 LoadRefDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
612 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800613 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000614 if (!field_info.IsInitialized() &&
615 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800616 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800617
618 // The slow path is invoked if the r_base is NULL or the class pointed
619 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800620 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800621 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800622 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800623 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800624 mirror::Class::StatusOffset().Int32Value(),
625 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800626 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800627
buzbee2700f7e2014-03-07 09:46:20 -0800628 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000629 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800630
631 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 FreeTemp(r_method);
634 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800635 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700636 RegisterClass result_reg_kind = kAnyReg;
637 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
638 // Force long/double volatile loads into SSE registers to avoid tearing.
639 result_reg_kind = kFPReg;
640 }
641 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800642
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800644 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
buzbee695d13a2014-04-19 13:32:20 -0700645 } else if (rl_result.ref) {
646 LoadRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 } else {
buzbee695d13a2014-04-19 13:32:20 -0700648 Load32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800650 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800651
652 if (field_info.IsVolatile()) {
653 // Without context sensitive analysis, we must issue the most conservative barriers.
654 // In this case, either a load or store may follow so we issue both barriers.
655 GenMemBarrier(kLoadLoad);
656 GenMemBarrier(kLoadStore);
657 }
658
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 if (is_long_or_double) {
660 StoreValueWide(rl_dest, rl_result);
661 } else {
662 StoreValue(rl_dest, rl_result);
663 }
664 } else {
665 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700666 ThreadOffset<4> getterOffset =
667 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
668 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
669 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000670 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 if (is_long_or_double) {
672 RegLocation rl_result = GetReturnWide(rl_dest.fp);
673 StoreValueWide(rl_dest, rl_result);
674 } else {
675 RegLocation rl_result = GetReturn(rl_dest.fp);
676 StoreValue(rl_dest, rl_result);
677 }
678 }
679}
680
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800681// Generate code for all slow paths.
682void Mir2Lir::HandleSlowPaths() {
683 int n = slow_paths_.Size();
684 for (int i = 0; i < n; ++i) {
685 LIRSlowPath* slowpath = slow_paths_.Get(i);
686 slowpath->Compile();
687 }
688 slow_paths_.Reset();
689}
690
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700691void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700693 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 for (int i = 0; i < num_elems; i++) {
695 ResetRegPool();
696 ResetDefTracking();
697 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700698 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 current_dalvik_offset_ = lab->operands[1];
700 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800701 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
703 OpUnconditionalBranch(resume_lab);
704 }
705}
706
Vladimir Markobe0e5462014-02-26 11:24:15 +0000707void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700709 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000710 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
711 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
712 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 RegLocation rl_result;
714 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000715 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 rl_obj = LoadValue(rl_obj, kCoreReg);
717 if (is_long_or_double) {
718 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800719 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700720 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700721 RegisterClass result_reg_kind = kAnyReg;
722 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
723 // Force long/double volatile loads into SSE registers to avoid tearing.
724 result_reg_kind = kFPReg;
725 }
726 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800727 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
728 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800729 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000730 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800731 // Without context sensitive analysis, we must issue the most conservative barriers.
732 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800734 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 }
736 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800737 RegStorage reg_ptr = AllocTemp();
738 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700739 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800740 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700741 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000742 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800743 // Without context sensitive analysis, we must issue the most conservative barriers.
744 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800746 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 }
748 FreeTemp(reg_ptr);
749 }
750 StoreValueWide(rl_dest, rl_result);
751 } else {
752 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800753 GenNullCheck(rl_obj.reg, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700754 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, k32,
buzbee2700f7e2014-03-07 09:46:20 -0800755 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800756 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000757 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800758 // Without context sensitive analysis, we must issue the most conservative barriers.
759 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800761 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 }
763 StoreValue(rl_dest, rl_result);
764 }
765 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700766 ThreadOffset<4> getterOffset =
767 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
768 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
769 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000770 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 if (is_long_or_double) {
772 RegLocation rl_result = GetReturnWide(rl_dest.fp);
773 StoreValueWide(rl_dest, rl_result);
774 } else {
775 RegLocation rl_result = GetReturn(rl_dest.fp);
776 StoreValue(rl_dest, rl_result);
777 }
778 }
779}
780
Vladimir Markobe0e5462014-02-26 11:24:15 +0000781void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700783 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000784 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
785 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
786 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000788 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 rl_obj = LoadValue(rl_obj, kCoreReg);
790 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700791 RegisterClass src_reg_kind = kAnyReg;
792 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
793 // Force long/double volatile stores into SSE registers to avoid tearing.
794 src_reg_kind = kFPReg;
795 }
796 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800797 GenNullCheck(rl_obj.reg, opt_flags);
798 RegStorage reg_ptr = AllocTemp();
799 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000800 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800801 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 GenMemBarrier(kStoreStore);
803 }
buzbee2700f7e2014-03-07 09:46:20 -0800804 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800805 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000806 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800807 // A load might follow the volatile store so insert a StoreLoad barrier.
808 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 }
810 FreeTemp(reg_ptr);
811 } else {
812 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800813 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000814 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800815 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 GenMemBarrier(kStoreStore);
817 }
buzbee695d13a2014-04-19 13:32:20 -0700818 Store32Disp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800819 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000820 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800821 // A load might follow the volatile store so insert a StoreLoad barrier.
822 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 }
824 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800825 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 }
827 }
828 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700829 ThreadOffset<4> setter_offset =
830 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
831 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
832 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000833 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
834 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 }
836}
837
Ian Rogersa9a82542013-10-04 11:17:26 -0700838void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
839 RegLocation rl_src) {
840 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
841 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
842 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700843 ThreadOffset<4> helper = needs_range_check
844 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
845 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
846 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700847 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
848}
849
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700850void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800852 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
854 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
855 *cu_->dex_file,
856 type_idx)) {
857 // Call out to helper which resolves type and verifies access.
858 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700859 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800860 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 RegLocation rl_result = GetReturn(false);
862 StoreValue(rl_dest, rl_result);
863 } else {
864 // We're don't need access checks, load type from dex cache
865 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700866 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700867 Load32Disp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampeb5a14d22014-04-24 17:30:35 +0000868 int32_t offset_of_type =
869 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
870 * type_idx);
buzbee695d13a2014-04-19 13:32:20 -0700871 Load32Disp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
873 type_idx) || SLOW_TYPE_PATH) {
874 // Slow path, at runtime test if type is null and if so initialize
875 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800876 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800877 LIR* cont = NewLIR0(kPseudoTargetLabel);
878
879 // Object to generate the slow path for class resolution.
880 class SlowPath : public LIRSlowPath {
881 public:
882 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
883 const RegLocation& rl_method, const RegLocation& rl_result) :
884 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
885 rl_method_(rl_method), rl_result_(rl_result) {
886 }
887
888 void Compile() {
889 GenerateTargetLabel();
890
Ian Rogersdd7624d2014-03-14 17:43:00 -0700891 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800892 rl_method_.reg, true);
893 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800894
895 m2l_->OpUnconditionalBranch(cont_);
896 }
897
898 private:
899 const int type_idx_;
900 const RegLocation rl_method_;
901 const RegLocation rl_result_;
902 };
903
904 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800905 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800906
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800908 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 // Fast path, we're done - just store result
910 StoreValue(rl_dest, rl_result);
911 }
912 }
913}
914
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700915void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916 /* NOTE: Most strings should be available at compile time */
Andreas Gampeb5a14d22014-04-24 17:30:35 +0000917 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
918 (sizeof(mirror::String*) * string_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
920 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
921 // slow path, resolve string if not in dex cache
922 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700923 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800924
925 // If the Method* is already in a register, we can save a copy.
926 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800927 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800928 if (rl_method.location == kLocPhysReg) {
929 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800930 DCHECK(!IsTemp(rl_method.reg));
931 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800932 } else {
933 r_method = TargetReg(kArg2);
934 LoadCurrMethodDirect(r_method);
935 }
buzbee695d13a2014-04-19 13:32:20 -0700936 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
937 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800938
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 // Might call out to helper, which will return resolved string in kRet0
buzbee695d13a2014-04-19 13:32:20 -0700940 Load32Disp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800941 if (cu_->instruction_set == kThumb2 ||
942 cu_->instruction_set == kMips) {
943 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800944 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800945 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
946 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700947 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800948
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800949 // Object to generate the slow path for string resolution.
950 class SlowPath : public LIRSlowPath {
951 public:
buzbee2700f7e2014-03-07 09:46:20 -0800952 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800953 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
954 }
955
956 void Compile() {
957 GenerateTargetLabel();
958
Dave Allisond6ed6422014-04-09 23:36:15 +0000959 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800960
Dave Allisond6ed6422014-04-09 23:36:15 +0000961 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
962 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
963 m2l_->MarkSafepointPC(call_inst);
964 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800965
966 m2l_->OpUnconditionalBranch(cont_);
967 }
968
969 private:
buzbee2700f7e2014-03-07 09:46:20 -0800970 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800971 };
972
973 // Add to list for future.
974 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700976 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -0800977 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
978 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700979 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -0800980 true);
Mark Mendell766e9292014-01-27 07:55:47 -0800981 LIR* target = NewLIR0(kPseudoTargetLabel);
982 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 }
984 GenBarrier();
985 StoreValue(rl_dest, GetReturn(false));
986 } else {
987 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800988 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700990 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
991 Load32Disp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 StoreValue(rl_dest, rl_result);
993 }
994}
995
996/*
997 * Let helper function take care of everything. Will
998 * call Class::NewInstanceFromCode(type_idx, method);
999 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001000void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 FlushAllRegs(); /* Everything to home location */
1002 // alloc will always check for resolution, do we also need to verify
1003 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001004 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001005 const DexFile* dex_file = cu_->dex_file;
1006 CompilerDriver* driver = cu_->compiler_driver;
1007 if (driver->CanAccessInstantiableTypeWithoutChecks(
1008 cu_->method_idx, *dex_file, type_idx)) {
1009 bool is_type_initialized;
1010 bool use_direct_type_ptr;
1011 uintptr_t direct_type_ptr;
1012 if (kEmbedClassInCode &&
1013 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1014 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1015 // The fast path.
1016 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001017 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001018 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001019 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001020 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1021 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001022 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001023 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1024 }
1025 } else {
1026 // Use the direct pointer.
1027 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001028 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001029 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1030 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001031 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001032 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1033 }
1034 }
1035 } else {
1036 // The slow path.
1037 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001038 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001039 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1040 }
1041 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001043 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001044 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 RegLocation rl_result = GetReturn(false);
1047 StoreValue(rl_dest, rl_result);
1048}
1049
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001050void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001052 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053}
1054
1055// For final classes there are no sub-classes to check and so we can answer the instance-of
1056// question with simple comparisons.
1057void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1058 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001059 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001060 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001061
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 RegLocation object = LoadValue(rl_src, kCoreReg);
1063 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001064 RegStorage result_reg = rl_result.reg;
1065 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 result_reg = AllocTypedTemp(false, kCoreReg);
1067 }
1068 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001069 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070
buzbee2700f7e2014-03-07 09:46:20 -08001071 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1072 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073
1074 LoadCurrMethodDirect(check_class);
1075 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001076 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1077 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 } else {
buzbee695d13a2014-04-19 13:32:20 -07001079 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1080 check_class);
1081 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampeb5a14d22014-04-24 17:30:35 +00001082 int32_t offset_of_type =
1083 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1084 (sizeof(mirror::Class*) * type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001085 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 }
1087
1088 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001089 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 if (cu_->instruction_set == kThumb2) {
1091 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001092 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001094 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 } else {
1096 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1097 LoadConstant(result_reg, 1); // eq case - load true
1098 }
1099 LIR* target = NewLIR0(kPseudoTargetLabel);
1100 null_branchover->target = target;
1101 if (ne_branchover != NULL) {
1102 ne_branchover->target = target;
1103 }
1104 FreeTemp(object_class);
1105 FreeTemp(check_class);
1106 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001107 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 FreeTemp(result_reg);
1109 }
1110 StoreValue(rl_dest, rl_result);
1111}
1112
1113void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1114 bool type_known_abstract, bool use_declaring_class,
1115 bool can_assume_type_is_in_dex_cache,
1116 uint32_t type_idx, RegLocation rl_dest,
1117 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001118 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001119 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001120
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 FlushAllRegs();
1122 // May generate a call - use explicit registers
1123 LockCallTemps();
1124 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001125 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 if (needs_access_check) {
1127 // Check we have access to type_idx and if not throw IllegalAccessError,
1128 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001129 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 type_idx, true);
1131 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1132 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1133 } else if (use_declaring_class) {
1134 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001135 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001136 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 } else {
1138 // Load dex cache entry into class_reg (kArg2)
1139 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001140 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1141 class_reg);
Andreas Gampeb5a14d22014-04-24 17:30:35 +00001142 int32_t offset_of_type =
1143 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1144 * type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001145 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 if (!can_assume_type_is_in_dex_cache) {
1147 // Need to test presence of type in dex cache at runtime
1148 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1149 // Not resolved
1150 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001151 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001152 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1154 // Rejoin code paths
1155 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1156 hop_branch->target = hop_target;
1157 }
1158 }
1159 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1160 RegLocation rl_result = GetReturn(false);
1161 if (cu_->instruction_set == kMips) {
1162 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001163 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 }
1165 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1166
1167 /* load object->klass_ */
1168 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001169 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1171 LIR* branchover = NULL;
1172 if (type_known_final) {
1173 // rl_result == ref == null == 0.
1174 if (cu_->instruction_set == kThumb2) {
1175 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001176 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001177 LoadConstant(rl_result.reg, 1); // .eq case - load true
1178 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001179 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001181 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001183 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 }
1185 } else {
1186 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001187 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001188 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 if (!type_known_abstract) {
1190 /* Uses conditional nullification */
1191 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001192 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1194 }
1195 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1196 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001197 if (it != nullptr) {
1198 OpEndIT(it);
1199 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 FreeTemp(r_tgt);
1201 } else {
1202 if (!type_known_abstract) {
1203 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001204 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1206 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001207 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001208 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1209 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1210 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 }
1212 }
1213 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001214 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 /* branch targets here */
1216 LIR* target = NewLIR0(kPseudoTargetLabel);
1217 StoreValue(rl_dest, rl_result);
1218 branch1->target = target;
1219 if (branchover != NULL) {
1220 branchover->target = target;
1221 }
1222}
1223
1224void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1225 bool type_known_final, type_known_abstract, use_declaring_class;
1226 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1227 *cu_->dex_file,
1228 type_idx,
1229 &type_known_final,
1230 &type_known_abstract,
1231 &use_declaring_class);
1232 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1233 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1234
1235 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1236 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1237 } else {
1238 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1239 use_declaring_class, can_assume_type_is_in_dex_cache,
1240 type_idx, rl_dest, rl_src);
1241 }
1242}
1243
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001244void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 bool type_known_final, type_known_abstract, use_declaring_class;
1246 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1247 *cu_->dex_file,
1248 type_idx,
1249 &type_known_final,
1250 &type_known_abstract,
1251 &use_declaring_class);
1252 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1253 // of the exception throw path.
1254 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001255 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 // Verifier type analysis proved this check cast would never cause an exception.
1257 return;
1258 }
1259 FlushAllRegs();
1260 // May generate a call - use explicit registers
1261 LockCallTemps();
1262 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001263 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 if (needs_access_check) {
1265 // Check we have access to type_idx and if not throw IllegalAccessError,
1266 // returns Class* in kRet0
1267 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001268 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 type_idx, TargetReg(kArg1), true);
1270 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1271 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001272 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1273 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 } else {
1275 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001276 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1277 class_reg);
Andreas Gampeb5a14d22014-04-24 17:30:35 +00001278 int32_t offset_of_type =
1279 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1280 (sizeof(mirror::Class*) * type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001281 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1283 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001284 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1285 LIR* cont = NewLIR0(kPseudoTargetLabel);
1286
1287 // Slow path to initialize the type. Executed if the type is NULL.
1288 class SlowPath : public LIRSlowPath {
1289 public:
1290 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001291 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001292 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1293 class_reg_(class_reg) {
1294 }
1295
1296 void Compile() {
1297 GenerateTargetLabel();
1298
1299 // Call out to helper, which will return resolved type in kArg0
1300 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001301 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001302 m2l_->TargetReg(kArg1), true);
1303 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1304 m2l_->OpUnconditionalBranch(cont_);
1305 }
1306 public:
1307 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001308 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001309 };
1310
buzbee2700f7e2014-03-07 09:46:20 -08001311 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 }
1313 }
1314 // At this point, class_reg (kArg2) has class
1315 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001316
1317 // Slow path for the case where the classes are not equal. In this case we need
1318 // to call a helper function to do the check.
1319 class SlowPath : public LIRSlowPath {
1320 public:
1321 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1322 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1323 }
1324
1325 void Compile() {
1326 GenerateTargetLabel();
1327
1328 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001329 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1330 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001331 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001332 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001333 m2l_->TargetReg(kArg1), true);
1334
1335 m2l_->OpUnconditionalBranch(cont_);
1336 }
1337
1338 private:
1339 bool load_;
1340 };
1341
1342 if (type_known_abstract) {
1343 // Easier case, run slow path if target is non-null (slow path will load from target)
1344 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1345 LIR* cont = NewLIR0(kPseudoTargetLabel);
1346 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1347 } else {
1348 // Harder, more common case. We need to generate a forward branch over the load
1349 // if the target is null. If it's non-null we perform the load and branch to the
1350 // slow path if the classes are not equal.
1351
1352 /* Null is OK - continue */
1353 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1354 /* load object->klass_ */
1355 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001356 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001357
1358 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1359 LIR* cont = NewLIR0(kPseudoTargetLabel);
1360
1361 // Add the slow path that will not perform load since this is already done.
1362 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1363
1364 // Set the null check to branch to the continuation.
1365 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 }
1367}
1368
1369void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001370 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 RegLocation rl_result;
1372 if (cu_->instruction_set == kThumb2) {
1373 /*
1374 * NOTE: This is the one place in the code in which we might have
1375 * as many as six live temporary registers. There are 5 in the normal
1376 * set for Arm. Until we have spill capabilities, temporarily add
1377 * lr to the temp set. It is safe to do this locally, but note that
1378 * lr is used explicitly elsewhere in the code generator and cannot
1379 * normally be used as a general temp register.
1380 */
1381 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1382 FreeTemp(TargetReg(kLr)); // and make it available
1383 }
1384 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1385 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1386 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1387 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001388 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1389 RegStorage t_reg = AllocTemp();
1390 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1391 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1392 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393 FreeTemp(t_reg);
1394 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001395 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1396 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 }
1398 /*
1399 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1400 * following StoreValueWide might need to allocate a temp register.
1401 * To further work around the lack of a spill capability, explicitly
1402 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1403 * Remove when spill is functional.
1404 */
1405 FreeRegLocTemps(rl_result, rl_src1);
1406 FreeRegLocTemps(rl_result, rl_src2);
1407 StoreValueWide(rl_dest, rl_result);
1408 if (cu_->instruction_set == kThumb2) {
1409 Clobber(TargetReg(kLr));
1410 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1411 }
1412}
1413
1414
1415void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001416 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001417 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001418
1419 switch (opcode) {
1420 case Instruction::SHL_LONG:
1421 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001422 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001423 break;
1424 case Instruction::SHR_LONG:
1425 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001426 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001427 break;
1428 case Instruction::USHR_LONG:
1429 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001430 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431 break;
1432 default:
1433 LOG(FATAL) << "Unexpected case";
1434 }
1435 FlushAllRegs(); /* Send everything to home location */
1436 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1437 RegLocation rl_result = GetReturnWide(false);
1438 StoreValueWide(rl_dest, rl_result);
1439}
1440
1441
1442void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001443 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001444 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 OpKind op = kOpBkpt;
1446 bool is_div_rem = false;
1447 bool check_zero = false;
1448 bool unary = false;
1449 RegLocation rl_result;
1450 bool shift_op = false;
1451 switch (opcode) {
1452 case Instruction::NEG_INT:
1453 op = kOpNeg;
1454 unary = true;
1455 break;
1456 case Instruction::NOT_INT:
1457 op = kOpMvn;
1458 unary = true;
1459 break;
1460 case Instruction::ADD_INT:
1461 case Instruction::ADD_INT_2ADDR:
1462 op = kOpAdd;
1463 break;
1464 case Instruction::SUB_INT:
1465 case Instruction::SUB_INT_2ADDR:
1466 op = kOpSub;
1467 break;
1468 case Instruction::MUL_INT:
1469 case Instruction::MUL_INT_2ADDR:
1470 op = kOpMul;
1471 break;
1472 case Instruction::DIV_INT:
1473 case Instruction::DIV_INT_2ADDR:
1474 check_zero = true;
1475 op = kOpDiv;
1476 is_div_rem = true;
1477 break;
1478 /* NOTE: returns in kArg1 */
1479 case Instruction::REM_INT:
1480 case Instruction::REM_INT_2ADDR:
1481 check_zero = true;
1482 op = kOpRem;
1483 is_div_rem = true;
1484 break;
1485 case Instruction::AND_INT:
1486 case Instruction::AND_INT_2ADDR:
1487 op = kOpAnd;
1488 break;
1489 case Instruction::OR_INT:
1490 case Instruction::OR_INT_2ADDR:
1491 op = kOpOr;
1492 break;
1493 case Instruction::XOR_INT:
1494 case Instruction::XOR_INT_2ADDR:
1495 op = kOpXor;
1496 break;
1497 case Instruction::SHL_INT:
1498 case Instruction::SHL_INT_2ADDR:
1499 shift_op = true;
1500 op = kOpLsl;
1501 break;
1502 case Instruction::SHR_INT:
1503 case Instruction::SHR_INT_2ADDR:
1504 shift_op = true;
1505 op = kOpAsr;
1506 break;
1507 case Instruction::USHR_INT:
1508 case Instruction::USHR_INT_2ADDR:
1509 shift_op = true;
1510 op = kOpLsr;
1511 break;
1512 default:
1513 LOG(FATAL) << "Invalid word arith op: " << opcode;
1514 }
1515 if (!is_div_rem) {
1516 if (unary) {
1517 rl_src1 = LoadValue(rl_src1, kCoreReg);
1518 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001519 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 } else {
1521 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001522 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001523 RegStorage t_reg = AllocTemp();
1524 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 rl_src1 = LoadValue(rl_src1, kCoreReg);
1526 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001527 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 FreeTemp(t_reg);
1529 } else {
1530 rl_src1 = LoadValue(rl_src1, kCoreReg);
1531 rl_src2 = LoadValue(rl_src2, kCoreReg);
1532 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001533 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001534 }
1535 }
1536 StoreValue(rl_dest, rl_result);
1537 } else {
Dave Allison70202782013-10-22 17:52:19 -07001538 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 -07001539 if (cu_->instruction_set == kMips) {
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);
Brian Carlstrom7940e442013-07-12 13:46:57 -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 } else if (cu_->instruction_set == kThumb2) {
1548 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1549 // Use ARM SDIV instruction for division. For remainder we also need to
1550 // calculate using a MUL and subtract.
1551 rl_src1 = LoadValue(rl_src1, kCoreReg);
1552 rl_src2 = LoadValue(rl_src2, kCoreReg);
1553 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001554 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001555 }
buzbee2700f7e2014-03-07 09:46:20 -08001556 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001557 done = true;
1558 }
1559 }
1560
1561 // If we haven't already generated the code use the callout function.
1562 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001563 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 FlushAllRegs(); /* Send everything to home location */
1565 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001566 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001567 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1568 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001569 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 }
Dave Allison70202782013-10-22 17:52:19 -07001571 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001572 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573 if (op == kOpDiv)
1574 rl_result = GetReturn(false);
1575 else
1576 rl_result = GetReturnAlt();
1577 }
1578 StoreValue(rl_dest, rl_result);
1579 }
1580}
1581
1582/*
1583 * The following are the first-level codegen routines that analyze the format
1584 * of each bytecode then either dispatch special purpose codegen routines
1585 * or produce corresponding Thumb instructions directly.
1586 */
1587
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001589static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 x &= x - 1;
1591 return (x & (x - 1)) == 0;
1592}
1593
Brian Carlstrom7940e442013-07-12 13:46:57 -07001594// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1595// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001596bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001597 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001598 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1599 return false;
1600 }
1601 // No divide instruction for Arm, so check for more special cases
1602 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001603 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 }
1605 int k = LowestSetBit(lit);
1606 if (k >= 30) {
1607 // Avoid special cases.
1608 return false;
1609 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 rl_src = LoadValue(rl_src, kCoreReg);
1611 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001612 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001613 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 if (lit == 2) {
1615 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001616 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1617 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1618 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001620 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001622 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1623 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 }
1625 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001626 RegStorage t_reg1 = AllocTemp();
1627 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001629 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1630 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001632 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001634 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001636 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001638 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 }
1640 }
1641 StoreValue(rl_dest, rl_result);
1642 return true;
1643}
1644
1645// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1646// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001647bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001648 if (lit < 0) {
1649 return false;
1650 }
1651 if (lit == 0) {
1652 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1653 LoadConstant(rl_result.reg, 0);
1654 StoreValue(rl_dest, rl_result);
1655 return true;
1656 }
1657 if (lit == 1) {
1658 rl_src = LoadValue(rl_src, kCoreReg);
1659 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1660 OpRegCopy(rl_result.reg, rl_src.reg);
1661 StoreValue(rl_dest, rl_result);
1662 return true;
1663 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001664 // There is RegRegRegShift on Arm, so check for more special cases
1665 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001666 return EasyMultiply(rl_src, rl_dest, lit);
1667 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 // Can we simplify this multiplication?
1669 bool power_of_two = false;
1670 bool pop_count_le2 = false;
1671 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001672 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 power_of_two = true;
1674 } else if (IsPopCountLE2(lit)) {
1675 pop_count_le2 = true;
1676 } else if (IsPowerOfTwo(lit + 1)) {
1677 power_of_two_minus_one = true;
1678 } else {
1679 return false;
1680 }
1681 rl_src = LoadValue(rl_src, kCoreReg);
1682 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1683 if (power_of_two) {
1684 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001685 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 } else if (pop_count_le2) {
1687 // Shift and add and shift.
1688 int first_bit = LowestSetBit(lit);
1689 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1690 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1691 } else {
1692 // Reverse subtract: (src << (shift + 1)) - src.
1693 DCHECK(power_of_two_minus_one);
1694 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001695 RegStorage t_reg = AllocTemp();
1696 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1697 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001698 }
1699 StoreValue(rl_dest, rl_result);
1700 return true;
1701}
1702
1703void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001704 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 RegLocation rl_result;
1706 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1707 int shift_op = false;
1708 bool is_div = false;
1709
1710 switch (opcode) {
1711 case Instruction::RSUB_INT_LIT8:
1712 case Instruction::RSUB_INT: {
1713 rl_src = LoadValue(rl_src, kCoreReg);
1714 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1715 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001716 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001718 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1719 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 }
1721 StoreValue(rl_dest, rl_result);
1722 return;
1723 }
1724
1725 case Instruction::SUB_INT:
1726 case Instruction::SUB_INT_2ADDR:
1727 lit = -lit;
1728 // Intended fallthrough
1729 case Instruction::ADD_INT:
1730 case Instruction::ADD_INT_2ADDR:
1731 case Instruction::ADD_INT_LIT8:
1732 case Instruction::ADD_INT_LIT16:
1733 op = kOpAdd;
1734 break;
1735 case Instruction::MUL_INT:
1736 case Instruction::MUL_INT_2ADDR:
1737 case Instruction::MUL_INT_LIT8:
1738 case Instruction::MUL_INT_LIT16: {
1739 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1740 return;
1741 }
1742 op = kOpMul;
1743 break;
1744 }
1745 case Instruction::AND_INT:
1746 case Instruction::AND_INT_2ADDR:
1747 case Instruction::AND_INT_LIT8:
1748 case Instruction::AND_INT_LIT16:
1749 op = kOpAnd;
1750 break;
1751 case Instruction::OR_INT:
1752 case Instruction::OR_INT_2ADDR:
1753 case Instruction::OR_INT_LIT8:
1754 case Instruction::OR_INT_LIT16:
1755 op = kOpOr;
1756 break;
1757 case Instruction::XOR_INT:
1758 case Instruction::XOR_INT_2ADDR:
1759 case Instruction::XOR_INT_LIT8:
1760 case Instruction::XOR_INT_LIT16:
1761 op = kOpXor;
1762 break;
1763 case Instruction::SHL_INT_LIT8:
1764 case Instruction::SHL_INT:
1765 case Instruction::SHL_INT_2ADDR:
1766 lit &= 31;
1767 shift_op = true;
1768 op = kOpLsl;
1769 break;
1770 case Instruction::SHR_INT_LIT8:
1771 case Instruction::SHR_INT:
1772 case Instruction::SHR_INT_2ADDR:
1773 lit &= 31;
1774 shift_op = true;
1775 op = kOpAsr;
1776 break;
1777 case Instruction::USHR_INT_LIT8:
1778 case Instruction::USHR_INT:
1779 case Instruction::USHR_INT_2ADDR:
1780 lit &= 31;
1781 shift_op = true;
1782 op = kOpLsr;
1783 break;
1784
1785 case Instruction::DIV_INT:
1786 case Instruction::DIV_INT_2ADDR:
1787 case Instruction::DIV_INT_LIT8:
1788 case Instruction::DIV_INT_LIT16:
1789 case Instruction::REM_INT:
1790 case Instruction::REM_INT_2ADDR:
1791 case Instruction::REM_INT_LIT8:
1792 case Instruction::REM_INT_LIT16: {
1793 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001794 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001795 return;
1796 }
buzbee11b63d12013-08-27 07:34:17 -07001797 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001798 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001799 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001800 (opcode == Instruction::DIV_INT_LIT16)) {
1801 is_div = true;
1802 } else {
1803 is_div = false;
1804 }
buzbee11b63d12013-08-27 07:34:17 -07001805 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1806 return;
1807 }
Dave Allison70202782013-10-22 17:52:19 -07001808
1809 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001810 if (cu_->instruction_set == kMips) {
1811 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001812 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001813 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001814 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001815 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1816 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001817 } else if (cu_->instruction_set == kThumb2) {
1818 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1819 // Use ARM SDIV instruction for division. For remainder we also need to
1820 // calculate using a MUL and subtract.
1821 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001822 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001823 done = true;
1824 }
1825 }
1826
1827 if (!done) {
1828 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001829 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1830 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001831 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001832 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1833 if (is_div)
1834 rl_result = GetReturn(false);
1835 else
1836 rl_result = GetReturnAlt();
1837 }
1838 StoreValue(rl_dest, rl_result);
1839 return;
1840 }
1841 default:
1842 LOG(FATAL) << "Unexpected opcode " << opcode;
1843 }
1844 rl_src = LoadValue(rl_src, kCoreReg);
1845 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001846 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001847 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001848 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001849 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001850 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001851 }
1852 StoreValue(rl_dest, rl_result);
1853}
1854
1855void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001856 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001857 RegLocation rl_result;
1858 OpKind first_op = kOpBkpt;
1859 OpKind second_op = kOpBkpt;
1860 bool call_out = false;
1861 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001862 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001863 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001864
1865 switch (opcode) {
1866 case Instruction::NOT_LONG:
1867 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1868 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1869 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001870 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1871 RegStorage t_reg = AllocTemp();
1872 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1873 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1874 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001875 FreeTemp(t_reg);
1876 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001877 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1878 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001879 }
1880 StoreValueWide(rl_dest, rl_result);
1881 return;
1882 case Instruction::ADD_LONG:
1883 case Instruction::ADD_LONG_2ADDR:
1884 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001885 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001886 return;
1887 }
1888 first_op = kOpAdd;
1889 second_op = kOpAdc;
1890 break;
1891 case Instruction::SUB_LONG:
1892 case Instruction::SUB_LONG_2ADDR:
1893 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001894 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 return;
1896 }
1897 first_op = kOpSub;
1898 second_op = kOpSbc;
1899 break;
1900 case Instruction::MUL_LONG:
1901 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001902 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001903 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001904 return;
1905 } else {
1906 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001907 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001908 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001909 }
1910 break;
1911 case Instruction::DIV_LONG:
1912 case Instruction::DIV_LONG_2ADDR:
1913 call_out = true;
1914 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001915 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001916 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001917 break;
1918 case Instruction::REM_LONG:
1919 case Instruction::REM_LONG_2ADDR:
1920 call_out = true;
1921 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001922 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001924 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001925 break;
1926 case Instruction::AND_LONG_2ADDR:
1927 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001928 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001929 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001930 }
1931 first_op = kOpAnd;
1932 second_op = kOpAnd;
1933 break;
1934 case Instruction::OR_LONG:
1935 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001936 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001937 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 return;
1939 }
1940 first_op = kOpOr;
1941 second_op = kOpOr;
1942 break;
1943 case Instruction::XOR_LONG:
1944 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001945 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001946 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001947 return;
1948 }
1949 first_op = kOpXor;
1950 second_op = kOpXor;
1951 break;
1952 case Instruction::NEG_LONG: {
1953 GenNegLong(rl_dest, rl_src2);
1954 return;
1955 }
1956 default:
1957 LOG(FATAL) << "Invalid long arith op";
1958 }
1959 if (!call_out) {
1960 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1961 } else {
1962 FlushAllRegs(); /* Send everything to home location */
1963 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001964 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1965 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1966 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1967 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07001968 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08001969 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001970 // NOTE: callout here is not a safepoint
1971 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1972 } else {
1973 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1974 }
1975 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001976 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001977 rl_result = GetReturnWide(false);
1978 else
1979 rl_result = GetReturnWideAlt();
1980 StoreValueWide(rl_dest, rl_result);
1981 }
1982}
1983
Ian Rogersdd7624d2014-03-14 17:43:00 -07001984void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001985 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001986 /*
1987 * Don't optimize the register usage since it calls out to support
1988 * functions
1989 */
1990 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001991 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1992 if (rl_dest.wide) {
1993 RegLocation rl_result;
1994 rl_result = GetReturnWide(rl_dest.fp);
1995 StoreValueWide(rl_dest, rl_result);
1996 } else {
1997 RegLocation rl_result;
1998 rl_result = GetReturn(rl_dest.fp);
1999 StoreValue(rl_dest, rl_result);
2000 }
2001}
2002
2003/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002004void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002005 if (Runtime::Current()->ExplicitSuspendChecks()) {
2006 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2007 return;
2008 }
2009 FlushAllRegs();
2010 LIR* branch = OpTestSuspend(NULL);
2011 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2012 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2013 current_dalvik_offset_);
2014 branch->target = target;
2015 suspend_launchpads_.Insert(target);
2016 } else {
2017 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2018 return;
2019 }
2020 FlushAllRegs(); // TODO: needed?
2021 LIR* inst = CheckSuspendUsingLoad();
2022 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002023 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002024}
2025
2026/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002027void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002028 if (Runtime::Current()->ExplicitSuspendChecks()) {
2029 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2030 OpUnconditionalBranch(target);
2031 return;
2032 }
2033 OpTestSuspend(target);
2034 LIR* launch_pad =
2035 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2036 current_dalvik_offset_);
2037 FlushAllRegs();
2038 OpUnconditionalBranch(launch_pad);
2039 suspend_launchpads_.Insert(launch_pad);
2040 } else {
2041 // For the implicit suspend check, just perform the trigger
2042 // load and branch to the target.
2043 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2044 OpUnconditionalBranch(target);
2045 return;
2046 }
2047 FlushAllRegs();
2048 LIR* inst = CheckSuspendUsingLoad();
2049 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002050 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002052}
2053
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002054/* Call out to helper assembly routine that will null check obj and then lock it. */
2055void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2056 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002057 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002058}
2059
2060/* Call out to helper assembly routine that will null check obj and then unlock it. */
2061void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2062 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002063 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002064}
2065
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002066/* Generic code for generating a wide constant into a VR. */
2067void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2068 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002069 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002070 StoreValueWide(rl_dest, rl_result);
2071}
2072
Brian Carlstrom7940e442013-07-12 13:46:57 -07002073} // namespace art