blob: de5e0410fb25d9ac7af333ac16b4c44dd6a21a0e [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Andreas Gampe7e499922015-01-06 08:28:12 -080016
Andreas Gampe0b9203e2015-01-22 20:39:27 -080017#include "mir_to_lir-inl.h"
18
Andreas Gampe7e499922015-01-06 08:28:12 -080019#include <functional>
20
Ian Rogersd582fa42014-11-05 23:46:43 -080021#include "arch/arm/instruction_set_features_arm.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080022#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "dex/compiler_ir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080024#include "dex/mir_graph.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070025#include "dex/quick/arm/arm_lir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080026#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070028#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "mirror/array.h"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000030#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080031#include "mirror/object-inl.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070032#include "mirror/object_reference.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080033#include "utils.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000034#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "verifier/method_verifier.h"
36
37namespace art {
38
Andreas Gampe9c3b0892014-04-24 17:33:34 +000039// Shortcuts to repeatedly used long types.
40typedef mirror::ObjectArray<mirror::Object> ObjArray;
41typedef mirror::ObjectArray<mirror::Class> ClassArray;
42
Brian Carlstrom7940e442013-07-12 13:46:57 -070043/*
44 * This source files contains "gen" codegen routines that should
45 * be applicable to most targets. Only mid-level support utilities
46 * and "op" calls may be used here.
47 */
48
Andreas Gampe0b9203e2015-01-22 20:39:27 -080049ALWAYS_INLINE static inline bool ForceSlowFieldPath(CompilationUnit* cu) {
50 return (cu->enable_debug & (1 << kDebugSlowFieldPath)) != 0;
51}
52
53ALWAYS_INLINE static inline bool ForceSlowStringPath(CompilationUnit* cu) {
54 return (cu->enable_debug & (1 << kDebugSlowStringPath)) != 0;
55}
56
57ALWAYS_INLINE static inline bool ForceSlowTypePath(CompilationUnit* cu) {
58 return (cu->enable_debug & (1 << kDebugSlowTypePath)) != 0;
59}
60
Vladimir Marko20f85592015-03-19 10:07:02 +000061void Mir2Lir::GenIfNullUseHelperImmMethod(
62 RegStorage r_result, QuickEntrypointEnum trampoline, int imm, RegStorage r_method) {
63 class CallHelperImmMethodSlowPath : public LIRSlowPath {
64 public:
65 CallHelperImmMethodSlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont,
66 QuickEntrypointEnum trampoline_in, int imm_in,
67 RegStorage r_method_in, RegStorage r_result_in)
68 : LIRSlowPath(m2l, fromfast, cont), trampoline_(trampoline_in),
69 imm_(imm_in), r_method_(r_method_in), r_result_(r_result_in) {
70 }
71
72 void Compile() {
73 GenerateTargetLabel();
74 if (r_method_.Valid()) {
75 m2l_->CallRuntimeHelperImmReg(trampoline_, imm_, r_method_, true);
76 } else {
77 m2l_->CallRuntimeHelperImmMethod(trampoline_, imm_, true);
78 }
79 m2l_->OpRegCopy(r_result_, m2l_->TargetReg(kRet0, kRef));
80 m2l_->OpUnconditionalBranch(cont_);
81 }
82
83 private:
84 QuickEntrypointEnum trampoline_;
85 const int imm_;
86 const RegStorage r_method_;
87 const RegStorage r_result_;
88 };
89
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 LIR* branch = OpCmpImmBranch(kCondEq, r_result, 0, nullptr);
Vladimir Marko20f85592015-03-19 10:07:02 +000091 LIR* cont = NewLIR0(kPseudoTargetLabel);
92
93 AddSlowPath(new (arena_) CallHelperImmMethodSlowPath(this, branch, cont, trampoline, imm,
94 r_method, r_result));
95}
96
Vladimir Marko34773072015-04-07 09:56:48 +010097RegStorage Mir2Lir::GenGetOtherTypeForSgetSput(const MirSFieldLoweringInfo& field_info,
98 int opt_flags) {
99 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
100 // May do runtime call so everything to home locations.
101 FlushAllRegs();
102 RegStorage r_base = TargetReg(kArg0, kRef);
103 LockTemp(r_base);
104 RegStorage r_method = RegStorage::InvalidReg(); // Loaded lazily, maybe in the slow-path.
105 if (CanUseOpPcRelDexCacheArrayLoad()) {
106 uint32_t offset = dex_cache_arrays_layout_.TypeOffset(field_info.StorageIndex());
107 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, r_base);
108 } else {
109 // Using fixed register to sync with possible call to runtime support.
110 r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
111 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
112 kNotVolatile);
113 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
114 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
115 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116 // r_base now points at static storage (Class*) or null if the type is not yet resolved.
Vladimir Marko34773072015-04-07 09:56:48 +0100117 LIR* unresolved_branch = nullptr;
118 if (!field_info.IsClassInDexCache() && (opt_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700119 // Check if r_base is null.
Vladimir Marko34773072015-04-07 09:56:48 +0100120 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, nullptr);
121 }
122 LIR* uninit_branch = nullptr;
123 if (!field_info.IsClassInitialized() && (opt_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
124 // Check if r_base is not yet initialized class.
125 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
126 LockTemp(r_tmp);
127 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
128 mirror::Class::StatusOffset().Int32Value(),
129 mirror::Class::kStatusInitialized, nullptr, nullptr);
130 FreeTemp(r_tmp);
131 }
132 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
133 //
134 // Slow path to ensure a class is initialized for sget/sput.
135 //
136 class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
137 public:
138 // There are up to two branches to the static field slow path, the "unresolved" when the type
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
140 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
Vladimir Marko34773072015-04-07 09:56:48 +0100141 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
142 RegStorage r_base_in, RegStorage r_method_in)
143 : LIRSlowPath(m2l, unresolved != nullptr ? unresolved : uninit, cont),
144 second_branch_(unresolved != nullptr ? uninit : nullptr),
145 storage_index_(storage_index), r_base_(r_base_in), r_method_(r_method_in) {
146 }
147
148 void Compile() {
149 LIR* target = GenerateTargetLabel();
150 if (second_branch_ != nullptr) {
151 second_branch_->target = target;
152 }
153 if (r_method_.Valid()) {
154 // ArtMethod* was loaded in normal path - use it.
155 m2l_->CallRuntimeHelperImmReg(kQuickInitializeStaticStorage, storage_index_, r_method_,
156 true);
157 } else {
158 // ArtMethod* wasn't loaded in normal path - use a helper that loads it.
159 m2l_->CallRuntimeHelperImmMethod(kQuickInitializeStaticStorage, storage_index_, true);
160 }
161 // Copy helper's result into r_base, a no-op on all but MIPS.
162 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
163
164 m2l_->OpUnconditionalBranch(cont_);
165 }
166
167 private:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700168 // Second branch to the slow path, or null if there's only one branch.
Vladimir Marko34773072015-04-07 09:56:48 +0100169 LIR* const second_branch_;
170
171 const int storage_index_;
172 const RegStorage r_base_;
173 RegStorage r_method_;
174 };
175
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 // The slow path is invoked if the r_base is null or the class pointed
Vladimir Marko34773072015-04-07 09:56:48 +0100177 // to by it is not initialized.
178 LIR* cont = NewLIR0(kPseudoTargetLabel);
179 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
180 field_info.StorageIndex(), r_base, r_method));
181 }
182 if (IsTemp(r_method)) {
183 FreeTemp(r_method);
184 }
185 return r_base;
186}
187
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188/*
buzbeeb48819d2013-09-14 16:15:25 -0700189 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 * blocks.
191 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700192void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193 LIR* barrier = NewLIR0(kPseudoBarrier);
194 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -0700195 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100196 barrier->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197}
198
Mingyao Yange643a172014-04-08 11:02:52 -0700199void Mir2Lir::GenDivZeroException() {
200 LIR* branch = OpUnconditionalBranch(nullptr);
201 AddDivZeroCheckSlowPath(branch);
202}
203
204void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -0700205 LIR* branch = OpCondBranch(c_code, nullptr);
206 AddDivZeroCheckSlowPath(branch);
207}
208
Mingyao Yange643a172014-04-08 11:02:52 -0700209void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
210 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -0700211 AddDivZeroCheckSlowPath(branch);
212}
213
214void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
215 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
216 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800217 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000218 : LIRSlowPath(m2l, branch_in) {
Mingyao Yang42894562014-04-07 12:42:16 -0700219 }
220
Mingyao Yange643a172014-04-08 11:02:52 -0700221 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -0700222 m2l_->ResetRegPool();
223 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700224 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700225 m2l_->CallRuntimeHelper(kQuickThrowDivZero, true);
Mingyao Yang42894562014-04-07 12:42:16 -0700226 }
227 };
228
229 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
230}
Dave Allisonb373e092014-02-20 16:06:36 -0800231
Mingyao Yang80365d92014-04-18 12:10:58 -0700232void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
233 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
234 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800235 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, RegStorage index_in,
236 RegStorage length_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000237 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800238 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700239 }
240
241 void Compile() OVERRIDE {
242 m2l_->ResetRegPool();
243 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700244 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700245 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700246 }
247
248 private:
249 const RegStorage index_;
250 const RegStorage length_;
251 };
252
253 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
254 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
255}
256
257void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
258 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
259 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800260 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, int index_in, RegStorage length_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000261 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800262 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700263 }
264
265 void Compile() OVERRIDE {
266 m2l_->ResetRegPool();
267 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700268 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700269
Andreas Gampeccc60262014-07-04 18:02:38 -0700270 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
271 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700272
273 m2l_->OpRegCopy(arg1_32, length_);
274 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700275 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700276 }
277
278 private:
279 const int32_t index_;
280 const RegStorage length_;
281 };
282
283 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
284 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
285}
286
Mingyao Yange643a172014-04-08 11:02:52 -0700287LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
288 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
289 public:
290 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000291 : LIRSlowPath(m2l, branch) {
Mingyao Yange643a172014-04-08 11:02:52 -0700292 }
293
294 void Compile() OVERRIDE {
295 m2l_->ResetRegPool();
296 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700297 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700298 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700299 }
300 };
301
302 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
303 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
304 return branch;
305}
306
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800308LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000309 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700310 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 }
Pavel Vyssotski9c3617a2014-11-13 18:25:23 +0600312 // If null check has not been eliminated, reset redundant store tracking.
313 if ((opt_flags & MIR_IGNORE_NULL_CHECK) == 0) {
314 ResetDefTracking();
315 }
Dave Allisonb373e092014-02-20 16:06:36 -0800316 return nullptr;
317}
318
Dave Allisonf9439142014-03-27 15:10:22 -0700319/* Perform an explicit null-check on a register. */
320LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
321 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700322 return nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700323 }
Mingyao Yange643a172014-04-08 11:02:52 -0700324 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700325}
326
Dave Allisonb373e092014-02-20 16:06:36 -0800327void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000328 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800329 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
330 return;
331 }
Dave Allison69dfe512014-07-11 17:11:58 +0000332 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800333 MarkSafepointPC(last_lir_insn_);
334 }
335}
336
Andreas Gampe3c12c512014-06-24 18:46:29 +0000337void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000338 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000339 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
340 return;
341 }
342 MarkSafepointPCAfter(after);
343 }
344}
345
Dave Allisonb373e092014-02-20 16:06:36 -0800346void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000347 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800348 MarkSafepointPC(last_lir_insn_);
349 }
350}
351
buzbee2700f7e2014-03-07 09:46:20 -0800352void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000353 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800354 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
355 return;
356 }
357 // Force an implicit null check by performing a memory operation (load) from the given
358 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800359 RegStorage tmp = AllocTemp();
360 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700361 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800362 FreeTemp(tmp);
363 MarkSafepointPC(load);
364 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365}
366
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700368 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700370 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 switch (opcode) {
372 case Instruction::IF_EQ:
373 cond = kCondEq;
374 break;
375 case Instruction::IF_NE:
376 cond = kCondNe;
377 break;
378 case Instruction::IF_LT:
379 cond = kCondLt;
380 break;
381 case Instruction::IF_GE:
382 cond = kCondGe;
383 break;
384 case Instruction::IF_GT:
385 cond = kCondGt;
386 break;
387 case Instruction::IF_LE:
388 cond = kCondLe;
389 break;
390 default:
391 cond = static_cast<ConditionCode>(0);
392 LOG(FATAL) << "Unexpected opcode " << opcode;
393 }
394
395 // Normalize such that if either operand is constant, src2 will be constant
396 if (rl_src1.is_const) {
397 RegLocation rl_temp = rl_src1;
398 rl_src1 = rl_src2;
399 rl_src2 = rl_temp;
400 cond = FlipComparisonOrder(cond);
401 }
402
buzbee7c02e912014-10-03 13:14:17 -0700403 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 // Is this really an immediate comparison?
405 if (rl_src2.is_const) {
406 // If it's already live in a register or not easily materialized, just keep going
407 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700408 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100410 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800412 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 return;
414 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700415
416 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
417 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
418 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
419 // the 32b literal 0 for null.
420 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
421 // Use the OpCmpImmBranch and ignore the value in the register.
422 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
423 return;
424 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700426
buzbee7c02e912014-10-03 13:14:17 -0700427 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800428 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429}
430
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700431void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700433 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
434 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 switch (opcode) {
436 case Instruction::IF_EQZ:
437 cond = kCondEq;
438 break;
439 case Instruction::IF_NEZ:
440 cond = kCondNe;
441 break;
442 case Instruction::IF_LTZ:
443 cond = kCondLt;
444 break;
445 case Instruction::IF_GEZ:
446 cond = kCondGe;
447 break;
448 case Instruction::IF_GTZ:
449 cond = kCondGt;
450 break;
451 case Instruction::IF_LEZ:
452 cond = kCondLe;
453 break;
454 default:
455 cond = static_cast<ConditionCode>(0);
456 LOG(FATAL) << "Unexpected opcode " << opcode;
457 }
buzbee2700f7e2014-03-07 09:46:20 -0800458 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459}
460
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700461void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
463 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800464 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800466 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 }
buzbee2700f7e2014-03-07 09:46:20 -0800468 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 StoreValueWide(rl_dest, rl_result);
470}
471
Yevgeny Rouban6af82062014-11-26 18:11:54 +0600472void Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
473 rl_src = UpdateLocWide(rl_src);
474 rl_src = NarrowRegLoc(rl_src);
475 StoreValue(rl_dest, rl_src);
476}
477
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700479 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700480 rl_src = LoadValue(rl_src, kCoreReg);
481 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
482 OpKind op = kOpInvalid;
483 switch (opcode) {
484 case Instruction::INT_TO_BYTE:
485 op = kOp2Byte;
486 break;
487 case Instruction::INT_TO_SHORT:
488 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700490 case Instruction::INT_TO_CHAR:
491 op = kOp2Char;
492 break;
493 default:
494 LOG(ERROR) << "Bad int conversion type";
495 }
buzbee2700f7e2014-03-07 09:46:20 -0800496 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700497 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498}
499
Andreas Gampe98430592014-07-27 19:44:50 -0700500/*
501 * Let helper function take care of everything. Will call
502 * Array::AllocFromCode(type_idx, method, count);
503 * Note: AllocFromCode will handle checks for errNegativeArraySize.
504 */
505void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
506 RegLocation rl_src) {
507 FlushAllRegs(); /* Everything to home location */
508 const DexFile* dex_file = cu_->dex_file;
509 CompilerDriver* driver = cu_->compiler_driver;
510 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800511 bool is_type_initialized; // Ignored as an array does not have an initializer.
512 bool use_direct_type_ptr;
513 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700514 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800515 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700516 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
517 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800518 // The fast path.
519 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700520 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800521 CallRuntimeHelperRegRegLocationMethod(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
Andreas Gampe98430592014-07-27 19:44:50 -0700522 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800523 } else {
524 // Use the direct pointer.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800525 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
Andreas Gampe98430592014-07-27 19:44:50 -0700526 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800527 }
528 } else {
529 // The slow path.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800530 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800531 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 } else {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800533 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 }
Andreas Gampe98430592014-07-27 19:44:50 -0700535 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536}
537
538/*
539 * Similar to GenNewArray, but with post-allocation initialization.
540 * Verifier guarantees we're dealing with an array class. Current
541 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
542 * Current code also throws internal unimp if not 'L', '[' or 'I'.
543 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700544void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000545 size_t elems = info->num_arg_words;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 int type_idx = info->index;
547 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700548 QuickEntrypointEnum target;
549 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
550 type_idx)) {
551 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700553 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 }
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800555 CallRuntimeHelperImmImmMethod(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700556 FreeTemp(TargetReg(kArg2, kNotWide));
557 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 /*
559 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
560 * return region. Because AllocFromCode placed the new array
561 * in kRet0, we'll just lock it into place. When debugger support is
562 * added, it may be necessary to additionally copy all return
563 * values to a home location in thread-local storage
564 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700565 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700566 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567
568 // TODO: use the correct component size, currently all supported types
569 // share array alignment with ints (see comment at head of function)
570 size_t component_size = sizeof(int32_t);
571
Vladimir Markobf535be2014-11-19 18:52:35 +0000572 if (elems > 5) {
573 DCHECK(info->is_range); // Non-range insn can't encode more than 5 elems.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 /*
575 * Bit of ugliness here. We're going generate a mem copy loop
576 * on the register range, but it is possible that some regs
577 * in the range have been promoted. This is unlikely, but
578 * before generating the copy, we'll just force a flush
579 * of any regs in the source range that have been promoted to
580 * home location.
581 */
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000582 for (size_t i = 0; i < elems; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 RegLocation loc = UpdateLoc(info->args[i]);
584 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100585 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov27503542014-11-06 14:45:44 +0600586 if (loc.ref) {
587 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
588 } else {
589 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
590 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 }
592 }
593 /*
594 * TUNING note: generated code here could be much improved, but
595 * this is an uncommon operation and isn't especially performance
596 * critical.
597 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700598 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700599 RegStorage r_src = AllocTempRef();
600 RegStorage r_dst = AllocTempRef();
601 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800602 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700603 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700605 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700606 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 break;
608 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700609 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700610 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 r_val = AllocTemp();
612 break;
613 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100614 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 r_val = AllocTemp();
616 break;
617 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
618 }
619 // Set up source pointer
620 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700621 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700623 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 mirror::Array::DataOffset(component_size).Int32Value());
625 // Set up the loop counter (known to be > 0)
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000626 LoadConstant(r_idx, static_cast<int>(elems - 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800628 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100630 {
631 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
632 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
633 // NOTE: No dalvik register annotation, local optimizations will be stopped
634 // by the loop boundaries.
635 }
buzbee695d13a2014-04-19 13:32:20 -0700636 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800638 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700639 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700641 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 -mirror::Array::DataOffset(component_size).Int32Value());
643 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000644 FreeTemp(r_idx);
645 FreeTemp(r_dst);
646 FreeTemp(r_src);
647 } else {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000648 DCHECK_LE(elems, 5u); // Usually but not necessarily non-range.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 // TUNING: interleave
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000650 for (size_t i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600651 RegLocation rl_arg;
652 if (info->args[i].ref) {
653 rl_arg = LoadValue(info->args[i], kRefReg);
654 StoreRefDisp(ref_reg,
655 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
656 kNotVolatile);
657 } else {
658 rl_arg = LoadValue(info->args[i], kCoreReg);
659 Store32Disp(ref_reg,
660 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
661 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800663 if (IsTemp(rl_arg.reg)) {
664 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 }
666 }
667 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000668 if (elems != 0 && info->args[0].ref) {
669 // If there is at least one potentially non-null value, unconditionally mark the GC card.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000670 for (size_t i = 0; i < elems; i++) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000671 if (!mir_graph_->IsConstantNullRef(info->args[i])) {
672 UnconditionallyMarkGCCard(ref_reg);
673 break;
674 }
675 }
676 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700678 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 }
680}
681
Ian Rogers832336b2014-10-08 15:35:22 -0700682/*
683 * Array data table format:
684 * ushort ident = 0x0300 magic value
685 * ushort width width of each element in the table
686 * uint size number of elements in the table
687 * ubyte data[size*width] table of data values (may contain a single-byte
688 * padding at the end)
689 *
690 * Total size is 4+(width * size + 1)/2 16-bit code units.
691 */
692void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
693 if (kIsDebugBuild) {
694 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
695 const Instruction::ArrayDataPayload* payload =
696 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
697 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
698 }
699 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
700 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
701}
702
Fred Shih37f05ef2014-07-16 18:38:08 -0700703void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000704 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000705 DCHECK_EQ(SPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000706 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800707 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000708 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800709 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000710 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 // Fast path, static storage base is this method's class
buzbeea0cd2d72014-06-01 09:33:49 -0700712 r_base = AllocTempRef();
Vladimir Marko34773072015-04-07 09:56:48 +0100713 RegStorage r_method = LoadCurrMethodWithHint(r_base);
714 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000715 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 } else {
717 // Medium path, static storage base in a different class which requires checks that the other
718 // class is initialized.
Vladimir Marko34773072015-04-07 09:56:48 +0100719 r_base = GenGetOtherTypeForSgetSput(field_info, mir->optimization_flags);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100720 if (!field_info.IsClassInitialized() &&
721 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Marko34773072015-04-07 09:56:48 +0100722 // Ensure load of status and store of value don't re-order.
723 // TODO: Presumably the actual value store is control-dependent on the status load,
724 // and will thus not be reordered in any case, since stores are never speculated.
725 // Does later code "know" that the class is now initialized? If so, we still
726 // need the barrier to guard later static loads.
727 GenMemBarrier(kLoadAny);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100728 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 }
730 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700731 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
732 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100733 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100735 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700737 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000738 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
739 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100740 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700741 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000742 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700744 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000745 MarkGCCard(mir->optimization_flags, rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800747 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 } else {
749 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700750 QuickEntrypointEnum target;
751 switch (size) {
752 case kReference:
753 target = kQuickSetObjStatic;
754 break;
755 case k64:
756 case kDouble:
757 target = kQuickSet64Static;
758 break;
759 case k32:
760 case kSingle:
761 target = kQuickSet32Static;
762 break;
763 case kSignedHalf:
764 case kUnsignedHalf:
765 target = kQuickSet16Static;
766 break;
767 case kSignedByte:
768 case kUnsignedByte:
769 target = kQuickSet8Static;
770 break;
771 case kWord: // Intentional fallthrough.
772 default:
773 LOG(FATAL) << "Can't determine entrypoint for: " << size;
774 target = kQuickSet32Static;
775 }
Andreas Gampe98430592014-07-27 19:44:50 -0700776 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 }
778}
779
Fred Shih37f05ef2014-07-16 18:38:08 -0700780void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000781 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000782 DCHECK_EQ(SGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000783 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700784
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800785 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000786 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800787 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000788 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 // Fast path, static storage base is this method's class
buzbeea0cd2d72014-06-01 09:33:49 -0700790 r_base = AllocTempRef();
Vladimir Marko34773072015-04-07 09:56:48 +0100791 RegStorage r_method = LoadCurrMethodWithHint(r_base);
792 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000793 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 } else {
795 // Medium path, static storage base in a different class which requires checks that the other
796 // class is initialized
Vladimir Marko34773072015-04-07 09:56:48 +0100797 r_base = GenGetOtherTypeForSgetSput(field_info, mir->optimization_flags);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100798 if (!field_info.IsClassInitialized() &&
799 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Marko34773072015-04-07 09:56:48 +0100800 // Ensure load of status and load of value don't re-order.
801 GenMemBarrier(kLoadAny);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100802 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800804 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700805 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100806 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800807
Vladimir Marko674744e2014-04-24 15:18:26 +0100808 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700809 if (IsRef(size)) {
810 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000811 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
812 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100813 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700814 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000815 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800816 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100817 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800818
Fred Shih37f05ef2014-07-16 18:38:08 -0700819 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 StoreValueWide(rl_dest, rl_result);
821 } else {
822 StoreValue(rl_dest, rl_result);
823 }
824 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700825 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700827 QuickEntrypointEnum target;
828 switch (type) {
829 case Primitive::kPrimNot:
830 target = kQuickGetObjStatic;
831 break;
832 case Primitive::kPrimLong:
833 case Primitive::kPrimDouble:
834 target = kQuickGet64Static;
835 break;
836 case Primitive::kPrimInt:
837 case Primitive::kPrimFloat:
838 target = kQuickGet32Static;
839 break;
840 case Primitive::kPrimShort:
841 target = kQuickGetShortStatic;
842 break;
843 case Primitive::kPrimChar:
844 target = kQuickGetCharStatic;
845 break;
846 case Primitive::kPrimByte:
847 target = kQuickGetByteStatic;
848 break;
849 case Primitive::kPrimBoolean:
850 target = kQuickGetBooleanStatic;
851 break;
852 case Primitive::kPrimVoid: // Intentional fallthrough.
853 default:
854 LOG(FATAL) << "Can't determine entrypoint for: " << type;
855 target = kQuickGet32Static;
856 }
Andreas Gampe98430592014-07-27 19:44:50 -0700857 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
858
Douglas Leung2db3e262014-06-25 16:02:55 -0700859 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700860 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700861 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 StoreValueWide(rl_dest, rl_result);
863 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700864 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 StoreValue(rl_dest, rl_result);
866 }
867 }
868}
869
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800870// Generate code for all slow paths.
871void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700872 // We should check slow_paths_.Size() every time, because a new slow path
873 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100874 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800875 slowpath->Compile();
876 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100877 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800878}
879
Fred Shih37f05ef2014-07-16 18:38:08 -0700880void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
881 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000882 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800883 if (kIsDebugBuild) {
884 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
885 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
886 IGetMemAccessType(mir->dalvikInsn.opcode);
887 DCHECK_EQ(mem_access_type, field_info.MemAccessType()) << mir->dalvikInsn.opcode;
888 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000889 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800890 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700891 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700892 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000893 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700894 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100895 GenNullCheck(rl_obj.reg, opt_flags);
896 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
897 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000898 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700899 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000900 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
901 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100902 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700903 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000904 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100905 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000906 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700907 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 StoreValueWide(rl_dest, rl_result);
909 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 StoreValue(rl_dest, rl_result);
911 }
912 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700913 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
914 QuickEntrypointEnum target;
915 switch (type) {
916 case Primitive::kPrimNot:
917 target = kQuickGetObjInstance;
918 break;
919 case Primitive::kPrimLong:
920 case Primitive::kPrimDouble:
921 target = kQuickGet64Instance;
922 break;
923 case Primitive::kPrimFloat:
924 case Primitive::kPrimInt:
925 target = kQuickGet32Instance;
926 break;
927 case Primitive::kPrimShort:
928 target = kQuickGetShortInstance;
929 break;
930 case Primitive::kPrimChar:
931 target = kQuickGetCharInstance;
932 break;
933 case Primitive::kPrimByte:
934 target = kQuickGetByteInstance;
935 break;
936 case Primitive::kPrimBoolean:
937 target = kQuickGetBooleanInstance;
938 break;
939 case Primitive::kPrimVoid: // Intentional fallthrough.
940 default:
941 LOG(FATAL) << "Can't determine entrypoint for: " << type;
942 target = kQuickGet32Instance;
943 }
Andreas Gampe98430592014-07-27 19:44:50 -0700944 // Second argument of pGetXXInstance is always a reference.
945 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
946 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
947
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700948 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700949 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700950 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700951 StoreValueWide(rl_dest, rl_result);
952 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700953 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 StoreValue(rl_dest, rl_result);
955 }
956 }
957}
958
Vladimir Markobe0e5462014-02-26 11:24:15 +0000959void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700960 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000961 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800962 if (kIsDebugBuild) {
963 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
964 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
965 IPutMemAccessType(mir->dalvikInsn.opcode);
966 DCHECK_EQ(mem_access_type, field_info.MemAccessType());
967 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000968 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800969 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700970 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700971 // Dex code never writes to the class field.
972 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
973 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700974 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700975 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100976 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 } else {
978 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100979 }
980 GenNullCheck(rl_obj.reg, opt_flags);
981 int field_offset = field_info.FieldOffset().Int32Value();
Vladimir Markoee5e2732015-01-13 17:34:28 +0000982 LIR* null_ck_insn;
Fred Shih37f05ef2014-07-16 18:38:08 -0700983 if (IsRef(size)) {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000984 null_ck_insn = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000985 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100986 } else {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000987 null_ck_insn = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
988 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100989 }
Vladimir Markoee5e2732015-01-13 17:34:28 +0000990 MarkPossibleNullPointerExceptionAfter(opt_flags, null_ck_insn);
Fred Shih37f05ef2014-07-16 18:38:08 -0700991 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000992 MarkGCCard(opt_flags, rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 }
994 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700995 QuickEntrypointEnum target;
996 switch (size) {
997 case kReference:
998 target = kQuickSetObjInstance;
999 break;
1000 case k64:
1001 case kDouble:
1002 target = kQuickSet64Instance;
1003 break;
1004 case k32:
1005 case kSingle:
1006 target = kQuickSet32Instance;
1007 break;
1008 case kSignedHalf:
1009 case kUnsignedHalf:
1010 target = kQuickSet16Instance;
1011 break;
1012 case kSignedByte:
1013 case kUnsignedByte:
1014 target = kQuickSet8Instance;
1015 break;
1016 case kWord: // Intentional fallthrough.
1017 default:
1018 LOG(FATAL) << "Can't determine entrypoint for: " << size;
1019 target = kQuickSet32Instance;
1020 }
Andreas Gampe98430592014-07-27 19:44:50 -07001021 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
1022 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 }
1024}
1025
Ian Rogersa9a82542013-10-04 11:17:26 -07001026void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
1027 RegLocation rl_src) {
1028 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
1029 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
1030 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -07001031 QuickEntrypointEnum target = needs_range_check
1032 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
1033 : kQuickAputObjectWithBoundCheck)
1034 : kQuickAputObject;
1035 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -07001036}
1037
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001038void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001039 RegLocation rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001041 *cu_->dex_file,
1042 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 // Call out to helper which resolves type and verifies access.
1044 // Resolved type returned in kRet0.
Vladimir Marko20f85592015-03-19 10:07:02 +00001045 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
1046 rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001048 rl_result = EvalLoc(rl_dest, kRefReg, true);
1049 // We don't need access checks, load type from dex cache
1050 RegStorage r_method = RegStorage::InvalidReg();
1051 if (CanUseOpPcRelDexCacheArrayLoad()) {
1052 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1053 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, rl_result.reg);
1054 } else {
1055 RegLocation rl_method = LoadCurrMethod();
1056 CheckRegLocation(rl_method);
1057 r_method = rl_method.reg;
1058 int32_t dex_cache_offset =
1059 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
1060 RegStorage res_reg = AllocTempRef();
1061 LoadRefDisp(r_method, dex_cache_offset, res_reg, kNotVolatile);
1062 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
1063 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
1064 FreeTemp(res_reg);
1065 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001067 type_idx) || ForceSlowTypePath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 // Slow path, at runtime test if type is null and if so initialize
1069 FlushAllRegs();
Vladimir Marko20f85592015-03-19 10:07:02 +00001070 GenIfNullUseHelperImmMethod(rl_result.reg, kQuickInitializeType, type_idx, r_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 }
1072 }
Vladimir Marko20f85592015-03-19 10:07:02 +00001073 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074}
1075
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001076void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001078 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1079 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001081 *cu_->dex_file, string_idx) || ForceSlowStringPath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 // slow path, resolve string if not in dex cache
1083 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001084 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001085
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 // Might call out to helper, which will return resolved string in kRet0
Vladimir Marko20f85592015-03-19 10:07:02 +00001087 RegStorage ret0 = TargetReg(kRet0, kRef);
1088 RegStorage r_method = RegStorage::InvalidReg();
1089 if (CanUseOpPcRelDexCacheArrayLoad()) {
1090 size_t offset = dex_cache_arrays_layout_.StringOffset(string_idx);
1091 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, ret0);
1092 } else {
1093 r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
1094 // Method to declaring class.
1095 RegStorage arg0 = TargetReg(kArg0, kRef);
1096 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1097 arg0, kNotVolatile);
1098 // Declaring class to dex cache strings.
1099 LoadRefDisp(arg0, mirror::Class::DexCacheStringsOffset().Int32Value(), arg0, kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001100
Vladimir Marko20f85592015-03-19 10:07:02 +00001101 LoadRefDisp(arg0, offset_of_string, ret0, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 }
Vladimir Marko20f85592015-03-19 10:07:02 +00001103 GenIfNullUseHelperImmMethod(ret0, kQuickResolveString, string_idx, r_method);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001104
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001106 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001108 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Vladimir Marko20f85592015-03-19 10:07:02 +00001109 if (CanUseOpPcRelDexCacheArrayLoad()) {
1110 size_t offset = dex_cache_arrays_layout_.StringOffset(string_idx);
1111 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, rl_result.reg);
1112 } else {
1113 RegLocation rl_method = LoadCurrMethod();
1114 RegStorage res_reg = AllocTempRef();
1115 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), res_reg,
1116 kNotVolatile);
1117 LoadRefDisp(res_reg, mirror::Class::DexCacheStringsOffset().Int32Value(), res_reg,
1118 kNotVolatile);
1119 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
1120 FreeTemp(res_reg);
1121 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 StoreValue(rl_dest, rl_result);
1123 }
1124}
1125
Andreas Gampe98430592014-07-27 19:44:50 -07001126/*
1127 * Let helper function take care of everything. Will
1128 * call Class::NewInstanceFromCode(type_idx, method);
1129 */
1130void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1131 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 // alloc will always check for resolution, do we also need to verify
1133 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001134 const DexFile* dex_file = cu_->dex_file;
1135 CompilerDriver* driver = cu_->compiler_driver;
1136 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001137 bool is_type_initialized;
1138 bool use_direct_type_ptr;
1139 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001140 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001141 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001142 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1143 &direct_type_ptr, &is_finalizable) &&
1144 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001145 // The fast path.
1146 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001147 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001148 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001149 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001150 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001151 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001152 }
1153 } else {
1154 // Use the direct pointer.
1155 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001156 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001157 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001158 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001159 }
1160 }
1161 } else {
1162 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001163 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001164 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001166 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 }
Andreas Gampe98430592014-07-27 19:44:50 -07001168 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169}
1170
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001171void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001173 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174}
1175
1176// For final classes there are no sub-classes to check and so we can answer the instance-of
1177// question with simple comparisons.
1178void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1179 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001180 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001181 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001182
buzbeea0cd2d72014-06-01 09:33:49 -07001183 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001185 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001186 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001188 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 }
1190 LoadConstant(result_reg, 0); // assume false
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001191 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192
buzbeea0cd2d72014-06-01 09:33:49 -07001193 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1194 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001197 RegStorage r_method = LoadCurrMethodWithHint(check_class);
1198 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001199 kNotVolatile);
1200 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1201 kNotVolatile);
Vladimir Marko20f85592015-03-19 10:07:02 +00001202 } else if (CanUseOpPcRelDexCacheArrayLoad()) {
1203 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1204 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, check_class);
1205 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1206 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001207 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001208 RegStorage r_method = LoadCurrMethodWithHint(check_class);
1209 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001210 check_class, kNotVolatile);
1211 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1212 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001213 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001214 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 }
1216
buzbee695d13a2014-04-19 13:32:20 -07001217 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 if (cu_->instruction_set == kThumb2) {
1219 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001220 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001222 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001224 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 }
1226 LIR* target = NewLIR0(kPseudoTargetLabel);
1227 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 FreeTemp(object_class);
1229 FreeTemp(check_class);
1230 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001231 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 FreeTemp(result_reg);
1233 }
1234 StoreValue(rl_dest, rl_result);
1235}
1236
1237void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1238 bool type_known_abstract, bool use_declaring_class,
1239 bool can_assume_type_is_in_dex_cache,
1240 uint32_t type_idx, RegLocation rl_dest,
1241 RegLocation rl_src) {
1242 FlushAllRegs();
1243 // May generate a call - use explicit registers
1244 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001245 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001246 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1247 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 if (needs_access_check) {
1249 // Check we have access to type_idx and if not throw IllegalAccessError,
1250 // returns Class* in kArg0
Vladimir Marko20f85592015-03-19 10:07:02 +00001251 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001252 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1253 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001255 RegStorage r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Serguei Katkov9ee45192014-07-17 14:39:03 +07001256 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Vladimir Marko20f85592015-03-19 10:07:02 +00001257 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001258 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001260 if (can_assume_type_is_in_dex_cache) {
1261 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001262 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001263 }
1264
Vladimir Marko20f85592015-03-19 10:07:02 +00001265 RegStorage r_method = RegStorage::InvalidReg();
1266 if (CanUseOpPcRelDexCacheArrayLoad()) {
1267 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1268 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, class_reg);
1269 } else {
1270 r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
1271 // Load dex cache entry into class_reg (kArg2)
1272 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1273 class_reg, kNotVolatile);
1274 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
1275 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
1276 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 if (!can_assume_type_is_in_dex_cache) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001278 GenIfNullUseHelperImmMethod(class_reg, kQuickInitializeType, type_idx, r_method);
Andreas Gampe90969af2014-07-15 23:02:11 -07001279
1280 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001281 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 }
1283 }
1284 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001285 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001286 if (!IsSameReg(rl_result.reg, ref_reg)) {
1287 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001288 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001290 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291
1292 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001293 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001295 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1296 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001298 LIR* branchover = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001299 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001300 // rl_result == ref == class.
1301 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001302 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 } else {
1304 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001305 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001306 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 if (!type_known_abstract) {
1308 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001309 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001310 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001311 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001313 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001315 if (it != nullptr) {
1316 OpEndIT(it);
1317 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 FreeTemp(r_tgt);
1319 } else {
1320 if (!type_known_abstract) {
1321 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001322 LoadConstant(rl_result.reg, 1); // assume true
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001323 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001325
Serguei Katkov9ee45192014-07-17 14:39:03 +07001326 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001327 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 }
1329 }
1330 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001331 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 /* branch targets here */
1333 LIR* target = NewLIR0(kPseudoTargetLabel);
1334 StoreValue(rl_dest, rl_result);
1335 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001336 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 branchover->target = target;
1338 }
1339}
1340
1341void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1342 bool type_known_final, type_known_abstract, use_declaring_class;
1343 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1344 *cu_->dex_file,
1345 type_idx,
1346 &type_known_final,
1347 &type_known_abstract,
1348 &use_declaring_class);
1349 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1350 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1351
1352 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1353 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1354 } else {
1355 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1356 use_declaring_class, can_assume_type_is_in_dex_cache,
1357 type_idx, rl_dest, rl_src);
1358 }
1359}
1360
Vladimir Marko22fe45d2015-03-18 11:33:58 +00001361void Mir2Lir::GenCheckCast(int opt_flags, uint32_t insn_idx, uint32_t type_idx,
1362 RegLocation rl_src) {
1363 if ((opt_flags & MIR_IGNORE_CHECK_CAST) != 0) {
1364 // Compiler analysis proved that this check-cast would never cause an exception.
1365 return;
1366 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367 bool type_known_final, type_known_abstract, use_declaring_class;
1368 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1369 *cu_->dex_file,
1370 type_idx,
1371 &type_known_final,
1372 &type_known_abstract,
1373 &use_declaring_class);
1374 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1375 // of the exception throw path.
1376 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001377 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 // Verifier type analysis proved this check cast would never cause an exception.
1379 return;
1380 }
1381 FlushAllRegs();
1382 // May generate a call - use explicit registers
1383 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001384 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 if (needs_access_check) {
1386 // Check we have access to type_idx and if not throw IllegalAccessError,
1387 // returns Class* in kRet0
1388 // InitializeTypeAndVerifyAccess(idx, method)
Vladimir Marko20f85592015-03-19 10:07:02 +00001389 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001390 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001392 RegStorage method_reg = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Andreas Gampe4b537a82014-06-30 22:24:53 -07001393 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001394 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 } else {
1396 // Load dex cache entry into class_reg (kArg2)
Vladimir Marko20f85592015-03-19 10:07:02 +00001397 RegStorage r_method = RegStorage::InvalidReg();
1398 if (CanUseOpPcRelDexCacheArrayLoad()) {
1399 size_t offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1400 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, class_reg);
1401 } else {
1402 r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
1403
1404 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1405 class_reg, kNotVolatile);
1406 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
1407 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
1408 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1410 // Need to test presence of type in dex cache at runtime
Vladimir Marko20f85592015-03-19 10:07:02 +00001411 GenIfNullUseHelperImmMethod(class_reg, kQuickInitializeType, type_idx, r_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 }
1413 }
1414 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001415 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001416
1417 // Slow path for the case where the classes are not equal. In this case we need
1418 // to call a helper function to do the check.
1419 class SlowPath : public LIRSlowPath {
1420 public:
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001421 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load)
1422 : LIRSlowPath(m2l, fromfast, cont), load_(load) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001423 }
1424
1425 void Compile() {
1426 GenerateTargetLabel();
1427
1428 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001429 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1430 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001431 }
Andreas Gampe98430592014-07-27 19:44:50 -07001432 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1433 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001434 m2l_->OpUnconditionalBranch(cont_);
1435 }
1436
1437 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001438 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001439 };
1440
1441 if (type_known_abstract) {
1442 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001443 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001444 LIR* cont = NewLIR0(kPseudoTargetLabel);
1445 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1446 } else {
1447 // Harder, more common case. We need to generate a forward branch over the load
1448 // if the target is null. If it's non-null we perform the load and branch to the
1449 // slow path if the classes are not equal.
1450
1451 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001452 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001453 /* load object->klass_ */
1454 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001455 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1456 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001457
Andreas Gampeccc60262014-07-04 18:02:38 -07001458 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001459 LIR* cont = NewLIR0(kPseudoTargetLabel);
1460
1461 // Add the slow path that will not perform load since this is already done.
1462 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1463
1464 // Set the null check to branch to the continuation.
1465 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 }
1467}
1468
1469void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001470 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 RegLocation rl_result;
1472 if (cu_->instruction_set == kThumb2) {
1473 /*
1474 * NOTE: This is the one place in the code in which we might have
1475 * as many as six live temporary registers. There are 5 in the normal
1476 * set for Arm. Until we have spill capabilities, temporarily add
1477 * lr to the temp set. It is safe to do this locally, but note that
1478 * lr is used explicitly elsewhere in the code generator and cannot
1479 * normally be used as a general temp register.
1480 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001481 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1482 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483 }
1484 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1485 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1486 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1487 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001488 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1489 RegStorage t_reg = AllocTemp();
1490 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1491 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1492 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 FreeTemp(t_reg);
1494 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001495 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1496 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 }
1498 /*
1499 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1500 * following StoreValueWide might need to allocate a temp register.
1501 * To further work around the lack of a spill capability, explicitly
1502 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1503 * Remove when spill is functional.
1504 */
1505 FreeRegLocTemps(rl_result, rl_src1);
1506 FreeRegLocTemps(rl_result, rl_src2);
1507 StoreValueWide(rl_dest, rl_result);
1508 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001509 Clobber(TargetReg(kLr, kNotWide));
1510 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 }
1512}
1513
Andreas Gampe98430592014-07-27 19:44:50 -07001514void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1515 RegLocation rl_src1, RegLocation rl_shift) {
1516 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 switch (opcode) {
1518 case Instruction::SHL_LONG:
1519 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001520 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 break;
1522 case Instruction::SHR_LONG:
1523 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001524 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 break;
1526 case Instruction::USHR_LONG:
1527 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001528 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 break;
1530 default:
1531 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001532 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533 }
Andreas Gampe98430592014-07-27 19:44:50 -07001534 FlushAllRegs(); /* Send everything to home location */
1535 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001536 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 StoreValueWide(rl_dest, rl_result);
1538}
1539
1540
1541void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001542 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001543 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001544 OpKind op = kOpBkpt;
1545 bool is_div_rem = false;
1546 bool check_zero = false;
1547 bool unary = false;
1548 RegLocation rl_result;
1549 bool shift_op = false;
1550 switch (opcode) {
1551 case Instruction::NEG_INT:
1552 op = kOpNeg;
1553 unary = true;
1554 break;
1555 case Instruction::NOT_INT:
1556 op = kOpMvn;
1557 unary = true;
1558 break;
1559 case Instruction::ADD_INT:
1560 case Instruction::ADD_INT_2ADDR:
1561 op = kOpAdd;
1562 break;
1563 case Instruction::SUB_INT:
1564 case Instruction::SUB_INT_2ADDR:
1565 op = kOpSub;
1566 break;
1567 case Instruction::MUL_INT:
1568 case Instruction::MUL_INT_2ADDR:
1569 op = kOpMul;
1570 break;
1571 case Instruction::DIV_INT:
1572 case Instruction::DIV_INT_2ADDR:
1573 check_zero = true;
1574 op = kOpDiv;
1575 is_div_rem = true;
1576 break;
1577 /* NOTE: returns in kArg1 */
1578 case Instruction::REM_INT:
1579 case Instruction::REM_INT_2ADDR:
1580 check_zero = true;
1581 op = kOpRem;
1582 is_div_rem = true;
1583 break;
1584 case Instruction::AND_INT:
1585 case Instruction::AND_INT_2ADDR:
1586 op = kOpAnd;
1587 break;
1588 case Instruction::OR_INT:
1589 case Instruction::OR_INT_2ADDR:
1590 op = kOpOr;
1591 break;
1592 case Instruction::XOR_INT:
1593 case Instruction::XOR_INT_2ADDR:
1594 op = kOpXor;
1595 break;
1596 case Instruction::SHL_INT:
1597 case Instruction::SHL_INT_2ADDR:
1598 shift_op = true;
1599 op = kOpLsl;
1600 break;
1601 case Instruction::SHR_INT:
1602 case Instruction::SHR_INT_2ADDR:
1603 shift_op = true;
1604 op = kOpAsr;
1605 break;
1606 case Instruction::USHR_INT:
1607 case Instruction::USHR_INT_2ADDR:
1608 shift_op = true;
1609 op = kOpLsr;
1610 break;
1611 default:
1612 LOG(FATAL) << "Invalid word arith op: " << opcode;
1613 }
1614 if (!is_div_rem) {
1615 if (unary) {
1616 rl_src1 = LoadValue(rl_src1, kCoreReg);
1617 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001618 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001620 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001621 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001622 RegStorage t_reg = AllocTemp();
1623 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 rl_src1 = LoadValue(rl_src1, kCoreReg);
1625 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001626 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001627 FreeTemp(t_reg);
1628 } else {
1629 rl_src1 = LoadValue(rl_src1, kCoreReg);
1630 rl_src2 = LoadValue(rl_src2, kCoreReg);
1631 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001632 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633 }
1634 }
1635 StoreValue(rl_dest, rl_result);
1636 } else {
Dave Allison70202782013-10-22 17:52:19 -07001637 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 +01001638 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1639 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 rl_src1 = LoadValue(rl_src1, kCoreReg);
1641 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001642 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001643 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 }
buzbee2700f7e2014-03-07 09:46:20 -08001645 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001646 done = true;
1647 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001648 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001649 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001650 // Use ARM SDIV instruction for division. For remainder we also need to
1651 // calculate using a MUL and subtract.
1652 rl_src1 = LoadValue(rl_src1, kCoreReg);
1653 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001654 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001655 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001656 }
buzbee2700f7e2014-03-07 09:46:20 -08001657 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001658 done = true;
1659 }
1660 }
1661
1662 // If we haven't already generated the code use the callout function.
1663 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001665 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001666 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001667 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001668 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001669 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 }
Dave Allison70202782013-10-22 17:52:19 -07001671 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001672 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001674 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 else
1676 rl_result = GetReturnAlt();
1677 }
1678 StoreValue(rl_dest, rl_result);
1679 }
1680}
1681
1682/*
1683 * The following are the first-level codegen routines that analyze the format
1684 * of each bytecode then either dispatch special purpose codegen routines
1685 * or produce corresponding Thumb instructions directly.
1686 */
1687
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001689static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 x &= x - 1;
1691 return (x & (x - 1)) == 0;
1692}
1693
Brian Carlstrom7940e442013-07-12 13:46:57 -07001694// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1695// and store the result in 'rl_dest'.
Andreas Gamped500b532015-01-16 22:09:55 -08001696bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode ATTRIBUTE_UNUSED, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001697 RegLocation rl_src, RegLocation rl_dest, int lit) {
Andreas Gamped500b532015-01-16 22:09:55 -08001698 if ((lit < 2) || (!IsPowerOfTwo(lit))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 return false;
1700 }
Andreas Gampe7e499922015-01-06 08:28:12 -08001701 int k = CTZ(lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001702 if (k >= 30) {
1703 // Avoid special cases.
1704 return false;
1705 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 rl_src = LoadValue(rl_src, kCoreReg);
1707 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001708 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001709 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 if (lit == 2) {
1711 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001712 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1713 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1714 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001715 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001716 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001718 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1719 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 }
1721 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001722 RegStorage t_reg1 = AllocTemp();
1723 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001725 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1726 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001728 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001730 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001732 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001733 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001734 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001735 }
1736 }
1737 StoreValue(rl_dest, rl_result);
1738 return true;
1739}
1740
1741// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1742// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001743bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001744 if (lit < 0) {
1745 return false;
1746 }
1747 if (lit == 0) {
1748 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1749 LoadConstant(rl_result.reg, 0);
1750 StoreValue(rl_dest, rl_result);
1751 return true;
1752 }
1753 if (lit == 1) {
1754 rl_src = LoadValue(rl_src, kCoreReg);
1755 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1756 OpRegCopy(rl_result.reg, rl_src.reg);
1757 StoreValue(rl_dest, rl_result);
1758 return true;
1759 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001760 // There is RegRegRegShift on Arm, so check for more special cases
1761 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001762 return EasyMultiply(rl_src, rl_dest, lit);
1763 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001764 // Can we simplify this multiplication?
1765 bool power_of_two = false;
1766 bool pop_count_le2 = false;
1767 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001768 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001769 power_of_two = true;
1770 } else if (IsPopCountLE2(lit)) {
1771 pop_count_le2 = true;
1772 } else if (IsPowerOfTwo(lit + 1)) {
1773 power_of_two_minus_one = true;
1774 } else {
1775 return false;
1776 }
1777 rl_src = LoadValue(rl_src, kCoreReg);
1778 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1779 if (power_of_two) {
1780 // Shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001781 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, CTZ(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 } else if (pop_count_le2) {
1783 // Shift and add and shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001784 int first_bit = CTZ(lit);
1785 int second_bit = CTZ(lit ^ (1 << first_bit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1787 } else {
1788 // Reverse subtract: (src << (shift + 1)) - src.
1789 DCHECK(power_of_two_minus_one);
Andreas Gampe7e499922015-01-06 08:28:12 -08001790 // TUNING: rsb dst, src, src lsl#CTZ(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001791 RegStorage t_reg = AllocTemp();
Andreas Gampe7e499922015-01-06 08:28:12 -08001792 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, CTZ(lit + 1));
buzbee2700f7e2014-03-07 09:46:20 -08001793 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001794 }
1795 StoreValue(rl_dest, rl_result);
1796 return true;
1797}
1798
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001799// Returns true if it generates instructions.
1800bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1801 RegLocation rl_src2) {
1802 if (!rl_src2.is_const ||
1803 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1804 return false;
1805 }
1806
1807 if (!rl_src2.wide) {
1808 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1809 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1810 // Generate multiply by reciprocal instead of div.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001811 float recip = 1.0f/bit_cast<float, int32_t>(divisor);
1812 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<int32_t, float>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001813 return true;
1814 }
1815 } else {
1816 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1817 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1818 // Generate multiply by reciprocal instead of div.
1819 double recip = 1.0/bit_cast<double, int64_t>(divisor);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001820 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<int64_t, double>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001821 return true;
1822 }
1823 }
1824 return false;
1825}
1826
Brian Carlstrom7940e442013-07-12 13:46:57 -07001827void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001828 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001829 RegLocation rl_result;
1830 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1831 int shift_op = false;
1832 bool is_div = false;
1833
1834 switch (opcode) {
1835 case Instruction::RSUB_INT_LIT8:
1836 case Instruction::RSUB_INT: {
1837 rl_src = LoadValue(rl_src, kCoreReg);
1838 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1839 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001840 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001841 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001842 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1843 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001844 }
1845 StoreValue(rl_dest, rl_result);
1846 return;
1847 }
1848
1849 case Instruction::SUB_INT:
1850 case Instruction::SUB_INT_2ADDR:
1851 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001852 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001853 case Instruction::ADD_INT:
1854 case Instruction::ADD_INT_2ADDR:
1855 case Instruction::ADD_INT_LIT8:
1856 case Instruction::ADD_INT_LIT16:
1857 op = kOpAdd;
1858 break;
1859 case Instruction::MUL_INT:
1860 case Instruction::MUL_INT_2ADDR:
1861 case Instruction::MUL_INT_LIT8:
1862 case Instruction::MUL_INT_LIT16: {
1863 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1864 return;
1865 }
1866 op = kOpMul;
1867 break;
1868 }
1869 case Instruction::AND_INT:
1870 case Instruction::AND_INT_2ADDR:
1871 case Instruction::AND_INT_LIT8:
1872 case Instruction::AND_INT_LIT16:
1873 op = kOpAnd;
1874 break;
1875 case Instruction::OR_INT:
1876 case Instruction::OR_INT_2ADDR:
1877 case Instruction::OR_INT_LIT8:
1878 case Instruction::OR_INT_LIT16:
1879 op = kOpOr;
1880 break;
1881 case Instruction::XOR_INT:
1882 case Instruction::XOR_INT_2ADDR:
1883 case Instruction::XOR_INT_LIT8:
1884 case Instruction::XOR_INT_LIT16:
1885 op = kOpXor;
1886 break;
1887 case Instruction::SHL_INT_LIT8:
1888 case Instruction::SHL_INT:
1889 case Instruction::SHL_INT_2ADDR:
1890 lit &= 31;
1891 shift_op = true;
1892 op = kOpLsl;
1893 break;
1894 case Instruction::SHR_INT_LIT8:
1895 case Instruction::SHR_INT:
1896 case Instruction::SHR_INT_2ADDR:
1897 lit &= 31;
1898 shift_op = true;
1899 op = kOpAsr;
1900 break;
1901 case Instruction::USHR_INT_LIT8:
1902 case Instruction::USHR_INT:
1903 case Instruction::USHR_INT_2ADDR:
1904 lit &= 31;
1905 shift_op = true;
1906 op = kOpLsr;
1907 break;
1908
1909 case Instruction::DIV_INT:
1910 case Instruction::DIV_INT_2ADDR:
1911 case Instruction::DIV_INT_LIT8:
1912 case Instruction::DIV_INT_LIT16:
1913 case Instruction::REM_INT:
1914 case Instruction::REM_INT_2ADDR:
1915 case Instruction::REM_INT_LIT8:
1916 case Instruction::REM_INT_LIT16: {
1917 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001918 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001919 return;
1920 }
buzbee11b63d12013-08-27 07:34:17 -07001921 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001922 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001923 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001924 (opcode == Instruction::DIV_INT_LIT16)) {
1925 is_div = true;
1926 } else {
1927 is_div = false;
1928 }
buzbee11b63d12013-08-27 07:34:17 -07001929 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1930 return;
1931 }
Dave Allison70202782013-10-22 17:52:19 -07001932
1933 bool done = false;
Maja Gagic6ea651f2015-02-24 16:55:04 +01001934 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1935 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001937 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001938 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001939 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001940 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1941 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001942 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001943 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001944 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001945 // Use ARM SDIV instruction for division. For remainder we also need to
1946 // calculate using a MUL and subtract.
1947 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001948 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001949 done = true;
1950 }
1951 }
1952
1953 if (!done) {
1954 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001955 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1956 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001957 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001959 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001960 else
1961 rl_result = GetReturnAlt();
1962 }
1963 StoreValue(rl_dest, rl_result);
1964 return;
1965 }
1966 default:
1967 LOG(FATAL) << "Unexpected opcode " << opcode;
1968 }
1969 rl_src = LoadValue(rl_src, kCoreReg);
1970 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001971 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001973 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001975 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 }
1977 StoreValue(rl_dest, rl_result);
1978}
1979
Andreas Gampe98430592014-07-27 19:44:50 -07001980void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001981 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 RegLocation rl_result;
1983 OpKind first_op = kOpBkpt;
1984 OpKind second_op = kOpBkpt;
1985 bool call_out = false;
1986 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001987 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1988 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001989
1990 switch (opcode) {
1991 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001992 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1993 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001995 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001996 RegStorage t_reg = AllocTemp();
1997 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1998 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1999 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2000 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002001 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002002 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2003 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 }
Andreas Gampe98430592014-07-27 19:44:50 -07002005 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 return;
2007 case Instruction::ADD_LONG:
2008 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002009 first_op = kOpAdd;
2010 second_op = kOpAdc;
2011 break;
2012 case Instruction::SUB_LONG:
2013 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 first_op = kOpSub;
2015 second_op = kOpSbc;
2016 break;
2017 case Instruction::MUL_LONG:
2018 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002019 call_out = true;
2020 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2021 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022 break;
2023 case Instruction::DIV_LONG:
2024 case Instruction::DIV_LONG_2ADDR:
2025 call_out = true;
2026 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002027 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2028 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 break;
2030 case Instruction::REM_LONG:
2031 case Instruction::REM_LONG_2ADDR:
2032 call_out = true;
2033 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002034 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002035 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002036 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2037 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002038 break;
2039 case Instruction::AND_LONG_2ADDR:
2040 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041 first_op = kOpAnd;
2042 second_op = kOpAnd;
2043 break;
2044 case Instruction::OR_LONG:
2045 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 first_op = kOpOr;
2047 second_op = kOpOr;
2048 break;
2049 case Instruction::XOR_LONG:
2050 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 first_op = kOpXor;
2052 second_op = kOpXor;
2053 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002054 default:
2055 LOG(FATAL) << "Invalid long arith op";
2056 }
2057 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002058 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002060 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002061 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002062 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2063 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2064 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2065 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002066 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2067 GenDivZeroCheckWide(r_tmp2);
2068 }
Andreas Gampe98430592014-07-27 19:44:50 -07002069 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002071 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002073 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002074 }
2075 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002076 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2077 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002078 else
Andreas Gampe98430592014-07-27 19:44:50 -07002079 rl_result = GetReturnWideAlt();
2080 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002081 }
2082}
2083
Mark Mendelle87f9b52014-04-30 14:13:18 -04002084void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2085 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2086 LoadConstantNoClobber(rl_result.reg, value);
2087 StoreValue(rl_dest, rl_result);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002088}
2089
Andreas Gampe98430592014-07-27 19:44:50 -07002090void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
Vladimir Markofac10702015-04-22 11:51:52 +01002091 RegLocation rl_src, RegisterClass return_reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 /*
2093 * Don't optimize the register usage since it calls out to support
2094 * functions
2095 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002096
Brian Carlstrom7940e442013-07-12 13:46:57 -07002097 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002098 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002099 if (rl_dest.wide) {
Vladimir Markofac10702015-04-22 11:51:52 +01002100 RegLocation rl_result = GetReturnWide(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 StoreValueWide(rl_dest, rl_result);
2102 } else {
Vladimir Markofac10702015-04-22 11:51:52 +01002103 RegLocation rl_result = GetReturn(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 StoreValue(rl_dest, rl_result);
2105 }
2106}
2107
Vladimir Marko6ce3eba2015-02-16 13:05:59 +00002108class Mir2Lir::SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002109 public:
2110 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00002111 : LIRSlowPath(m2l, branch, cont) {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002112 }
2113
2114 void Compile() OVERRIDE {
2115 m2l_->ResetRegPool();
2116 m2l_->ResetDefTracking();
2117 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002118 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002119 if (cont_ != nullptr) {
2120 m2l_->OpUnconditionalBranch(cont_);
2121 }
2122 }
2123};
2124
Brian Carlstrom7940e442013-07-12 13:46:57 -07002125/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002126void Mir2Lir::GenSuspendTest(int opt_flags) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002127 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2128 return;
2129 }
Dave Allison69dfe512014-07-11 17:11:58 +00002130 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002131 FlushAllRegs();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002132 LIR* branch = OpTestSuspend(nullptr);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002133 LIR* cont = NewLIR0(kPseudoTargetLabel);
2134 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002135 } else {
Dave Allisonb373e092014-02-20 16:06:36 -08002136 FlushAllRegs(); // TODO: needed?
2137 LIR* inst = CheckSuspendUsingLoad();
2138 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002139 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002140}
2141
2142/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002143void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002144 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2145 OpUnconditionalBranch(target);
2146 return;
2147 }
Dave Allison69dfe512014-07-11 17:11:58 +00002148 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002149 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002150 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002151 LIR* branch = OpUnconditionalBranch(nullptr);
2152 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002153 } else {
2154 // For the implicit suspend check, just perform the trigger
2155 // load and branch to the target.
Dave Allisonb373e092014-02-20 16:06:36 -08002156 FlushAllRegs();
2157 LIR* inst = CheckSuspendUsingLoad();
2158 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002159 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002160 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002161}
2162
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002163/* Call out to helper assembly routine that will null check obj and then lock it. */
2164void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002165 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002166 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002167 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002168}
2169
2170/* Call out to helper assembly routine that will null check obj and then unlock it. */
2171void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002172 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002173 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002174 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002175}
2176
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002177/* Generic code for generating a wide constant into a VR. */
2178void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2179 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002180 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002181 StoreValueWide(rl_dest, rl_result);
2182}
2183
Andreas Gampe48971b32014-08-06 10:09:01 -07002184void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002185 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2186 DCHECK(bb != nullptr);
2187 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002188 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002189 const uint16_t entries = table[1];
2190 // Chained cmp-and-branch.
2191 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002192 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002193 rl_src = LoadValue(rl_src, kCoreReg);
2194 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002195 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002196 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002197 // Switch to using a temp and add.
2198 break;
2199 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002200 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2201 DCHECK(successor_block_info != nullptr);
2202 int case_block_id = successor_block_info->block;
2203 DCHECK_EQ(starting_key + i, successor_block_info->key);
2204 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002205 }
2206 if (i < entries) {
2207 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2208 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2209 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002210 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002211 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2212 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2213 DCHECK(successor_block_info != nullptr);
2214 int case_block_id = successor_block_info->block;
2215 DCHECK_EQ(starting_key + i, successor_block_info->key);
2216 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002217 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2218 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002219 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2220 DCHECK(successor_block_info != nullptr);
2221 int case_block_id = successor_block_info->block;
2222 DCHECK_EQ(starting_key + i, successor_block_info->key);
2223 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002224 } else {
2225 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002226 for (; i < entries; ++i, ++succ_bb_iter) {
2227 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2228 DCHECK(successor_block_info != nullptr);
2229 int case_block_id = successor_block_info->block;
2230 DCHECK_EQ(starting_key + i, successor_block_info->key);
2231 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002232 }
2233 }
2234 }
2235}
2236
2237void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002238 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002239 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002240 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002241 }
2242
2243 const uint16_t entries = table[1];
2244 if (entries <= kSmallSwitchThreshold) {
2245 GenSmallPackedSwitch(mir, table_offset, rl_src);
2246 } else {
2247 // Use the backend-specific implementation.
2248 GenLargePackedSwitch(mir, table_offset, rl_src);
2249 }
2250}
2251
2252void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002253 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2254 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002255 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002256 const uint16_t entries = table[1];
2257 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002258 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002259 int i = 0;
2260 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2261 int case_block_id = successor_block_info->block;
2262 int key = successor_block_info->key;
2263 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2264 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002265 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002266 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002267}
2268
2269void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002270 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002271 if (cu_->verbose) {
2272 DumpSparseSwitchTable(table);
2273 }
2274
2275 const uint16_t entries = table[1];
2276 if (entries <= kSmallSwitchThreshold) {
2277 GenSmallSparseSwitch(mir, table_offset, rl_src);
2278 } else {
2279 // Use the backend-specific implementation.
2280 GenLargeSparseSwitch(mir, table_offset, rl_src);
2281 }
2282}
2283
Fred Shih37f05ef2014-07-16 18:38:08 -07002284bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2285 switch (size) {
2286 case kReference:
2287 return type == Primitive::kPrimNot;
2288 case k64:
2289 case kDouble:
2290 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2291 case k32:
2292 case kSingle:
2293 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2294 case kSignedHalf:
2295 return type == Primitive::kPrimShort;
2296 case kUnsignedHalf:
2297 return type == Primitive::kPrimChar;
2298 case kSignedByte:
2299 return type == Primitive::kPrimByte;
2300 case kUnsignedByte:
2301 return type == Primitive::kPrimBoolean;
2302 case kWord: // Intentional fallthrough.
2303 default:
2304 return false; // There are no sane types with this op size.
2305 }
2306}
2307
Brian Carlstrom7940e442013-07-12 13:46:57 -07002308} // namespace art