blob: 70f2ba8bc9761855d6a7831b4174b581e06706a2 [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"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000022#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080023#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080025#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27namespace art {
28
Andreas Gampe9c3b0892014-04-24 17:33:34 +000029// Shortcuts to repeatedly used long types.
30typedef mirror::ObjectArray<mirror::Object> ObjArray;
31typedef mirror::ObjectArray<mirror::Class> ClassArray;
32
Brian Carlstrom7940e442013-07-12 13:46:57 -070033/*
34 * This source files contains "gen" codegen routines that should
35 * be applicable to most targets. Only mid-level support utilities
36 * and "op" calls may be used here.
37 */
38
39/*
buzbeeb48819d2013-09-14 16:15:25 -070040 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070041 * blocks.
42 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070043void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 LIR* barrier = NewLIR0(kPseudoBarrier);
45 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070046 DCHECK(!barrier->flags.use_def_invalid);
47 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070048}
49
Mingyao Yange643a172014-04-08 11:02:52 -070050void Mir2Lir::GenDivZeroException() {
51 LIR* branch = OpUnconditionalBranch(nullptr);
52 AddDivZeroCheckSlowPath(branch);
53}
54
55void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070056 LIR* branch = OpCondBranch(c_code, nullptr);
57 AddDivZeroCheckSlowPath(branch);
58}
59
Mingyao Yange643a172014-04-08 11:02:52 -070060void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
61 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070062 AddDivZeroCheckSlowPath(branch);
63}
64
65void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
66 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
67 public:
68 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
69 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
70 }
71
Mingyao Yange643a172014-04-08 11:02:52 -070072 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070073 m2l_->ResetRegPool();
74 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070075 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang42894562014-04-07 12:42:16 -070076 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
77 }
78 };
79
80 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
81}
Dave Allisonb373e092014-02-20 16:06:36 -080082
Mingyao Yang80365d92014-04-18 12:10:58 -070083void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
84 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
85 public:
86 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, RegStorage index, RegStorage length)
87 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
88 index_(index), length_(length) {
89 }
90
91 void Compile() OVERRIDE {
92 m2l_->ResetRegPool();
93 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070094 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -070095 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
96 index_, length_, true);
97 }
98
99 private:
100 const RegStorage index_;
101 const RegStorage length_;
102 };
103
104 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
105 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
106}
107
108void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
109 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
110 public:
111 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, int index, RegStorage length)
112 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
113 index_(index), length_(length) {
114 }
115
116 void Compile() OVERRIDE {
117 m2l_->ResetRegPool();
118 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700119 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700120
121 m2l_->OpRegCopy(m2l_->TargetReg(kArg1), length_);
122 m2l_->LoadConstant(m2l_->TargetReg(kArg0), index_);
123 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
124 m2l_->TargetReg(kArg0), m2l_->TargetReg(kArg1), true);
125 }
126
127 private:
128 const int32_t index_;
129 const RegStorage length_;
130 };
131
132 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
133 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
134}
135
Mingyao Yange643a172014-04-08 11:02:52 -0700136LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
137 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
138 public:
139 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
140 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
141 }
142
143 void Compile() OVERRIDE {
144 m2l_->ResetRegPool();
145 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700146 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yange643a172014-04-08 11:02:52 -0700147 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
148 }
149 };
150
151 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
152 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
153 return branch;
154}
155
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800157LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800158 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700159 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 }
Dave Allisonb373e092014-02-20 16:06:36 -0800161 return nullptr;
162}
163
Dave Allisonf9439142014-03-27 15:10:22 -0700164/* Perform an explicit null-check on a register. */
165LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
166 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
167 return NULL;
168 }
Mingyao Yange643a172014-04-08 11:02:52 -0700169 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700170}
171
Dave Allisonb373e092014-02-20 16:06:36 -0800172void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
173 if (!Runtime::Current()->ExplicitNullChecks()) {
174 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
175 return;
176 }
177 MarkSafepointPC(last_lir_insn_);
178 }
179}
180
181void Mir2Lir::MarkPossibleStackOverflowException() {
182 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
183 MarkSafepointPC(last_lir_insn_);
184 }
185}
186
buzbee2700f7e2014-03-07 09:46:20 -0800187void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800188 if (!Runtime::Current()->ExplicitNullChecks()) {
189 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
190 return;
191 }
192 // Force an implicit null check by performing a memory operation (load) from the given
193 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800194 RegStorage tmp = AllocTemp();
195 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700196 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800197 FreeTemp(tmp);
198 MarkSafepointPC(load);
199 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200}
201
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
203 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700204 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 ConditionCode cond;
206 switch (opcode) {
207 case Instruction::IF_EQ:
208 cond = kCondEq;
209 break;
210 case Instruction::IF_NE:
211 cond = kCondNe;
212 break;
213 case Instruction::IF_LT:
214 cond = kCondLt;
215 break;
216 case Instruction::IF_GE:
217 cond = kCondGe;
218 break;
219 case Instruction::IF_GT:
220 cond = kCondGt;
221 break;
222 case Instruction::IF_LE:
223 cond = kCondLe;
224 break;
225 default:
226 cond = static_cast<ConditionCode>(0);
227 LOG(FATAL) << "Unexpected opcode " << opcode;
228 }
229
230 // Normalize such that if either operand is constant, src2 will be constant
231 if (rl_src1.is_const) {
232 RegLocation rl_temp = rl_src1;
233 rl_src1 = rl_src2;
234 rl_src2 = rl_temp;
235 cond = FlipComparisonOrder(cond);
236 }
237
238 rl_src1 = LoadValue(rl_src1, kCoreReg);
239 // Is this really an immediate comparison?
240 if (rl_src2.is_const) {
241 // If it's already live in a register or not easily materialized, just keep going
242 RegLocation rl_temp = UpdateLoc(rl_src2);
243 if ((rl_temp.location == kLocDalvikFrame) &&
244 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
245 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800246 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 return;
248 }
249 }
250 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800251 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252}
253
254void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700255 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 ConditionCode cond;
257 rl_src = LoadValue(rl_src, kCoreReg);
258 switch (opcode) {
259 case Instruction::IF_EQZ:
260 cond = kCondEq;
261 break;
262 case Instruction::IF_NEZ:
263 cond = kCondNe;
264 break;
265 case Instruction::IF_LTZ:
266 cond = kCondLt;
267 break;
268 case Instruction::IF_GEZ:
269 cond = kCondGe;
270 break;
271 case Instruction::IF_GTZ:
272 cond = kCondGt;
273 break;
274 case Instruction::IF_LEZ:
275 cond = kCondLe;
276 break;
277 default:
278 cond = static_cast<ConditionCode>(0);
279 LOG(FATAL) << "Unexpected opcode " << opcode;
280 }
buzbee2700f7e2014-03-07 09:46:20 -0800281 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282}
283
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700284void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
286 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800287 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800289 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 }
buzbee2700f7e2014-03-07 09:46:20 -0800291 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 StoreValueWide(rl_dest, rl_result);
293}
294
295void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700296 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700297 rl_src = LoadValue(rl_src, kCoreReg);
298 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
299 OpKind op = kOpInvalid;
300 switch (opcode) {
301 case Instruction::INT_TO_BYTE:
302 op = kOp2Byte;
303 break;
304 case Instruction::INT_TO_SHORT:
305 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700307 case Instruction::INT_TO_CHAR:
308 op = kOp2Char;
309 break;
310 default:
311 LOG(ERROR) << "Bad int conversion type";
312 }
buzbee2700f7e2014-03-07 09:46:20 -0800313 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700314 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315}
316
317/*
318 * Let helper function take care of everything. Will call
319 * Array::AllocFromCode(type_idx, method, count);
320 * Note: AllocFromCode will handle checks for errNegativeArraySize.
321 */
322void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700323 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700325 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800326 const DexFile* dex_file = cu_->dex_file;
327 CompilerDriver* driver = cu_->compiler_driver;
328 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800330 bool is_type_initialized; // Ignored as an array does not have an initializer.
331 bool use_direct_type_ptr;
332 uintptr_t direct_type_ptr;
333 if (kEmbedClassInCode &&
334 driver->CanEmbedTypeInCode(*dex_file, type_idx,
335 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
336 // The fast path.
337 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800338 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700339 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800340 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
341 } else {
342 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700343 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800344 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
345 }
346 } else {
347 // The slow path.
348 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700349 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800350 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
351 }
352 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700354 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800355 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 RegLocation rl_result = GetReturn(false);
358 StoreValue(rl_dest, rl_result);
359}
360
361/*
362 * Similar to GenNewArray, but with post-allocation initialization.
363 * Verifier guarantees we're dealing with an array class. Current
364 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
365 * Current code also throws internal unimp if not 'L', '[' or 'I'.
366 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700367void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 int elems = info->num_arg_words;
369 int type_idx = info->index;
370 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700371 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
373 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700374 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700376 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 }
378 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
379 FreeTemp(TargetReg(kArg2));
380 FreeTemp(TargetReg(kArg1));
381 /*
382 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
383 * return region. Because AllocFromCode placed the new array
384 * in kRet0, we'll just lock it into place. When debugger support is
385 * added, it may be necessary to additionally copy all return
386 * values to a home location in thread-local storage
387 */
388 LockTemp(TargetReg(kRet0));
389
390 // TODO: use the correct component size, currently all supported types
391 // share array alignment with ints (see comment at head of function)
392 size_t component_size = sizeof(int32_t);
393
394 // Having a range of 0 is legal
395 if (info->is_range && (elems > 0)) {
396 /*
397 * Bit of ugliness here. We're going generate a mem copy loop
398 * on the register range, but it is possible that some regs
399 * in the range have been promoted. This is unlikely, but
400 * before generating the copy, we'll just force a flush
401 * of any regs in the source range that have been promoted to
402 * home location.
403 */
404 for (int i = 0; i < elems; i++) {
405 RegLocation loc = UpdateLoc(info->args[i]);
406 if (loc.location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700407 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 }
409 }
410 /*
411 * TUNING note: generated code here could be much improved, but
412 * this is an uncommon operation and isn't especially performance
413 * critical.
414 */
buzbee2700f7e2014-03-07 09:46:20 -0800415 RegStorage r_src = AllocTemp();
416 RegStorage r_dst = AllocTemp();
417 RegStorage r_idx = AllocTemp();
418 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700419 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 case kThumb2:
421 r_val = TargetReg(kLr);
422 break;
423 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700424 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 FreeTemp(TargetReg(kRet0));
426 r_val = AllocTemp();
427 break;
428 case kMips:
429 r_val = AllocTemp();
430 break;
431 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
432 }
433 // Set up source pointer
434 RegLocation rl_first = info->args[0];
435 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
436 // Set up the target pointer
437 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
438 mirror::Array::DataOffset(component_size).Int32Value());
439 // Set up the loop counter (known to be > 0)
440 LoadConstant(r_idx, elems - 1);
441 // Generate the copy loop. Going backwards for convenience
442 LIR* target = NewLIR0(kPseudoTargetLabel);
443 // Copy next element
buzbee695d13a2014-04-19 13:32:20 -0700444 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
445 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 FreeTemp(r_val);
447 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700448 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 // Restore the target pointer
450 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
451 -mirror::Array::DataOffset(component_size).Int32Value());
452 }
453 } else if (!info->is_range) {
454 // TUNING: interleave
455 for (int i = 0; i < elems; i++) {
456 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700457 Store32Disp(TargetReg(kRet0),
458 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800460 if (IsTemp(rl_arg.reg)) {
461 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 }
463 }
464 }
465 if (info->result.location != kLocInvalid) {
466 StoreValue(info->result, GetReturn(false /* not fp */));
467 }
468}
469
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800470//
471// Slow path to ensure a class is initialized for sget/sput.
472//
473class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
474 public:
buzbee2700f7e2014-03-07 09:46:20 -0800475 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
476 RegStorage r_base) :
477 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
478 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800479 }
480
481 void Compile() {
482 LIR* unresolved_target = GenerateTargetLabel();
483 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700484 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800485 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800486 // Copy helper's result into r_base, a no-op on all but MIPS.
487 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
488
489 m2l_->OpUnconditionalBranch(cont_);
490 }
491
492 private:
493 LIR* const uninit_;
494 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800495 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800496};
497
Vladimir Markobe0e5462014-02-26 11:24:15 +0000498void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700499 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000500 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
501 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
502 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
503 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800504 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000505 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 // Fast path, static storage base is this method's class
507 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800508 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700509 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800510 if (IsTemp(rl_method.reg)) {
511 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 }
513 } else {
514 // Medium path, static storage base in a different class which requires checks that the other
515 // class is initialized.
516 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000517 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 // May do runtime call so everything to home locations.
519 FlushAllRegs();
520 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800521 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 LockTemp(r_method);
523 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800524 r_base = TargetReg(kArg0);
525 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700526 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000527 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
528 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800529 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000530 if (!field_info.IsInitialized() &&
531 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800532 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800533
534 // The slow path is invoked if the r_base is NULL or the class pointed
535 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800536 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800537 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800538 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800539 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800540 mirror::Class::StatusOffset().Int32Value(),
541 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800542 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800543
buzbee2700f7e2014-03-07 09:46:20 -0800544 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000545 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800546
547 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 FreeTemp(r_method);
550 }
551 // rBase now holds static storage base
552 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700553 RegisterClass register_kind = kAnyReg;
554 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
555 // Force long/double volatile stores into SSE registers to avoid tearing.
556 register_kind = kFPReg;
557 }
558 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 } else {
560 rl_src = LoadValue(rl_src, kAnyReg);
561 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000562 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800563 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 GenMemBarrier(kStoreStore);
565 }
566 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800567 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
buzbee695d13a2014-04-19 13:32:20 -0700568 } else if (rl_src.ref) {
569 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 } else {
buzbee695d13a2014-04-19 13:32:20 -0700571 Store32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000573 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800574 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 GenMemBarrier(kStoreLoad);
576 }
577 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800578 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800580 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 } else {
582 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700583 ThreadOffset<4> setter_offset =
584 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
585 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
586 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000587 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588 }
589}
590
Vladimir Markobe0e5462014-02-26 11:24:15 +0000591void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700592 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000593 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
594 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
595 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
596 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800597 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000598 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 // Fast path, static storage base is this method's class
600 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800601 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700602 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 } else {
604 // Medium path, static storage base in a different class which requires checks that the other
605 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000606 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 // May do runtime call so everything to home locations.
608 FlushAllRegs();
609 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800610 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 LockTemp(r_method);
612 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800613 r_base = TargetReg(kArg0);
614 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700615 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000616 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
617 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800618 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000619 if (!field_info.IsInitialized() &&
620 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800621 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800622
623 // The slow path is invoked if the r_base is NULL or the class pointed
624 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800625 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800626 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800627 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800628 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800629 mirror::Class::StatusOffset().Int32Value(),
630 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800631 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800632
buzbee2700f7e2014-03-07 09:46:20 -0800633 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000634 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800635
636 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 FreeTemp(r_method);
639 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800640 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700641 RegisterClass result_reg_kind = kAnyReg;
642 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
643 // Force long/double volatile loads into SSE registers to avoid tearing.
644 result_reg_kind = kFPReg;
645 }
646 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800647
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800649 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
buzbee695d13a2014-04-19 13:32:20 -0700650 } else if (rl_result.ref) {
651 LoadRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 } else {
buzbee695d13a2014-04-19 13:32:20 -0700653 Load32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800655 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800656
657 if (field_info.IsVolatile()) {
658 // Without context sensitive analysis, we must issue the most conservative barriers.
659 // In this case, either a load or store may follow so we issue both barriers.
660 GenMemBarrier(kLoadLoad);
661 GenMemBarrier(kLoadStore);
662 }
663
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 if (is_long_or_double) {
665 StoreValueWide(rl_dest, rl_result);
666 } else {
667 StoreValue(rl_dest, rl_result);
668 }
669 } else {
670 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700671 ThreadOffset<4> getterOffset =
672 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
673 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
674 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000675 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 if (is_long_or_double) {
677 RegLocation rl_result = GetReturnWide(rl_dest.fp);
678 StoreValueWide(rl_dest, rl_result);
679 } else {
680 RegLocation rl_result = GetReturn(rl_dest.fp);
681 StoreValue(rl_dest, rl_result);
682 }
683 }
684}
685
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800686// Generate code for all slow paths.
687void Mir2Lir::HandleSlowPaths() {
688 int n = slow_paths_.Size();
689 for (int i = 0; i < n; ++i) {
690 LIRSlowPath* slowpath = slow_paths_.Get(i);
691 slowpath->Compile();
692 }
693 slow_paths_.Reset();
694}
695
Vladimir Markobe0e5462014-02-26 11:24:15 +0000696void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700698 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000699 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
700 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
701 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 RegLocation rl_result;
703 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000704 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 rl_obj = LoadValue(rl_obj, kCoreReg);
706 if (is_long_or_double) {
707 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800708 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700709 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700710 RegisterClass result_reg_kind = kAnyReg;
711 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
712 // Force long/double volatile loads into SSE registers to avoid tearing.
713 result_reg_kind = kFPReg;
714 }
715 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800716 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
717 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800718 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000719 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800720 // Without context sensitive analysis, we must issue the most conservative barriers.
721 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800723 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 }
725 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800726 RegStorage reg_ptr = AllocTemp();
727 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800729 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700730 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000731 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800732 // Without context sensitive analysis, we must issue the most conservative barriers.
733 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800735 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 }
737 FreeTemp(reg_ptr);
738 }
739 StoreValueWide(rl_dest, rl_result);
740 } else {
741 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800742 GenNullCheck(rl_obj.reg, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700743 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, k32,
buzbee2700f7e2014-03-07 09:46:20 -0800744 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800745 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000746 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800747 // Without context sensitive analysis, we must issue the most conservative barriers.
748 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800750 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 }
752 StoreValue(rl_dest, rl_result);
753 }
754 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700755 ThreadOffset<4> getterOffset =
756 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
757 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
758 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000759 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 if (is_long_or_double) {
761 RegLocation rl_result = GetReturnWide(rl_dest.fp);
762 StoreValueWide(rl_dest, rl_result);
763 } else {
764 RegLocation rl_result = GetReturn(rl_dest.fp);
765 StoreValue(rl_dest, rl_result);
766 }
767 }
768}
769
Vladimir Markobe0e5462014-02-26 11:24:15 +0000770void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700772 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000773 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
774 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
775 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000777 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 rl_obj = LoadValue(rl_obj, kCoreReg);
779 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700780 RegisterClass src_reg_kind = kAnyReg;
781 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
782 // Force long/double volatile stores into SSE registers to avoid tearing.
783 src_reg_kind = kFPReg;
784 }
785 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800786 GenNullCheck(rl_obj.reg, opt_flags);
787 RegStorage reg_ptr = AllocTemp();
788 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000789 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800790 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 GenMemBarrier(kStoreStore);
792 }
buzbee2700f7e2014-03-07 09:46:20 -0800793 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800794 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000795 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800796 // A load might follow the volatile store so insert a StoreLoad barrier.
797 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 }
799 FreeTemp(reg_ptr);
800 } else {
801 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800802 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000803 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800804 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 GenMemBarrier(kStoreStore);
806 }
buzbee695d13a2014-04-19 13:32:20 -0700807 Store32Disp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800808 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000809 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800810 // A load might follow the volatile store so insert a StoreLoad barrier.
811 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 }
813 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800814 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700815 }
816 }
817 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700818 ThreadOffset<4> setter_offset =
819 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
820 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
821 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000822 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
823 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824 }
825}
826
Ian Rogersa9a82542013-10-04 11:17:26 -0700827void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
828 RegLocation rl_src) {
829 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
830 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
831 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700832 ThreadOffset<4> helper = needs_range_check
833 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
834 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
835 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700836 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
837}
838
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700839void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800841 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700842 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
843 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
844 *cu_->dex_file,
845 type_idx)) {
846 // Call out to helper which resolves type and verifies access.
847 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700848 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800849 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 RegLocation rl_result = GetReturn(false);
851 StoreValue(rl_dest, rl_result);
852 } else {
853 // We're don't need access checks, load type from dex cache
854 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700855 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700856 Load32Disp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000857 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700858 Load32Disp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700859 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
860 type_idx) || SLOW_TYPE_PATH) {
861 // Slow path, at runtime test if type is null and if so initialize
862 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800863 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800864 LIR* cont = NewLIR0(kPseudoTargetLabel);
865
866 // Object to generate the slow path for class resolution.
867 class SlowPath : public LIRSlowPath {
868 public:
869 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
870 const RegLocation& rl_method, const RegLocation& rl_result) :
871 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
872 rl_method_(rl_method), rl_result_(rl_result) {
873 }
874
875 void Compile() {
876 GenerateTargetLabel();
877
Ian Rogersdd7624d2014-03-14 17:43:00 -0700878 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800879 rl_method_.reg, true);
880 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800881
882 m2l_->OpUnconditionalBranch(cont_);
883 }
884
885 private:
886 const int type_idx_;
887 const RegLocation rl_method_;
888 const RegLocation rl_result_;
889 };
890
891 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800892 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800893
Brian Carlstrom7940e442013-07-12 13:46:57 -0700894 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800895 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 // Fast path, we're done - just store result
897 StoreValue(rl_dest, rl_result);
898 }
899 }
900}
901
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700902void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000904 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
905 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700906 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
907 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
908 // slow path, resolve string if not in dex cache
909 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700910 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800911
912 // If the Method* is already in a register, we can save a copy.
913 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800914 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800915 if (rl_method.location == kLocPhysReg) {
916 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800917 DCHECK(!IsTemp(rl_method.reg));
918 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800919 } else {
920 r_method = TargetReg(kArg2);
921 LoadCurrMethodDirect(r_method);
922 }
buzbee695d13a2014-04-19 13:32:20 -0700923 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
924 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800925
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 // Might call out to helper, which will return resolved string in kRet0
buzbee695d13a2014-04-19 13:32:20 -0700927 Load32Disp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800928 if (cu_->instruction_set == kThumb2 ||
929 cu_->instruction_set == kMips) {
930 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800931 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800932 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
933 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800935
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800936 // Object to generate the slow path for string resolution.
937 class SlowPath : public LIRSlowPath {
938 public:
buzbee2700f7e2014-03-07 09:46:20 -0800939 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800940 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
941 }
942
943 void Compile() {
944 GenerateTargetLabel();
945
Dave Allisond6ed6422014-04-09 23:36:15 +0000946 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800947
Dave Allisond6ed6422014-04-09 23:36:15 +0000948 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
949 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
950 m2l_->MarkSafepointPC(call_inst);
951 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800952
953 m2l_->OpUnconditionalBranch(cont_);
954 }
955
956 private:
buzbee2700f7e2014-03-07 09:46:20 -0800957 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800958 };
959
960 // Add to list for future.
961 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700963 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -0800964 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
965 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700966 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -0800967 true);
Mark Mendell766e9292014-01-27 07:55:47 -0800968 LIR* target = NewLIR0(kPseudoTargetLabel);
969 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 }
971 GenBarrier();
972 StoreValue(rl_dest, GetReturn(false));
973 } else {
974 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800975 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700977 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
978 Load32Disp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 StoreValue(rl_dest, rl_result);
980 }
981}
982
983/*
984 * Let helper function take care of everything. Will
985 * call Class::NewInstanceFromCode(type_idx, method);
986 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700987void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 FlushAllRegs(); /* Everything to home location */
989 // alloc will always check for resolution, do we also need to verify
990 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -0700991 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800992 const DexFile* dex_file = cu_->dex_file;
993 CompilerDriver* driver = cu_->compiler_driver;
994 if (driver->CanAccessInstantiableTypeWithoutChecks(
995 cu_->method_idx, *dex_file, type_idx)) {
996 bool is_type_initialized;
997 bool use_direct_type_ptr;
998 uintptr_t direct_type_ptr;
999 if (kEmbedClassInCode &&
1000 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1001 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1002 // The fast path.
1003 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001004 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001005 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001006 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001007 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1008 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001009 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001010 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1011 }
1012 } else {
1013 // Use the direct pointer.
1014 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001015 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001016 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1017 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001018 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001019 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1020 }
1021 }
1022 } else {
1023 // The slow path.
1024 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001025 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001026 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1027 }
1028 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001030 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001031 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 RegLocation rl_result = GetReturn(false);
1034 StoreValue(rl_dest, rl_result);
1035}
1036
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001037void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001039 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040}
1041
1042// For final classes there are no sub-classes to check and so we can answer the instance-of
1043// question with simple comparisons.
1044void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1045 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001046 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001047 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001048
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049 RegLocation object = LoadValue(rl_src, kCoreReg);
1050 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001051 RegStorage result_reg = rl_result.reg;
1052 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 result_reg = AllocTypedTemp(false, kCoreReg);
1054 }
1055 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001056 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057
buzbee2700f7e2014-03-07 09:46:20 -08001058 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1059 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060
1061 LoadCurrMethodDirect(check_class);
1062 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001063 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1064 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 } else {
buzbee695d13a2014-04-19 13:32:20 -07001066 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1067 check_class);
1068 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001069 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001070 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 }
1072
1073 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001074 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 if (cu_->instruction_set == kThumb2) {
1076 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001077 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001079 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 } else {
1081 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1082 LoadConstant(result_reg, 1); // eq case - load true
1083 }
1084 LIR* target = NewLIR0(kPseudoTargetLabel);
1085 null_branchover->target = target;
1086 if (ne_branchover != NULL) {
1087 ne_branchover->target = target;
1088 }
1089 FreeTemp(object_class);
1090 FreeTemp(check_class);
1091 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001092 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 FreeTemp(result_reg);
1094 }
1095 StoreValue(rl_dest, rl_result);
1096}
1097
1098void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1099 bool type_known_abstract, bool use_declaring_class,
1100 bool can_assume_type_is_in_dex_cache,
1101 uint32_t type_idx, RegLocation rl_dest,
1102 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001103 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001104 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001105
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 FlushAllRegs();
1107 // May generate a call - use explicit registers
1108 LockCallTemps();
1109 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001110 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 if (needs_access_check) {
1112 // Check we have access to type_idx and if not throw IllegalAccessError,
1113 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001114 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 type_idx, true);
1116 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1117 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1118 } else if (use_declaring_class) {
1119 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001120 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001121 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 } else {
1123 // Load dex cache entry into class_reg (kArg2)
1124 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001125 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1126 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001127 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001128 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 if (!can_assume_type_is_in_dex_cache) {
1130 // Need to test presence of type in dex cache at runtime
1131 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1132 // Not resolved
1133 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001134 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001135 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1137 // Rejoin code paths
1138 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1139 hop_branch->target = hop_target;
1140 }
1141 }
1142 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1143 RegLocation rl_result = GetReturn(false);
1144 if (cu_->instruction_set == kMips) {
1145 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001146 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 }
1148 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1149
1150 /* load object->klass_ */
1151 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001152 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1154 LIR* branchover = NULL;
1155 if (type_known_final) {
1156 // rl_result == ref == null == 0.
1157 if (cu_->instruction_set == kThumb2) {
1158 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001159 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001160 LoadConstant(rl_result.reg, 1); // .eq case - load true
1161 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001162 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001164 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001166 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 }
1168 } else {
1169 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001170 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001171 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 if (!type_known_abstract) {
1173 /* Uses conditional nullification */
1174 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001175 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1177 }
1178 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1179 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001180 if (it != nullptr) {
1181 OpEndIT(it);
1182 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 FreeTemp(r_tgt);
1184 } else {
1185 if (!type_known_abstract) {
1186 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001187 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1189 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001190 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001191 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1192 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1193 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 }
1195 }
1196 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001197 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 /* branch targets here */
1199 LIR* target = NewLIR0(kPseudoTargetLabel);
1200 StoreValue(rl_dest, rl_result);
1201 branch1->target = target;
1202 if (branchover != NULL) {
1203 branchover->target = target;
1204 }
1205}
1206
1207void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1208 bool type_known_final, type_known_abstract, use_declaring_class;
1209 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1210 *cu_->dex_file,
1211 type_idx,
1212 &type_known_final,
1213 &type_known_abstract,
1214 &use_declaring_class);
1215 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1216 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1217
1218 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1219 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1220 } else {
1221 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1222 use_declaring_class, can_assume_type_is_in_dex_cache,
1223 type_idx, rl_dest, rl_src);
1224 }
1225}
1226
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001227void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 bool type_known_final, type_known_abstract, use_declaring_class;
1229 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1230 *cu_->dex_file,
1231 type_idx,
1232 &type_known_final,
1233 &type_known_abstract,
1234 &use_declaring_class);
1235 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1236 // of the exception throw path.
1237 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001238 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 // Verifier type analysis proved this check cast would never cause an exception.
1240 return;
1241 }
1242 FlushAllRegs();
1243 // May generate a call - use explicit registers
1244 LockCallTemps();
1245 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001246 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 if (needs_access_check) {
1248 // Check we have access to type_idx and if not throw IllegalAccessError,
1249 // returns Class* in kRet0
1250 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001251 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252 type_idx, TargetReg(kArg1), true);
1253 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1254 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001255 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1256 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 } else {
1258 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001259 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1260 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001261 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001262 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1264 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001265 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1266 LIR* cont = NewLIR0(kPseudoTargetLabel);
1267
1268 // Slow path to initialize the type. Executed if the type is NULL.
1269 class SlowPath : public LIRSlowPath {
1270 public:
1271 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001272 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001273 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1274 class_reg_(class_reg) {
1275 }
1276
1277 void Compile() {
1278 GenerateTargetLabel();
1279
1280 // Call out to helper, which will return resolved type in kArg0
1281 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001282 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001283 m2l_->TargetReg(kArg1), true);
1284 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1285 m2l_->OpUnconditionalBranch(cont_);
1286 }
1287 public:
1288 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001289 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001290 };
1291
buzbee2700f7e2014-03-07 09:46:20 -08001292 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 }
1294 }
1295 // At this point, class_reg (kArg2) has class
1296 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001297
1298 // Slow path for the case where the classes are not equal. In this case we need
1299 // to call a helper function to do the check.
1300 class SlowPath : public LIRSlowPath {
1301 public:
1302 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1303 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1304 }
1305
1306 void Compile() {
1307 GenerateTargetLabel();
1308
1309 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001310 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1311 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001312 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001313 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001314 m2l_->TargetReg(kArg1), true);
1315
1316 m2l_->OpUnconditionalBranch(cont_);
1317 }
1318
1319 private:
1320 bool load_;
1321 };
1322
1323 if (type_known_abstract) {
1324 // Easier case, run slow path if target is non-null (slow path will load from target)
1325 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1326 LIR* cont = NewLIR0(kPseudoTargetLabel);
1327 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1328 } else {
1329 // Harder, more common case. We need to generate a forward branch over the load
1330 // if the target is null. If it's non-null we perform the load and branch to the
1331 // slow path if the classes are not equal.
1332
1333 /* Null is OK - continue */
1334 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1335 /* load object->klass_ */
1336 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001337 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001338
1339 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1340 LIR* cont = NewLIR0(kPseudoTargetLabel);
1341
1342 // Add the slow path that will not perform load since this is already done.
1343 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1344
1345 // Set the null check to branch to the continuation.
1346 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 }
1348}
1349
1350void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001351 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 RegLocation rl_result;
1353 if (cu_->instruction_set == kThumb2) {
1354 /*
1355 * NOTE: This is the one place in the code in which we might have
1356 * as many as six live temporary registers. There are 5 in the normal
1357 * set for Arm. Until we have spill capabilities, temporarily add
1358 * lr to the temp set. It is safe to do this locally, but note that
1359 * lr is used explicitly elsewhere in the code generator and cannot
1360 * normally be used as a general temp register.
1361 */
1362 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1363 FreeTemp(TargetReg(kLr)); // and make it available
1364 }
1365 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1366 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1367 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1368 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001369 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1370 RegStorage t_reg = AllocTemp();
1371 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1372 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1373 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 FreeTemp(t_reg);
1375 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001376 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1377 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 }
1379 /*
1380 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1381 * following StoreValueWide might need to allocate a temp register.
1382 * To further work around the lack of a spill capability, explicitly
1383 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1384 * Remove when spill is functional.
1385 */
1386 FreeRegLocTemps(rl_result, rl_src1);
1387 FreeRegLocTemps(rl_result, rl_src2);
1388 StoreValueWide(rl_dest, rl_result);
1389 if (cu_->instruction_set == kThumb2) {
1390 Clobber(TargetReg(kLr));
1391 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1392 }
1393}
1394
1395
1396void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001397 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001398 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399
1400 switch (opcode) {
1401 case Instruction::SHL_LONG:
1402 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001403 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 break;
1405 case Instruction::SHR_LONG:
1406 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001407 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408 break;
1409 case Instruction::USHR_LONG:
1410 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001411 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 break;
1413 default:
1414 LOG(FATAL) << "Unexpected case";
1415 }
1416 FlushAllRegs(); /* Send everything to home location */
1417 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1418 RegLocation rl_result = GetReturnWide(false);
1419 StoreValueWide(rl_dest, rl_result);
1420}
1421
1422
1423void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001424 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001425 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 OpKind op = kOpBkpt;
1427 bool is_div_rem = false;
1428 bool check_zero = false;
1429 bool unary = false;
1430 RegLocation rl_result;
1431 bool shift_op = false;
1432 switch (opcode) {
1433 case Instruction::NEG_INT:
1434 op = kOpNeg;
1435 unary = true;
1436 break;
1437 case Instruction::NOT_INT:
1438 op = kOpMvn;
1439 unary = true;
1440 break;
1441 case Instruction::ADD_INT:
1442 case Instruction::ADD_INT_2ADDR:
1443 op = kOpAdd;
1444 break;
1445 case Instruction::SUB_INT:
1446 case Instruction::SUB_INT_2ADDR:
1447 op = kOpSub;
1448 break;
1449 case Instruction::MUL_INT:
1450 case Instruction::MUL_INT_2ADDR:
1451 op = kOpMul;
1452 break;
1453 case Instruction::DIV_INT:
1454 case Instruction::DIV_INT_2ADDR:
1455 check_zero = true;
1456 op = kOpDiv;
1457 is_div_rem = true;
1458 break;
1459 /* NOTE: returns in kArg1 */
1460 case Instruction::REM_INT:
1461 case Instruction::REM_INT_2ADDR:
1462 check_zero = true;
1463 op = kOpRem;
1464 is_div_rem = true;
1465 break;
1466 case Instruction::AND_INT:
1467 case Instruction::AND_INT_2ADDR:
1468 op = kOpAnd;
1469 break;
1470 case Instruction::OR_INT:
1471 case Instruction::OR_INT_2ADDR:
1472 op = kOpOr;
1473 break;
1474 case Instruction::XOR_INT:
1475 case Instruction::XOR_INT_2ADDR:
1476 op = kOpXor;
1477 break;
1478 case Instruction::SHL_INT:
1479 case Instruction::SHL_INT_2ADDR:
1480 shift_op = true;
1481 op = kOpLsl;
1482 break;
1483 case Instruction::SHR_INT:
1484 case Instruction::SHR_INT_2ADDR:
1485 shift_op = true;
1486 op = kOpAsr;
1487 break;
1488 case Instruction::USHR_INT:
1489 case Instruction::USHR_INT_2ADDR:
1490 shift_op = true;
1491 op = kOpLsr;
1492 break;
1493 default:
1494 LOG(FATAL) << "Invalid word arith op: " << opcode;
1495 }
1496 if (!is_div_rem) {
1497 if (unary) {
1498 rl_src1 = LoadValue(rl_src1, kCoreReg);
1499 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001500 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 } else {
1502 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001503 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001504 RegStorage t_reg = AllocTemp();
1505 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 rl_src1 = LoadValue(rl_src1, kCoreReg);
1507 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001508 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 FreeTemp(t_reg);
1510 } else {
1511 rl_src1 = LoadValue(rl_src1, kCoreReg);
1512 rl_src2 = LoadValue(rl_src2, kCoreReg);
1513 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001514 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 }
1516 }
1517 StoreValue(rl_dest, rl_result);
1518 } else {
Dave Allison70202782013-10-22 17:52:19 -07001519 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 -07001520 if (cu_->instruction_set == kMips) {
1521 rl_src1 = LoadValue(rl_src1, kCoreReg);
1522 rl_src2 = LoadValue(rl_src2, kCoreReg);
1523 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001524 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 }
buzbee2700f7e2014-03-07 09:46:20 -08001526 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001527 done = true;
1528 } else if (cu_->instruction_set == kThumb2) {
1529 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1530 // Use ARM SDIV instruction for division. For remainder we also need to
1531 // calculate using a MUL and subtract.
1532 rl_src1 = LoadValue(rl_src1, kCoreReg);
1533 rl_src2 = LoadValue(rl_src2, kCoreReg);
1534 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001535 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001536 }
buzbee2700f7e2014-03-07 09:46:20 -08001537 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001538 done = true;
1539 }
1540 }
1541
1542 // If we haven't already generated the code use the callout function.
1543 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001544 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 FlushAllRegs(); /* Send everything to home location */
1546 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001547 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1549 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001550 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001551 }
Dave Allison70202782013-10-22 17:52:19 -07001552 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001553 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001554 if (op == kOpDiv)
1555 rl_result = GetReturn(false);
1556 else
1557 rl_result = GetReturnAlt();
1558 }
1559 StoreValue(rl_dest, rl_result);
1560 }
1561}
1562
1563/*
1564 * The following are the first-level codegen routines that analyze the format
1565 * of each bytecode then either dispatch special purpose codegen routines
1566 * or produce corresponding Thumb instructions directly.
1567 */
1568
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001570static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 x &= x - 1;
1572 return (x & (x - 1)) == 0;
1573}
1574
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1576// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001577bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001578 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1580 return false;
1581 }
1582 // No divide instruction for Arm, so check for more special cases
1583 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001584 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 }
1586 int k = LowestSetBit(lit);
1587 if (k >= 30) {
1588 // Avoid special cases.
1589 return false;
1590 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001591 rl_src = LoadValue(rl_src, kCoreReg);
1592 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001593 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001594 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 if (lit == 2) {
1596 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001597 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1598 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1599 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001601 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001603 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1604 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 }
1606 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001607 RegStorage t_reg1 = AllocTemp();
1608 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1611 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001613 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001615 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001617 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001619 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 }
1621 }
1622 StoreValue(rl_dest, rl_result);
1623 return true;
1624}
1625
1626// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1627// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001628bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001629 if (lit < 0) {
1630 return false;
1631 }
1632 if (lit == 0) {
1633 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1634 LoadConstant(rl_result.reg, 0);
1635 StoreValue(rl_dest, rl_result);
1636 return true;
1637 }
1638 if (lit == 1) {
1639 rl_src = LoadValue(rl_src, kCoreReg);
1640 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1641 OpRegCopy(rl_result.reg, rl_src.reg);
1642 StoreValue(rl_dest, rl_result);
1643 return true;
1644 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001645 // There is RegRegRegShift on Arm, so check for more special cases
1646 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001647 return EasyMultiply(rl_src, rl_dest, lit);
1648 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 // Can we simplify this multiplication?
1650 bool power_of_two = false;
1651 bool pop_count_le2 = false;
1652 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001653 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 power_of_two = true;
1655 } else if (IsPopCountLE2(lit)) {
1656 pop_count_le2 = true;
1657 } else if (IsPowerOfTwo(lit + 1)) {
1658 power_of_two_minus_one = true;
1659 } else {
1660 return false;
1661 }
1662 rl_src = LoadValue(rl_src, kCoreReg);
1663 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1664 if (power_of_two) {
1665 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001666 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001667 } else if (pop_count_le2) {
1668 // Shift and add and shift.
1669 int first_bit = LowestSetBit(lit);
1670 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1671 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1672 } else {
1673 // Reverse subtract: (src << (shift + 1)) - src.
1674 DCHECK(power_of_two_minus_one);
1675 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001676 RegStorage t_reg = AllocTemp();
1677 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1678 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 }
1680 StoreValue(rl_dest, rl_result);
1681 return true;
1682}
1683
1684void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001685 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 RegLocation rl_result;
1687 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1688 int shift_op = false;
1689 bool is_div = false;
1690
1691 switch (opcode) {
1692 case Instruction::RSUB_INT_LIT8:
1693 case Instruction::RSUB_INT: {
1694 rl_src = LoadValue(rl_src, kCoreReg);
1695 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1696 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001697 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001698 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001699 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1700 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001701 }
1702 StoreValue(rl_dest, rl_result);
1703 return;
1704 }
1705
1706 case Instruction::SUB_INT:
1707 case Instruction::SUB_INT_2ADDR:
1708 lit = -lit;
1709 // Intended fallthrough
1710 case Instruction::ADD_INT:
1711 case Instruction::ADD_INT_2ADDR:
1712 case Instruction::ADD_INT_LIT8:
1713 case Instruction::ADD_INT_LIT16:
1714 op = kOpAdd;
1715 break;
1716 case Instruction::MUL_INT:
1717 case Instruction::MUL_INT_2ADDR:
1718 case Instruction::MUL_INT_LIT8:
1719 case Instruction::MUL_INT_LIT16: {
1720 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1721 return;
1722 }
1723 op = kOpMul;
1724 break;
1725 }
1726 case Instruction::AND_INT:
1727 case Instruction::AND_INT_2ADDR:
1728 case Instruction::AND_INT_LIT8:
1729 case Instruction::AND_INT_LIT16:
1730 op = kOpAnd;
1731 break;
1732 case Instruction::OR_INT:
1733 case Instruction::OR_INT_2ADDR:
1734 case Instruction::OR_INT_LIT8:
1735 case Instruction::OR_INT_LIT16:
1736 op = kOpOr;
1737 break;
1738 case Instruction::XOR_INT:
1739 case Instruction::XOR_INT_2ADDR:
1740 case Instruction::XOR_INT_LIT8:
1741 case Instruction::XOR_INT_LIT16:
1742 op = kOpXor;
1743 break;
1744 case Instruction::SHL_INT_LIT8:
1745 case Instruction::SHL_INT:
1746 case Instruction::SHL_INT_2ADDR:
1747 lit &= 31;
1748 shift_op = true;
1749 op = kOpLsl;
1750 break;
1751 case Instruction::SHR_INT_LIT8:
1752 case Instruction::SHR_INT:
1753 case Instruction::SHR_INT_2ADDR:
1754 lit &= 31;
1755 shift_op = true;
1756 op = kOpAsr;
1757 break;
1758 case Instruction::USHR_INT_LIT8:
1759 case Instruction::USHR_INT:
1760 case Instruction::USHR_INT_2ADDR:
1761 lit &= 31;
1762 shift_op = true;
1763 op = kOpLsr;
1764 break;
1765
1766 case Instruction::DIV_INT:
1767 case Instruction::DIV_INT_2ADDR:
1768 case Instruction::DIV_INT_LIT8:
1769 case Instruction::DIV_INT_LIT16:
1770 case Instruction::REM_INT:
1771 case Instruction::REM_INT_2ADDR:
1772 case Instruction::REM_INT_LIT8:
1773 case Instruction::REM_INT_LIT16: {
1774 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001775 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001776 return;
1777 }
buzbee11b63d12013-08-27 07:34:17 -07001778 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001779 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001780 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001781 (opcode == Instruction::DIV_INT_LIT16)) {
1782 is_div = true;
1783 } else {
1784 is_div = false;
1785 }
buzbee11b63d12013-08-27 07:34:17 -07001786 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1787 return;
1788 }
Dave Allison70202782013-10-22 17:52:19 -07001789
1790 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001791 if (cu_->instruction_set == kMips) {
1792 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001793 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001794 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001795 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001796 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1797 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001798 } else if (cu_->instruction_set == kThumb2) {
1799 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1800 // Use ARM SDIV instruction for division. For remainder we also need to
1801 // calculate using a MUL and subtract.
1802 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001803 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001804 done = true;
1805 }
1806 }
1807
1808 if (!done) {
1809 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001810 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1811 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001812 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001813 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1814 if (is_div)
1815 rl_result = GetReturn(false);
1816 else
1817 rl_result = GetReturnAlt();
1818 }
1819 StoreValue(rl_dest, rl_result);
1820 return;
1821 }
1822 default:
1823 LOG(FATAL) << "Unexpected opcode " << opcode;
1824 }
1825 rl_src = LoadValue(rl_src, kCoreReg);
1826 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001827 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001828 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001829 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001831 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001832 }
1833 StoreValue(rl_dest, rl_result);
1834}
1835
1836void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001837 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001838 RegLocation rl_result;
1839 OpKind first_op = kOpBkpt;
1840 OpKind second_op = kOpBkpt;
1841 bool call_out = false;
1842 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001843 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001844 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001845
1846 switch (opcode) {
1847 case Instruction::NOT_LONG:
1848 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1849 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1850 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001851 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1852 RegStorage t_reg = AllocTemp();
1853 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1854 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1855 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001856 FreeTemp(t_reg);
1857 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001858 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1859 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001860 }
1861 StoreValueWide(rl_dest, rl_result);
1862 return;
1863 case Instruction::ADD_LONG:
1864 case Instruction::ADD_LONG_2ADDR:
1865 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001866 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001867 return;
1868 }
1869 first_op = kOpAdd;
1870 second_op = kOpAdc;
1871 break;
1872 case Instruction::SUB_LONG:
1873 case Instruction::SUB_LONG_2ADDR:
1874 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001875 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001876 return;
1877 }
1878 first_op = kOpSub;
1879 second_op = kOpSbc;
1880 break;
1881 case Instruction::MUL_LONG:
1882 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001883 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001884 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001885 return;
1886 } else {
1887 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001888 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001889 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001890 }
1891 break;
1892 case Instruction::DIV_LONG:
1893 case Instruction::DIV_LONG_2ADDR:
1894 call_out = true;
1895 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001896 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001897 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001898 break;
1899 case Instruction::REM_LONG:
1900 case Instruction::REM_LONG_2ADDR:
1901 call_out = true;
1902 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001903 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001904 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001905 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001906 break;
1907 case Instruction::AND_LONG_2ADDR:
1908 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001909 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001910 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001911 }
1912 first_op = kOpAnd;
1913 second_op = kOpAnd;
1914 break;
1915 case Instruction::OR_LONG:
1916 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001917 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001918 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001919 return;
1920 }
1921 first_op = kOpOr;
1922 second_op = kOpOr;
1923 break;
1924 case Instruction::XOR_LONG:
1925 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001926 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001927 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001928 return;
1929 }
1930 first_op = kOpXor;
1931 second_op = kOpXor;
1932 break;
1933 case Instruction::NEG_LONG: {
1934 GenNegLong(rl_dest, rl_src2);
1935 return;
1936 }
1937 default:
1938 LOG(FATAL) << "Invalid long arith op";
1939 }
1940 if (!call_out) {
1941 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1942 } else {
1943 FlushAllRegs(); /* Send everything to home location */
1944 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001945 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1946 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1947 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1948 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07001949 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08001950 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001951 // NOTE: callout here is not a safepoint
1952 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1953 } else {
1954 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1955 }
1956 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001957 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 rl_result = GetReturnWide(false);
1959 else
1960 rl_result = GetReturnWideAlt();
1961 StoreValueWide(rl_dest, rl_result);
1962 }
1963}
1964
Ian Rogersdd7624d2014-03-14 17:43:00 -07001965void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001966 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 /*
1968 * Don't optimize the register usage since it calls out to support
1969 * functions
1970 */
1971 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1973 if (rl_dest.wide) {
1974 RegLocation rl_result;
1975 rl_result = GetReturnWide(rl_dest.fp);
1976 StoreValueWide(rl_dest, rl_result);
1977 } else {
1978 RegLocation rl_result;
1979 rl_result = GetReturn(rl_dest.fp);
1980 StoreValue(rl_dest, rl_result);
1981 }
1982}
1983
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001984class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
1985 public:
1986 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
1987 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
1988 }
1989
1990 void Compile() OVERRIDE {
1991 m2l_->ResetRegPool();
1992 m2l_->ResetDefTracking();
1993 GenerateTargetLabel(kPseudoSuspendTarget);
1994 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
1995 if (cont_ != nullptr) {
1996 m2l_->OpUnconditionalBranch(cont_);
1997 }
1998 }
1999};
2000
Brian Carlstrom7940e442013-07-12 13:46:57 -07002001/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002002void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002003 if (Runtime::Current()->ExplicitSuspendChecks()) {
2004 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2005 return;
2006 }
2007 FlushAllRegs();
2008 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002009 LIR* cont = NewLIR0(kPseudoTargetLabel);
2010 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002011 } else {
2012 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2013 return;
2014 }
2015 FlushAllRegs(); // TODO: needed?
2016 LIR* inst = CheckSuspendUsingLoad();
2017 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002018 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019}
2020
2021/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002022void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002023 if (Runtime::Current()->ExplicitSuspendChecks()) {
2024 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2025 OpUnconditionalBranch(target);
2026 return;
2027 }
2028 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002029 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002030 LIR* branch = OpUnconditionalBranch(nullptr);
2031 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002032 } else {
2033 // For the implicit suspend check, just perform the trigger
2034 // load and branch to the target.
2035 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2036 OpUnconditionalBranch(target);
2037 return;
2038 }
2039 FlushAllRegs();
2040 LIR* inst = CheckSuspendUsingLoad();
2041 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002044}
2045
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002046/* Call out to helper assembly routine that will null check obj and then lock it. */
2047void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2048 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002049 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002050}
2051
2052/* Call out to helper assembly routine that will null check obj and then unlock it. */
2053void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2054 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002055 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002056}
2057
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002058/* Generic code for generating a wide constant into a VR. */
2059void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2060 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002061 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002062 StoreValueWide(rl_dest, rl_result);
2063}
2064
Brian Carlstrom7940e442013-07-12 13:46:57 -07002065} // namespace art