blob: 732e77647a000e1b3eed43a6a87f19af83815c1e [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());
Vladimir Marko674744e2014-04-24 15:18:26 +0100503 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
504 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
505 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000506 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800507 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000508 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 // Fast path, static storage base is this method's class
510 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800511 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700512 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800513 if (IsTemp(rl_method.reg)) {
514 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 }
516 } else {
517 // Medium path, static storage base in a different class which requires checks that the other
518 // class is initialized.
519 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000520 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 // May do runtime call so everything to home locations.
522 FlushAllRegs();
523 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800524 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 LockTemp(r_method);
526 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800527 r_base = TargetReg(kArg0);
528 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700529 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000530 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
531 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800532 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000533 if (!field_info.IsInitialized() &&
534 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800535 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800536
537 // The slow path is invoked if the r_base is NULL or the class pointed
538 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800539 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800540 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800541 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800542 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800543 mirror::Class::StatusOffset().Int32Value(),
544 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800545 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800546
buzbee2700f7e2014-03-07 09:46:20 -0800547 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000548 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800549
550 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 FreeTemp(r_method);
553 }
554 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100555 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100557 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100559 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000561 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800562 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100564 StoreBaseDispVolatile(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800565 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100567 } else {
568 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 }
570 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800571 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800573 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 } else {
575 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700576 ThreadOffset<4> setter_offset =
577 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
578 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
579 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000580 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 }
582}
583
Vladimir Markobe0e5462014-02-26 11:24:15 +0000584void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700585 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000586 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
587 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100588 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
589 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
590 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000591 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800592 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000593 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 // Fast path, static storage base is this method's class
595 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800596 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700597 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 } else {
599 // Medium path, static storage base in a different class which requires checks that the other
600 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000601 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 // May do runtime call so everything to home locations.
603 FlushAllRegs();
604 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800605 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 LockTemp(r_method);
607 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800608 r_base = TargetReg(kArg0);
609 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700610 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000611 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
612 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800613 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000614 if (!field_info.IsInitialized() &&
615 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800616 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800617
618 // The slow path is invoked if the r_base is NULL or the class pointed
619 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800620 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800621 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800622 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800623 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800624 mirror::Class::StatusOffset().Int32Value(),
625 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800626 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800627
buzbee2700f7e2014-03-07 09:46:20 -0800628 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000629 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800630
631 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 FreeTemp(r_method);
634 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800635 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100636 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
637 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800638
Vladimir Marko674744e2014-04-24 15:18:26 +0100639 int field_offset = field_info.FieldOffset().Int32Value();
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800640 if (field_info.IsVolatile()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100641 LoadBaseDispVolatile(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800642 // Without context sensitive analysis, we must issue the most conservative barriers.
643 // In this case, either a load or store may follow so we issue both barriers.
644 GenMemBarrier(kLoadLoad);
645 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100646 } else {
647 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800648 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100649 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800650
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 if (is_long_or_double) {
652 StoreValueWide(rl_dest, rl_result);
653 } else {
654 StoreValue(rl_dest, rl_result);
655 }
656 } else {
657 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700658 ThreadOffset<4> getterOffset =
659 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
660 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
661 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000662 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 if (is_long_or_double) {
664 RegLocation rl_result = GetReturnWide(rl_dest.fp);
665 StoreValueWide(rl_dest, rl_result);
666 } else {
667 RegLocation rl_result = GetReturn(rl_dest.fp);
668 StoreValue(rl_dest, rl_result);
669 }
670 }
671}
672
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800673// Generate code for all slow paths.
674void Mir2Lir::HandleSlowPaths() {
675 int n = slow_paths_.Size();
676 for (int i = 0; i < n; ++i) {
677 LIRSlowPath* slowpath = slow_paths_.Get(i);
678 slowpath->Compile();
679 }
680 slow_paths_.Reset();
681}
682
Vladimir Markobe0e5462014-02-26 11:24:15 +0000683void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700685 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000686 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
687 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100688 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
689 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
690 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
691 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000692 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 rl_obj = LoadValue(rl_obj, kCoreReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100694 GenNullCheck(rl_obj.reg, opt_flags);
695 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
696 int field_offset = field_info.FieldOffset().Int32Value();
697 if (field_info.IsVolatile()) {
698 LoadBaseDispVolatile(rl_obj.reg, field_offset, rl_result.reg, load_size);
699 MarkPossibleNullPointerException(opt_flags);
700 // Without context sensitive analysis, we must issue the most conservative barriers.
701 // In this case, either a load or store may follow so we issue both barriers.
702 GenMemBarrier(kLoadLoad);
703 GenMemBarrier(kLoadStore);
704 } else {
705 LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size);
706 MarkPossibleNullPointerException(opt_flags);
707 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 StoreValueWide(rl_dest, rl_result);
710 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 StoreValue(rl_dest, rl_result);
712 }
713 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700714 ThreadOffset<4> getterOffset =
715 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
716 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
717 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000718 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 if (is_long_or_double) {
720 RegLocation rl_result = GetReturnWide(rl_dest.fp);
721 StoreValueWide(rl_dest, rl_result);
722 } else {
723 RegLocation rl_result = GetReturn(rl_dest.fp);
724 StoreValue(rl_dest, rl_result);
725 }
726 }
727}
728
Vladimir Markobe0e5462014-02-26 11:24:15 +0000729void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700731 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000732 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
733 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100734 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
735 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
736 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
737 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000738 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700739 rl_obj = LoadValue(rl_obj, kCoreReg);
740 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100741 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 } else {
743 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100744 }
745 GenNullCheck(rl_obj.reg, opt_flags);
746 int field_offset = field_info.FieldOffset().Int32Value();
747 if (field_info.IsVolatile()) {
748 // There might have been a store before this volatile one so insert StoreStore barrier.
749 GenMemBarrier(kStoreStore);
750 StoreBaseDispVolatile(rl_obj.reg, field_offset, rl_src.reg, store_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800751 MarkPossibleNullPointerException(opt_flags);
Vladimir Marko674744e2014-04-24 15:18:26 +0100752 // A load might follow the volatile store so insert a StoreLoad barrier.
753 GenMemBarrier(kStoreLoad);
754 } else {
755 StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size);
756 MarkPossibleNullPointerException(opt_flags);
757 }
758 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
759 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 }
761 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700762 ThreadOffset<4> setter_offset =
763 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
764 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
765 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000766 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
767 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 }
769}
770
Ian Rogersa9a82542013-10-04 11:17:26 -0700771void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
772 RegLocation rl_src) {
773 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
774 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
775 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700776 ThreadOffset<4> helper = needs_range_check
777 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
778 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
779 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700780 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
781}
782
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700783void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800785 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
787 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
788 *cu_->dex_file,
789 type_idx)) {
790 // Call out to helper which resolves type and verifies access.
791 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700792 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800793 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 RegLocation rl_result = GetReturn(false);
795 StoreValue(rl_dest, rl_result);
796 } else {
797 // We're don't need access checks, load type from dex cache
798 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700799 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700800 Load32Disp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000801 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700802 Load32Disp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
804 type_idx) || SLOW_TYPE_PATH) {
805 // Slow path, at runtime test if type is null and if so initialize
806 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800807 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800808 LIR* cont = NewLIR0(kPseudoTargetLabel);
809
810 // Object to generate the slow path for class resolution.
811 class SlowPath : public LIRSlowPath {
812 public:
813 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
814 const RegLocation& rl_method, const RegLocation& rl_result) :
815 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
816 rl_method_(rl_method), rl_result_(rl_result) {
817 }
818
819 void Compile() {
820 GenerateTargetLabel();
821
Ian Rogersdd7624d2014-03-14 17:43:00 -0700822 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800823 rl_method_.reg, true);
824 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800825
826 m2l_->OpUnconditionalBranch(cont_);
827 }
828
829 private:
830 const int type_idx_;
831 const RegLocation rl_method_;
832 const RegLocation rl_result_;
833 };
834
835 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800836 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800837
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800839 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 // Fast path, we're done - just store result
841 StoreValue(rl_dest, rl_result);
842 }
843 }
844}
845
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700846void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000848 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
849 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
851 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
852 // slow path, resolve string if not in dex cache
853 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700854 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800855
856 // If the Method* is already in a register, we can save a copy.
857 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800858 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800859 if (rl_method.location == kLocPhysReg) {
860 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800861 DCHECK(!IsTemp(rl_method.reg));
862 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800863 } else {
864 r_method = TargetReg(kArg2);
865 LoadCurrMethodDirect(r_method);
866 }
buzbee695d13a2014-04-19 13:32:20 -0700867 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
868 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800869
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 // Might call out to helper, which will return resolved string in kRet0
buzbee695d13a2014-04-19 13:32:20 -0700871 Load32Disp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700872 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
873 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800874
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700875 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800876 // Object to generate the slow path for string resolution.
877 class SlowPath : public LIRSlowPath {
878 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700879 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
880 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
881 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800882 }
883
884 void Compile() {
885 GenerateTargetLabel();
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700886 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
887 r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800888 m2l_->OpUnconditionalBranch(cont_);
889 }
890
891 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700892 const RegStorage r_method_;
893 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800894 };
895
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700896 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700898
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 GenBarrier();
900 StoreValue(rl_dest, GetReturn(false));
901 } else {
902 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800903 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700905 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
906 Load32Disp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 StoreValue(rl_dest, rl_result);
908 }
909}
910
911/*
912 * Let helper function take care of everything. Will
913 * call Class::NewInstanceFromCode(type_idx, method);
914 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700915void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916 FlushAllRegs(); /* Everything to home location */
917 // alloc will always check for resolution, do we also need to verify
918 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -0700919 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800920 const DexFile* dex_file = cu_->dex_file;
921 CompilerDriver* driver = cu_->compiler_driver;
922 if (driver->CanAccessInstantiableTypeWithoutChecks(
923 cu_->method_idx, *dex_file, type_idx)) {
924 bool is_type_initialized;
925 bool use_direct_type_ptr;
926 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700927 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800928 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700929 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
930 &direct_type_ptr, &is_finalizable) &&
931 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800932 // The fast path.
933 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800934 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800935 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700936 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800937 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
938 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700939 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800940 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
941 }
942 } else {
943 // Use the direct pointer.
944 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700945 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800946 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
947 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700948 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800949 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
950 }
951 }
952 } else {
953 // The slow path.
954 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700955 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800956 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
957 }
958 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700960 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800961 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 RegLocation rl_result = GetReturn(false);
964 StoreValue(rl_dest, rl_result);
965}
966
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700967void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700969 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970}
971
972// For final classes there are no sub-classes to check and so we can answer the instance-of
973// question with simple comparisons.
974void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
975 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -0800976 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700977 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -0800978
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 RegLocation object = LoadValue(rl_src, kCoreReg);
980 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800981 RegStorage result_reg = rl_result.reg;
982 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 result_reg = AllocTypedTemp(false, kCoreReg);
984 }
985 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -0800986 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987
buzbee2700f7e2014-03-07 09:46:20 -0800988 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
989 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990
991 LoadCurrMethodDirect(check_class);
992 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -0700993 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
994 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 } else {
buzbee695d13a2014-04-19 13:32:20 -0700996 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
997 check_class);
998 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000999 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001000 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 }
1002
1003 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001004 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005 if (cu_->instruction_set == kThumb2) {
1006 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001007 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001008 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001009 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 } else {
1011 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1012 LoadConstant(result_reg, 1); // eq case - load true
1013 }
1014 LIR* target = NewLIR0(kPseudoTargetLabel);
1015 null_branchover->target = target;
1016 if (ne_branchover != NULL) {
1017 ne_branchover->target = target;
1018 }
1019 FreeTemp(object_class);
1020 FreeTemp(check_class);
1021 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001022 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 FreeTemp(result_reg);
1024 }
1025 StoreValue(rl_dest, rl_result);
1026}
1027
1028void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1029 bool type_known_abstract, bool use_declaring_class,
1030 bool can_assume_type_is_in_dex_cache,
1031 uint32_t type_idx, RegLocation rl_dest,
1032 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -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 Mendell6607d972014-02-10 06:54:18 -08001035
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 FlushAllRegs();
1037 // May generate a call - use explicit registers
1038 LockCallTemps();
1039 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001040 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 if (needs_access_check) {
1042 // Check we have access to type_idx and if not throw IllegalAccessError,
1043 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001044 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 type_idx, true);
1046 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1047 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1048 } else if (use_declaring_class) {
1049 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001050 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001051 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 } else {
1053 // Load dex cache entry into class_reg (kArg2)
1054 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001055 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1056 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001057 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001058 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 if (!can_assume_type_is_in_dex_cache) {
1060 // Need to test presence of type in dex cache at runtime
1061 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1062 // Not resolved
1063 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001064 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001065 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1067 // Rejoin code paths
1068 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1069 hop_branch->target = hop_target;
1070 }
1071 }
1072 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1073 RegLocation rl_result = GetReturn(false);
1074 if (cu_->instruction_set == kMips) {
1075 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001076 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 }
1078 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1079
1080 /* load object->klass_ */
1081 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001082 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1084 LIR* branchover = NULL;
1085 if (type_known_final) {
1086 // rl_result == ref == null == 0.
1087 if (cu_->instruction_set == kThumb2) {
1088 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001089 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001090 LoadConstant(rl_result.reg, 1); // .eq case - load true
1091 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001092 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001094 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001096 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 }
1098 } else {
1099 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001100 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001101 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 if (!type_known_abstract) {
1103 /* Uses conditional nullification */
1104 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001105 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1107 }
1108 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1109 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001110 if (it != nullptr) {
1111 OpEndIT(it);
1112 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 FreeTemp(r_tgt);
1114 } else {
1115 if (!type_known_abstract) {
1116 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001117 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1119 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001120 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001121 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1122 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1123 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 }
1125 }
1126 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001127 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 /* branch targets here */
1129 LIR* target = NewLIR0(kPseudoTargetLabel);
1130 StoreValue(rl_dest, rl_result);
1131 branch1->target = target;
1132 if (branchover != NULL) {
1133 branchover->target = target;
1134 }
1135}
1136
1137void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1138 bool type_known_final, type_known_abstract, use_declaring_class;
1139 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1140 *cu_->dex_file,
1141 type_idx,
1142 &type_known_final,
1143 &type_known_abstract,
1144 &use_declaring_class);
1145 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1146 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1147
1148 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1149 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1150 } else {
1151 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1152 use_declaring_class, can_assume_type_is_in_dex_cache,
1153 type_idx, rl_dest, rl_src);
1154 }
1155}
1156
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 bool type_known_final, type_known_abstract, use_declaring_class;
1159 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1160 *cu_->dex_file,
1161 type_idx,
1162 &type_known_final,
1163 &type_known_abstract,
1164 &use_declaring_class);
1165 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1166 // of the exception throw path.
1167 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001168 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 // Verifier type analysis proved this check cast would never cause an exception.
1170 return;
1171 }
1172 FlushAllRegs();
1173 // May generate a call - use explicit registers
1174 LockCallTemps();
1175 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001176 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 if (needs_access_check) {
1178 // Check we have access to type_idx and if not throw IllegalAccessError,
1179 // returns Class* in kRet0
1180 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001181 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 type_idx, TargetReg(kArg1), true);
1183 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1184 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001185 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1186 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 } else {
1188 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001189 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1190 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001191 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001192 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1194 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001195 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1196 LIR* cont = NewLIR0(kPseudoTargetLabel);
1197
1198 // Slow path to initialize the type. Executed if the type is NULL.
1199 class SlowPath : public LIRSlowPath {
1200 public:
1201 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001202 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001203 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1204 class_reg_(class_reg) {
1205 }
1206
1207 void Compile() {
1208 GenerateTargetLabel();
1209
1210 // Call out to helper, which will return resolved type in kArg0
1211 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001212 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001213 m2l_->TargetReg(kArg1), true);
1214 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1215 m2l_->OpUnconditionalBranch(cont_);
1216 }
1217 public:
1218 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001219 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001220 };
1221
buzbee2700f7e2014-03-07 09:46:20 -08001222 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 }
1224 }
1225 // At this point, class_reg (kArg2) has class
1226 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001227
1228 // Slow path for the case where the classes are not equal. In this case we need
1229 // to call a helper function to do the check.
1230 class SlowPath : public LIRSlowPath {
1231 public:
1232 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1233 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1234 }
1235
1236 void Compile() {
1237 GenerateTargetLabel();
1238
1239 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001240 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1241 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001242 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001243 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001244 m2l_->TargetReg(kArg1), true);
1245
1246 m2l_->OpUnconditionalBranch(cont_);
1247 }
1248
1249 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001250 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001251 };
1252
1253 if (type_known_abstract) {
1254 // Easier case, run slow path if target is non-null (slow path will load from target)
1255 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1256 LIR* cont = NewLIR0(kPseudoTargetLabel);
1257 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1258 } else {
1259 // Harder, more common case. We need to generate a forward branch over the load
1260 // if the target is null. If it's non-null we perform the load and branch to the
1261 // slow path if the classes are not equal.
1262
1263 /* Null is OK - continue */
1264 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1265 /* load object->klass_ */
1266 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001267 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001268
1269 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1270 LIR* cont = NewLIR0(kPseudoTargetLabel);
1271
1272 // Add the slow path that will not perform load since this is already done.
1273 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1274
1275 // Set the null check to branch to the continuation.
1276 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 }
1278}
1279
1280void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001281 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 RegLocation rl_result;
1283 if (cu_->instruction_set == kThumb2) {
1284 /*
1285 * NOTE: This is the one place in the code in which we might have
1286 * as many as six live temporary registers. There are 5 in the normal
1287 * set for Arm. Until we have spill capabilities, temporarily add
1288 * lr to the temp set. It is safe to do this locally, but note that
1289 * lr is used explicitly elsewhere in the code generator and cannot
1290 * normally be used as a general temp register.
1291 */
1292 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1293 FreeTemp(TargetReg(kLr)); // and make it available
1294 }
1295 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1296 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1297 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1298 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001299 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1300 RegStorage t_reg = AllocTemp();
1301 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1302 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1303 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001304 FreeTemp(t_reg);
1305 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001306 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1307 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 }
1309 /*
1310 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1311 * following StoreValueWide might need to allocate a temp register.
1312 * To further work around the lack of a spill capability, explicitly
1313 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1314 * Remove when spill is functional.
1315 */
1316 FreeRegLocTemps(rl_result, rl_src1);
1317 FreeRegLocTemps(rl_result, rl_src2);
1318 StoreValueWide(rl_dest, rl_result);
1319 if (cu_->instruction_set == kThumb2) {
1320 Clobber(TargetReg(kLr));
1321 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1322 }
1323}
1324
1325
1326void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001327 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001328 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329
1330 switch (opcode) {
1331 case Instruction::SHL_LONG:
1332 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001333 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 break;
1335 case Instruction::SHR_LONG:
1336 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001337 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338 break;
1339 case Instruction::USHR_LONG:
1340 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001341 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 break;
1343 default:
1344 LOG(FATAL) << "Unexpected case";
1345 }
1346 FlushAllRegs(); /* Send everything to home location */
1347 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1348 RegLocation rl_result = GetReturnWide(false);
1349 StoreValueWide(rl_dest, rl_result);
1350}
1351
1352
1353void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001354 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001355 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 OpKind op = kOpBkpt;
1357 bool is_div_rem = false;
1358 bool check_zero = false;
1359 bool unary = false;
1360 RegLocation rl_result;
1361 bool shift_op = false;
1362 switch (opcode) {
1363 case Instruction::NEG_INT:
1364 op = kOpNeg;
1365 unary = true;
1366 break;
1367 case Instruction::NOT_INT:
1368 op = kOpMvn;
1369 unary = true;
1370 break;
1371 case Instruction::ADD_INT:
1372 case Instruction::ADD_INT_2ADDR:
1373 op = kOpAdd;
1374 break;
1375 case Instruction::SUB_INT:
1376 case Instruction::SUB_INT_2ADDR:
1377 op = kOpSub;
1378 break;
1379 case Instruction::MUL_INT:
1380 case Instruction::MUL_INT_2ADDR:
1381 op = kOpMul;
1382 break;
1383 case Instruction::DIV_INT:
1384 case Instruction::DIV_INT_2ADDR:
1385 check_zero = true;
1386 op = kOpDiv;
1387 is_div_rem = true;
1388 break;
1389 /* NOTE: returns in kArg1 */
1390 case Instruction::REM_INT:
1391 case Instruction::REM_INT_2ADDR:
1392 check_zero = true;
1393 op = kOpRem;
1394 is_div_rem = true;
1395 break;
1396 case Instruction::AND_INT:
1397 case Instruction::AND_INT_2ADDR:
1398 op = kOpAnd;
1399 break;
1400 case Instruction::OR_INT:
1401 case Instruction::OR_INT_2ADDR:
1402 op = kOpOr;
1403 break;
1404 case Instruction::XOR_INT:
1405 case Instruction::XOR_INT_2ADDR:
1406 op = kOpXor;
1407 break;
1408 case Instruction::SHL_INT:
1409 case Instruction::SHL_INT_2ADDR:
1410 shift_op = true;
1411 op = kOpLsl;
1412 break;
1413 case Instruction::SHR_INT:
1414 case Instruction::SHR_INT_2ADDR:
1415 shift_op = true;
1416 op = kOpAsr;
1417 break;
1418 case Instruction::USHR_INT:
1419 case Instruction::USHR_INT_2ADDR:
1420 shift_op = true;
1421 op = kOpLsr;
1422 break;
1423 default:
1424 LOG(FATAL) << "Invalid word arith op: " << opcode;
1425 }
1426 if (!is_div_rem) {
1427 if (unary) {
1428 rl_src1 = LoadValue(rl_src1, kCoreReg);
1429 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001430 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431 } else {
1432 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001433 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001434 RegStorage t_reg = AllocTemp();
1435 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 rl_src1 = LoadValue(rl_src1, kCoreReg);
1437 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001438 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001439 FreeTemp(t_reg);
1440 } else {
1441 rl_src1 = LoadValue(rl_src1, kCoreReg);
1442 rl_src2 = LoadValue(rl_src2, kCoreReg);
1443 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001444 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 }
1446 }
1447 StoreValue(rl_dest, rl_result);
1448 } else {
Dave Allison70202782013-10-22 17:52:19 -07001449 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 -07001450 if (cu_->instruction_set == kMips) {
1451 rl_src1 = LoadValue(rl_src1, kCoreReg);
1452 rl_src2 = LoadValue(rl_src2, kCoreReg);
1453 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001454 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001455 }
buzbee2700f7e2014-03-07 09:46:20 -08001456 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001457 done = true;
1458 } else if (cu_->instruction_set == kThumb2) {
1459 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1460 // Use ARM SDIV instruction for division. For remainder we also need to
1461 // calculate using a MUL and subtract.
1462 rl_src1 = LoadValue(rl_src1, kCoreReg);
1463 rl_src2 = LoadValue(rl_src2, kCoreReg);
1464 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001465 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001466 }
buzbee2700f7e2014-03-07 09:46:20 -08001467 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001468 done = true;
1469 }
1470 }
1471
1472 // If we haven't already generated the code use the callout function.
1473 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001474 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 FlushAllRegs(); /* Send everything to home location */
1476 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001477 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1479 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001480 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 }
Dave Allison70202782013-10-22 17:52:19 -07001482 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001483 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484 if (op == kOpDiv)
1485 rl_result = GetReturn(false);
1486 else
1487 rl_result = GetReturnAlt();
1488 }
1489 StoreValue(rl_dest, rl_result);
1490 }
1491}
1492
1493/*
1494 * The following are the first-level codegen routines that analyze the format
1495 * of each bytecode then either dispatch special purpose codegen routines
1496 * or produce corresponding Thumb instructions directly.
1497 */
1498
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001500static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 x &= x - 1;
1502 return (x & (x - 1)) == 0;
1503}
1504
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1506// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001507bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001508 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1510 return false;
1511 }
1512 // No divide instruction for Arm, so check for more special cases
1513 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001514 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 }
1516 int k = LowestSetBit(lit);
1517 if (k >= 30) {
1518 // Avoid special cases.
1519 return false;
1520 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 rl_src = LoadValue(rl_src, kCoreReg);
1522 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001523 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001524 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 if (lit == 2) {
1526 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001527 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1528 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1529 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001531 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001533 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1534 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 }
1536 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001537 RegStorage t_reg1 = AllocTemp();
1538 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001539 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001540 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1541 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001542 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001543 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001544 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001545 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001547 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001549 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001550 }
1551 }
1552 StoreValue(rl_dest, rl_result);
1553 return true;
1554}
1555
1556// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1557// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001558bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001559 if (lit < 0) {
1560 return false;
1561 }
1562 if (lit == 0) {
1563 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1564 LoadConstant(rl_result.reg, 0);
1565 StoreValue(rl_dest, rl_result);
1566 return true;
1567 }
1568 if (lit == 1) {
1569 rl_src = LoadValue(rl_src, kCoreReg);
1570 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1571 OpRegCopy(rl_result.reg, rl_src.reg);
1572 StoreValue(rl_dest, rl_result);
1573 return true;
1574 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001575 // There is RegRegRegShift on Arm, so check for more special cases
1576 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001577 return EasyMultiply(rl_src, rl_dest, lit);
1578 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 // Can we simplify this multiplication?
1580 bool power_of_two = false;
1581 bool pop_count_le2 = false;
1582 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001583 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 power_of_two = true;
1585 } else if (IsPopCountLE2(lit)) {
1586 pop_count_le2 = true;
1587 } else if (IsPowerOfTwo(lit + 1)) {
1588 power_of_two_minus_one = true;
1589 } else {
1590 return false;
1591 }
1592 rl_src = LoadValue(rl_src, kCoreReg);
1593 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1594 if (power_of_two) {
1595 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001596 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001597 } else if (pop_count_le2) {
1598 // Shift and add and shift.
1599 int first_bit = LowestSetBit(lit);
1600 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1601 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1602 } else {
1603 // Reverse subtract: (src << (shift + 1)) - src.
1604 DCHECK(power_of_two_minus_one);
1605 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001606 RegStorage t_reg = AllocTemp();
1607 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1608 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 }
1610 StoreValue(rl_dest, rl_result);
1611 return true;
1612}
1613
1614void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001615 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616 RegLocation rl_result;
1617 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1618 int shift_op = false;
1619 bool is_div = false;
1620
1621 switch (opcode) {
1622 case Instruction::RSUB_INT_LIT8:
1623 case Instruction::RSUB_INT: {
1624 rl_src = LoadValue(rl_src, kCoreReg);
1625 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1626 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001627 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001629 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1630 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 }
1632 StoreValue(rl_dest, rl_result);
1633 return;
1634 }
1635
1636 case Instruction::SUB_INT:
1637 case Instruction::SUB_INT_2ADDR:
1638 lit = -lit;
1639 // Intended fallthrough
1640 case Instruction::ADD_INT:
1641 case Instruction::ADD_INT_2ADDR:
1642 case Instruction::ADD_INT_LIT8:
1643 case Instruction::ADD_INT_LIT16:
1644 op = kOpAdd;
1645 break;
1646 case Instruction::MUL_INT:
1647 case Instruction::MUL_INT_2ADDR:
1648 case Instruction::MUL_INT_LIT8:
1649 case Instruction::MUL_INT_LIT16: {
1650 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1651 return;
1652 }
1653 op = kOpMul;
1654 break;
1655 }
1656 case Instruction::AND_INT:
1657 case Instruction::AND_INT_2ADDR:
1658 case Instruction::AND_INT_LIT8:
1659 case Instruction::AND_INT_LIT16:
1660 op = kOpAnd;
1661 break;
1662 case Instruction::OR_INT:
1663 case Instruction::OR_INT_2ADDR:
1664 case Instruction::OR_INT_LIT8:
1665 case Instruction::OR_INT_LIT16:
1666 op = kOpOr;
1667 break;
1668 case Instruction::XOR_INT:
1669 case Instruction::XOR_INT_2ADDR:
1670 case Instruction::XOR_INT_LIT8:
1671 case Instruction::XOR_INT_LIT16:
1672 op = kOpXor;
1673 break;
1674 case Instruction::SHL_INT_LIT8:
1675 case Instruction::SHL_INT:
1676 case Instruction::SHL_INT_2ADDR:
1677 lit &= 31;
1678 shift_op = true;
1679 op = kOpLsl;
1680 break;
1681 case Instruction::SHR_INT_LIT8:
1682 case Instruction::SHR_INT:
1683 case Instruction::SHR_INT_2ADDR:
1684 lit &= 31;
1685 shift_op = true;
1686 op = kOpAsr;
1687 break;
1688 case Instruction::USHR_INT_LIT8:
1689 case Instruction::USHR_INT:
1690 case Instruction::USHR_INT_2ADDR:
1691 lit &= 31;
1692 shift_op = true;
1693 op = kOpLsr;
1694 break;
1695
1696 case Instruction::DIV_INT:
1697 case Instruction::DIV_INT_2ADDR:
1698 case Instruction::DIV_INT_LIT8:
1699 case Instruction::DIV_INT_LIT16:
1700 case Instruction::REM_INT:
1701 case Instruction::REM_INT_2ADDR:
1702 case Instruction::REM_INT_LIT8:
1703 case Instruction::REM_INT_LIT16: {
1704 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001705 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 return;
1707 }
buzbee11b63d12013-08-27 07:34:17 -07001708 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001710 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 (opcode == Instruction::DIV_INT_LIT16)) {
1712 is_div = true;
1713 } else {
1714 is_div = false;
1715 }
buzbee11b63d12013-08-27 07:34:17 -07001716 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1717 return;
1718 }
Dave Allison70202782013-10-22 17:52:19 -07001719
1720 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 if (cu_->instruction_set == kMips) {
1722 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001723 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001724 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001725 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001726 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1727 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001728 } else if (cu_->instruction_set == kThumb2) {
1729 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1730 // Use ARM SDIV instruction for division. For remainder we also need to
1731 // calculate using a MUL and subtract.
1732 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001733 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001734 done = true;
1735 }
1736 }
1737
1738 if (!done) {
1739 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001740 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1741 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001742 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001743 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1744 if (is_div)
1745 rl_result = GetReturn(false);
1746 else
1747 rl_result = GetReturnAlt();
1748 }
1749 StoreValue(rl_dest, rl_result);
1750 return;
1751 }
1752 default:
1753 LOG(FATAL) << "Unexpected opcode " << opcode;
1754 }
1755 rl_src = LoadValue(rl_src, kCoreReg);
1756 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001757 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001758 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001759 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001760 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001761 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 }
1763 StoreValue(rl_dest, rl_result);
1764}
1765
1766void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001767 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 RegLocation rl_result;
1769 OpKind first_op = kOpBkpt;
1770 OpKind second_op = kOpBkpt;
1771 bool call_out = false;
1772 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001773 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001774 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001775
1776 switch (opcode) {
1777 case Instruction::NOT_LONG:
1778 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1779 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1780 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001781 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1782 RegStorage t_reg = AllocTemp();
1783 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1784 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1785 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 FreeTemp(t_reg);
1787 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001788 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1789 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001790 }
1791 StoreValueWide(rl_dest, rl_result);
1792 return;
1793 case Instruction::ADD_LONG:
1794 case Instruction::ADD_LONG_2ADDR:
1795 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001796 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001797 return;
1798 }
1799 first_op = kOpAdd;
1800 second_op = kOpAdc;
1801 break;
1802 case Instruction::SUB_LONG:
1803 case Instruction::SUB_LONG_2ADDR:
1804 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001805 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 return;
1807 }
1808 first_op = kOpSub;
1809 second_op = kOpSbc;
1810 break;
1811 case Instruction::MUL_LONG:
1812 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001813 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001814 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 return;
1816 } else {
1817 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001818 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001819 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820 }
1821 break;
1822 case Instruction::DIV_LONG:
1823 case Instruction::DIV_LONG_2ADDR:
1824 call_out = true;
1825 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001826 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001827 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001828 break;
1829 case Instruction::REM_LONG:
1830 case Instruction::REM_LONG_2ADDR:
1831 call_out = true;
1832 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001833 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001835 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001836 break;
1837 case Instruction::AND_LONG_2ADDR:
1838 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001839 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001840 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001841 }
1842 first_op = kOpAnd;
1843 second_op = kOpAnd;
1844 break;
1845 case Instruction::OR_LONG:
1846 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001847 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001848 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001849 return;
1850 }
1851 first_op = kOpOr;
1852 second_op = kOpOr;
1853 break;
1854 case Instruction::XOR_LONG:
1855 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001856 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001857 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 return;
1859 }
1860 first_op = kOpXor;
1861 second_op = kOpXor;
1862 break;
1863 case Instruction::NEG_LONG: {
1864 GenNegLong(rl_dest, rl_src2);
1865 return;
1866 }
1867 default:
1868 LOG(FATAL) << "Invalid long arith op";
1869 }
1870 if (!call_out) {
1871 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1872 } else {
1873 FlushAllRegs(); /* Send everything to home location */
1874 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001875 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1876 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1877 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1878 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07001879 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08001880 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001881 // NOTE: callout here is not a safepoint
1882 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1883 } else {
1884 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1885 }
1886 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001887 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001888 rl_result = GetReturnWide(false);
1889 else
1890 rl_result = GetReturnWideAlt();
1891 StoreValueWide(rl_dest, rl_result);
1892 }
1893}
1894
Ian Rogersdd7624d2014-03-14 17:43:00 -07001895void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001896 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 /*
1898 * Don't optimize the register usage since it calls out to support
1899 * functions
1900 */
1901 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001902 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1903 if (rl_dest.wide) {
1904 RegLocation rl_result;
1905 rl_result = GetReturnWide(rl_dest.fp);
1906 StoreValueWide(rl_dest, rl_result);
1907 } else {
1908 RegLocation rl_result;
1909 rl_result = GetReturn(rl_dest.fp);
1910 StoreValue(rl_dest, rl_result);
1911 }
1912}
1913
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001914class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
1915 public:
1916 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
1917 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
1918 }
1919
1920 void Compile() OVERRIDE {
1921 m2l_->ResetRegPool();
1922 m2l_->ResetDefTracking();
1923 GenerateTargetLabel(kPseudoSuspendTarget);
1924 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
1925 if (cont_ != nullptr) {
1926 m2l_->OpUnconditionalBranch(cont_);
1927 }
1928 }
1929};
1930
Brian Carlstrom7940e442013-07-12 13:46:57 -07001931/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001932void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001933 if (Runtime::Current()->ExplicitSuspendChecks()) {
1934 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1935 return;
1936 }
1937 FlushAllRegs();
1938 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001939 LIR* cont = NewLIR0(kPseudoTargetLabel);
1940 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08001941 } else {
1942 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1943 return;
1944 }
1945 FlushAllRegs(); // TODO: needed?
1946 LIR* inst = CheckSuspendUsingLoad();
1947 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001948 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001949}
1950
1951/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001952void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08001953 if (Runtime::Current()->ExplicitSuspendChecks()) {
1954 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1955 OpUnconditionalBranch(target);
1956 return;
1957 }
1958 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08001959 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001960 LIR* branch = OpUnconditionalBranch(nullptr);
1961 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08001962 } else {
1963 // For the implicit suspend check, just perform the trigger
1964 // load and branch to the target.
1965 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1966 OpUnconditionalBranch(target);
1967 return;
1968 }
1969 FlushAllRegs();
1970 LIR* inst = CheckSuspendUsingLoad();
1971 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974}
1975
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001976/* Call out to helper assembly routine that will null check obj and then lock it. */
1977void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
1978 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001979 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001980}
1981
1982/* Call out to helper assembly routine that will null check obj and then unlock it. */
1983void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
1984 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001985 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001986}
1987
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001988/* Generic code for generating a wide constant into a VR. */
1989void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
1990 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001991 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001992 StoreValueWide(rl_dest, rl_result);
1993}
1994
Brian Carlstrom7940e442013-07-12 13:46:57 -07001995} // namespace art