blob: af108170e6581684944b74b40e69d555d19b1d14 [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"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex/compiler_ir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "dex/mir_graph.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070026#include "dex/quick/arm/arm_lir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080027#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070029#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "mirror/array.h"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000031#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080032#include "mirror/object-inl.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070033#include "mirror/object_reference.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());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, r_base, false);
Vladimir Marko34773072015-04-07 09:56:48 +0100101 } 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);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104 LoadRefDisp(r_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
Vladimir Marko34773072015-04-07 09:56:48 +0100105 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);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700696 LoadRefDisp(r_method, 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);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700774 LoadRefDisp(r_method, 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);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001034 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, rl_result.reg, false);
Vladimir Marko20f85592015-03-19 10:07:02 +00001035 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001036 int32_t dex_cache_offset =
Mathieu Chartiere401d142015-04-22 13:56:20 -07001037 ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Vladimir Marko20f85592015-03-19 10:07:02 +00001038 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);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001069 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, ret0, false);
Vladimir Marko20f85592015-03-19 10:07:02 +00001070 } 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);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001074 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(), arg0, kNotVolatile);
Vladimir Marko20f85592015-03-19 10:07:02 +00001075 // Declaring class to dex cache strings.
1076 LoadRefDisp(arg0, mirror::Class::DexCacheStringsOffset().Int32Value(), arg0, kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001077
Vladimir Marko20f85592015-03-19 10:07:02 +00001078 LoadRefDisp(arg0, offset_of_string, ret0, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001080 GenIfNullUseHelperImm(ret0, kQuickResolveString, string_idx);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001081
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001083 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001085 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Vladimir Marko20f85592015-03-19 10:07:02 +00001086 if (CanUseOpPcRelDexCacheArrayLoad()) {
1087 size_t offset = dex_cache_arrays_layout_.StringOffset(string_idx);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001088 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, rl_result.reg, false);
Vladimir Marko20f85592015-03-19 10:07:02 +00001089 } else {
1090 RegLocation rl_method = LoadCurrMethod();
1091 RegStorage res_reg = AllocTempRef();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001092 LoadRefDisp(rl_method.reg, ArtMethod::DeclaringClassOffset().Int32Value(), res_reg,
Vladimir Marko20f85592015-03-19 10:07:02 +00001093 kNotVolatile);
1094 LoadRefDisp(res_reg, mirror::Class::DexCacheStringsOffset().Int32Value(), res_reg,
1095 kNotVolatile);
1096 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
1097 FreeTemp(res_reg);
1098 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 StoreValue(rl_dest, rl_result);
1100 }
1101}
1102
Andreas Gampe98430592014-07-27 19:44:50 -07001103/*
1104 * Let helper function take care of everything. Will
1105 * call Class::NewInstanceFromCode(type_idx, method);
1106 */
1107void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1108 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 // alloc will always check for resolution, do we also need to verify
1110 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001111 const DexFile* dex_file = cu_->dex_file;
1112 CompilerDriver* driver = cu_->compiler_driver;
1113 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001114 bool is_type_initialized;
1115 bool use_direct_type_ptr;
1116 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001117 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001118 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001119 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1120 &direct_type_ptr, &is_finalizable) &&
1121 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001122 // The fast path.
1123 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001124 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001125 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001126 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001127 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001128 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001129 }
1130 } else {
1131 // Use the direct pointer.
1132 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001133 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001134 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001135 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001136 }
1137 }
1138 } else {
1139 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001140 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001141 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001143 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 }
Andreas Gampe98430592014-07-27 19:44:50 -07001145 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146}
1147
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001148void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001150 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151}
1152
1153// For final classes there are no sub-classes to check and so we can answer the instance-of
1154// question with simple comparisons.
1155void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1156 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001157 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001158 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001159
buzbeea0cd2d72014-06-01 09:33:49 -07001160 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001162 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001163 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001165 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 }
1167 LoadConstant(result_reg, 0); // assume false
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001168 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169
buzbeea0cd2d72014-06-01 09:33:49 -07001170 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1171 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001174 RegStorage r_method = LoadCurrMethodWithHint(check_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001175 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001176 kNotVolatile);
1177 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1178 kNotVolatile);
Vladimir Marko20f85592015-03-19 10:07:02 +00001179 } else if (CanUseOpPcRelDexCacheArrayLoad()) {
1180 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001181 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, check_class, false);
Vladimir Marko20f85592015-03-19 10:07:02 +00001182 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1183 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001185 RegStorage r_method = LoadCurrMethodWithHint(check_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001186 LoadRefDisp(r_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001187 check_class, kNotVolatile);
1188 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1189 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001190 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001191 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 }
1193
buzbee695d13a2014-04-19 13:32:20 -07001194 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 if (cu_->instruction_set == kThumb2) {
1196 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001197 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001199 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001201 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 }
1203 LIR* target = NewLIR0(kPseudoTargetLabel);
1204 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 FreeTemp(object_class);
1206 FreeTemp(check_class);
1207 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001208 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 FreeTemp(result_reg);
1210 }
1211 StoreValue(rl_dest, rl_result);
1212}
1213
1214void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1215 bool type_known_abstract, bool use_declaring_class,
1216 bool can_assume_type_is_in_dex_cache,
1217 uint32_t type_idx, RegLocation rl_dest,
1218 RegLocation rl_src) {
1219 FlushAllRegs();
1220 // May generate a call - use explicit registers
1221 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001222 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001223 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1224 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 if (needs_access_check) {
1226 // Check we have access to type_idx and if not throw IllegalAccessError,
1227 // returns Class* in kArg0
Vladimir Marko20f85592015-03-19 10:07:02 +00001228 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001229 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1230 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001232 RegStorage r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Serguei Katkov9ee45192014-07-17 14:39:03 +07001233 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Mathieu Chartiere401d142015-04-22 13:56:20 -07001234 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001235 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001237 if (can_assume_type_is_in_dex_cache) {
1238 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001239 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001240 }
1241
Vladimir Marko20f85592015-03-19 10:07:02 +00001242 if (CanUseOpPcRelDexCacheArrayLoad()) {
1243 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001244 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, class_reg, false);
Vladimir Marko20f85592015-03-19 10:07:02 +00001245 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001246 RegStorage r_method = LoadCurrMethodWithHint(class_reg);
Vladimir Marko20f85592015-03-19 10:07:02 +00001247 // Load dex cache entry into class_reg (kArg2)
Mathieu Chartiere401d142015-04-22 13:56:20 -07001248 LoadRefDisp(r_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Vladimir Marko20f85592015-03-19 10:07:02 +00001249 class_reg, kNotVolatile);
1250 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
1251 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
1252 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 if (!can_assume_type_is_in_dex_cache) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001254 GenIfNullUseHelperImm(class_reg, kQuickInitializeType, type_idx);
Andreas Gampe90969af2014-07-15 23:02:11 -07001255
1256 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001257 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 }
1259 }
1260 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001261 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001262 if (!IsSameReg(rl_result.reg, ref_reg)) {
1263 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001264 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001266 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267
1268 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001269 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001270 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001271 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1272 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001274 LIR* branchover = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001276 // rl_result == ref == class.
1277 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001278 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 } else {
1280 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001281 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001282 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 if (!type_known_abstract) {
1284 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001285 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001286 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001287 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001289 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001291 if (it != nullptr) {
1292 OpEndIT(it);
1293 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 FreeTemp(r_tgt);
1295 } else {
1296 if (!type_known_abstract) {
1297 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001298 LoadConstant(rl_result.reg, 1); // assume true
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001299 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001301
Serguei Katkov9ee45192014-07-17 14:39:03 +07001302 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001303 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001304 }
1305 }
1306 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001307 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 /* branch targets here */
1309 LIR* target = NewLIR0(kPseudoTargetLabel);
1310 StoreValue(rl_dest, rl_result);
1311 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001312 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 branchover->target = target;
1314 }
1315}
1316
1317void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1318 bool type_known_final, type_known_abstract, use_declaring_class;
1319 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1320 *cu_->dex_file,
1321 type_idx,
1322 &type_known_final,
1323 &type_known_abstract,
1324 &use_declaring_class);
1325 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1326 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1327
1328 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1329 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1330 } else {
1331 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1332 use_declaring_class, can_assume_type_is_in_dex_cache,
1333 type_idx, rl_dest, rl_src);
1334 }
1335}
1336
Vladimir Marko22fe45d2015-03-18 11:33:58 +00001337void Mir2Lir::GenCheckCast(int opt_flags, uint32_t insn_idx, uint32_t type_idx,
1338 RegLocation rl_src) {
1339 if ((opt_flags & MIR_IGNORE_CHECK_CAST) != 0) {
1340 // Compiler analysis proved that this check-cast would never cause an exception.
1341 return;
1342 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 bool type_known_final, type_known_abstract, use_declaring_class;
1344 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1345 *cu_->dex_file,
1346 type_idx,
1347 &type_known_final,
1348 &type_known_abstract,
1349 &use_declaring_class);
1350 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1351 // of the exception throw path.
1352 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001353 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 // Verifier type analysis proved this check cast would never cause an exception.
1355 return;
1356 }
1357 FlushAllRegs();
1358 // May generate a call - use explicit registers
1359 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001360 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 if (needs_access_check) {
1362 // Check we have access to type_idx and if not throw IllegalAccessError,
1363 // returns Class* in kRet0
1364 // InitializeTypeAndVerifyAccess(idx, method)
Vladimir Marko20f85592015-03-19 10:07:02 +00001365 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001366 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001368 RegStorage method_reg = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001369 LoadRefDisp(method_reg, ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001370 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 } else {
1372 // Load dex cache entry into class_reg (kArg2)
Vladimir Marko20f85592015-03-19 10:07:02 +00001373 if (CanUseOpPcRelDexCacheArrayLoad()) {
1374 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001375 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, class_reg, false);
Vladimir Marko20f85592015-03-19 10:07:02 +00001376 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001377 RegStorage r_method = LoadCurrMethodWithHint(class_reg);
Vladimir Marko20f85592015-03-19 10:07:02 +00001378
Mathieu Chartiere401d142015-04-22 13:56:20 -07001379 LoadRefDisp(r_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Vladimir Marko20f85592015-03-19 10:07:02 +00001380 class_reg, kNotVolatile);
1381 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
1382 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
1383 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1385 // Need to test presence of type in dex cache at runtime
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001386 GenIfNullUseHelperImm(class_reg, kQuickInitializeType, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 }
1388 }
1389 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001390 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001391
1392 // Slow path for the case where the classes are not equal. In this case we need
1393 // to call a helper function to do the check.
1394 class SlowPath : public LIRSlowPath {
1395 public:
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001396 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load)
1397 : LIRSlowPath(m2l, fromfast, cont), load_(load) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001398 }
1399
1400 void Compile() {
1401 GenerateTargetLabel();
1402
1403 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001404 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1405 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001406 }
Andreas Gampe98430592014-07-27 19:44:50 -07001407 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1408 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001409 m2l_->OpUnconditionalBranch(cont_);
1410 }
1411
1412 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001413 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001414 };
1415
1416 if (type_known_abstract) {
1417 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001418 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001419 LIR* cont = NewLIR0(kPseudoTargetLabel);
1420 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1421 } else {
1422 // Harder, more common case. We need to generate a forward branch over the load
1423 // if the target is null. If it's non-null we perform the load and branch to the
1424 // slow path if the classes are not equal.
1425
1426 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001427 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001428 /* load object->klass_ */
1429 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001430 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1431 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001432
Andreas Gampeccc60262014-07-04 18:02:38 -07001433 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001434 LIR* cont = NewLIR0(kPseudoTargetLabel);
1435
1436 // Add the slow path that will not perform load since this is already done.
1437 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1438
1439 // Set the null check to branch to the continuation.
1440 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 }
1442}
1443
1444void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001445 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446 RegLocation rl_result;
1447 if (cu_->instruction_set == kThumb2) {
1448 /*
1449 * NOTE: This is the one place in the code in which we might have
1450 * as many as six live temporary registers. There are 5 in the normal
1451 * set for Arm. Until we have spill capabilities, temporarily add
1452 * lr to the temp set. It is safe to do this locally, but note that
1453 * lr is used explicitly elsewhere in the code generator and cannot
1454 * normally be used as a general temp register.
1455 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001456 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1457 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 }
1459 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1460 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1461 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1462 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001463 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1464 RegStorage t_reg = AllocTemp();
1465 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1466 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1467 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 FreeTemp(t_reg);
1469 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001470 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1471 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 }
1473 /*
1474 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1475 * following StoreValueWide might need to allocate a temp register.
1476 * To further work around the lack of a spill capability, explicitly
1477 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1478 * Remove when spill is functional.
1479 */
1480 FreeRegLocTemps(rl_result, rl_src1);
1481 FreeRegLocTemps(rl_result, rl_src2);
1482 StoreValueWide(rl_dest, rl_result);
1483 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001484 Clobber(TargetReg(kLr, kNotWide));
1485 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486 }
1487}
1488
Andreas Gampe98430592014-07-27 19:44:50 -07001489void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1490 RegLocation rl_src1, RegLocation rl_shift) {
1491 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 switch (opcode) {
1493 case Instruction::SHL_LONG:
1494 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001495 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 break;
1497 case Instruction::SHR_LONG:
1498 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001499 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 break;
1501 case Instruction::USHR_LONG:
1502 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001503 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 break;
1505 default:
1506 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001507 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508 }
Andreas Gampe98430592014-07-27 19:44:50 -07001509 FlushAllRegs(); /* Send everything to home location */
1510 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001511 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512 StoreValueWide(rl_dest, rl_result);
1513}
1514
1515
1516void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001517 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001518 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519 OpKind op = kOpBkpt;
1520 bool is_div_rem = false;
1521 bool check_zero = false;
1522 bool unary = false;
1523 RegLocation rl_result;
1524 bool shift_op = false;
1525 switch (opcode) {
1526 case Instruction::NEG_INT:
1527 op = kOpNeg;
1528 unary = true;
1529 break;
1530 case Instruction::NOT_INT:
1531 op = kOpMvn;
1532 unary = true;
1533 break;
1534 case Instruction::ADD_INT:
1535 case Instruction::ADD_INT_2ADDR:
1536 op = kOpAdd;
1537 break;
1538 case Instruction::SUB_INT:
1539 case Instruction::SUB_INT_2ADDR:
1540 op = kOpSub;
1541 break;
1542 case Instruction::MUL_INT:
1543 case Instruction::MUL_INT_2ADDR:
1544 op = kOpMul;
1545 break;
1546 case Instruction::DIV_INT:
1547 case Instruction::DIV_INT_2ADDR:
1548 check_zero = true;
1549 op = kOpDiv;
1550 is_div_rem = true;
1551 break;
1552 /* NOTE: returns in kArg1 */
1553 case Instruction::REM_INT:
1554 case Instruction::REM_INT_2ADDR:
1555 check_zero = true;
1556 op = kOpRem;
1557 is_div_rem = true;
1558 break;
1559 case Instruction::AND_INT:
1560 case Instruction::AND_INT_2ADDR:
1561 op = kOpAnd;
1562 break;
1563 case Instruction::OR_INT:
1564 case Instruction::OR_INT_2ADDR:
1565 op = kOpOr;
1566 break;
1567 case Instruction::XOR_INT:
1568 case Instruction::XOR_INT_2ADDR:
1569 op = kOpXor;
1570 break;
1571 case Instruction::SHL_INT:
1572 case Instruction::SHL_INT_2ADDR:
1573 shift_op = true;
1574 op = kOpLsl;
1575 break;
1576 case Instruction::SHR_INT:
1577 case Instruction::SHR_INT_2ADDR:
1578 shift_op = true;
1579 op = kOpAsr;
1580 break;
1581 case Instruction::USHR_INT:
1582 case Instruction::USHR_INT_2ADDR:
1583 shift_op = true;
1584 op = kOpLsr;
1585 break;
1586 default:
1587 LOG(FATAL) << "Invalid word arith op: " << opcode;
1588 }
1589 if (!is_div_rem) {
1590 if (unary) {
1591 rl_src1 = LoadValue(rl_src1, kCoreReg);
1592 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001593 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001594 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001595 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001596 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001597 RegStorage t_reg = AllocTemp();
1598 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 rl_src1 = LoadValue(rl_src1, kCoreReg);
1600 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001601 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 FreeTemp(t_reg);
1603 } else {
1604 rl_src1 = LoadValue(rl_src1, kCoreReg);
1605 rl_src2 = LoadValue(rl_src2, kCoreReg);
1606 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001607 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 }
1609 }
1610 StoreValue(rl_dest, rl_result);
1611 } else {
Dave Allison70202782013-10-22 17:52:19 -07001612 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 +01001613 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1614 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 rl_src1 = LoadValue(rl_src1, kCoreReg);
1616 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001617 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001618 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 }
buzbee2700f7e2014-03-07 09:46:20 -08001620 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001621 done = true;
1622 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001623 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001624 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001625 // Use ARM SDIV instruction for division. For remainder we also need to
1626 // calculate using a MUL and subtract.
1627 rl_src1 = LoadValue(rl_src1, kCoreReg);
1628 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001629 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001630 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001631 }
buzbee2700f7e2014-03-07 09:46:20 -08001632 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001633 done = true;
1634 }
1635 }
1636
1637 // If we haven't already generated the code use the callout function.
1638 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001640 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001641 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001642 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001643 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001644 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001645 }
Dave Allison70202782013-10-22 17:52:19 -07001646 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001647 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001649 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 else
1651 rl_result = GetReturnAlt();
1652 }
1653 StoreValue(rl_dest, rl_result);
1654 }
1655}
1656
1657/*
1658 * The following are the first-level codegen routines that analyze the format
1659 * of each bytecode then either dispatch special purpose codegen routines
1660 * or produce corresponding Thumb instructions directly.
1661 */
1662
Brian Carlstrom7940e442013-07-12 13:46:57 -07001663// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001664static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 x &= x - 1;
1666 return (x & (x - 1)) == 0;
1667}
1668
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1670// and store the result in 'rl_dest'.
Andreas Gamped500b532015-01-16 22:09:55 -08001671bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode ATTRIBUTE_UNUSED, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001672 RegLocation rl_src, RegLocation rl_dest, int lit) {
Andreas Gamped500b532015-01-16 22:09:55 -08001673 if ((lit < 2) || (!IsPowerOfTwo(lit))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 return false;
1675 }
Andreas Gampe7e499922015-01-06 08:28:12 -08001676 int k = CTZ(lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001677 if (k >= 30) {
1678 // Avoid special cases.
1679 return false;
1680 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 rl_src = LoadValue(rl_src, kCoreReg);
1682 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001683 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001684 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 if (lit == 2) {
1686 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001687 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1688 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1689 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001691 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001693 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1694 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001695 }
1696 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001697 RegStorage t_reg1 = AllocTemp();
1698 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001700 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1701 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001702 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001703 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001704 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001705 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001707 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001709 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 }
1711 }
1712 StoreValue(rl_dest, rl_result);
1713 return true;
1714}
1715
1716// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1717// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001718bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001719 if (lit < 0) {
1720 return false;
1721 }
1722 if (lit == 0) {
1723 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1724 LoadConstant(rl_result.reg, 0);
1725 StoreValue(rl_dest, rl_result);
1726 return true;
1727 }
1728 if (lit == 1) {
1729 rl_src = LoadValue(rl_src, kCoreReg);
1730 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1731 OpRegCopy(rl_result.reg, rl_src.reg);
1732 StoreValue(rl_dest, rl_result);
1733 return true;
1734 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001735 // There is RegRegRegShift on Arm, so check for more special cases
1736 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001737 return EasyMultiply(rl_src, rl_dest, lit);
1738 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 // Can we simplify this multiplication?
1740 bool power_of_two = false;
1741 bool pop_count_le2 = false;
1742 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001743 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 power_of_two = true;
1745 } else if (IsPopCountLE2(lit)) {
1746 pop_count_le2 = true;
1747 } else if (IsPowerOfTwo(lit + 1)) {
1748 power_of_two_minus_one = true;
1749 } else {
1750 return false;
1751 }
1752 rl_src = LoadValue(rl_src, kCoreReg);
1753 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1754 if (power_of_two) {
1755 // Shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001756 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, CTZ(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 } else if (pop_count_le2) {
1758 // Shift and add and shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001759 int first_bit = CTZ(lit);
1760 int second_bit = CTZ(lit ^ (1 << first_bit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001761 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1762 } else {
1763 // Reverse subtract: (src << (shift + 1)) - src.
1764 DCHECK(power_of_two_minus_one);
Andreas Gampe7e499922015-01-06 08:28:12 -08001765 // TUNING: rsb dst, src, src lsl#CTZ(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001766 RegStorage t_reg = AllocTemp();
Andreas Gampe7e499922015-01-06 08:28:12 -08001767 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, CTZ(lit + 1));
buzbee2700f7e2014-03-07 09:46:20 -08001768 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001769 }
1770 StoreValue(rl_dest, rl_result);
1771 return true;
1772}
1773
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001774// Returns true if it generates instructions.
1775bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1776 RegLocation rl_src2) {
1777 if (!rl_src2.is_const ||
1778 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1779 return false;
1780 }
1781
1782 if (!rl_src2.wide) {
1783 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1784 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1785 // Generate multiply by reciprocal instead of div.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001786 float recip = 1.0f/bit_cast<float, int32_t>(divisor);
1787 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<int32_t, float>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001788 return true;
1789 }
1790 } else {
1791 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1792 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1793 // Generate multiply by reciprocal instead of div.
1794 double recip = 1.0/bit_cast<double, int64_t>(divisor);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001795 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<int64_t, double>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001796 return true;
1797 }
1798 }
1799 return false;
1800}
1801
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001803 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001804 RegLocation rl_result;
1805 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1806 int shift_op = false;
1807 bool is_div = false;
1808
1809 switch (opcode) {
1810 case Instruction::RSUB_INT_LIT8:
1811 case Instruction::RSUB_INT: {
1812 rl_src = LoadValue(rl_src, kCoreReg);
1813 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1814 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001815 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001816 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001817 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1818 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001819 }
1820 StoreValue(rl_dest, rl_result);
1821 return;
1822 }
1823
1824 case Instruction::SUB_INT:
1825 case Instruction::SUB_INT_2ADDR:
1826 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001827 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001828 case Instruction::ADD_INT:
1829 case Instruction::ADD_INT_2ADDR:
1830 case Instruction::ADD_INT_LIT8:
1831 case Instruction::ADD_INT_LIT16:
1832 op = kOpAdd;
1833 break;
1834 case Instruction::MUL_INT:
1835 case Instruction::MUL_INT_2ADDR:
1836 case Instruction::MUL_INT_LIT8:
1837 case Instruction::MUL_INT_LIT16: {
1838 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1839 return;
1840 }
1841 op = kOpMul;
1842 break;
1843 }
1844 case Instruction::AND_INT:
1845 case Instruction::AND_INT_2ADDR:
1846 case Instruction::AND_INT_LIT8:
1847 case Instruction::AND_INT_LIT16:
1848 op = kOpAnd;
1849 break;
1850 case Instruction::OR_INT:
1851 case Instruction::OR_INT_2ADDR:
1852 case Instruction::OR_INT_LIT8:
1853 case Instruction::OR_INT_LIT16:
1854 op = kOpOr;
1855 break;
1856 case Instruction::XOR_INT:
1857 case Instruction::XOR_INT_2ADDR:
1858 case Instruction::XOR_INT_LIT8:
1859 case Instruction::XOR_INT_LIT16:
1860 op = kOpXor;
1861 break;
1862 case Instruction::SHL_INT_LIT8:
1863 case Instruction::SHL_INT:
1864 case Instruction::SHL_INT_2ADDR:
1865 lit &= 31;
1866 shift_op = true;
1867 op = kOpLsl;
1868 break;
1869 case Instruction::SHR_INT_LIT8:
1870 case Instruction::SHR_INT:
1871 case Instruction::SHR_INT_2ADDR:
1872 lit &= 31;
1873 shift_op = true;
1874 op = kOpAsr;
1875 break;
1876 case Instruction::USHR_INT_LIT8:
1877 case Instruction::USHR_INT:
1878 case Instruction::USHR_INT_2ADDR:
1879 lit &= 31;
1880 shift_op = true;
1881 op = kOpLsr;
1882 break;
1883
1884 case Instruction::DIV_INT:
1885 case Instruction::DIV_INT_2ADDR:
1886 case Instruction::DIV_INT_LIT8:
1887 case Instruction::DIV_INT_LIT16:
1888 case Instruction::REM_INT:
1889 case Instruction::REM_INT_2ADDR:
1890 case Instruction::REM_INT_LIT8:
1891 case Instruction::REM_INT_LIT16: {
1892 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001893 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001894 return;
1895 }
buzbee11b63d12013-08-27 07:34:17 -07001896 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001898 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001899 (opcode == Instruction::DIV_INT_LIT16)) {
1900 is_div = true;
1901 } else {
1902 is_div = false;
1903 }
buzbee11b63d12013-08-27 07:34:17 -07001904 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1905 return;
1906 }
Dave Allison70202782013-10-22 17:52:19 -07001907
1908 bool done = false;
Maja Gagic6ea651f2015-02-24 16:55:04 +01001909 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1910 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001911 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001912 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001913 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001914 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001915 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1916 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001917 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001918 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001919 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001920 // Use ARM SDIV instruction for division. For remainder we also need to
1921 // calculate using a MUL and subtract.
1922 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001923 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001924 done = true;
1925 }
1926 }
1927
1928 if (!done) {
1929 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001930 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1931 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001932 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001933 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001934 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001935 else
1936 rl_result = GetReturnAlt();
1937 }
1938 StoreValue(rl_dest, rl_result);
1939 return;
1940 }
1941 default:
1942 LOG(FATAL) << "Unexpected opcode " << opcode;
1943 }
1944 rl_src = LoadValue(rl_src, kCoreReg);
1945 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001946 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001947 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001948 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001949 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001950 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001951 }
1952 StoreValue(rl_dest, rl_result);
1953}
1954
Andreas Gampe98430592014-07-27 19:44:50 -07001955void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001956 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001957 RegLocation rl_result;
1958 OpKind first_op = kOpBkpt;
1959 OpKind second_op = kOpBkpt;
1960 bool call_out = false;
1961 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001962 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1963 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964
1965 switch (opcode) {
1966 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001967 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1968 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001969 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001970 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001971 RegStorage t_reg = AllocTemp();
1972 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1973 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1974 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1975 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001977 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1978 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001979 }
Andreas Gampe98430592014-07-27 19:44:50 -07001980 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 return;
1982 case Instruction::ADD_LONG:
1983 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 first_op = kOpAdd;
1985 second_op = kOpAdc;
1986 break;
1987 case Instruction::SUB_LONG:
1988 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001989 first_op = kOpSub;
1990 second_op = kOpSbc;
1991 break;
1992 case Instruction::MUL_LONG:
1993 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07001994 call_out = true;
1995 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1996 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001997 break;
1998 case Instruction::DIV_LONG:
1999 case Instruction::DIV_LONG_2ADDR:
2000 call_out = true;
2001 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002002 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2003 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 break;
2005 case Instruction::REM_LONG:
2006 case Instruction::REM_LONG_2ADDR:
2007 call_out = true;
2008 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002009 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002010 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002011 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2012 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 break;
2014 case Instruction::AND_LONG_2ADDR:
2015 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 first_op = kOpAnd;
2017 second_op = kOpAnd;
2018 break;
2019 case Instruction::OR_LONG:
2020 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002021 first_op = kOpOr;
2022 second_op = kOpOr;
2023 break;
2024 case Instruction::XOR_LONG:
2025 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002026 first_op = kOpXor;
2027 second_op = kOpXor;
2028 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 default:
2030 LOG(FATAL) << "Invalid long arith op";
2031 }
2032 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002033 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002035 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002037 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2038 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2039 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2040 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002041 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2042 GenDivZeroCheckWide(r_tmp2);
2043 }
Andreas Gampe98430592014-07-27 19:44:50 -07002044 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002045 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002046 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002047 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002048 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002049 }
2050 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002051 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2052 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002053 else
Andreas Gampe98430592014-07-27 19:44:50 -07002054 rl_result = GetReturnWideAlt();
2055 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002056 }
2057}
2058
Mark Mendelle87f9b52014-04-30 14:13:18 -04002059void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2060 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2061 LoadConstantNoClobber(rl_result.reg, value);
2062 StoreValue(rl_dest, rl_result);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002063}
2064
Andreas Gampe98430592014-07-27 19:44:50 -07002065void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
Vladimir Markofac10702015-04-22 11:51:52 +01002066 RegLocation rl_src, RegisterClass return_reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002067 /*
2068 * Don't optimize the register usage since it calls out to support
2069 * functions
2070 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002071
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002073 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002074 if (rl_dest.wide) {
Vladimir Markofac10702015-04-22 11:51:52 +01002075 RegLocation rl_result = GetReturnWide(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002076 StoreValueWide(rl_dest, rl_result);
2077 } else {
Vladimir Markofac10702015-04-22 11:51:52 +01002078 RegLocation rl_result = GetReturn(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 StoreValue(rl_dest, rl_result);
2080 }
2081}
2082
Vladimir Marko6ce3eba2015-02-16 13:05:59 +00002083class Mir2Lir::SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002084 public:
2085 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00002086 : LIRSlowPath(m2l, branch, cont) {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002087 }
2088
2089 void Compile() OVERRIDE {
2090 m2l_->ResetRegPool();
2091 m2l_->ResetDefTracking();
2092 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002093 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002094 if (cont_ != nullptr) {
2095 m2l_->OpUnconditionalBranch(cont_);
2096 }
2097 }
2098};
2099
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002101void Mir2Lir::GenSuspendTest(int opt_flags) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002102 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2103 return;
2104 }
Dave Allison69dfe512014-07-11 17:11:58 +00002105 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002106 FlushAllRegs();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002107 LIR* branch = OpTestSuspend(nullptr);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002108 LIR* cont = NewLIR0(kPseudoTargetLabel);
2109 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002110 } else {
Dave Allisonb373e092014-02-20 16:06:36 -08002111 FlushAllRegs(); // TODO: needed?
2112 LIR* inst = CheckSuspendUsingLoad();
2113 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002114 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002115}
2116
2117/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002118void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002119 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2120 OpUnconditionalBranch(target);
2121 return;
2122 }
Dave Allison69dfe512014-07-11 17:11:58 +00002123 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002124 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002125 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002126 LIR* branch = OpUnconditionalBranch(nullptr);
2127 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002128 } else {
2129 // For the implicit suspend check, just perform the trigger
2130 // load and branch to the target.
Dave Allisonb373e092014-02-20 16:06:36 -08002131 FlushAllRegs();
2132 LIR* inst = CheckSuspendUsingLoad();
2133 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002134 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002135 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002136}
2137
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002138/* Call out to helper assembly routine that will null check obj and then lock it. */
2139void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002140 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002141 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002142 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002143}
2144
2145/* Call out to helper assembly routine that will null check obj and then unlock it. */
2146void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002147 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002148 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002149 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002150}
2151
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002152/* Generic code for generating a wide constant into a VR. */
2153void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2154 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002155 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002156 StoreValueWide(rl_dest, rl_result);
2157}
2158
Andreas Gampe48971b32014-08-06 10:09:01 -07002159void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002160 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2161 DCHECK(bb != nullptr);
2162 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002163 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002164 const uint16_t entries = table[1];
2165 // Chained cmp-and-branch.
2166 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002167 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002168 rl_src = LoadValue(rl_src, kCoreReg);
2169 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002170 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002171 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002172 // Switch to using a temp and add.
2173 break;
2174 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002175 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2176 DCHECK(successor_block_info != nullptr);
2177 int case_block_id = successor_block_info->block;
2178 DCHECK_EQ(starting_key + i, successor_block_info->key);
2179 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002180 }
2181 if (i < entries) {
2182 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2183 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2184 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002185 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002186 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2187 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2188 DCHECK(successor_block_info != nullptr);
2189 int case_block_id = successor_block_info->block;
2190 DCHECK_EQ(starting_key + i, successor_block_info->key);
2191 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002192 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2193 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002194 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2195 DCHECK(successor_block_info != nullptr);
2196 int case_block_id = successor_block_info->block;
2197 DCHECK_EQ(starting_key + i, successor_block_info->key);
2198 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002199 } else {
2200 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002201 for (; i < entries; ++i, ++succ_bb_iter) {
2202 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2203 DCHECK(successor_block_info != nullptr);
2204 int case_block_id = successor_block_info->block;
2205 DCHECK_EQ(starting_key + i, successor_block_info->key);
2206 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002207 }
2208 }
2209 }
2210}
2211
2212void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002213 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002214 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002215 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002216 }
2217
2218 const uint16_t entries = table[1];
2219 if (entries <= kSmallSwitchThreshold) {
2220 GenSmallPackedSwitch(mir, table_offset, rl_src);
2221 } else {
2222 // Use the backend-specific implementation.
2223 GenLargePackedSwitch(mir, table_offset, rl_src);
2224 }
2225}
2226
2227void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002228 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2229 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002230 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002231 const uint16_t entries = table[1];
2232 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002233 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002234 int i = 0;
2235 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2236 int case_block_id = successor_block_info->block;
2237 int key = successor_block_info->key;
2238 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2239 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002240 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002241 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002242}
2243
2244void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002245 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002246 if (cu_->verbose) {
2247 DumpSparseSwitchTable(table);
2248 }
2249
2250 const uint16_t entries = table[1];
2251 if (entries <= kSmallSwitchThreshold) {
2252 GenSmallSparseSwitch(mir, table_offset, rl_src);
2253 } else {
2254 // Use the backend-specific implementation.
2255 GenLargeSparseSwitch(mir, table_offset, rl_src);
2256 }
2257}
2258
Fred Shih37f05ef2014-07-16 18:38:08 -07002259bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2260 switch (size) {
2261 case kReference:
2262 return type == Primitive::kPrimNot;
2263 case k64:
2264 case kDouble:
2265 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2266 case k32:
2267 case kSingle:
2268 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2269 case kSignedHalf:
2270 return type == Primitive::kPrimShort;
2271 case kUnsignedHalf:
2272 return type == Primitive::kPrimChar;
2273 case kSignedByte:
2274 return type == Primitive::kPrimByte;
2275 case kUnsignedByte:
2276 return type == Primitive::kPrimBoolean;
2277 case kWord: // Intentional fallthrough.
2278 default:
2279 return false; // There are no sane types with this op size.
2280 }
2281}
2282
Brian Carlstrom7940e442013-07-12 13:46:57 -07002283} // namespace art