blob: 3cc2ba0042c1ace9ac58eb1f0f141b00941984f8 [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;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700333 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800334 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700335 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
336 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800337 // The fast path.
338 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800339 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700340 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800341 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
342 } else {
343 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700344 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800345 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
346 }
347 } else {
348 // The slow path.
349 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700350 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800351 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
352 }
353 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700355 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800356 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 RegLocation rl_result = GetReturn(false);
359 StoreValue(rl_dest, rl_result);
360}
361
362/*
363 * Similar to GenNewArray, but with post-allocation initialization.
364 * Verifier guarantees we're dealing with an array class. Current
365 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
366 * Current code also throws internal unimp if not 'L', '[' or 'I'.
367 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700368void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 int elems = info->num_arg_words;
370 int type_idx = info->index;
371 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700372 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
374 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700375 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700377 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 }
379 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
380 FreeTemp(TargetReg(kArg2));
381 FreeTemp(TargetReg(kArg1));
382 /*
383 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
384 * return region. Because AllocFromCode placed the new array
385 * in kRet0, we'll just lock it into place. When debugger support is
386 * added, it may be necessary to additionally copy all return
387 * values to a home location in thread-local storage
388 */
389 LockTemp(TargetReg(kRet0));
390
391 // TODO: use the correct component size, currently all supported types
392 // share array alignment with ints (see comment at head of function)
393 size_t component_size = sizeof(int32_t);
394
395 // Having a range of 0 is legal
396 if (info->is_range && (elems > 0)) {
397 /*
398 * Bit of ugliness here. We're going generate a mem copy loop
399 * on the register range, but it is possible that some regs
400 * in the range have been promoted. This is unlikely, but
401 * before generating the copy, we'll just force a flush
402 * of any regs in the source range that have been promoted to
403 * home location.
404 */
405 for (int i = 0; i < elems; i++) {
406 RegLocation loc = UpdateLoc(info->args[i]);
407 if (loc.location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700408 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 }
410 }
411 /*
412 * TUNING note: generated code here could be much improved, but
413 * this is an uncommon operation and isn't especially performance
414 * critical.
415 */
buzbee2700f7e2014-03-07 09:46:20 -0800416 RegStorage r_src = AllocTemp();
417 RegStorage r_dst = AllocTemp();
418 RegStorage r_idx = AllocTemp();
419 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700420 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 case kThumb2:
422 r_val = TargetReg(kLr);
423 break;
424 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700425 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 FreeTemp(TargetReg(kRet0));
427 r_val = AllocTemp();
428 break;
429 case kMips:
430 r_val = AllocTemp();
431 break;
432 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
433 }
434 // Set up source pointer
435 RegLocation rl_first = info->args[0];
436 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
437 // Set up the target pointer
438 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
439 mirror::Array::DataOffset(component_size).Int32Value());
440 // Set up the loop counter (known to be > 0)
441 LoadConstant(r_idx, elems - 1);
442 // Generate the copy loop. Going backwards for convenience
443 LIR* target = NewLIR0(kPseudoTargetLabel);
444 // Copy next element
buzbee695d13a2014-04-19 13:32:20 -0700445 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
446 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 FreeTemp(r_val);
448 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700449 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 // Restore the target pointer
451 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
452 -mirror::Array::DataOffset(component_size).Int32Value());
453 }
454 } else if (!info->is_range) {
455 // TUNING: interleave
456 for (int i = 0; i < elems; i++) {
457 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700458 Store32Disp(TargetReg(kRet0),
459 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800461 if (IsTemp(rl_arg.reg)) {
462 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 }
464 }
465 }
466 if (info->result.location != kLocInvalid) {
467 StoreValue(info->result, GetReturn(false /* not fp */));
468 }
469}
470
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800471//
472// Slow path to ensure a class is initialized for sget/sput.
473//
474class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
475 public:
buzbee2700f7e2014-03-07 09:46:20 -0800476 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
477 RegStorage r_base) :
478 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
479 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800480 }
481
482 void Compile() {
483 LIR* unresolved_target = GenerateTargetLabel();
484 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700485 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800486 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800487 // Copy helper's result into r_base, a no-op on all but MIPS.
488 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
489
490 m2l_->OpUnconditionalBranch(cont_);
491 }
492
493 private:
494 LIR* const uninit_;
495 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800496 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800497};
498
Vladimir Markobe0e5462014-02-26 11:24:15 +0000499void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700500 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000501 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
502 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
503 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
504 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800505 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000506 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 // Fast path, static storage base is this method's class
508 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800509 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700510 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800511 if (IsTemp(rl_method.reg)) {
512 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 }
514 } else {
515 // Medium path, static storage base in a different class which requires checks that the other
516 // class is initialized.
517 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000518 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 // May do runtime call so everything to home locations.
520 FlushAllRegs();
521 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800522 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 LockTemp(r_method);
524 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800525 r_base = TargetReg(kArg0);
526 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700527 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000528 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
529 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800530 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000531 if (!field_info.IsInitialized() &&
532 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800533 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800534
535 // The slow path is invoked if the r_base is NULL or the class pointed
536 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800537 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800538 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800539 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800540 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800541 mirror::Class::StatusOffset().Int32Value(),
542 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800543 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800544
buzbee2700f7e2014-03-07 09:46:20 -0800545 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000546 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800547
548 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 FreeTemp(r_method);
551 }
552 // rBase now holds static storage base
553 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700554 RegisterClass register_kind = kAnyReg;
555 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
556 // Force long/double volatile stores into SSE registers to avoid tearing.
557 register_kind = kFPReg;
558 }
559 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 } else {
561 rl_src = LoadValue(rl_src, kAnyReg);
562 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000563 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800564 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 GenMemBarrier(kStoreStore);
566 }
567 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800568 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
buzbee695d13a2014-04-19 13:32:20 -0700569 } else if (rl_src.ref) {
570 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 } else {
buzbee695d13a2014-04-19 13:32:20 -0700572 Store32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000574 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800575 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 GenMemBarrier(kStoreLoad);
577 }
578 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800579 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800581 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 } else {
583 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700584 ThreadOffset<4> setter_offset =
585 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
586 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
587 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000588 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 }
590}
591
Vladimir Markobe0e5462014-02-26 11:24:15 +0000592void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700593 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000594 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
595 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
596 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
597 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800598 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000599 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 // Fast path, static storage base is this method's class
601 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800602 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700603 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 } else {
605 // Medium path, static storage base in a different class which requires checks that the other
606 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000607 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 // May do runtime call so everything to home locations.
609 FlushAllRegs();
610 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800611 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 LockTemp(r_method);
613 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614 r_base = TargetReg(kArg0);
615 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700616 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000617 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
618 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800619 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000620 if (!field_info.IsInitialized() &&
621 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800622 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800623
624 // The slow path is invoked if the r_base is NULL or the class pointed
625 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800626 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800627 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800628 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800629 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800630 mirror::Class::StatusOffset().Int32Value(),
631 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800632 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800633
buzbee2700f7e2014-03-07 09:46:20 -0800634 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000635 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800636
637 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 FreeTemp(r_method);
640 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800641 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700642 RegisterClass result_reg_kind = kAnyReg;
643 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
644 // Force long/double volatile loads into SSE registers to avoid tearing.
645 result_reg_kind = kFPReg;
646 }
647 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800648
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800650 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
buzbee695d13a2014-04-19 13:32:20 -0700651 } else if (rl_result.ref) {
652 LoadRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 } else {
buzbee695d13a2014-04-19 13:32:20 -0700654 Load32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800656 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800657
658 if (field_info.IsVolatile()) {
659 // Without context sensitive analysis, we must issue the most conservative barriers.
660 // In this case, either a load or store may follow so we issue both barriers.
661 GenMemBarrier(kLoadLoad);
662 GenMemBarrier(kLoadStore);
663 }
664
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 if (is_long_or_double) {
666 StoreValueWide(rl_dest, rl_result);
667 } else {
668 StoreValue(rl_dest, rl_result);
669 }
670 } else {
671 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700672 ThreadOffset<4> getterOffset =
673 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
674 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
675 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000676 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 if (is_long_or_double) {
678 RegLocation rl_result = GetReturnWide(rl_dest.fp);
679 StoreValueWide(rl_dest, rl_result);
680 } else {
681 RegLocation rl_result = GetReturn(rl_dest.fp);
682 StoreValue(rl_dest, rl_result);
683 }
684 }
685}
686
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800687// Generate code for all slow paths.
688void Mir2Lir::HandleSlowPaths() {
689 int n = slow_paths_.Size();
690 for (int i = 0; i < n; ++i) {
691 LIRSlowPath* slowpath = slow_paths_.Get(i);
692 slowpath->Compile();
693 }
694 slow_paths_.Reset();
695}
696
Vladimir Markobe0e5462014-02-26 11:24:15 +0000697void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700699 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000700 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
701 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
702 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 RegLocation rl_result;
704 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000705 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 rl_obj = LoadValue(rl_obj, kCoreReg);
707 if (is_long_or_double) {
708 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800709 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700710 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700711 RegisterClass result_reg_kind = kAnyReg;
712 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
713 // Force long/double volatile loads into SSE registers to avoid tearing.
714 result_reg_kind = kFPReg;
715 }
716 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800717 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
718 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800719 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000720 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800721 // Without context sensitive analysis, we must issue the most conservative barriers.
722 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800724 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 }
726 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800727 RegStorage reg_ptr = AllocTemp();
728 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800730 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700731 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000732 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800733 // Without context sensitive analysis, we must issue the most conservative barriers.
734 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800736 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 }
738 FreeTemp(reg_ptr);
739 }
740 StoreValueWide(rl_dest, rl_result);
741 } else {
742 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800743 GenNullCheck(rl_obj.reg, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700744 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, k32,
buzbee2700f7e2014-03-07 09:46:20 -0800745 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800746 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000747 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800748 // Without context sensitive analysis, we must issue the most conservative barriers.
749 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800751 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 }
753 StoreValue(rl_dest, rl_result);
754 }
755 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700756 ThreadOffset<4> getterOffset =
757 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
758 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
759 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000760 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 if (is_long_or_double) {
762 RegLocation rl_result = GetReturnWide(rl_dest.fp);
763 StoreValueWide(rl_dest, rl_result);
764 } else {
765 RegLocation rl_result = GetReturn(rl_dest.fp);
766 StoreValue(rl_dest, rl_result);
767 }
768 }
769}
770
Vladimir Markobe0e5462014-02-26 11:24:15 +0000771void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700773 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000774 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
775 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
776 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000778 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 rl_obj = LoadValue(rl_obj, kCoreReg);
780 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700781 RegisterClass src_reg_kind = kAnyReg;
782 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
783 // Force long/double volatile stores into SSE registers to avoid tearing.
784 src_reg_kind = kFPReg;
785 }
786 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800787 GenNullCheck(rl_obj.reg, opt_flags);
788 RegStorage reg_ptr = AllocTemp();
789 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000790 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800791 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 GenMemBarrier(kStoreStore);
793 }
buzbee2700f7e2014-03-07 09:46:20 -0800794 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800795 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000796 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800797 // A load might follow the volatile store so insert a StoreLoad barrier.
798 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 }
800 FreeTemp(reg_ptr);
801 } else {
802 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800803 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000804 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800805 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 GenMemBarrier(kStoreStore);
807 }
buzbee695d13a2014-04-19 13:32:20 -0700808 Store32Disp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800809 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000810 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800811 // A load might follow the volatile store so insert a StoreLoad barrier.
812 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813 }
814 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800815 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 }
817 }
818 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700819 ThreadOffset<4> setter_offset =
820 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
821 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
822 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000823 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
824 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 }
826}
827
Ian Rogersa9a82542013-10-04 11:17:26 -0700828void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
829 RegLocation rl_src) {
830 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
831 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
832 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700833 ThreadOffset<4> helper = needs_range_check
834 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
835 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
836 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700837 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
838}
839
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700840void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800842 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
844 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
845 *cu_->dex_file,
846 type_idx)) {
847 // Call out to helper which resolves type and verifies access.
848 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700849 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800850 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 RegLocation rl_result = GetReturn(false);
852 StoreValue(rl_dest, rl_result);
853 } else {
854 // We're don't need access checks, load type from dex cache
855 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700856 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700857 Load32Disp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000858 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700859 Load32Disp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
861 type_idx) || SLOW_TYPE_PATH) {
862 // Slow path, at runtime test if type is null and if so initialize
863 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800864 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800865 LIR* cont = NewLIR0(kPseudoTargetLabel);
866
867 // Object to generate the slow path for class resolution.
868 class SlowPath : public LIRSlowPath {
869 public:
870 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
871 const RegLocation& rl_method, const RegLocation& rl_result) :
872 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
873 rl_method_(rl_method), rl_result_(rl_result) {
874 }
875
876 void Compile() {
877 GenerateTargetLabel();
878
Ian Rogersdd7624d2014-03-14 17:43:00 -0700879 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800880 rl_method_.reg, true);
881 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800882
883 m2l_->OpUnconditionalBranch(cont_);
884 }
885
886 private:
887 const int type_idx_;
888 const RegLocation rl_method_;
889 const RegLocation rl_result_;
890 };
891
892 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800893 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800894
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800896 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 // Fast path, we're done - just store result
898 StoreValue(rl_dest, rl_result);
899 }
900 }
901}
902
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700903void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000905 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
906 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
908 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
909 // slow path, resolve string if not in dex cache
910 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700911 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800912
913 // If the Method* is already in a register, we can save a copy.
914 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800915 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800916 if (rl_method.location == kLocPhysReg) {
917 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800918 DCHECK(!IsTemp(rl_method.reg));
919 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800920 } else {
921 r_method = TargetReg(kArg2);
922 LoadCurrMethodDirect(r_method);
923 }
buzbee695d13a2014-04-19 13:32:20 -0700924 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
925 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800926
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 // Might call out to helper, which will return resolved string in kRet0
buzbee695d13a2014-04-19 13:32:20 -0700928 Load32Disp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700929 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
930 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800931
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700932 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800933 // Object to generate the slow path for string resolution.
934 class SlowPath : public LIRSlowPath {
935 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700936 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
937 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
938 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800939 }
940
941 void Compile() {
942 GenerateTargetLabel();
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700943 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
944 r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800945 m2l_->OpUnconditionalBranch(cont_);
946 }
947
948 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700949 const RegStorage r_method_;
950 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800951 };
952
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700953 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700955
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 GenBarrier();
957 StoreValue(rl_dest, GetReturn(false));
958 } else {
959 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800960 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700962 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
963 Load32Disp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 StoreValue(rl_dest, rl_result);
965 }
966}
967
968/*
969 * Let helper function take care of everything. Will
970 * call Class::NewInstanceFromCode(type_idx, method);
971 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700972void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973 FlushAllRegs(); /* Everything to home location */
974 // alloc will always check for resolution, do we also need to verify
975 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -0700976 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800977 const DexFile* dex_file = cu_->dex_file;
978 CompilerDriver* driver = cu_->compiler_driver;
979 if (driver->CanAccessInstantiableTypeWithoutChecks(
980 cu_->method_idx, *dex_file, type_idx)) {
981 bool is_type_initialized;
982 bool use_direct_type_ptr;
983 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700984 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800985 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700986 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
987 &direct_type_ptr, &is_finalizable) &&
988 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800989 // The fast path.
990 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800991 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800992 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700993 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800994 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
995 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700996 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800997 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
998 }
999 } else {
1000 // Use the direct pointer.
1001 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001002 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001003 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1004 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001005 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001006 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1007 }
1008 }
1009 } else {
1010 // The slow path.
1011 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001012 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001013 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1014 }
1015 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001017 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001018 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 RegLocation rl_result = GetReturn(false);
1021 StoreValue(rl_dest, rl_result);
1022}
1023
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001024void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001026 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027}
1028
1029// For final classes there are no sub-classes to check and so we can answer the instance-of
1030// question with simple comparisons.
1031void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1032 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001033 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001034 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001035
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 RegLocation object = LoadValue(rl_src, kCoreReg);
1037 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001038 RegStorage result_reg = rl_result.reg;
1039 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 result_reg = AllocTypedTemp(false, kCoreReg);
1041 }
1042 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001043 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044
buzbee2700f7e2014-03-07 09:46:20 -08001045 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1046 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047
1048 LoadCurrMethodDirect(check_class);
1049 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001050 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1051 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 } else {
buzbee695d13a2014-04-19 13:32:20 -07001053 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1054 check_class);
1055 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001056 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001057 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 }
1059
1060 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001061 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 if (cu_->instruction_set == kThumb2) {
1063 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001064 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001066 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 } else {
1068 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1069 LoadConstant(result_reg, 1); // eq case - load true
1070 }
1071 LIR* target = NewLIR0(kPseudoTargetLabel);
1072 null_branchover->target = target;
1073 if (ne_branchover != NULL) {
1074 ne_branchover->target = target;
1075 }
1076 FreeTemp(object_class);
1077 FreeTemp(check_class);
1078 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001079 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 FreeTemp(result_reg);
1081 }
1082 StoreValue(rl_dest, rl_result);
1083}
1084
1085void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1086 bool type_known_abstract, bool use_declaring_class,
1087 bool can_assume_type_is_in_dex_cache,
1088 uint32_t type_idx, RegLocation rl_dest,
1089 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001090 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001091 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001092
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 FlushAllRegs();
1094 // May generate a call - use explicit registers
1095 LockCallTemps();
1096 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001097 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 if (needs_access_check) {
1099 // Check we have access to type_idx and if not throw IllegalAccessError,
1100 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001101 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 type_idx, true);
1103 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1104 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1105 } else if (use_declaring_class) {
1106 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001107 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001108 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 } else {
1110 // Load dex cache entry into class_reg (kArg2)
1111 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001112 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1113 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001114 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001115 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 if (!can_assume_type_is_in_dex_cache) {
1117 // Need to test presence of type in dex cache at runtime
1118 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1119 // Not resolved
1120 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001121 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001122 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1124 // Rejoin code paths
1125 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1126 hop_branch->target = hop_target;
1127 }
1128 }
1129 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1130 RegLocation rl_result = GetReturn(false);
1131 if (cu_->instruction_set == kMips) {
1132 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001133 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 }
1135 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1136
1137 /* load object->klass_ */
1138 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001139 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1141 LIR* branchover = NULL;
1142 if (type_known_final) {
1143 // rl_result == ref == null == 0.
1144 if (cu_->instruction_set == kThumb2) {
1145 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001146 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001147 LoadConstant(rl_result.reg, 1); // .eq case - load true
1148 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001149 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001151 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001153 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 }
1155 } else {
1156 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001157 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001158 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 if (!type_known_abstract) {
1160 /* Uses conditional nullification */
1161 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001162 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1164 }
1165 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1166 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001167 if (it != nullptr) {
1168 OpEndIT(it);
1169 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 FreeTemp(r_tgt);
1171 } else {
1172 if (!type_known_abstract) {
1173 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001174 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1176 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001177 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001178 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1179 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1180 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 }
1182 }
1183 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001184 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 /* branch targets here */
1186 LIR* target = NewLIR0(kPseudoTargetLabel);
1187 StoreValue(rl_dest, rl_result);
1188 branch1->target = target;
1189 if (branchover != NULL) {
1190 branchover->target = target;
1191 }
1192}
1193
1194void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1195 bool type_known_final, type_known_abstract, use_declaring_class;
1196 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1197 *cu_->dex_file,
1198 type_idx,
1199 &type_known_final,
1200 &type_known_abstract,
1201 &use_declaring_class);
1202 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1203 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1204
1205 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1206 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1207 } else {
1208 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1209 use_declaring_class, can_assume_type_is_in_dex_cache,
1210 type_idx, rl_dest, rl_src);
1211 }
1212}
1213
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001214void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 bool type_known_final, type_known_abstract, use_declaring_class;
1216 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1217 *cu_->dex_file,
1218 type_idx,
1219 &type_known_final,
1220 &type_known_abstract,
1221 &use_declaring_class);
1222 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1223 // of the exception throw path.
1224 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001225 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 // Verifier type analysis proved this check cast would never cause an exception.
1227 return;
1228 }
1229 FlushAllRegs();
1230 // May generate a call - use explicit registers
1231 LockCallTemps();
1232 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001233 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 if (needs_access_check) {
1235 // Check we have access to type_idx and if not throw IllegalAccessError,
1236 // returns Class* in kRet0
1237 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001238 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 type_idx, TargetReg(kArg1), true);
1240 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1241 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001242 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1243 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 } else {
1245 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001246 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1247 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001248 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001249 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1251 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001252 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1253 LIR* cont = NewLIR0(kPseudoTargetLabel);
1254
1255 // Slow path to initialize the type. Executed if the type is NULL.
1256 class SlowPath : public LIRSlowPath {
1257 public:
1258 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001259 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001260 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1261 class_reg_(class_reg) {
1262 }
1263
1264 void Compile() {
1265 GenerateTargetLabel();
1266
1267 // Call out to helper, which will return resolved type in kArg0
1268 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001269 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001270 m2l_->TargetReg(kArg1), true);
1271 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1272 m2l_->OpUnconditionalBranch(cont_);
1273 }
1274 public:
1275 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001276 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001277 };
1278
buzbee2700f7e2014-03-07 09:46:20 -08001279 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 }
1281 }
1282 // At this point, class_reg (kArg2) has class
1283 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001284
1285 // Slow path for the case where the classes are not equal. In this case we need
1286 // to call a helper function to do the check.
1287 class SlowPath : public LIRSlowPath {
1288 public:
1289 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1290 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1291 }
1292
1293 void Compile() {
1294 GenerateTargetLabel();
1295
1296 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001297 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1298 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001299 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001300 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001301 m2l_->TargetReg(kArg1), true);
1302
1303 m2l_->OpUnconditionalBranch(cont_);
1304 }
1305
1306 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001307 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001308 };
1309
1310 if (type_known_abstract) {
1311 // Easier case, run slow path if target is non-null (slow path will load from target)
1312 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1313 LIR* cont = NewLIR0(kPseudoTargetLabel);
1314 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1315 } else {
1316 // Harder, more common case. We need to generate a forward branch over the load
1317 // if the target is null. If it's non-null we perform the load and branch to the
1318 // slow path if the classes are not equal.
1319
1320 /* Null is OK - continue */
1321 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1322 /* load object->klass_ */
1323 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001324 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001325
1326 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1327 LIR* cont = NewLIR0(kPseudoTargetLabel);
1328
1329 // Add the slow path that will not perform load since this is already done.
1330 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1331
1332 // Set the null check to branch to the continuation.
1333 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 }
1335}
1336
1337void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001338 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 RegLocation rl_result;
1340 if (cu_->instruction_set == kThumb2) {
1341 /*
1342 * NOTE: This is the one place in the code in which we might have
1343 * as many as six live temporary registers. There are 5 in the normal
1344 * set for Arm. Until we have spill capabilities, temporarily add
1345 * lr to the temp set. It is safe to do this locally, but note that
1346 * lr is used explicitly elsewhere in the code generator and cannot
1347 * normally be used as a general temp register.
1348 */
1349 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1350 FreeTemp(TargetReg(kLr)); // and make it available
1351 }
1352 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1353 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1354 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1355 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001356 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1357 RegStorage t_reg = AllocTemp();
1358 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1359 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1360 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 FreeTemp(t_reg);
1362 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001363 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1364 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 }
1366 /*
1367 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1368 * following StoreValueWide might need to allocate a temp register.
1369 * To further work around the lack of a spill capability, explicitly
1370 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1371 * Remove when spill is functional.
1372 */
1373 FreeRegLocTemps(rl_result, rl_src1);
1374 FreeRegLocTemps(rl_result, rl_src2);
1375 StoreValueWide(rl_dest, rl_result);
1376 if (cu_->instruction_set == kThumb2) {
1377 Clobber(TargetReg(kLr));
1378 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1379 }
1380}
1381
1382
1383void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001384 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001385 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386
1387 switch (opcode) {
1388 case Instruction::SHL_LONG:
1389 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001390 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 break;
1392 case Instruction::SHR_LONG:
1393 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001394 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 break;
1396 case Instruction::USHR_LONG:
1397 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001398 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 break;
1400 default:
1401 LOG(FATAL) << "Unexpected case";
1402 }
1403 FlushAllRegs(); /* Send everything to home location */
1404 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1405 RegLocation rl_result = GetReturnWide(false);
1406 StoreValueWide(rl_dest, rl_result);
1407}
1408
1409
1410void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001411 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001412 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 OpKind op = kOpBkpt;
1414 bool is_div_rem = false;
1415 bool check_zero = false;
1416 bool unary = false;
1417 RegLocation rl_result;
1418 bool shift_op = false;
1419 switch (opcode) {
1420 case Instruction::NEG_INT:
1421 op = kOpNeg;
1422 unary = true;
1423 break;
1424 case Instruction::NOT_INT:
1425 op = kOpMvn;
1426 unary = true;
1427 break;
1428 case Instruction::ADD_INT:
1429 case Instruction::ADD_INT_2ADDR:
1430 op = kOpAdd;
1431 break;
1432 case Instruction::SUB_INT:
1433 case Instruction::SUB_INT_2ADDR:
1434 op = kOpSub;
1435 break;
1436 case Instruction::MUL_INT:
1437 case Instruction::MUL_INT_2ADDR:
1438 op = kOpMul;
1439 break;
1440 case Instruction::DIV_INT:
1441 case Instruction::DIV_INT_2ADDR:
1442 check_zero = true;
1443 op = kOpDiv;
1444 is_div_rem = true;
1445 break;
1446 /* NOTE: returns in kArg1 */
1447 case Instruction::REM_INT:
1448 case Instruction::REM_INT_2ADDR:
1449 check_zero = true;
1450 op = kOpRem;
1451 is_div_rem = true;
1452 break;
1453 case Instruction::AND_INT:
1454 case Instruction::AND_INT_2ADDR:
1455 op = kOpAnd;
1456 break;
1457 case Instruction::OR_INT:
1458 case Instruction::OR_INT_2ADDR:
1459 op = kOpOr;
1460 break;
1461 case Instruction::XOR_INT:
1462 case Instruction::XOR_INT_2ADDR:
1463 op = kOpXor;
1464 break;
1465 case Instruction::SHL_INT:
1466 case Instruction::SHL_INT_2ADDR:
1467 shift_op = true;
1468 op = kOpLsl;
1469 break;
1470 case Instruction::SHR_INT:
1471 case Instruction::SHR_INT_2ADDR:
1472 shift_op = true;
1473 op = kOpAsr;
1474 break;
1475 case Instruction::USHR_INT:
1476 case Instruction::USHR_INT_2ADDR:
1477 shift_op = true;
1478 op = kOpLsr;
1479 break;
1480 default:
1481 LOG(FATAL) << "Invalid word arith op: " << opcode;
1482 }
1483 if (!is_div_rem) {
1484 if (unary) {
1485 rl_src1 = LoadValue(rl_src1, kCoreReg);
1486 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001487 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001488 } else {
1489 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001490 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001491 RegStorage t_reg = AllocTemp();
1492 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 rl_src1 = LoadValue(rl_src1, kCoreReg);
1494 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001495 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 FreeTemp(t_reg);
1497 } else {
1498 rl_src1 = LoadValue(rl_src1, kCoreReg);
1499 rl_src2 = LoadValue(rl_src2, kCoreReg);
1500 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001501 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 }
1503 }
1504 StoreValue(rl_dest, rl_result);
1505 } else {
Dave Allison70202782013-10-22 17:52:19 -07001506 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 -07001507 if (cu_->instruction_set == kMips) {
1508 rl_src1 = LoadValue(rl_src1, kCoreReg);
1509 rl_src2 = LoadValue(rl_src2, kCoreReg);
1510 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001511 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512 }
buzbee2700f7e2014-03-07 09:46:20 -08001513 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001514 done = true;
1515 } else if (cu_->instruction_set == kThumb2) {
1516 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1517 // Use ARM SDIV instruction for division. For remainder we also need to
1518 // calculate using a MUL and subtract.
1519 rl_src1 = LoadValue(rl_src1, kCoreReg);
1520 rl_src2 = LoadValue(rl_src2, kCoreReg);
1521 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001522 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001523 }
buzbee2700f7e2014-03-07 09:46:20 -08001524 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001525 done = true;
1526 }
1527 }
1528
1529 // If we haven't already generated the code use the callout function.
1530 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001531 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 FlushAllRegs(); /* Send everything to home location */
1533 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001534 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1536 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001537 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 }
Dave Allison70202782013-10-22 17:52:19 -07001539 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001540 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 if (op == kOpDiv)
1542 rl_result = GetReturn(false);
1543 else
1544 rl_result = GetReturnAlt();
1545 }
1546 StoreValue(rl_dest, rl_result);
1547 }
1548}
1549
1550/*
1551 * The following are the first-level codegen routines that analyze the format
1552 * of each bytecode then either dispatch special purpose codegen routines
1553 * or produce corresponding Thumb instructions directly.
1554 */
1555
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001557static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001558 x &= x - 1;
1559 return (x & (x - 1)) == 0;
1560}
1561
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1563// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001564bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001565 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1567 return false;
1568 }
1569 // No divide instruction for Arm, so check for more special cases
1570 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001571 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001572 }
1573 int k = LowestSetBit(lit);
1574 if (k >= 30) {
1575 // Avoid special cases.
1576 return false;
1577 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 rl_src = LoadValue(rl_src, kCoreReg);
1579 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001580 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001581 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 if (lit == 2) {
1583 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001584 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1585 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1586 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001588 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001589 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001590 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1591 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001592 }
1593 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001594 RegStorage t_reg1 = AllocTemp();
1595 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001597 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1598 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001600 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001601 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001602 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001604 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001606 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001607 }
1608 }
1609 StoreValue(rl_dest, rl_result);
1610 return true;
1611}
1612
1613// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1614// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001615bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001616 if (lit < 0) {
1617 return false;
1618 }
1619 if (lit == 0) {
1620 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1621 LoadConstant(rl_result.reg, 0);
1622 StoreValue(rl_dest, rl_result);
1623 return true;
1624 }
1625 if (lit == 1) {
1626 rl_src = LoadValue(rl_src, kCoreReg);
1627 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1628 OpRegCopy(rl_result.reg, rl_src.reg);
1629 StoreValue(rl_dest, rl_result);
1630 return true;
1631 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001632 // There is RegRegRegShift on Arm, so check for more special cases
1633 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001634 return EasyMultiply(rl_src, rl_dest, lit);
1635 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001636 // Can we simplify this multiplication?
1637 bool power_of_two = false;
1638 bool pop_count_le2 = false;
1639 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001640 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001641 power_of_two = true;
1642 } else if (IsPopCountLE2(lit)) {
1643 pop_count_le2 = true;
1644 } else if (IsPowerOfTwo(lit + 1)) {
1645 power_of_two_minus_one = true;
1646 } else {
1647 return false;
1648 }
1649 rl_src = LoadValue(rl_src, kCoreReg);
1650 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1651 if (power_of_two) {
1652 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001653 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 } else if (pop_count_le2) {
1655 // Shift and add and shift.
1656 int first_bit = LowestSetBit(lit);
1657 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1658 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1659 } else {
1660 // Reverse subtract: (src << (shift + 1)) - src.
1661 DCHECK(power_of_two_minus_one);
1662 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001663 RegStorage t_reg = AllocTemp();
1664 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1665 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001666 }
1667 StoreValue(rl_dest, rl_result);
1668 return true;
1669}
1670
1671void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001672 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 RegLocation rl_result;
1674 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1675 int shift_op = false;
1676 bool is_div = false;
1677
1678 switch (opcode) {
1679 case Instruction::RSUB_INT_LIT8:
1680 case Instruction::RSUB_INT: {
1681 rl_src = LoadValue(rl_src, kCoreReg);
1682 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1683 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001684 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001686 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1687 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 }
1689 StoreValue(rl_dest, rl_result);
1690 return;
1691 }
1692
1693 case Instruction::SUB_INT:
1694 case Instruction::SUB_INT_2ADDR:
1695 lit = -lit;
1696 // Intended fallthrough
1697 case Instruction::ADD_INT:
1698 case Instruction::ADD_INT_2ADDR:
1699 case Instruction::ADD_INT_LIT8:
1700 case Instruction::ADD_INT_LIT16:
1701 op = kOpAdd;
1702 break;
1703 case Instruction::MUL_INT:
1704 case Instruction::MUL_INT_2ADDR:
1705 case Instruction::MUL_INT_LIT8:
1706 case Instruction::MUL_INT_LIT16: {
1707 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1708 return;
1709 }
1710 op = kOpMul;
1711 break;
1712 }
1713 case Instruction::AND_INT:
1714 case Instruction::AND_INT_2ADDR:
1715 case Instruction::AND_INT_LIT8:
1716 case Instruction::AND_INT_LIT16:
1717 op = kOpAnd;
1718 break;
1719 case Instruction::OR_INT:
1720 case Instruction::OR_INT_2ADDR:
1721 case Instruction::OR_INT_LIT8:
1722 case Instruction::OR_INT_LIT16:
1723 op = kOpOr;
1724 break;
1725 case Instruction::XOR_INT:
1726 case Instruction::XOR_INT_2ADDR:
1727 case Instruction::XOR_INT_LIT8:
1728 case Instruction::XOR_INT_LIT16:
1729 op = kOpXor;
1730 break;
1731 case Instruction::SHL_INT_LIT8:
1732 case Instruction::SHL_INT:
1733 case Instruction::SHL_INT_2ADDR:
1734 lit &= 31;
1735 shift_op = true;
1736 op = kOpLsl;
1737 break;
1738 case Instruction::SHR_INT_LIT8:
1739 case Instruction::SHR_INT:
1740 case Instruction::SHR_INT_2ADDR:
1741 lit &= 31;
1742 shift_op = true;
1743 op = kOpAsr;
1744 break;
1745 case Instruction::USHR_INT_LIT8:
1746 case Instruction::USHR_INT:
1747 case Instruction::USHR_INT_2ADDR:
1748 lit &= 31;
1749 shift_op = true;
1750 op = kOpLsr;
1751 break;
1752
1753 case Instruction::DIV_INT:
1754 case Instruction::DIV_INT_2ADDR:
1755 case Instruction::DIV_INT_LIT8:
1756 case Instruction::DIV_INT_LIT16:
1757 case Instruction::REM_INT:
1758 case Instruction::REM_INT_2ADDR:
1759 case Instruction::REM_INT_LIT8:
1760 case Instruction::REM_INT_LIT16: {
1761 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001762 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001763 return;
1764 }
buzbee11b63d12013-08-27 07:34:17 -07001765 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001766 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001767 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 (opcode == Instruction::DIV_INT_LIT16)) {
1769 is_div = true;
1770 } else {
1771 is_div = false;
1772 }
buzbee11b63d12013-08-27 07:34:17 -07001773 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1774 return;
1775 }
Dave Allison70202782013-10-22 17:52:19 -07001776
1777 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001778 if (cu_->instruction_set == kMips) {
1779 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001780 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001781 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001782 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001783 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1784 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001785 } else if (cu_->instruction_set == kThumb2) {
1786 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1787 // Use ARM SDIV instruction for division. For remainder we also need to
1788 // calculate using a MUL and subtract.
1789 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001790 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001791 done = true;
1792 }
1793 }
1794
1795 if (!done) {
1796 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001797 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1798 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001799 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001800 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1801 if (is_div)
1802 rl_result = GetReturn(false);
1803 else
1804 rl_result = GetReturnAlt();
1805 }
1806 StoreValue(rl_dest, rl_result);
1807 return;
1808 }
1809 default:
1810 LOG(FATAL) << "Unexpected opcode " << opcode;
1811 }
1812 rl_src = LoadValue(rl_src, kCoreReg);
1813 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001814 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001816 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001817 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001818 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001819 }
1820 StoreValue(rl_dest, rl_result);
1821}
1822
1823void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001824 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001825 RegLocation rl_result;
1826 OpKind first_op = kOpBkpt;
1827 OpKind second_op = kOpBkpt;
1828 bool call_out = false;
1829 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001830 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001831 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001832
1833 switch (opcode) {
1834 case Instruction::NOT_LONG:
1835 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1836 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1837 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001838 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1839 RegStorage t_reg = AllocTemp();
1840 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1841 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1842 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001843 FreeTemp(t_reg);
1844 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001845 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1846 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001847 }
1848 StoreValueWide(rl_dest, rl_result);
1849 return;
1850 case Instruction::ADD_LONG:
1851 case Instruction::ADD_LONG_2ADDR:
1852 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001853 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001854 return;
1855 }
1856 first_op = kOpAdd;
1857 second_op = kOpAdc;
1858 break;
1859 case Instruction::SUB_LONG:
1860 case Instruction::SUB_LONG_2ADDR:
1861 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001862 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001863 return;
1864 }
1865 first_op = kOpSub;
1866 second_op = kOpSbc;
1867 break;
1868 case Instruction::MUL_LONG:
1869 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001870 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001871 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 return;
1873 } else {
1874 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001875 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001876 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 }
1878 break;
1879 case Instruction::DIV_LONG:
1880 case Instruction::DIV_LONG_2ADDR:
1881 call_out = true;
1882 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001883 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001884 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001885 break;
1886 case Instruction::REM_LONG:
1887 case Instruction::REM_LONG_2ADDR:
1888 call_out = true;
1889 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001890 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001891 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001892 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893 break;
1894 case Instruction::AND_LONG_2ADDR:
1895 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001896 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001897 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001898 }
1899 first_op = kOpAnd;
1900 second_op = kOpAnd;
1901 break;
1902 case Instruction::OR_LONG:
1903 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001904 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001905 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001906 return;
1907 }
1908 first_op = kOpOr;
1909 second_op = kOpOr;
1910 break;
1911 case Instruction::XOR_LONG:
1912 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001913 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001914 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 return;
1916 }
1917 first_op = kOpXor;
1918 second_op = kOpXor;
1919 break;
1920 case Instruction::NEG_LONG: {
1921 GenNegLong(rl_dest, rl_src2);
1922 return;
1923 }
1924 default:
1925 LOG(FATAL) << "Invalid long arith op";
1926 }
1927 if (!call_out) {
1928 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1929 } else {
1930 FlushAllRegs(); /* Send everything to home location */
1931 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001932 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1933 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1934 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1935 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07001936 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08001937 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 // NOTE: callout here is not a safepoint
1939 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1940 } else {
1941 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1942 }
1943 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001944 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001945 rl_result = GetReturnWide(false);
1946 else
1947 rl_result = GetReturnWideAlt();
1948 StoreValueWide(rl_dest, rl_result);
1949 }
1950}
1951
Ian Rogersdd7624d2014-03-14 17:43:00 -07001952void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001953 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 /*
1955 * Don't optimize the register usage since it calls out to support
1956 * functions
1957 */
1958 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1960 if (rl_dest.wide) {
1961 RegLocation rl_result;
1962 rl_result = GetReturnWide(rl_dest.fp);
1963 StoreValueWide(rl_dest, rl_result);
1964 } else {
1965 RegLocation rl_result;
1966 rl_result = GetReturn(rl_dest.fp);
1967 StoreValue(rl_dest, rl_result);
1968 }
1969}
1970
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001971class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
1972 public:
1973 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
1974 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
1975 }
1976
1977 void Compile() OVERRIDE {
1978 m2l_->ResetRegPool();
1979 m2l_->ResetDefTracking();
1980 GenerateTargetLabel(kPseudoSuspendTarget);
1981 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
1982 if (cont_ != nullptr) {
1983 m2l_->OpUnconditionalBranch(cont_);
1984 }
1985 }
1986};
1987
Brian Carlstrom7940e442013-07-12 13:46:57 -07001988/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001989void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001990 if (Runtime::Current()->ExplicitSuspendChecks()) {
1991 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1992 return;
1993 }
1994 FlushAllRegs();
1995 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001996 LIR* cont = NewLIR0(kPseudoTargetLabel);
1997 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08001998 } else {
1999 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2000 return;
2001 }
2002 FlushAllRegs(); // TODO: needed?
2003 LIR* inst = CheckSuspendUsingLoad();
2004 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006}
2007
2008/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002009void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002010 if (Runtime::Current()->ExplicitSuspendChecks()) {
2011 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2012 OpUnconditionalBranch(target);
2013 return;
2014 }
2015 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002016 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002017 LIR* branch = OpUnconditionalBranch(nullptr);
2018 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002019 } else {
2020 // For the implicit suspend check, just perform the trigger
2021 // load and branch to the target.
2022 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2023 OpUnconditionalBranch(target);
2024 return;
2025 }
2026 FlushAllRegs();
2027 LIR* inst = CheckSuspendUsingLoad();
2028 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002031}
2032
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002033/* Call out to helper assembly routine that will null check obj and then lock it. */
2034void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2035 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002036 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002037}
2038
2039/* Call out to helper assembly routine that will null check obj and then unlock it. */
2040void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2041 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002042 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002043}
2044
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002045/* Generic code for generating a wide constant into a VR. */
2046void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2047 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002048 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002049 StoreValueWide(rl_dest, rl_result);
2050}
2051
Brian Carlstrom7940e442013-07-12 13:46:57 -07002052} // namespace art