blob: 0592c74181a07a8b2aaa14deab94f83d6f205cf8 [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 */
Andreas Gampe7e499922015-01-06 08:28:12 -080016
Andreas Gampe0b9203e2015-01-22 20:39:27 -080017#include "mir_to_lir-inl.h"
18
Andreas Gampe7e499922015-01-06 08:28:12 -080019#include <functional>
20
Ian Rogersd582fa42014-11-05 23:46:43 -080021#include "arch/arm/instruction_set_features_arm.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080022#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "dex/compiler_ir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080024#include "dex/mir_graph.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070025#include "dex/quick/arm/arm_lir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080026#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070028#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "mirror/array.h"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000030#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080031#include "mirror/object-inl.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070032#include "mirror/object_reference.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080033#include "utils.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000034#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "verifier/method_verifier.h"
36
37namespace art {
38
Andreas Gampe9c3b0892014-04-24 17:33:34 +000039// Shortcuts to repeatedly used long types.
40typedef mirror::ObjectArray<mirror::Object> ObjArray;
41typedef mirror::ObjectArray<mirror::Class> ClassArray;
42
Brian Carlstrom7940e442013-07-12 13:46:57 -070043/*
44 * This source files contains "gen" codegen routines that should
45 * be applicable to most targets. Only mid-level support utilities
46 * and "op" calls may be used here.
47 */
48
Andreas Gampe0b9203e2015-01-22 20:39:27 -080049ALWAYS_INLINE static inline bool ForceSlowFieldPath(CompilationUnit* cu) {
50 return (cu->enable_debug & (1 << kDebugSlowFieldPath)) != 0;
51}
52
53ALWAYS_INLINE static inline bool ForceSlowStringPath(CompilationUnit* cu) {
54 return (cu->enable_debug & (1 << kDebugSlowStringPath)) != 0;
55}
56
57ALWAYS_INLINE static inline bool ForceSlowTypePath(CompilationUnit* cu) {
58 return (cu->enable_debug & (1 << kDebugSlowTypePath)) != 0;
59}
60
Vladimir Marko5ea536a2015-04-20 20:11:30 +010061void Mir2Lir::GenIfNullUseHelperImm(RegStorage r_result, QuickEntrypointEnum trampoline, int imm) {
Vladimir Marko20f85592015-03-19 10:07:02 +000062 class CallHelperImmMethodSlowPath : public LIRSlowPath {
63 public:
64 CallHelperImmMethodSlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont,
65 QuickEntrypointEnum trampoline_in, int imm_in,
Vladimir Marko5ea536a2015-04-20 20:11:30 +010066 RegStorage r_result_in)
Vladimir Marko20f85592015-03-19 10:07:02 +000067 : LIRSlowPath(m2l, fromfast, cont), trampoline_(trampoline_in),
Vladimir Marko5ea536a2015-04-20 20:11:30 +010068 imm_(imm_in), r_result_(r_result_in) {
Vladimir Marko20f85592015-03-19 10:07:02 +000069 }
70
71 void Compile() {
72 GenerateTargetLabel();
Vladimir Marko5ea536a2015-04-20 20:11:30 +010073 m2l_->CallRuntimeHelperImm(trampoline_, imm_, true);
Vladimir Marko20f85592015-03-19 10:07:02 +000074 m2l_->OpRegCopy(r_result_, m2l_->TargetReg(kRet0, kRef));
75 m2l_->OpUnconditionalBranch(cont_);
76 }
77
78 private:
79 QuickEntrypointEnum trampoline_;
80 const int imm_;
Vladimir Marko20f85592015-03-19 10:07:02 +000081 const RegStorage r_result_;
82 };
83
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 LIR* branch = OpCmpImmBranch(kCondEq, r_result, 0, nullptr);
Vladimir Marko20f85592015-03-19 10:07:02 +000085 LIR* cont = NewLIR0(kPseudoTargetLabel);
86
87 AddSlowPath(new (arena_) CallHelperImmMethodSlowPath(this, branch, cont, trampoline, imm,
Vladimir Marko5ea536a2015-04-20 20:11:30 +010088 r_result));
Vladimir Marko20f85592015-03-19 10:07:02 +000089}
90
Vladimir Marko34773072015-04-07 09:56:48 +010091RegStorage Mir2Lir::GenGetOtherTypeForSgetSput(const MirSFieldLoweringInfo& field_info,
92 int opt_flags) {
93 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
94 // May do runtime call so everything to home locations.
95 FlushAllRegs();
96 RegStorage r_base = TargetReg(kArg0, kRef);
97 LockTemp(r_base);
Vladimir Marko34773072015-04-07 09:56:48 +010098 if (CanUseOpPcRelDexCacheArrayLoad()) {
99 uint32_t offset = dex_cache_arrays_layout_.TypeOffset(field_info.StorageIndex());
100 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, r_base);
101 } else {
102 // Using fixed register to sync with possible call to runtime support.
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100103 RegStorage r_method = LoadCurrMethodWithHint(r_base);
Vladimir Marko34773072015-04-07 09:56:48 +0100104 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
105 kNotVolatile);
106 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
107 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
108 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 // r_base now points at static storage (Class*) or null if the type is not yet resolved.
Vladimir Marko34773072015-04-07 09:56:48 +0100110 LIR* unresolved_branch = nullptr;
111 if (!field_info.IsClassInDexCache() && (opt_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112 // Check if r_base is null.
Vladimir Marko34773072015-04-07 09:56:48 +0100113 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, nullptr);
114 }
115 LIR* uninit_branch = nullptr;
116 if (!field_info.IsClassInitialized() && (opt_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
117 // Check if r_base is not yet initialized class.
118 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
119 LockTemp(r_tmp);
120 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
121 mirror::Class::StatusOffset().Int32Value(),
122 mirror::Class::kStatusInitialized, nullptr, nullptr);
123 FreeTemp(r_tmp);
124 }
125 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
126 //
127 // Slow path to ensure a class is initialized for sget/sput.
128 //
129 class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
130 public:
131 // There are up to two branches to the static field slow path, the "unresolved" when the type
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700132 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
133 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
Vladimir Marko34773072015-04-07 09:56:48 +0100134 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100135 RegStorage r_base_in)
Vladimir Marko34773072015-04-07 09:56:48 +0100136 : LIRSlowPath(m2l, unresolved != nullptr ? unresolved : uninit, cont),
137 second_branch_(unresolved != nullptr ? uninit : nullptr),
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100138 storage_index_(storage_index), r_base_(r_base_in) {
Vladimir Marko34773072015-04-07 09:56:48 +0100139 }
140
141 void Compile() {
142 LIR* target = GenerateTargetLabel();
143 if (second_branch_ != nullptr) {
144 second_branch_->target = target;
145 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100146 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Vladimir Marko34773072015-04-07 09:56:48 +0100147 // Copy helper's result into r_base, a no-op on all but MIPS.
148 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
149
150 m2l_->OpUnconditionalBranch(cont_);
151 }
152
153 private:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700154 // Second branch to the slow path, or null if there's only one branch.
Vladimir Marko34773072015-04-07 09:56:48 +0100155 LIR* const second_branch_;
156
157 const int storage_index_;
158 const RegStorage r_base_;
Vladimir Marko34773072015-04-07 09:56:48 +0100159 };
160
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700161 // The slow path is invoked if the r_base is null or the class pointed
Vladimir Marko34773072015-04-07 09:56:48 +0100162 // to by it is not initialized.
163 LIR* cont = NewLIR0(kPseudoTargetLabel);
164 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100165 field_info.StorageIndex(), r_base));
Vladimir Marko34773072015-04-07 09:56:48 +0100166 }
167 return r_base;
168}
169
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170/*
buzbeeb48819d2013-09-14 16:15:25 -0700171 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 * blocks.
173 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700174void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 LIR* barrier = NewLIR0(kPseudoBarrier);
176 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -0700177 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100178 barrier->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179}
180
Mingyao Yange643a172014-04-08 11:02:52 -0700181void Mir2Lir::GenDivZeroException() {
182 LIR* branch = OpUnconditionalBranch(nullptr);
183 AddDivZeroCheckSlowPath(branch);
184}
185
186void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -0700187 LIR* branch = OpCondBranch(c_code, nullptr);
188 AddDivZeroCheckSlowPath(branch);
189}
190
Mingyao Yange643a172014-04-08 11:02:52 -0700191void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
192 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -0700193 AddDivZeroCheckSlowPath(branch);
194}
195
196void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
197 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
198 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800199 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000200 : LIRSlowPath(m2l, branch_in) {
Mingyao Yang42894562014-04-07 12:42:16 -0700201 }
202
Mingyao Yange643a172014-04-08 11:02:52 -0700203 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -0700204 m2l_->ResetRegPool();
205 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700206 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700207 m2l_->CallRuntimeHelper(kQuickThrowDivZero, true);
Mingyao Yang42894562014-04-07 12:42:16 -0700208 }
209 };
210
211 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
212}
Dave Allisonb373e092014-02-20 16:06:36 -0800213
Mingyao Yang80365d92014-04-18 12:10:58 -0700214void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
215 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
216 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800217 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, RegStorage index_in,
218 RegStorage length_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000219 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800220 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700221 }
222
223 void Compile() OVERRIDE {
224 m2l_->ResetRegPool();
225 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700226 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700227 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700228 }
229
230 private:
231 const RegStorage index_;
232 const RegStorage length_;
233 };
234
235 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
236 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
237}
238
239void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
240 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
241 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800242 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, int index_in, RegStorage length_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000243 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800244 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700245 }
246
247 void Compile() OVERRIDE {
248 m2l_->ResetRegPool();
249 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700250 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700251
Andreas Gampeccc60262014-07-04 18:02:38 -0700252 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
253 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700254
255 m2l_->OpRegCopy(arg1_32, length_);
256 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700257 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700258 }
259
260 private:
261 const int32_t index_;
262 const RegStorage length_;
263 };
264
265 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
266 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
267}
268
Mingyao Yange643a172014-04-08 11:02:52 -0700269LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
270 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
271 public:
272 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000273 : LIRSlowPath(m2l, branch) {
Mingyao Yange643a172014-04-08 11:02:52 -0700274 }
275
276 void Compile() OVERRIDE {
277 m2l_->ResetRegPool();
278 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700279 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700280 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700281 }
282 };
283
284 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
285 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
286 return branch;
287}
288
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800290LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000291 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700292 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 }
Pavel Vyssotski9c3617a2014-11-13 18:25:23 +0600294 // If null check has not been eliminated, reset redundant store tracking.
295 if ((opt_flags & MIR_IGNORE_NULL_CHECK) == 0) {
296 ResetDefTracking();
297 }
Dave Allisonb373e092014-02-20 16:06:36 -0800298 return nullptr;
299}
300
Dave Allisonf9439142014-03-27 15:10:22 -0700301/* Perform an explicit null-check on a register. */
302LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
303 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700304 return nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700305 }
Mingyao Yange643a172014-04-08 11:02:52 -0700306 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700307}
308
Dave Allisonb373e092014-02-20 16:06:36 -0800309void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000310 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800311 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
312 return;
313 }
Dave Allison69dfe512014-07-11 17:11:58 +0000314 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800315 MarkSafepointPC(last_lir_insn_);
316 }
317}
318
Andreas Gampe3c12c512014-06-24 18:46:29 +0000319void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000320 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000321 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
322 return;
323 }
324 MarkSafepointPCAfter(after);
325 }
326}
327
Dave Allisonb373e092014-02-20 16:06:36 -0800328void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000329 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800330 MarkSafepointPC(last_lir_insn_);
331 }
332}
333
buzbee2700f7e2014-03-07 09:46:20 -0800334void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000335 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800336 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
337 return;
338 }
339 // Force an implicit null check by performing a memory operation (load) from the given
340 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800341 RegStorage tmp = AllocTemp();
342 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700343 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800344 FreeTemp(tmp);
345 MarkSafepointPC(load);
346 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347}
348
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700350 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700352 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 switch (opcode) {
354 case Instruction::IF_EQ:
355 cond = kCondEq;
356 break;
357 case Instruction::IF_NE:
358 cond = kCondNe;
359 break;
360 case Instruction::IF_LT:
361 cond = kCondLt;
362 break;
363 case Instruction::IF_GE:
364 cond = kCondGe;
365 break;
366 case Instruction::IF_GT:
367 cond = kCondGt;
368 break;
369 case Instruction::IF_LE:
370 cond = kCondLe;
371 break;
372 default:
373 cond = static_cast<ConditionCode>(0);
374 LOG(FATAL) << "Unexpected opcode " << opcode;
375 }
376
377 // Normalize such that if either operand is constant, src2 will be constant
378 if (rl_src1.is_const) {
379 RegLocation rl_temp = rl_src1;
380 rl_src1 = rl_src2;
381 rl_src2 = rl_temp;
382 cond = FlipComparisonOrder(cond);
383 }
384
buzbee7c02e912014-10-03 13:14:17 -0700385 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 // Is this really an immediate comparison?
387 if (rl_src2.is_const) {
388 // If it's already live in a register or not easily materialized, just keep going
389 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700390 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100392 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800394 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 return;
396 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700397
398 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
399 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
400 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
401 // the 32b literal 0 for null.
402 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
403 // Use the OpCmpImmBranch and ignore the value in the register.
404 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
405 return;
406 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700408
buzbee7c02e912014-10-03 13:14:17 -0700409 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800410 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411}
412
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700413void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700415 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
416 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 switch (opcode) {
418 case Instruction::IF_EQZ:
419 cond = kCondEq;
420 break;
421 case Instruction::IF_NEZ:
422 cond = kCondNe;
423 break;
424 case Instruction::IF_LTZ:
425 cond = kCondLt;
426 break;
427 case Instruction::IF_GEZ:
428 cond = kCondGe;
429 break;
430 case Instruction::IF_GTZ:
431 cond = kCondGt;
432 break;
433 case Instruction::IF_LEZ:
434 cond = kCondLe;
435 break;
436 default:
437 cond = static_cast<ConditionCode>(0);
438 LOG(FATAL) << "Unexpected opcode " << opcode;
439 }
buzbee2700f7e2014-03-07 09:46:20 -0800440 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441}
442
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700443void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
445 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800446 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800448 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 }
buzbee2700f7e2014-03-07 09:46:20 -0800450 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 StoreValueWide(rl_dest, rl_result);
452}
453
Yevgeny Rouban6af82062014-11-26 18:11:54 +0600454void Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
455 rl_src = UpdateLocWide(rl_src);
456 rl_src = NarrowRegLoc(rl_src);
457 StoreValue(rl_dest, rl_src);
458}
459
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700461 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700462 rl_src = LoadValue(rl_src, kCoreReg);
463 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
464 OpKind op = kOpInvalid;
465 switch (opcode) {
466 case Instruction::INT_TO_BYTE:
467 op = kOp2Byte;
468 break;
469 case Instruction::INT_TO_SHORT:
470 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700472 case Instruction::INT_TO_CHAR:
473 op = kOp2Char;
474 break;
475 default:
476 LOG(ERROR) << "Bad int conversion type";
477 }
buzbee2700f7e2014-03-07 09:46:20 -0800478 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700479 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480}
481
Andreas Gampe98430592014-07-27 19:44:50 -0700482/*
483 * Let helper function take care of everything. Will call
484 * Array::AllocFromCode(type_idx, method, count);
485 * Note: AllocFromCode will handle checks for errNegativeArraySize.
486 */
487void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
488 RegLocation rl_src) {
489 FlushAllRegs(); /* Everything to home location */
490 const DexFile* dex_file = cu_->dex_file;
491 CompilerDriver* driver = cu_->compiler_driver;
492 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800493 bool is_type_initialized; // Ignored as an array does not have an initializer.
494 bool use_direct_type_ptr;
495 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700496 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800497 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700498 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
499 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800500 // The fast path.
501 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700502 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800503 CallRuntimeHelperRegRegLocationMethod(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
Andreas Gampe98430592014-07-27 19:44:50 -0700504 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800505 } else {
506 // Use the direct pointer.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800507 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
Andreas Gampe98430592014-07-27 19:44:50 -0700508 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800509 }
510 } else {
511 // The slow path.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800512 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800513 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 } else {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800515 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 }
Andreas Gampe98430592014-07-27 19:44:50 -0700517 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518}
519
520/*
521 * Similar to GenNewArray, but with post-allocation initialization.
522 * Verifier guarantees we're dealing with an array class. Current
523 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
524 * Current code also throws internal unimp if not 'L', '[' or 'I'.
525 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700526void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000527 size_t elems = info->num_arg_words;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 int type_idx = info->index;
529 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700530 QuickEntrypointEnum target;
531 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
532 type_idx)) {
533 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700535 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 }
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800537 CallRuntimeHelperImmImmMethod(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700538 FreeTemp(TargetReg(kArg2, kNotWide));
539 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 /*
541 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
542 * return region. Because AllocFromCode placed the new array
543 * in kRet0, we'll just lock it into place. When debugger support is
544 * added, it may be necessary to additionally copy all return
545 * values to a home location in thread-local storage
546 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700547 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700548 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549
550 // TODO: use the correct component size, currently all supported types
551 // share array alignment with ints (see comment at head of function)
552 size_t component_size = sizeof(int32_t);
553
Vladimir Markobf535be2014-11-19 18:52:35 +0000554 if (elems > 5) {
555 DCHECK(info->is_range); // Non-range insn can't encode more than 5 elems.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 /*
557 * Bit of ugliness here. We're going generate a mem copy loop
558 * on the register range, but it is possible that some regs
559 * in the range have been promoted. This is unlikely, but
560 * before generating the copy, we'll just force a flush
561 * of any regs in the source range that have been promoted to
562 * home location.
563 */
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000564 for (size_t i = 0; i < elems; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 RegLocation loc = UpdateLoc(info->args[i]);
566 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100567 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov27503542014-11-06 14:45:44 +0600568 if (loc.ref) {
569 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
570 } else {
571 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
572 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
574 }
575 /*
576 * TUNING note: generated code here could be much improved, but
577 * this is an uncommon operation and isn't especially performance
578 * critical.
579 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700580 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700581 RegStorage r_src = AllocTempRef();
582 RegStorage r_dst = AllocTempRef();
583 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800584 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700585 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700587 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700588 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 break;
590 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700591 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700592 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 r_val = AllocTemp();
594 break;
595 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100596 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 r_val = AllocTemp();
598 break;
599 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
600 }
601 // Set up source pointer
602 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700603 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700605 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 mirror::Array::DataOffset(component_size).Int32Value());
607 // Set up the loop counter (known to be > 0)
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000608 LoadConstant(r_idx, static_cast<int>(elems - 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800610 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100612 {
613 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
614 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
615 // NOTE: No dalvik register annotation, local optimizations will be stopped
616 // by the loop boundaries.
617 }
buzbee695d13a2014-04-19 13:32:20 -0700618 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800620 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700621 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700623 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 -mirror::Array::DataOffset(component_size).Int32Value());
625 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000626 FreeTemp(r_idx);
627 FreeTemp(r_dst);
628 FreeTemp(r_src);
629 } else {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000630 DCHECK_LE(elems, 5u); // Usually but not necessarily non-range.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 // TUNING: interleave
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000632 for (size_t i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600633 RegLocation rl_arg;
634 if (info->args[i].ref) {
635 rl_arg = LoadValue(info->args[i], kRefReg);
636 StoreRefDisp(ref_reg,
637 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
638 kNotVolatile);
639 } else {
640 rl_arg = LoadValue(info->args[i], kCoreReg);
641 Store32Disp(ref_reg,
642 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
643 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800645 if (IsTemp(rl_arg.reg)) {
646 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 }
648 }
649 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000650 if (elems != 0 && info->args[0].ref) {
651 // If there is at least one potentially non-null value, unconditionally mark the GC card.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000652 for (size_t i = 0; i < elems; i++) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000653 if (!mir_graph_->IsConstantNullRef(info->args[i])) {
654 UnconditionallyMarkGCCard(ref_reg);
655 break;
656 }
657 }
658 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700660 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 }
662}
663
Ian Rogers832336b2014-10-08 15:35:22 -0700664/*
665 * Array data table format:
666 * ushort ident = 0x0300 magic value
667 * ushort width width of each element in the table
668 * uint size number of elements in the table
669 * ubyte data[size*width] table of data values (may contain a single-byte
670 * padding at the end)
671 *
672 * Total size is 4+(width * size + 1)/2 16-bit code units.
673 */
674void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
675 if (kIsDebugBuild) {
676 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
677 const Instruction::ArrayDataPayload* payload =
678 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
679 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
680 }
681 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
682 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
683}
684
Fred Shih37f05ef2014-07-16 18:38:08 -0700685void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000686 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000687 DCHECK_EQ(SPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000688 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800689 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000690 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800691 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000692 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 // Fast path, static storage base is this method's class
buzbeea0cd2d72014-06-01 09:33:49 -0700694 r_base = AllocTempRef();
Vladimir Marko34773072015-04-07 09:56:48 +0100695 RegStorage r_method = LoadCurrMethodWithHint(r_base);
696 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000697 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 } else {
699 // Medium path, static storage base in a different class which requires checks that the other
700 // class is initialized.
Vladimir Marko34773072015-04-07 09:56:48 +0100701 r_base = GenGetOtherTypeForSgetSput(field_info, mir->optimization_flags);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100702 if (!field_info.IsClassInitialized() &&
703 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Marko34773072015-04-07 09:56:48 +0100704 // Ensure load of status and store of value don't re-order.
705 // TODO: Presumably the actual value store is control-dependent on the status load,
706 // and will thus not be reordered in any case, since stores are never speculated.
707 // Does later code "know" that the class is now initialized? If so, we still
708 // need the barrier to guard later static loads.
709 GenMemBarrier(kLoadAny);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100710 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 }
712 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700713 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
714 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100715 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100717 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700719 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000720 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
721 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100722 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700723 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000724 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700726 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000727 MarkGCCard(mir->optimization_flags, rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800729 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 } else {
731 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700732 QuickEntrypointEnum target;
733 switch (size) {
734 case kReference:
735 target = kQuickSetObjStatic;
736 break;
737 case k64:
738 case kDouble:
739 target = kQuickSet64Static;
740 break;
741 case k32:
742 case kSingle:
743 target = kQuickSet32Static;
744 break;
745 case kSignedHalf:
746 case kUnsignedHalf:
747 target = kQuickSet16Static;
748 break;
749 case kSignedByte:
750 case kUnsignedByte:
751 target = kQuickSet8Static;
752 break;
753 case kWord: // Intentional fallthrough.
754 default:
755 LOG(FATAL) << "Can't determine entrypoint for: " << size;
756 target = kQuickSet32Static;
757 }
Andreas Gampe98430592014-07-27 19:44:50 -0700758 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 }
760}
761
Fred Shih37f05ef2014-07-16 18:38:08 -0700762void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000763 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000764 DCHECK_EQ(SGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000765 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700766
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800767 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000768 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800769 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000770 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 // Fast path, static storage base is this method's class
buzbeea0cd2d72014-06-01 09:33:49 -0700772 r_base = AllocTempRef();
Vladimir Marko34773072015-04-07 09:56:48 +0100773 RegStorage r_method = LoadCurrMethodWithHint(r_base);
774 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000775 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776 } else {
777 // Medium path, static storage base in a different class which requires checks that the other
778 // class is initialized
Vladimir Marko34773072015-04-07 09:56:48 +0100779 r_base = GenGetOtherTypeForSgetSput(field_info, mir->optimization_flags);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100780 if (!field_info.IsClassInitialized() &&
781 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Marko34773072015-04-07 09:56:48 +0100782 // Ensure load of status and load of value don't re-order.
783 GenMemBarrier(kLoadAny);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100784 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800786 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700787 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100788 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800789
Vladimir Marko674744e2014-04-24 15:18:26 +0100790 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700791 if (IsRef(size)) {
792 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000793 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
794 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100795 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700796 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000797 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800798 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100799 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800800
Fred Shih37f05ef2014-07-16 18:38:08 -0700801 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 StoreValueWide(rl_dest, rl_result);
803 } else {
804 StoreValue(rl_dest, rl_result);
805 }
806 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700807 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700809 QuickEntrypointEnum target;
810 switch (type) {
811 case Primitive::kPrimNot:
812 target = kQuickGetObjStatic;
813 break;
814 case Primitive::kPrimLong:
815 case Primitive::kPrimDouble:
816 target = kQuickGet64Static;
817 break;
818 case Primitive::kPrimInt:
819 case Primitive::kPrimFloat:
820 target = kQuickGet32Static;
821 break;
822 case Primitive::kPrimShort:
823 target = kQuickGetShortStatic;
824 break;
825 case Primitive::kPrimChar:
826 target = kQuickGetCharStatic;
827 break;
828 case Primitive::kPrimByte:
829 target = kQuickGetByteStatic;
830 break;
831 case Primitive::kPrimBoolean:
832 target = kQuickGetBooleanStatic;
833 break;
834 case Primitive::kPrimVoid: // Intentional fallthrough.
835 default:
836 LOG(FATAL) << "Can't determine entrypoint for: " << type;
837 target = kQuickGet32Static;
838 }
Andreas Gampe98430592014-07-27 19:44:50 -0700839 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
840
Douglas Leung2db3e262014-06-25 16:02:55 -0700841 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700842 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700843 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 StoreValueWide(rl_dest, rl_result);
845 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700846 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 StoreValue(rl_dest, rl_result);
848 }
849 }
850}
851
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800852// Generate code for all slow paths.
853void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700854 // We should check slow_paths_.Size() every time, because a new slow path
855 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100856 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800857 slowpath->Compile();
858 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100859 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800860}
861
Fred Shih37f05ef2014-07-16 18:38:08 -0700862void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
863 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000864 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800865 if (kIsDebugBuild) {
866 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
867 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
868 IGetMemAccessType(mir->dalvikInsn.opcode);
869 DCHECK_EQ(mem_access_type, field_info.MemAccessType()) << mir->dalvikInsn.opcode;
870 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000871 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800872 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700873 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700874 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000875 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700876 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100877 GenNullCheck(rl_obj.reg, opt_flags);
878 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
879 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000880 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700881 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000882 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
883 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100884 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700885 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000886 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100887 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000888 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700889 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700890 StoreValueWide(rl_dest, rl_result);
891 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892 StoreValue(rl_dest, rl_result);
893 }
894 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700895 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
896 QuickEntrypointEnum target;
897 switch (type) {
898 case Primitive::kPrimNot:
899 target = kQuickGetObjInstance;
900 break;
901 case Primitive::kPrimLong:
902 case Primitive::kPrimDouble:
903 target = kQuickGet64Instance;
904 break;
905 case Primitive::kPrimFloat:
906 case Primitive::kPrimInt:
907 target = kQuickGet32Instance;
908 break;
909 case Primitive::kPrimShort:
910 target = kQuickGetShortInstance;
911 break;
912 case Primitive::kPrimChar:
913 target = kQuickGetCharInstance;
914 break;
915 case Primitive::kPrimByte:
916 target = kQuickGetByteInstance;
917 break;
918 case Primitive::kPrimBoolean:
919 target = kQuickGetBooleanInstance;
920 break;
921 case Primitive::kPrimVoid: // Intentional fallthrough.
922 default:
923 LOG(FATAL) << "Can't determine entrypoint for: " << type;
924 target = kQuickGet32Instance;
925 }
Andreas Gampe98430592014-07-27 19:44:50 -0700926 // Second argument of pGetXXInstance is always a reference.
927 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
928 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
929
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700930 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700931 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700932 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 StoreValueWide(rl_dest, rl_result);
934 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700935 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700936 StoreValue(rl_dest, rl_result);
937 }
938 }
939}
940
Vladimir Markobe0e5462014-02-26 11:24:15 +0000941void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700942 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000943 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800944 if (kIsDebugBuild) {
945 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
946 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
947 IPutMemAccessType(mir->dalvikInsn.opcode);
948 DCHECK_EQ(mem_access_type, field_info.MemAccessType());
949 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000950 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800951 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700952 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700953 // Dex code never writes to the class field.
954 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
955 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700956 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700957 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100958 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 } else {
960 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100961 }
962 GenNullCheck(rl_obj.reg, opt_flags);
963 int field_offset = field_info.FieldOffset().Int32Value();
Vladimir Markoee5e2732015-01-13 17:34:28 +0000964 LIR* null_ck_insn;
Fred Shih37f05ef2014-07-16 18:38:08 -0700965 if (IsRef(size)) {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000966 null_ck_insn = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000967 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100968 } else {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000969 null_ck_insn = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
970 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100971 }
Vladimir Markoee5e2732015-01-13 17:34:28 +0000972 MarkPossibleNullPointerExceptionAfter(opt_flags, null_ck_insn);
Fred Shih37f05ef2014-07-16 18:38:08 -0700973 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000974 MarkGCCard(opt_flags, rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 }
976 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700977 QuickEntrypointEnum target;
978 switch (size) {
979 case kReference:
980 target = kQuickSetObjInstance;
981 break;
982 case k64:
983 case kDouble:
984 target = kQuickSet64Instance;
985 break;
986 case k32:
987 case kSingle:
988 target = kQuickSet32Instance;
989 break;
990 case kSignedHalf:
991 case kUnsignedHalf:
992 target = kQuickSet16Instance;
993 break;
994 case kSignedByte:
995 case kUnsignedByte:
996 target = kQuickSet8Instance;
997 break;
998 case kWord: // Intentional fallthrough.
999 default:
1000 LOG(FATAL) << "Can't determine entrypoint for: " << size;
1001 target = kQuickSet32Instance;
1002 }
Andreas Gampe98430592014-07-27 19:44:50 -07001003 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
1004 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005 }
1006}
1007
Ian Rogersa9a82542013-10-04 11:17:26 -07001008void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
1009 RegLocation rl_src) {
1010 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
1011 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
1012 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -07001013 QuickEntrypointEnum target = needs_range_check
1014 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
1015 : kQuickAputObjectWithBoundCheck)
1016 : kQuickAputObject;
1017 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -07001018}
1019
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001020void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001021 RegLocation rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001023 *cu_->dex_file,
1024 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 // Call out to helper which resolves type and verifies access.
1026 // Resolved type returned in kRet0.
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001027 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Vladimir Marko20f85592015-03-19 10:07:02 +00001028 rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001030 rl_result = EvalLoc(rl_dest, kRefReg, true);
1031 // We don't need access checks, load type from dex cache
Vladimir Marko20f85592015-03-19 10:07:02 +00001032 if (CanUseOpPcRelDexCacheArrayLoad()) {
1033 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1034 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, rl_result.reg);
1035 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001036 int32_t dex_cache_offset =
1037 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
1038 RegStorage res_reg = AllocTempRef();
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001039 RegStorage r_method = LoadCurrMethodWithHint(res_reg);
Vladimir Marko20f85592015-03-19 10:07:02 +00001040 LoadRefDisp(r_method, dex_cache_offset, res_reg, kNotVolatile);
1041 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
1042 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
1043 FreeTemp(res_reg);
1044 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001046 type_idx) || ForceSlowTypePath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 // Slow path, at runtime test if type is null and if so initialize
1048 FlushAllRegs();
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001049 GenIfNullUseHelperImm(rl_result.reg, kQuickInitializeType, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 }
1051 }
Vladimir Marko20f85592015-03-19 10:07:02 +00001052 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053}
1054
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001055void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001057 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1058 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001060 *cu_->dex_file, string_idx) || ForceSlowStringPath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 // slow path, resolve string if not in dex cache
1062 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001063 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001064
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 // Might call out to helper, which will return resolved string in kRet0
Vladimir Marko20f85592015-03-19 10:07:02 +00001066 RegStorage ret0 = TargetReg(kRet0, kRef);
Vladimir Marko20f85592015-03-19 10:07:02 +00001067 if (CanUseOpPcRelDexCacheArrayLoad()) {
1068 size_t offset = dex_cache_arrays_layout_.StringOffset(string_idx);
1069 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, ret0);
1070 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001071 // Method to declaring class.
1072 RegStorage arg0 = TargetReg(kArg0, kRef);
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001073 RegStorage r_method = LoadCurrMethodWithHint(arg0);
Vladimir Marko20f85592015-03-19 10:07:02 +00001074 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1075 arg0, kNotVolatile);
1076 // Declaring class to dex cache strings.
1077 LoadRefDisp(arg0, mirror::Class::DexCacheStringsOffset().Int32Value(), arg0, kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001078
Vladimir Marko20f85592015-03-19 10:07:02 +00001079 LoadRefDisp(arg0, offset_of_string, ret0, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001081 GenIfNullUseHelperImm(ret0, kQuickResolveString, string_idx);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001082
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001084 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001086 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Vladimir Marko20f85592015-03-19 10:07:02 +00001087 if (CanUseOpPcRelDexCacheArrayLoad()) {
1088 size_t offset = dex_cache_arrays_layout_.StringOffset(string_idx);
1089 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, rl_result.reg);
1090 } else {
1091 RegLocation rl_method = LoadCurrMethod();
1092 RegStorage res_reg = AllocTempRef();
1093 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), res_reg,
1094 kNotVolatile);
1095 LoadRefDisp(res_reg, mirror::Class::DexCacheStringsOffset().Int32Value(), res_reg,
1096 kNotVolatile);
1097 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
1098 FreeTemp(res_reg);
1099 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 StoreValue(rl_dest, rl_result);
1101 }
1102}
1103
Andreas Gampe98430592014-07-27 19:44:50 -07001104/*
1105 * Let helper function take care of everything. Will
1106 * call Class::NewInstanceFromCode(type_idx, method);
1107 */
1108void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1109 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 // alloc will always check for resolution, do we also need to verify
1111 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001112 const DexFile* dex_file = cu_->dex_file;
1113 CompilerDriver* driver = cu_->compiler_driver;
1114 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001115 bool is_type_initialized;
1116 bool use_direct_type_ptr;
1117 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001118 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001119 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001120 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1121 &direct_type_ptr, &is_finalizable) &&
1122 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001123 // The fast path.
1124 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001125 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001126 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001127 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001128 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001129 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001130 }
1131 } else {
1132 // Use the direct pointer.
1133 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001134 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001135 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001136 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001137 }
1138 }
1139 } else {
1140 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001141 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001142 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001144 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 }
Andreas Gampe98430592014-07-27 19:44:50 -07001146 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147}
1148
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001149void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001151 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152}
1153
1154// For final classes there are no sub-classes to check and so we can answer the instance-of
1155// question with simple comparisons.
1156void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1157 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001158 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001159 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001160
buzbeea0cd2d72014-06-01 09:33:49 -07001161 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001163 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001164 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001166 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 }
1168 LoadConstant(result_reg, 0); // assume false
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001169 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170
buzbeea0cd2d72014-06-01 09:33:49 -07001171 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1172 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001175 RegStorage r_method = LoadCurrMethodWithHint(check_class);
1176 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001177 kNotVolatile);
1178 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1179 kNotVolatile);
Vladimir Marko20f85592015-03-19 10:07:02 +00001180 } else if (CanUseOpPcRelDexCacheArrayLoad()) {
1181 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1182 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, check_class);
1183 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1184 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001186 RegStorage r_method = LoadCurrMethodWithHint(check_class);
1187 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001188 check_class, kNotVolatile);
1189 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1190 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001191 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001192 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 }
1194
buzbee695d13a2014-04-19 13:32:20 -07001195 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 if (cu_->instruction_set == kThumb2) {
1197 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001198 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001200 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001202 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 }
1204 LIR* target = NewLIR0(kPseudoTargetLabel);
1205 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 FreeTemp(object_class);
1207 FreeTemp(check_class);
1208 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001209 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 FreeTemp(result_reg);
1211 }
1212 StoreValue(rl_dest, rl_result);
1213}
1214
1215void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1216 bool type_known_abstract, bool use_declaring_class,
1217 bool can_assume_type_is_in_dex_cache,
1218 uint32_t type_idx, RegLocation rl_dest,
1219 RegLocation rl_src) {
1220 FlushAllRegs();
1221 // May generate a call - use explicit registers
1222 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001223 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001224 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1225 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 if (needs_access_check) {
1227 // Check we have access to type_idx and if not throw IllegalAccessError,
1228 // returns Class* in kArg0
Vladimir Marko20f85592015-03-19 10:07:02 +00001229 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001230 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1231 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001233 RegStorage r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Serguei Katkov9ee45192014-07-17 14:39:03 +07001234 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Vladimir Marko20f85592015-03-19 10:07:02 +00001235 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001236 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001238 if (can_assume_type_is_in_dex_cache) {
1239 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001240 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001241 }
1242
Vladimir Marko20f85592015-03-19 10:07:02 +00001243 if (CanUseOpPcRelDexCacheArrayLoad()) {
1244 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1245 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, class_reg);
1246 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001247 RegStorage r_method = LoadCurrMethodWithHint(class_reg);
Vladimir Marko20f85592015-03-19 10:07:02 +00001248 // Load dex cache entry into class_reg (kArg2)
1249 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1250 class_reg, kNotVolatile);
1251 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
1252 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
1253 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 if (!can_assume_type_is_in_dex_cache) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001255 GenIfNullUseHelperImm(class_reg, kQuickInitializeType, type_idx);
Andreas Gampe90969af2014-07-15 23:02:11 -07001256
1257 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001258 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 }
1260 }
1261 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001262 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001263 if (!IsSameReg(rl_result.reg, ref_reg)) {
1264 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001265 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001267 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268
1269 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001270 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001272 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1273 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001275 LIR* branchover = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001277 // rl_result == ref == class.
1278 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001279 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 } else {
1281 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001282 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001283 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 if (!type_known_abstract) {
1285 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001286 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001287 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001288 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001290 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001292 if (it != nullptr) {
1293 OpEndIT(it);
1294 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 FreeTemp(r_tgt);
1296 } else {
1297 if (!type_known_abstract) {
1298 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001299 LoadConstant(rl_result.reg, 1); // assume true
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001300 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001302
Serguei Katkov9ee45192014-07-17 14:39:03 +07001303 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001304 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 }
1306 }
1307 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001308 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001309 /* branch targets here */
1310 LIR* target = NewLIR0(kPseudoTargetLabel);
1311 StoreValue(rl_dest, rl_result);
1312 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001313 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 branchover->target = target;
1315 }
1316}
1317
1318void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1319 bool type_known_final, type_known_abstract, use_declaring_class;
1320 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1321 *cu_->dex_file,
1322 type_idx,
1323 &type_known_final,
1324 &type_known_abstract,
1325 &use_declaring_class);
1326 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1327 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1328
1329 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1330 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1331 } else {
1332 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1333 use_declaring_class, can_assume_type_is_in_dex_cache,
1334 type_idx, rl_dest, rl_src);
1335 }
1336}
1337
Vladimir Marko22fe45d2015-03-18 11:33:58 +00001338void Mir2Lir::GenCheckCast(int opt_flags, uint32_t insn_idx, uint32_t type_idx,
1339 RegLocation rl_src) {
1340 if ((opt_flags & MIR_IGNORE_CHECK_CAST) != 0) {
1341 // Compiler analysis proved that this check-cast would never cause an exception.
1342 return;
1343 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 bool type_known_final, type_known_abstract, use_declaring_class;
1345 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1346 *cu_->dex_file,
1347 type_idx,
1348 &type_known_final,
1349 &type_known_abstract,
1350 &use_declaring_class);
1351 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1352 // of the exception throw path.
1353 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001354 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 // Verifier type analysis proved this check cast would never cause an exception.
1356 return;
1357 }
1358 FlushAllRegs();
1359 // May generate a call - use explicit registers
1360 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001361 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 if (needs_access_check) {
1363 // Check we have access to type_idx and if not throw IllegalAccessError,
1364 // returns Class* in kRet0
1365 // InitializeTypeAndVerifyAccess(idx, method)
Vladimir Marko20f85592015-03-19 10:07:02 +00001366 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001367 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001369 RegStorage method_reg = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Andreas Gampe4b537a82014-06-30 22:24:53 -07001370 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001371 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 } else {
1373 // Load dex cache entry into class_reg (kArg2)
Vladimir Marko20f85592015-03-19 10:07:02 +00001374 if (CanUseOpPcRelDexCacheArrayLoad()) {
1375 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1376 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, class_reg);
1377 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001378 RegStorage r_method = LoadCurrMethodWithHint(class_reg);
Vladimir Marko20f85592015-03-19 10:07:02 +00001379
1380 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1381 class_reg, kNotVolatile);
1382 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
1383 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
1384 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1386 // Need to test presence of type in dex cache at runtime
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001387 GenIfNullUseHelperImm(class_reg, kQuickInitializeType, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 }
1389 }
1390 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001391 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001392
1393 // Slow path for the case where the classes are not equal. In this case we need
1394 // to call a helper function to do the check.
1395 class SlowPath : public LIRSlowPath {
1396 public:
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001397 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load)
1398 : LIRSlowPath(m2l, fromfast, cont), load_(load) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001399 }
1400
1401 void Compile() {
1402 GenerateTargetLabel();
1403
1404 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001405 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1406 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001407 }
Andreas Gampe98430592014-07-27 19:44:50 -07001408 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1409 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001410 m2l_->OpUnconditionalBranch(cont_);
1411 }
1412
1413 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001414 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001415 };
1416
1417 if (type_known_abstract) {
1418 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001419 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001420 LIR* cont = NewLIR0(kPseudoTargetLabel);
1421 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1422 } else {
1423 // Harder, more common case. We need to generate a forward branch over the load
1424 // if the target is null. If it's non-null we perform the load and branch to the
1425 // slow path if the classes are not equal.
1426
1427 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001428 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001429 /* load object->klass_ */
1430 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001431 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1432 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001433
Andreas Gampeccc60262014-07-04 18:02:38 -07001434 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001435 LIR* cont = NewLIR0(kPseudoTargetLabel);
1436
1437 // Add the slow path that will not perform load since this is already done.
1438 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1439
1440 // Set the null check to branch to the continuation.
1441 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 }
1443}
1444
1445void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001446 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 RegLocation rl_result;
1448 if (cu_->instruction_set == kThumb2) {
1449 /*
1450 * NOTE: This is the one place in the code in which we might have
1451 * as many as six live temporary registers. There are 5 in the normal
1452 * set for Arm. Until we have spill capabilities, temporarily add
1453 * lr to the temp set. It is safe to do this locally, but note that
1454 * lr is used explicitly elsewhere in the code generator and cannot
1455 * normally be used as a general temp register.
1456 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001457 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1458 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 }
1460 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1461 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1462 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1463 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001464 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1465 RegStorage t_reg = AllocTemp();
1466 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1467 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1468 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 FreeTemp(t_reg);
1470 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001471 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1472 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 }
1474 /*
1475 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1476 * following StoreValueWide might need to allocate a temp register.
1477 * To further work around the lack of a spill capability, explicitly
1478 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1479 * Remove when spill is functional.
1480 */
1481 FreeRegLocTemps(rl_result, rl_src1);
1482 FreeRegLocTemps(rl_result, rl_src2);
1483 StoreValueWide(rl_dest, rl_result);
1484 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001485 Clobber(TargetReg(kLr, kNotWide));
1486 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 }
1488}
1489
Andreas Gampe98430592014-07-27 19:44:50 -07001490void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1491 RegLocation rl_src1, RegLocation rl_shift) {
1492 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 switch (opcode) {
1494 case Instruction::SHL_LONG:
1495 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001496 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 break;
1498 case Instruction::SHR_LONG:
1499 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001500 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 break;
1502 case Instruction::USHR_LONG:
1503 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001504 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 break;
1506 default:
1507 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001508 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 }
Andreas Gampe98430592014-07-27 19:44:50 -07001510 FlushAllRegs(); /* Send everything to home location */
1511 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001512 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 StoreValueWide(rl_dest, rl_result);
1514}
1515
1516
1517void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001518 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001519 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 OpKind op = kOpBkpt;
1521 bool is_div_rem = false;
1522 bool check_zero = false;
1523 bool unary = false;
1524 RegLocation rl_result;
1525 bool shift_op = false;
1526 switch (opcode) {
1527 case Instruction::NEG_INT:
1528 op = kOpNeg;
1529 unary = true;
1530 break;
1531 case Instruction::NOT_INT:
1532 op = kOpMvn;
1533 unary = true;
1534 break;
1535 case Instruction::ADD_INT:
1536 case Instruction::ADD_INT_2ADDR:
1537 op = kOpAdd;
1538 break;
1539 case Instruction::SUB_INT:
1540 case Instruction::SUB_INT_2ADDR:
1541 op = kOpSub;
1542 break;
1543 case Instruction::MUL_INT:
1544 case Instruction::MUL_INT_2ADDR:
1545 op = kOpMul;
1546 break;
1547 case Instruction::DIV_INT:
1548 case Instruction::DIV_INT_2ADDR:
1549 check_zero = true;
1550 op = kOpDiv;
1551 is_div_rem = true;
1552 break;
1553 /* NOTE: returns in kArg1 */
1554 case Instruction::REM_INT:
1555 case Instruction::REM_INT_2ADDR:
1556 check_zero = true;
1557 op = kOpRem;
1558 is_div_rem = true;
1559 break;
1560 case Instruction::AND_INT:
1561 case Instruction::AND_INT_2ADDR:
1562 op = kOpAnd;
1563 break;
1564 case Instruction::OR_INT:
1565 case Instruction::OR_INT_2ADDR:
1566 op = kOpOr;
1567 break;
1568 case Instruction::XOR_INT:
1569 case Instruction::XOR_INT_2ADDR:
1570 op = kOpXor;
1571 break;
1572 case Instruction::SHL_INT:
1573 case Instruction::SHL_INT_2ADDR:
1574 shift_op = true;
1575 op = kOpLsl;
1576 break;
1577 case Instruction::SHR_INT:
1578 case Instruction::SHR_INT_2ADDR:
1579 shift_op = true;
1580 op = kOpAsr;
1581 break;
1582 case Instruction::USHR_INT:
1583 case Instruction::USHR_INT_2ADDR:
1584 shift_op = true;
1585 op = kOpLsr;
1586 break;
1587 default:
1588 LOG(FATAL) << "Invalid word arith op: " << opcode;
1589 }
1590 if (!is_div_rem) {
1591 if (unary) {
1592 rl_src1 = LoadValue(rl_src1, kCoreReg);
1593 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001594 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001596 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001597 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001598 RegStorage t_reg = AllocTemp();
1599 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 rl_src1 = LoadValue(rl_src1, kCoreReg);
1601 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001602 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 FreeTemp(t_reg);
1604 } else {
1605 rl_src1 = LoadValue(rl_src1, kCoreReg);
1606 rl_src2 = LoadValue(rl_src2, kCoreReg);
1607 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001608 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 }
1610 }
1611 StoreValue(rl_dest, rl_result);
1612 } else {
Dave Allison70202782013-10-22 17:52:19 -07001613 bool done = false; // Set to true if we happen to find a way to use a real instruction.
Maja Gagic6ea651f2015-02-24 16:55:04 +01001614 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1615 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616 rl_src1 = LoadValue(rl_src1, kCoreReg);
1617 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001618 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001619 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 }
buzbee2700f7e2014-03-07 09:46:20 -08001621 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001622 done = true;
1623 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001624 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001625 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001626 // Use ARM SDIV instruction for division. For remainder we also need to
1627 // calculate using a MUL and subtract.
1628 rl_src1 = LoadValue(rl_src1, kCoreReg);
1629 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001630 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001631 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001632 }
buzbee2700f7e2014-03-07 09:46:20 -08001633 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001634 done = true;
1635 }
1636 }
1637
1638 // If we haven't already generated the code use the callout function.
1639 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001641 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001642 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001643 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001644 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001645 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001646 }
Dave Allison70202782013-10-22 17:52:19 -07001647 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001648 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001650 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 else
1652 rl_result = GetReturnAlt();
1653 }
1654 StoreValue(rl_dest, rl_result);
1655 }
1656}
1657
1658/*
1659 * The following are the first-level codegen routines that analyze the format
1660 * of each bytecode then either dispatch special purpose codegen routines
1661 * or produce corresponding Thumb instructions directly.
1662 */
1663
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001665static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001666 x &= x - 1;
1667 return (x & (x - 1)) == 0;
1668}
1669
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1671// and store the result in 'rl_dest'.
Andreas Gamped500b532015-01-16 22:09:55 -08001672bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode ATTRIBUTE_UNUSED, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001673 RegLocation rl_src, RegLocation rl_dest, int lit) {
Andreas Gamped500b532015-01-16 22:09:55 -08001674 if ((lit < 2) || (!IsPowerOfTwo(lit))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 return false;
1676 }
Andreas Gampe7e499922015-01-06 08:28:12 -08001677 int k = CTZ(lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 if (k >= 30) {
1679 // Avoid special cases.
1680 return false;
1681 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001682 rl_src = LoadValue(rl_src, kCoreReg);
1683 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001684 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001685 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 if (lit == 2) {
1687 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001688 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1689 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1690 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001691 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001692 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001694 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1695 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001696 }
1697 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001698 RegStorage t_reg1 = AllocTemp();
1699 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001700 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001701 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1702 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001704 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001706 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001708 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001710 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 }
1712 }
1713 StoreValue(rl_dest, rl_result);
1714 return true;
1715}
1716
1717// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1718// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001719bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001720 if (lit < 0) {
1721 return false;
1722 }
1723 if (lit == 0) {
1724 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1725 LoadConstant(rl_result.reg, 0);
1726 StoreValue(rl_dest, rl_result);
1727 return true;
1728 }
1729 if (lit == 1) {
1730 rl_src = LoadValue(rl_src, kCoreReg);
1731 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1732 OpRegCopy(rl_result.reg, rl_src.reg);
1733 StoreValue(rl_dest, rl_result);
1734 return true;
1735 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001736 // There is RegRegRegShift on Arm, so check for more special cases
1737 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001738 return EasyMultiply(rl_src, rl_dest, lit);
1739 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001740 // Can we simplify this multiplication?
1741 bool power_of_two = false;
1742 bool pop_count_le2 = false;
1743 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001744 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 power_of_two = true;
1746 } else if (IsPopCountLE2(lit)) {
1747 pop_count_le2 = true;
1748 } else if (IsPowerOfTwo(lit + 1)) {
1749 power_of_two_minus_one = true;
1750 } else {
1751 return false;
1752 }
1753 rl_src = LoadValue(rl_src, kCoreReg);
1754 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1755 if (power_of_two) {
1756 // Shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001757 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, CTZ(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001758 } else if (pop_count_le2) {
1759 // Shift and add and shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001760 int first_bit = CTZ(lit);
1761 int second_bit = CTZ(lit ^ (1 << first_bit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1763 } else {
1764 // Reverse subtract: (src << (shift + 1)) - src.
1765 DCHECK(power_of_two_minus_one);
Andreas Gampe7e499922015-01-06 08:28:12 -08001766 // TUNING: rsb dst, src, src lsl#CTZ(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001767 RegStorage t_reg = AllocTemp();
Andreas Gampe7e499922015-01-06 08:28:12 -08001768 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, CTZ(lit + 1));
buzbee2700f7e2014-03-07 09:46:20 -08001769 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001770 }
1771 StoreValue(rl_dest, rl_result);
1772 return true;
1773}
1774
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001775// Returns true if it generates instructions.
1776bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1777 RegLocation rl_src2) {
1778 if (!rl_src2.is_const ||
1779 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1780 return false;
1781 }
1782
1783 if (!rl_src2.wide) {
1784 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1785 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1786 // Generate multiply by reciprocal instead of div.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001787 float recip = 1.0f/bit_cast<float, int32_t>(divisor);
1788 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<int32_t, float>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001789 return true;
1790 }
1791 } else {
1792 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1793 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1794 // Generate multiply by reciprocal instead of div.
1795 double recip = 1.0/bit_cast<double, int64_t>(divisor);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001796 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<int64_t, double>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001797 return true;
1798 }
1799 }
1800 return false;
1801}
1802
Brian Carlstrom7940e442013-07-12 13:46:57 -07001803void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001804 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001805 RegLocation rl_result;
1806 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1807 int shift_op = false;
1808 bool is_div = false;
1809
1810 switch (opcode) {
1811 case Instruction::RSUB_INT_LIT8:
1812 case Instruction::RSUB_INT: {
1813 rl_src = LoadValue(rl_src, kCoreReg);
1814 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1815 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001816 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001817 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001818 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1819 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820 }
1821 StoreValue(rl_dest, rl_result);
1822 return;
1823 }
1824
1825 case Instruction::SUB_INT:
1826 case Instruction::SUB_INT_2ADDR:
1827 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001828 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001829 case Instruction::ADD_INT:
1830 case Instruction::ADD_INT_2ADDR:
1831 case Instruction::ADD_INT_LIT8:
1832 case Instruction::ADD_INT_LIT16:
1833 op = kOpAdd;
1834 break;
1835 case Instruction::MUL_INT:
1836 case Instruction::MUL_INT_2ADDR:
1837 case Instruction::MUL_INT_LIT8:
1838 case Instruction::MUL_INT_LIT16: {
1839 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1840 return;
1841 }
1842 op = kOpMul;
1843 break;
1844 }
1845 case Instruction::AND_INT:
1846 case Instruction::AND_INT_2ADDR:
1847 case Instruction::AND_INT_LIT8:
1848 case Instruction::AND_INT_LIT16:
1849 op = kOpAnd;
1850 break;
1851 case Instruction::OR_INT:
1852 case Instruction::OR_INT_2ADDR:
1853 case Instruction::OR_INT_LIT8:
1854 case Instruction::OR_INT_LIT16:
1855 op = kOpOr;
1856 break;
1857 case Instruction::XOR_INT:
1858 case Instruction::XOR_INT_2ADDR:
1859 case Instruction::XOR_INT_LIT8:
1860 case Instruction::XOR_INT_LIT16:
1861 op = kOpXor;
1862 break;
1863 case Instruction::SHL_INT_LIT8:
1864 case Instruction::SHL_INT:
1865 case Instruction::SHL_INT_2ADDR:
1866 lit &= 31;
1867 shift_op = true;
1868 op = kOpLsl;
1869 break;
1870 case Instruction::SHR_INT_LIT8:
1871 case Instruction::SHR_INT:
1872 case Instruction::SHR_INT_2ADDR:
1873 lit &= 31;
1874 shift_op = true;
1875 op = kOpAsr;
1876 break;
1877 case Instruction::USHR_INT_LIT8:
1878 case Instruction::USHR_INT:
1879 case Instruction::USHR_INT_2ADDR:
1880 lit &= 31;
1881 shift_op = true;
1882 op = kOpLsr;
1883 break;
1884
1885 case Instruction::DIV_INT:
1886 case Instruction::DIV_INT_2ADDR:
1887 case Instruction::DIV_INT_LIT8:
1888 case Instruction::DIV_INT_LIT16:
1889 case Instruction::REM_INT:
1890 case Instruction::REM_INT_2ADDR:
1891 case Instruction::REM_INT_LIT8:
1892 case Instruction::REM_INT_LIT16: {
1893 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001894 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 return;
1896 }
buzbee11b63d12013-08-27 07:34:17 -07001897 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001898 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001899 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001900 (opcode == Instruction::DIV_INT_LIT16)) {
1901 is_div = true;
1902 } else {
1903 is_div = false;
1904 }
buzbee11b63d12013-08-27 07:34:17 -07001905 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1906 return;
1907 }
Dave Allison70202782013-10-22 17:52:19 -07001908
1909 bool done = false;
Maja Gagic6ea651f2015-02-24 16:55:04 +01001910 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1911 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001912 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001913 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001914 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001915 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001916 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1917 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001918 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001919 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001920 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001921 // Use ARM SDIV instruction for division. For remainder we also need to
1922 // calculate using a MUL and subtract.
1923 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001924 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001925 done = true;
1926 }
1927 }
1928
1929 if (!done) {
1930 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001931 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1932 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001933 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001935 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 else
1937 rl_result = GetReturnAlt();
1938 }
1939 StoreValue(rl_dest, rl_result);
1940 return;
1941 }
1942 default:
1943 LOG(FATAL) << "Unexpected opcode " << opcode;
1944 }
1945 rl_src = LoadValue(rl_src, kCoreReg);
1946 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001947 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001948 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001949 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001951 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 }
1953 StoreValue(rl_dest, rl_result);
1954}
1955
Andreas Gampe98430592014-07-27 19:44:50 -07001956void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001957 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 RegLocation rl_result;
1959 OpKind first_op = kOpBkpt;
1960 OpKind second_op = kOpBkpt;
1961 bool call_out = false;
1962 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001963 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1964 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001965
1966 switch (opcode) {
1967 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001968 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1969 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001970 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001971 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001972 RegStorage t_reg = AllocTemp();
1973 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1974 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1975 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1976 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001977 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001978 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1979 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 }
Andreas Gampe98430592014-07-27 19:44:50 -07001981 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 return;
1983 case Instruction::ADD_LONG:
1984 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985 first_op = kOpAdd;
1986 second_op = kOpAdc;
1987 break;
1988 case Instruction::SUB_LONG:
1989 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 first_op = kOpSub;
1991 second_op = kOpSbc;
1992 break;
1993 case Instruction::MUL_LONG:
1994 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07001995 call_out = true;
1996 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1997 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998 break;
1999 case Instruction::DIV_LONG:
2000 case Instruction::DIV_LONG_2ADDR:
2001 call_out = true;
2002 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002003 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2004 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005 break;
2006 case Instruction::REM_LONG:
2007 case Instruction::REM_LONG_2ADDR:
2008 call_out = true;
2009 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002010 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002012 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2013 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 break;
2015 case Instruction::AND_LONG_2ADDR:
2016 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017 first_op = kOpAnd;
2018 second_op = kOpAnd;
2019 break;
2020 case Instruction::OR_LONG:
2021 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022 first_op = kOpOr;
2023 second_op = kOpOr;
2024 break;
2025 case Instruction::XOR_LONG:
2026 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 first_op = kOpXor;
2028 second_op = kOpXor;
2029 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030 default:
2031 LOG(FATAL) << "Invalid long arith op";
2032 }
2033 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002034 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002035 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002036 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002037 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002038 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2039 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2040 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2041 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002042 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2043 GenDivZeroCheckWide(r_tmp2);
2044 }
Andreas Gampe98430592014-07-27 19:44:50 -07002045 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002047 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002048 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002049 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002050 }
2051 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002052 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2053 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002054 else
Andreas Gampe98430592014-07-27 19:44:50 -07002055 rl_result = GetReturnWideAlt();
2056 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002057 }
2058}
2059
Mark Mendelle87f9b52014-04-30 14:13:18 -04002060void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2061 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2062 LoadConstantNoClobber(rl_result.reg, value);
2063 StoreValue(rl_dest, rl_result);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002064}
2065
Andreas Gampe98430592014-07-27 19:44:50 -07002066void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
Vladimir Markofac10702015-04-22 11:51:52 +01002067 RegLocation rl_src, RegisterClass return_reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002068 /*
2069 * Don't optimize the register usage since it calls out to support
2070 * functions
2071 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002072
Brian Carlstrom7940e442013-07-12 13:46:57 -07002073 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002074 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002075 if (rl_dest.wide) {
Vladimir Markofac10702015-04-22 11:51:52 +01002076 RegLocation rl_result = GetReturnWide(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002077 StoreValueWide(rl_dest, rl_result);
2078 } else {
Vladimir Markofac10702015-04-22 11:51:52 +01002079 RegLocation rl_result = GetReturn(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 StoreValue(rl_dest, rl_result);
2081 }
2082}
2083
Vladimir Marko6ce3eba2015-02-16 13:05:59 +00002084class Mir2Lir::SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002085 public:
2086 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00002087 : LIRSlowPath(m2l, branch, cont) {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002088 }
2089
2090 void Compile() OVERRIDE {
2091 m2l_->ResetRegPool();
2092 m2l_->ResetDefTracking();
2093 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002094 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002095 if (cont_ != nullptr) {
2096 m2l_->OpUnconditionalBranch(cont_);
2097 }
2098 }
2099};
2100
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002102void Mir2Lir::GenSuspendTest(int opt_flags) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002103 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2104 return;
2105 }
Dave Allison69dfe512014-07-11 17:11:58 +00002106 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002107 FlushAllRegs();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002108 LIR* branch = OpTestSuspend(nullptr);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002109 LIR* cont = NewLIR0(kPseudoTargetLabel);
2110 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002111 } else {
Dave Allisonb373e092014-02-20 16:06:36 -08002112 FlushAllRegs(); // TODO: needed?
2113 LIR* inst = CheckSuspendUsingLoad();
2114 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002115 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002116}
2117
2118/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002119void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002120 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2121 OpUnconditionalBranch(target);
2122 return;
2123 }
Dave Allison69dfe512014-07-11 17:11:58 +00002124 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002125 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002126 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002127 LIR* branch = OpUnconditionalBranch(nullptr);
2128 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002129 } else {
2130 // For the implicit suspend check, just perform the trigger
2131 // load and branch to the target.
Dave Allisonb373e092014-02-20 16:06:36 -08002132 FlushAllRegs();
2133 LIR* inst = CheckSuspendUsingLoad();
2134 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002135 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002136 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002137}
2138
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002139/* Call out to helper assembly routine that will null check obj and then lock it. */
2140void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002141 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002142 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002143 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002144}
2145
2146/* Call out to helper assembly routine that will null check obj and then unlock it. */
2147void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002148 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002149 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002150 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002151}
2152
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002153/* Generic code for generating a wide constant into a VR. */
2154void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2155 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002156 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002157 StoreValueWide(rl_dest, rl_result);
2158}
2159
Andreas Gampe48971b32014-08-06 10:09:01 -07002160void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002161 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2162 DCHECK(bb != nullptr);
2163 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002164 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002165 const uint16_t entries = table[1];
2166 // Chained cmp-and-branch.
2167 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002168 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002169 rl_src = LoadValue(rl_src, kCoreReg);
2170 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002171 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002172 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002173 // Switch to using a temp and add.
2174 break;
2175 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002176 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2177 DCHECK(successor_block_info != nullptr);
2178 int case_block_id = successor_block_info->block;
2179 DCHECK_EQ(starting_key + i, successor_block_info->key);
2180 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002181 }
2182 if (i < entries) {
2183 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2184 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2185 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002186 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002187 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2188 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2189 DCHECK(successor_block_info != nullptr);
2190 int case_block_id = successor_block_info->block;
2191 DCHECK_EQ(starting_key + i, successor_block_info->key);
2192 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002193 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2194 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002195 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2196 DCHECK(successor_block_info != nullptr);
2197 int case_block_id = successor_block_info->block;
2198 DCHECK_EQ(starting_key + i, successor_block_info->key);
2199 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002200 } else {
2201 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002202 for (; i < entries; ++i, ++succ_bb_iter) {
2203 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2204 DCHECK(successor_block_info != nullptr);
2205 int case_block_id = successor_block_info->block;
2206 DCHECK_EQ(starting_key + i, successor_block_info->key);
2207 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002208 }
2209 }
2210 }
2211}
2212
2213void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002214 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002215 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002216 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002217 }
2218
2219 const uint16_t entries = table[1];
2220 if (entries <= kSmallSwitchThreshold) {
2221 GenSmallPackedSwitch(mir, table_offset, rl_src);
2222 } else {
2223 // Use the backend-specific implementation.
2224 GenLargePackedSwitch(mir, table_offset, rl_src);
2225 }
2226}
2227
2228void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002229 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2230 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002231 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002232 const uint16_t entries = table[1];
2233 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002234 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002235 int i = 0;
2236 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2237 int case_block_id = successor_block_info->block;
2238 int key = successor_block_info->key;
2239 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2240 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002241 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002242 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002243}
2244
2245void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002246 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002247 if (cu_->verbose) {
2248 DumpSparseSwitchTable(table);
2249 }
2250
2251 const uint16_t entries = table[1];
2252 if (entries <= kSmallSwitchThreshold) {
2253 GenSmallSparseSwitch(mir, table_offset, rl_src);
2254 } else {
2255 // Use the backend-specific implementation.
2256 GenLargeSparseSwitch(mir, table_offset, rl_src);
2257 }
2258}
2259
Fred Shih37f05ef2014-07-16 18:38:08 -07002260bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2261 switch (size) {
2262 case kReference:
2263 return type == Primitive::kPrimNot;
2264 case k64:
2265 case kDouble:
2266 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2267 case k32:
2268 case kSingle:
2269 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2270 case kSignedHalf:
2271 return type == Primitive::kPrimShort;
2272 case kUnsignedHalf:
2273 return type == Primitive::kPrimChar;
2274 case kSignedByte:
2275 return type == Primitive::kPrimByte;
2276 case kUnsignedByte:
2277 return type == Primitive::kPrimBoolean;
2278 case kWord: // Intentional fallthrough.
2279 default:
2280 return false; // There are no sane types with this op size.
2281 }
2282}
2283
Brian Carlstrom7940e442013-07-12 13:46:57 -07002284} // namespace art