blob: 2b60a51e22b55eda620d4a39dc095c9006be006c [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Andreas Gampe7e499922015-01-06 08:28:12 -080016
Andreas Gampe0b9203e2015-01-22 20:39:27 -080017#include "mir_to_lir-inl.h"
18
Andreas Gampe7e499922015-01-06 08:28:12 -080019#include <functional>
20
Ian Rogersd582fa42014-11-05 23:46:43 -080021#include "arch/arm/instruction_set_features_arm.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex/compiler_ir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "dex/mir_graph.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070026#include "dex/quick/arm/arm_lir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080027#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070029#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "mirror/array.h"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000031#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080032#include "mirror/object-inl.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070033#include "mirror/object_reference.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000034#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "verifier/method_verifier.h"
36
37namespace art {
38
Andreas Gampe9c3b0892014-04-24 17:33:34 +000039// Shortcuts to repeatedly used long types.
40typedef mirror::ObjectArray<mirror::Object> ObjArray;
41typedef mirror::ObjectArray<mirror::Class> ClassArray;
42
Brian Carlstrom7940e442013-07-12 13:46:57 -070043/*
44 * This source files contains "gen" codegen routines that should
45 * be applicable to most targets. Only mid-level support utilities
46 * and "op" calls may be used here.
47 */
48
Andreas Gampe0b9203e2015-01-22 20:39:27 -080049ALWAYS_INLINE static inline bool ForceSlowFieldPath(CompilationUnit* cu) {
50 return (cu->enable_debug & (1 << kDebugSlowFieldPath)) != 0;
51}
52
53ALWAYS_INLINE static inline bool ForceSlowStringPath(CompilationUnit* cu) {
54 return (cu->enable_debug & (1 << kDebugSlowStringPath)) != 0;
55}
56
57ALWAYS_INLINE static inline bool ForceSlowTypePath(CompilationUnit* cu) {
58 return (cu->enable_debug & (1 << kDebugSlowTypePath)) != 0;
59}
60
Vladimir Marko5ea536a2015-04-20 20:11:30 +010061void Mir2Lir::GenIfNullUseHelperImm(RegStorage r_result, QuickEntrypointEnum trampoline, int imm) {
Vladimir Marko20f85592015-03-19 10:07:02 +000062 class CallHelperImmMethodSlowPath : public LIRSlowPath {
63 public:
64 CallHelperImmMethodSlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont,
65 QuickEntrypointEnum trampoline_in, int imm_in,
Vladimir Marko5ea536a2015-04-20 20:11:30 +010066 RegStorage r_result_in)
Vladimir Marko20f85592015-03-19 10:07:02 +000067 : LIRSlowPath(m2l, fromfast, cont), trampoline_(trampoline_in),
Vladimir Marko5ea536a2015-04-20 20:11:30 +010068 imm_(imm_in), r_result_(r_result_in) {
Vladimir Marko20f85592015-03-19 10:07:02 +000069 }
70
71 void Compile() {
72 GenerateTargetLabel();
Vladimir Marko5ea536a2015-04-20 20:11:30 +010073 m2l_->CallRuntimeHelperImm(trampoline_, imm_, true);
Vladimir Marko20f85592015-03-19 10:07:02 +000074 m2l_->OpRegCopy(r_result_, m2l_->TargetReg(kRet0, kRef));
75 m2l_->OpUnconditionalBranch(cont_);
76 }
77
78 private:
79 QuickEntrypointEnum trampoline_;
80 const int imm_;
Vladimir Marko20f85592015-03-19 10:07:02 +000081 const RegStorage r_result_;
82 };
83
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 LIR* branch = OpCmpImmBranch(kCondEq, r_result, 0, nullptr);
Vladimir Marko20f85592015-03-19 10:07:02 +000085 LIR* cont = NewLIR0(kPseudoTargetLabel);
86
87 AddSlowPath(new (arena_) CallHelperImmMethodSlowPath(this, branch, cont, trampoline, imm,
Vladimir Marko5ea536a2015-04-20 20:11:30 +010088 r_result));
Vladimir Marko20f85592015-03-19 10:07:02 +000089}
90
Vladimir Marko05792b92015-08-03 11:56:49 +010091void Mir2Lir::LoadTypeFromCache(uint32_t type_index, RegStorage class_reg) {
92 if (CanUseOpPcRelDexCacheArrayLoad()) {
93 uint32_t offset = dex_cache_arrays_layout_.TypeOffset(type_index);
94 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, class_reg, false);
95 } else {
96 RegStorage r_method = LoadCurrMethodWithHint(class_reg);
97 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(
98 GetInstructionSetPointerSize(cu_->instruction_set));
99 LoadBaseDisp(r_method, resolved_types_offset.Int32Value(), class_reg,
100 cu_->target64 ? k64 : k32, kNotVolatile);
101 int32_t offset_of_type = GetCacheOffset(type_index);
102 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
103 }
104}
105
Vladimir Marko34773072015-04-07 09:56:48 +0100106RegStorage Mir2Lir::GenGetOtherTypeForSgetSput(const MirSFieldLoweringInfo& field_info,
107 int opt_flags) {
108 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
109 // May do runtime call so everything to home locations.
110 FlushAllRegs();
Vladimir Marko05792b92015-08-03 11:56:49 +0100111 // Using fixed register to sync with possible call to runtime support.
Vladimir Marko34773072015-04-07 09:56:48 +0100112 RegStorage r_base = TargetReg(kArg0, kRef);
113 LockTemp(r_base);
Vladimir Marko05792b92015-08-03 11:56:49 +0100114 LoadTypeFromCache(field_info.StorageIndex(), r_base);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 // r_base now points at static storage (Class*) or null if the type is not yet resolved.
Vladimir Marko34773072015-04-07 09:56:48 +0100116 LIR* unresolved_branch = nullptr;
117 if (!field_info.IsClassInDexCache() && (opt_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 // Check if r_base is null.
Vladimir Marko34773072015-04-07 09:56:48 +0100119 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, nullptr);
120 }
121 LIR* uninit_branch = nullptr;
122 if (!field_info.IsClassInitialized() && (opt_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
123 // Check if r_base is not yet initialized class.
124 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
125 LockTemp(r_tmp);
126 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
127 mirror::Class::StatusOffset().Int32Value(),
128 mirror::Class::kStatusInitialized, nullptr, nullptr);
129 FreeTemp(r_tmp);
130 }
131 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
132 //
133 // Slow path to ensure a class is initialized for sget/sput.
134 //
135 class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
136 public:
137 // There are up to two branches to the static field slow path, the "unresolved" when the type
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700138 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
139 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
Vladimir Marko34773072015-04-07 09:56:48 +0100140 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100141 RegStorage r_base_in)
Vladimir Marko34773072015-04-07 09:56:48 +0100142 : LIRSlowPath(m2l, unresolved != nullptr ? unresolved : uninit, cont),
143 second_branch_(unresolved != nullptr ? uninit : nullptr),
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100144 storage_index_(storage_index), r_base_(r_base_in) {
Vladimir Marko34773072015-04-07 09:56:48 +0100145 }
146
147 void Compile() {
148 LIR* target = GenerateTargetLabel();
149 if (second_branch_ != nullptr) {
150 second_branch_->target = target;
151 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100152 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Vladimir Marko34773072015-04-07 09:56:48 +0100153 // Copy helper's result into r_base, a no-op on all but MIPS.
154 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
155
156 m2l_->OpUnconditionalBranch(cont_);
157 }
158
159 private:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700160 // Second branch to the slow path, or null if there's only one branch.
Vladimir Marko34773072015-04-07 09:56:48 +0100161 LIR* const second_branch_;
162
163 const int storage_index_;
164 const RegStorage r_base_;
Vladimir Marko34773072015-04-07 09:56:48 +0100165 };
166
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700167 // The slow path is invoked if the r_base is null or the class pointed
Vladimir Marko34773072015-04-07 09:56:48 +0100168 // to by it is not initialized.
169 LIR* cont = NewLIR0(kPseudoTargetLabel);
170 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100171 field_info.StorageIndex(), r_base));
Vladimir Marko34773072015-04-07 09:56:48 +0100172 }
173 return r_base;
174}
175
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176/*
buzbeeb48819d2013-09-14 16:15:25 -0700177 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 * blocks.
179 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700180void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 LIR* barrier = NewLIR0(kPseudoBarrier);
182 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -0700183 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100184 barrier->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185}
186
Mingyao Yange643a172014-04-08 11:02:52 -0700187void Mir2Lir::GenDivZeroException() {
188 LIR* branch = OpUnconditionalBranch(nullptr);
189 AddDivZeroCheckSlowPath(branch);
190}
191
192void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -0700193 LIR* branch = OpCondBranch(c_code, nullptr);
194 AddDivZeroCheckSlowPath(branch);
195}
196
Mingyao Yange643a172014-04-08 11:02:52 -0700197void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
198 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -0700199 AddDivZeroCheckSlowPath(branch);
200}
201
202void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
203 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
204 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800205 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000206 : LIRSlowPath(m2l, branch_in) {
Mingyao Yang42894562014-04-07 12:42:16 -0700207 }
208
Mingyao Yange643a172014-04-08 11:02:52 -0700209 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -0700210 m2l_->ResetRegPool();
211 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700212 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700213 m2l_->CallRuntimeHelper(kQuickThrowDivZero, true);
Mingyao Yang42894562014-04-07 12:42:16 -0700214 }
215 };
216
217 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
218}
Dave Allisonb373e092014-02-20 16:06:36 -0800219
Mingyao Yang80365d92014-04-18 12:10:58 -0700220void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
221 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
222 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800223 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, RegStorage index_in,
224 RegStorage length_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000225 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800226 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700227 }
228
229 void Compile() OVERRIDE {
230 m2l_->ResetRegPool();
231 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700232 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700233 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700234 }
235
236 private:
237 const RegStorage index_;
238 const RegStorage length_;
239 };
240
241 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
242 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
243}
244
245void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
246 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
247 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800248 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, int index_in, RegStorage length_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000249 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800250 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700251 }
252
253 void Compile() OVERRIDE {
254 m2l_->ResetRegPool();
255 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700256 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700257
Andreas Gampeccc60262014-07-04 18:02:38 -0700258 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
259 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700260
261 m2l_->OpRegCopy(arg1_32, length_);
262 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700263 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700264 }
265
266 private:
267 const int32_t index_;
268 const RegStorage length_;
269 };
270
271 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
272 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
273}
274
Mingyao Yange643a172014-04-08 11:02:52 -0700275LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
276 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
277 public:
278 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000279 : LIRSlowPath(m2l, branch) {
Mingyao Yange643a172014-04-08 11:02:52 -0700280 }
281
282 void Compile() OVERRIDE {
283 m2l_->ResetRegPool();
284 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700285 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700286 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700287 }
288 };
289
290 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
291 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
292 return branch;
293}
294
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800296LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000297 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700298 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 }
Pavel Vyssotski9c3617a2014-11-13 18:25:23 +0600300 // If null check has not been eliminated, reset redundant store tracking.
301 if ((opt_flags & MIR_IGNORE_NULL_CHECK) == 0) {
302 ResetDefTracking();
303 }
Dave Allisonb373e092014-02-20 16:06:36 -0800304 return nullptr;
305}
306
Dave Allisonf9439142014-03-27 15:10:22 -0700307/* Perform an explicit null-check on a register. */
308LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
309 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700310 return nullptr;
Dave Allisonf9439142014-03-27 15:10:22 -0700311 }
Mingyao Yange643a172014-04-08 11:02:52 -0700312 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700313}
314
Dave Allisonb373e092014-02-20 16:06:36 -0800315void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000316 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800317 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
318 return;
319 }
Dave Allison69dfe512014-07-11 17:11:58 +0000320 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800321 MarkSafepointPC(last_lir_insn_);
322 }
323}
324
Andreas Gampe3c12c512014-06-24 18:46:29 +0000325void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000326 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000327 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
328 return;
329 }
330 MarkSafepointPCAfter(after);
331 }
332}
333
Dave Allisonb373e092014-02-20 16:06:36 -0800334void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000335 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800336 MarkSafepointPC(last_lir_insn_);
337 }
338}
339
buzbee2700f7e2014-03-07 09:46:20 -0800340void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000341 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800342 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
343 return;
344 }
345 // Force an implicit null check by performing a memory operation (load) from the given
346 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800347 RegStorage tmp = AllocTemp();
348 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700349 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800350 FreeTemp(tmp);
351 MarkSafepointPC(load);
352 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353}
354
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700356 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700358 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 switch (opcode) {
360 case Instruction::IF_EQ:
361 cond = kCondEq;
362 break;
363 case Instruction::IF_NE:
364 cond = kCondNe;
365 break;
366 case Instruction::IF_LT:
367 cond = kCondLt;
368 break;
369 case Instruction::IF_GE:
370 cond = kCondGe;
371 break;
372 case Instruction::IF_GT:
373 cond = kCondGt;
374 break;
375 case Instruction::IF_LE:
376 cond = kCondLe;
377 break;
378 default:
379 cond = static_cast<ConditionCode>(0);
380 LOG(FATAL) << "Unexpected opcode " << opcode;
381 }
382
383 // Normalize such that if either operand is constant, src2 will be constant
384 if (rl_src1.is_const) {
385 RegLocation rl_temp = rl_src1;
386 rl_src1 = rl_src2;
387 rl_src2 = rl_temp;
388 cond = FlipComparisonOrder(cond);
389 }
390
buzbee7c02e912014-10-03 13:14:17 -0700391 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 // Is this really an immediate comparison?
393 if (rl_src2.is_const) {
394 // If it's already live in a register or not easily materialized, just keep going
395 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700396 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100398 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800400 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 return;
402 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700403
404 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
405 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
406 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
407 // the 32b literal 0 for null.
408 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
409 // Use the OpCmpImmBranch and ignore the value in the register.
410 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
411 return;
412 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700414
buzbee7c02e912014-10-03 13:14:17 -0700415 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800416 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417}
418
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700419void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700421 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
422 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 switch (opcode) {
424 case Instruction::IF_EQZ:
425 cond = kCondEq;
426 break;
427 case Instruction::IF_NEZ:
428 cond = kCondNe;
429 break;
430 case Instruction::IF_LTZ:
431 cond = kCondLt;
432 break;
433 case Instruction::IF_GEZ:
434 cond = kCondGe;
435 break;
436 case Instruction::IF_GTZ:
437 cond = kCondGt;
438 break;
439 case Instruction::IF_LEZ:
440 cond = kCondLe;
441 break;
442 default:
443 cond = static_cast<ConditionCode>(0);
444 LOG(FATAL) << "Unexpected opcode " << opcode;
445 }
buzbee2700f7e2014-03-07 09:46:20 -0800446 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447}
448
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700449void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
451 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800452 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800454 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 }
buzbee2700f7e2014-03-07 09:46:20 -0800456 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 StoreValueWide(rl_dest, rl_result);
458}
459
Yevgeny Rouban6af82062014-11-26 18:11:54 +0600460void Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
461 rl_src = UpdateLocWide(rl_src);
462 rl_src = NarrowRegLoc(rl_src);
463 StoreValue(rl_dest, rl_src);
464}
465
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700467 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700468 rl_src = LoadValue(rl_src, kCoreReg);
469 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
470 OpKind op = kOpInvalid;
471 switch (opcode) {
472 case Instruction::INT_TO_BYTE:
473 op = kOp2Byte;
474 break;
475 case Instruction::INT_TO_SHORT:
476 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700478 case Instruction::INT_TO_CHAR:
479 op = kOp2Char;
480 break;
481 default:
482 LOG(ERROR) << "Bad int conversion type";
483 }
buzbee2700f7e2014-03-07 09:46:20 -0800484 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700485 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486}
487
Andreas Gampe98430592014-07-27 19:44:50 -0700488/*
489 * Let helper function take care of everything. Will call
490 * Array::AllocFromCode(type_idx, method, count);
491 * Note: AllocFromCode will handle checks for errNegativeArraySize.
492 */
493void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
494 RegLocation rl_src) {
495 FlushAllRegs(); /* Everything to home location */
496 const DexFile* dex_file = cu_->dex_file;
497 CompilerDriver* driver = cu_->compiler_driver;
498 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800499 bool is_type_initialized; // Ignored as an array does not have an initializer.
500 bool use_direct_type_ptr;
501 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700502 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800503 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700504 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
505 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800506 // The fast path.
507 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700508 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800509 CallRuntimeHelperRegRegLocationMethod(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
Andreas Gampe98430592014-07-27 19:44:50 -0700510 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800511 } else {
512 // Use the direct pointer.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800513 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
Andreas Gampe98430592014-07-27 19:44:50 -0700514 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800515 }
516 } else {
517 // The slow path.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800518 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800519 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 } else {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800521 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 }
Andreas Gampe98430592014-07-27 19:44:50 -0700523 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524}
525
526/*
527 * Similar to GenNewArray, but with post-allocation initialization.
528 * Verifier guarantees we're dealing with an array class. Current
529 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
530 * Current code also throws internal unimp if not 'L', '[' or 'I'.
531 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700532void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000533 size_t elems = info->num_arg_words;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 int type_idx = info->index;
535 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700536 QuickEntrypointEnum target;
537 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
538 type_idx)) {
539 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700541 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 }
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800543 CallRuntimeHelperImmImmMethod(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700544 FreeTemp(TargetReg(kArg2, kNotWide));
545 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 /*
547 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
548 * return region. Because AllocFromCode placed the new array
549 * in kRet0, we'll just lock it into place. When debugger support is
550 * added, it may be necessary to additionally copy all return
551 * values to a home location in thread-local storage
552 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700553 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700554 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555
556 // TODO: use the correct component size, currently all supported types
557 // share array alignment with ints (see comment at head of function)
558 size_t component_size = sizeof(int32_t);
559
Vladimir Markobf535be2014-11-19 18:52:35 +0000560 if (elems > 5) {
561 DCHECK(info->is_range); // Non-range insn can't encode more than 5 elems.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 /*
563 * Bit of ugliness here. We're going generate a mem copy loop
564 * on the register range, but it is possible that some regs
565 * in the range have been promoted. This is unlikely, but
566 * before generating the copy, we'll just force a flush
567 * of any regs in the source range that have been promoted to
568 * home location.
569 */
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000570 for (size_t i = 0; i < elems; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 RegLocation loc = UpdateLoc(info->args[i]);
572 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100573 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov27503542014-11-06 14:45:44 +0600574 if (loc.ref) {
575 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
576 } else {
577 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
578 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 }
580 }
581 /*
582 * TUNING note: generated code here could be much improved, but
583 * this is an uncommon operation and isn't especially performance
584 * critical.
585 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700586 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700587 RegStorage r_src = AllocTempRef();
588 RegStorage r_dst = AllocTempRef();
589 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800590 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700591 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700593 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700594 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 break;
596 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700597 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700598 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 r_val = AllocTemp();
600 break;
601 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100602 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 r_val = AllocTemp();
604 break;
605 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
606 }
607 // Set up source pointer
608 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700609 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700611 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 mirror::Array::DataOffset(component_size).Int32Value());
613 // Set up the loop counter (known to be > 0)
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000614 LoadConstant(r_idx, static_cast<int>(elems - 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800616 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100618 {
619 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
620 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
621 // NOTE: No dalvik register annotation, local optimizations will be stopped
622 // by the loop boundaries.
623 }
buzbee695d13a2014-04-19 13:32:20 -0700624 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800626 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700627 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700629 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 -mirror::Array::DataOffset(component_size).Int32Value());
631 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000632 FreeTemp(r_idx);
633 FreeTemp(r_dst);
634 FreeTemp(r_src);
635 } else {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000636 DCHECK_LE(elems, 5u); // Usually but not necessarily non-range.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 // TUNING: interleave
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000638 for (size_t i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600639 RegLocation rl_arg;
640 if (info->args[i].ref) {
641 rl_arg = LoadValue(info->args[i], kRefReg);
642 StoreRefDisp(ref_reg,
643 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
644 kNotVolatile);
645 } else {
646 rl_arg = LoadValue(info->args[i], kCoreReg);
647 Store32Disp(ref_reg,
648 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
649 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800651 if (IsTemp(rl_arg.reg)) {
652 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 }
654 }
655 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000656 if (elems != 0 && info->args[0].ref) {
657 // If there is at least one potentially non-null value, unconditionally mark the GC card.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000658 for (size_t i = 0; i < elems; i++) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000659 if (!mir_graph_->IsConstantNullRef(info->args[i])) {
660 UnconditionallyMarkGCCard(ref_reg);
661 break;
662 }
663 }
664 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700666 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 }
668}
669
Ian Rogers832336b2014-10-08 15:35:22 -0700670/*
671 * Array data table format:
672 * ushort ident = 0x0300 magic value
673 * ushort width width of each element in the table
674 * uint size number of elements in the table
675 * ubyte data[size*width] table of data values (may contain a single-byte
676 * padding at the end)
677 *
678 * Total size is 4+(width * size + 1)/2 16-bit code units.
679 */
680void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
681 if (kIsDebugBuild) {
682 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
683 const Instruction::ArrayDataPayload* payload =
684 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
685 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
686 }
687 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
688 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
689}
690
Fred Shih37f05ef2014-07-16 18:38:08 -0700691void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000692 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000693 DCHECK_EQ(SPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000694 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800695 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000696 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800697 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000698 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 // Fast path, static storage base is this method's class
buzbeea0cd2d72014-06-01 09:33:49 -0700700 r_base = AllocTempRef();
Vladimir Marko34773072015-04-07 09:56:48 +0100701 RegStorage r_method = LoadCurrMethodWithHint(r_base);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000703 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 } else {
705 // Medium path, static storage base in a different class which requires checks that the other
706 // class is initialized.
Vladimir Marko34773072015-04-07 09:56:48 +0100707 r_base = GenGetOtherTypeForSgetSput(field_info, mir->optimization_flags);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100708 if (!field_info.IsClassInitialized() &&
709 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Marko34773072015-04-07 09:56:48 +0100710 // Ensure load of status and store of value don't re-order.
711 // TODO: Presumably the actual value store is control-dependent on the status load,
712 // and will thus not be reordered in any case, since stores are never speculated.
713 // Does later code "know" that the class is now initialized? If so, we still
714 // need the barrier to guard later static loads.
715 GenMemBarrier(kLoadAny);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100716 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 }
718 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700719 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
720 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100721 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100723 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700725 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000726 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
727 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100728 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700729 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000730 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700732 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000733 MarkGCCard(mir->optimization_flags, rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800735 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 } else {
737 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700738 QuickEntrypointEnum target;
739 switch (size) {
740 case kReference:
741 target = kQuickSetObjStatic;
742 break;
743 case k64:
744 case kDouble:
745 target = kQuickSet64Static;
746 break;
747 case k32:
748 case kSingle:
749 target = kQuickSet32Static;
750 break;
751 case kSignedHalf:
752 case kUnsignedHalf:
753 target = kQuickSet16Static;
754 break;
755 case kSignedByte:
756 case kUnsignedByte:
757 target = kQuickSet8Static;
758 break;
759 case kWord: // Intentional fallthrough.
760 default:
761 LOG(FATAL) << "Can't determine entrypoint for: " << size;
762 target = kQuickSet32Static;
763 }
Andreas Gampe98430592014-07-27 19:44:50 -0700764 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 }
766}
767
Fred Shih37f05ef2014-07-16 18:38:08 -0700768void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000769 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000770 DCHECK_EQ(SGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000771 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700772
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800773 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000774 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800775 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000776 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 // Fast path, static storage base is this method's class
buzbeea0cd2d72014-06-01 09:33:49 -0700778 r_base = AllocTempRef();
Vladimir Marko34773072015-04-07 09:56:48 +0100779 RegStorage r_method = LoadCurrMethodWithHint(r_base);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700780 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000781 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 } else {
783 // Medium path, static storage base in a different class which requires checks that the other
784 // class is initialized
Vladimir Marko34773072015-04-07 09:56:48 +0100785 r_base = GenGetOtherTypeForSgetSput(field_info, mir->optimization_flags);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100786 if (!field_info.IsClassInitialized() &&
787 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
Vladimir Marko34773072015-04-07 09:56:48 +0100788 // Ensure load of status and load of value don't re-order.
789 GenMemBarrier(kLoadAny);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100790 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800792 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700793 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100794 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800795
Vladimir Marko674744e2014-04-24 15:18:26 +0100796 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700797 if (IsRef(size)) {
798 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000799 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
800 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100801 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700802 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000803 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800804 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100805 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800806
Fred Shih37f05ef2014-07-16 18:38:08 -0700807 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 StoreValueWide(rl_dest, rl_result);
809 } else {
810 StoreValue(rl_dest, rl_result);
811 }
812 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700813 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700815 QuickEntrypointEnum target;
816 switch (type) {
817 case Primitive::kPrimNot:
818 target = kQuickGetObjStatic;
819 break;
820 case Primitive::kPrimLong:
821 case Primitive::kPrimDouble:
822 target = kQuickGet64Static;
823 break;
824 case Primitive::kPrimInt:
825 case Primitive::kPrimFloat:
826 target = kQuickGet32Static;
827 break;
828 case Primitive::kPrimShort:
829 target = kQuickGetShortStatic;
830 break;
831 case Primitive::kPrimChar:
832 target = kQuickGetCharStatic;
833 break;
834 case Primitive::kPrimByte:
835 target = kQuickGetByteStatic;
836 break;
837 case Primitive::kPrimBoolean:
838 target = kQuickGetBooleanStatic;
839 break;
840 case Primitive::kPrimVoid: // Intentional fallthrough.
841 default:
842 LOG(FATAL) << "Can't determine entrypoint for: " << type;
843 target = kQuickGet32Static;
844 }
Andreas Gampe98430592014-07-27 19:44:50 -0700845 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
846
Douglas Leung2db3e262014-06-25 16:02:55 -0700847 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700848 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700849 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 StoreValueWide(rl_dest, rl_result);
851 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700852 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 StoreValue(rl_dest, rl_result);
854 }
855 }
856}
857
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800858// Generate code for all slow paths.
859void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700860 // We should check slow_paths_.Size() every time, because a new slow path
861 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100862 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800863 slowpath->Compile();
864 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100865 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800866}
867
Fred Shih37f05ef2014-07-16 18:38:08 -0700868void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
869 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000870 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800871 if (kIsDebugBuild) {
872 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
873 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
874 IGetMemAccessType(mir->dalvikInsn.opcode);
875 DCHECK_EQ(mem_access_type, field_info.MemAccessType()) << mir->dalvikInsn.opcode;
876 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000877 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800878 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700879 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700880 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000881 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700882 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100883 GenNullCheck(rl_obj.reg, opt_flags);
884 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
885 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000886 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700887 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000888 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
889 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100890 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700891 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000892 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100893 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000894 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700895 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 StoreValueWide(rl_dest, rl_result);
897 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 StoreValue(rl_dest, rl_result);
899 }
900 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700901 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
902 QuickEntrypointEnum target;
903 switch (type) {
904 case Primitive::kPrimNot:
905 target = kQuickGetObjInstance;
906 break;
907 case Primitive::kPrimLong:
908 case Primitive::kPrimDouble:
909 target = kQuickGet64Instance;
910 break;
911 case Primitive::kPrimFloat:
912 case Primitive::kPrimInt:
913 target = kQuickGet32Instance;
914 break;
915 case Primitive::kPrimShort:
916 target = kQuickGetShortInstance;
917 break;
918 case Primitive::kPrimChar:
919 target = kQuickGetCharInstance;
920 break;
921 case Primitive::kPrimByte:
922 target = kQuickGetByteInstance;
923 break;
924 case Primitive::kPrimBoolean:
925 target = kQuickGetBooleanInstance;
926 break;
927 case Primitive::kPrimVoid: // Intentional fallthrough.
928 default:
929 LOG(FATAL) << "Can't determine entrypoint for: " << type;
930 target = kQuickGet32Instance;
931 }
Andreas Gampe98430592014-07-27 19:44:50 -0700932 // Second argument of pGetXXInstance is always a reference.
933 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
934 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
935
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700936 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700937 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700938 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 StoreValueWide(rl_dest, rl_result);
940 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700941 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942 StoreValue(rl_dest, rl_result);
943 }
944 }
945}
946
Vladimir Markobe0e5462014-02-26 11:24:15 +0000947void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700948 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000949 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800950 if (kIsDebugBuild) {
951 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
952 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
953 IPutMemAccessType(mir->dalvikInsn.opcode);
954 DCHECK_EQ(mem_access_type, field_info.MemAccessType());
955 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000956 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800957 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700958 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700959 // Dex code never writes to the class field.
960 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
961 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700962 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700963 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100964 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 } else {
966 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100967 }
968 GenNullCheck(rl_obj.reg, opt_flags);
969 int field_offset = field_info.FieldOffset().Int32Value();
Vladimir Markoee5e2732015-01-13 17:34:28 +0000970 LIR* null_ck_insn;
Fred Shih37f05ef2014-07-16 18:38:08 -0700971 if (IsRef(size)) {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000972 null_ck_insn = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000973 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100974 } else {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000975 null_ck_insn = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
976 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100977 }
Vladimir Markoee5e2732015-01-13 17:34:28 +0000978 MarkPossibleNullPointerExceptionAfter(opt_flags, null_ck_insn);
Fred Shih37f05ef2014-07-16 18:38:08 -0700979 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000980 MarkGCCard(opt_flags, rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 }
982 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700983 QuickEntrypointEnum target;
984 switch (size) {
985 case kReference:
986 target = kQuickSetObjInstance;
987 break;
988 case k64:
989 case kDouble:
990 target = kQuickSet64Instance;
991 break;
992 case k32:
993 case kSingle:
994 target = kQuickSet32Instance;
995 break;
996 case kSignedHalf:
997 case kUnsignedHalf:
998 target = kQuickSet16Instance;
999 break;
1000 case kSignedByte:
1001 case kUnsignedByte:
1002 target = kQuickSet8Instance;
1003 break;
1004 case kWord: // Intentional fallthrough.
1005 default:
1006 LOG(FATAL) << "Can't determine entrypoint for: " << size;
1007 target = kQuickSet32Instance;
1008 }
Andreas Gampe98430592014-07-27 19:44:50 -07001009 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
1010 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 }
1012}
1013
Ian Rogersa9a82542013-10-04 11:17:26 -07001014void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
1015 RegLocation rl_src) {
1016 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
1017 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
1018 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -07001019 QuickEntrypointEnum target = needs_range_check
1020 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
1021 : kQuickAputObjectWithBoundCheck)
1022 : kQuickAputObject;
1023 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -07001024}
1025
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001026void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001027 RegLocation rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001029 *cu_->dex_file,
1030 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 // Call out to helper which resolves type and verifies access.
1032 // Resolved type returned in kRet0.
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001033 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Vladimir Marko20f85592015-03-19 10:07:02 +00001034 rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001036 rl_result = EvalLoc(rl_dest, kRefReg, true);
1037 // We don't need access checks, load type from dex cache
Vladimir Marko05792b92015-08-03 11:56:49 +01001038 LoadTypeFromCache(type_idx, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001040 type_idx) || ForceSlowTypePath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 // Slow path, at runtime test if type is null and if so initialize
1042 FlushAllRegs();
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001043 GenIfNullUseHelperImm(rl_result.reg, kQuickInitializeType, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 }
1045 }
Vladimir Marko20f85592015-03-19 10:07:02 +00001046 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047}
1048
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001049void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 /* NOTE: Most strings should be available at compile time */
Vladimir Marko05792b92015-08-03 11:56:49 +01001051 int32_t offset_of_string = GetCacheOffset(string_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001053 *cu_->dex_file, string_idx) || ForceSlowStringPath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 // slow path, resolve string if not in dex cache
1055 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001056 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001057
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 // Might call out to helper, which will return resolved string in kRet0
Vladimir Marko20f85592015-03-19 10:07:02 +00001059 RegStorage ret0 = TargetReg(kRet0, kRef);
Vladimir Marko20f85592015-03-19 10:07:02 +00001060 if (CanUseOpPcRelDexCacheArrayLoad()) {
1061 size_t offset = dex_cache_arrays_layout_.StringOffset(string_idx);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001062 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, ret0, false);
Vladimir Marko20f85592015-03-19 10:07:02 +00001063 } else {
Vladimir Marko20f85592015-03-19 10:07:02 +00001064 // Method to declaring class.
1065 RegStorage arg0 = TargetReg(kArg0, kRef);
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001066 RegStorage r_method = LoadCurrMethodWithHint(arg0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001067 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(), arg0, kNotVolatile);
Vladimir Marko20f85592015-03-19 10:07:02 +00001068 // Declaring class to dex cache strings.
Vladimir Marko05792b92015-08-03 11:56:49 +01001069 LoadBaseDisp(arg0, mirror::Class::DexCacheStringsOffset().Int32Value(), arg0,
1070 cu_->target64 ? k64 : k32, kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001071
Vladimir Marko20f85592015-03-19 10:07:02 +00001072 LoadRefDisp(arg0, offset_of_string, ret0, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001074 GenIfNullUseHelperImm(ret0, kQuickResolveString, string_idx);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001075
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001077 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 } else {
buzbeea0cd2d72014-06-01 09:33:49 -07001079 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Vladimir Marko20f85592015-03-19 10:07:02 +00001080 if (CanUseOpPcRelDexCacheArrayLoad()) {
1081 size_t offset = dex_cache_arrays_layout_.StringOffset(string_idx);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001082 OpPcRelDexCacheArrayLoad(cu_->dex_file, offset, rl_result.reg, false);
Vladimir Marko20f85592015-03-19 10:07:02 +00001083 } else {
1084 RegLocation rl_method = LoadCurrMethod();
1085 RegStorage res_reg = AllocTempRef();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001086 LoadRefDisp(rl_method.reg, ArtMethod::DeclaringClassOffset().Int32Value(), res_reg,
Vladimir Marko20f85592015-03-19 10:07:02 +00001087 kNotVolatile);
Vladimir Marko05792b92015-08-03 11:56:49 +01001088 LoadBaseDisp(res_reg, mirror::Class::DexCacheStringsOffset().Int32Value(), res_reg,
1089 cu_->target64 ? k64 : k32, kNotVolatile);
Vladimir Marko20f85592015-03-19 10:07:02 +00001090 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
1091 FreeTemp(res_reg);
1092 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 StoreValue(rl_dest, rl_result);
1094 }
1095}
1096
Andreas Gampe98430592014-07-27 19:44:50 -07001097/*
1098 * Let helper function take care of everything. Will
1099 * call Class::NewInstanceFromCode(type_idx, method);
1100 */
1101void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1102 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 // alloc will always check for resolution, do we also need to verify
1104 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001105 const DexFile* dex_file = cu_->dex_file;
1106 CompilerDriver* driver = cu_->compiler_driver;
1107 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001108 bool is_type_initialized;
1109 bool use_direct_type_ptr;
1110 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001111 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001112 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001113 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1114 &direct_type_ptr, &is_finalizable) &&
1115 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001116 // The fast path.
1117 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001118 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001119 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001120 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001121 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001122 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001123 }
1124 } else {
1125 // Use the direct pointer.
1126 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001127 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001128 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001129 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001130 }
1131 }
1132 } else {
1133 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001134 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001135 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001137 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 }
Andreas Gampe98430592014-07-27 19:44:50 -07001139 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140}
1141
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001142void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001144 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145}
1146
1147// For final classes there are no sub-classes to check and so we can answer the instance-of
1148// question with simple comparisons.
1149void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1150 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001151 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001152 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001153
buzbeea0cd2d72014-06-01 09:33:49 -07001154 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001156 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001157 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001159 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 }
1161 LoadConstant(result_reg, 0); // assume false
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001162 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163
buzbeea0cd2d72014-06-01 09:33:49 -07001164 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1165 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001168 RegStorage r_method = LoadCurrMethodWithHint(check_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001169 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001170 kNotVolatile);
1171 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1172 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001174 LoadTypeFromCache(type_idx, check_class);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001175 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1176 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 }
1178
buzbee695d13a2014-04-19 13:32:20 -07001179 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 if (cu_->instruction_set == kThumb2) {
1181 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001182 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001184 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001186 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 }
1188 LIR* target = NewLIR0(kPseudoTargetLabel);
1189 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 FreeTemp(object_class);
1191 FreeTemp(check_class);
1192 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001193 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 FreeTemp(result_reg);
1195 }
1196 StoreValue(rl_dest, rl_result);
1197}
1198
1199void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1200 bool type_known_abstract, bool use_declaring_class,
1201 bool can_assume_type_is_in_dex_cache,
1202 uint32_t type_idx, RegLocation rl_dest,
1203 RegLocation rl_src) {
1204 FlushAllRegs();
1205 // May generate a call - use explicit registers
1206 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001207 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001208 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1209 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 if (needs_access_check) {
1211 // Check we have access to type_idx and if not throw IllegalAccessError,
1212 // returns Class* in kArg0
Vladimir Marko20f85592015-03-19 10:07:02 +00001213 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001214 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1215 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001217 RegStorage r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Serguei Katkov9ee45192014-07-17 14:39:03 +07001218 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Mathieu Chartiere401d142015-04-22 13:56:20 -07001219 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001220 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001222 if (can_assume_type_is_in_dex_cache) {
1223 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001224 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001225 }
1226
Vladimir Marko05792b92015-08-03 11:56:49 +01001227 // Load dex cache entry into class_reg (kArg2)
1228 LoadTypeFromCache(type_idx, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 if (!can_assume_type_is_in_dex_cache) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001230 GenIfNullUseHelperImm(class_reg, kQuickInitializeType, type_idx);
Andreas Gampe90969af2014-07-15 23:02:11 -07001231
1232 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001233 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 }
1235 }
1236 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001237 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001238 if (!IsSameReg(rl_result.reg, ref_reg)) {
1239 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001240 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001242 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243
1244 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001245 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001247 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1248 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001250 LIR* branchover = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001252 // rl_result == ref == class.
1253 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001254 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 } else {
1256 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001257 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001258 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 if (!type_known_abstract) {
1260 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001261 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001262 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001263 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001265 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001267 if (it != nullptr) {
1268 OpEndIT(it);
1269 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001270 FreeTemp(r_tgt);
1271 } else {
1272 if (!type_known_abstract) {
1273 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001274 LoadConstant(rl_result.reg, 1); // assume true
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001275 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001277
Serguei Katkov9ee45192014-07-17 14:39:03 +07001278 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001279 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 }
1281 }
1282 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001283 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 /* branch targets here */
1285 LIR* target = NewLIR0(kPseudoTargetLabel);
1286 StoreValue(rl_dest, rl_result);
1287 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001288 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 branchover->target = target;
1290 }
1291}
1292
1293void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1294 bool type_known_final, type_known_abstract, use_declaring_class;
1295 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1296 *cu_->dex_file,
1297 type_idx,
1298 &type_known_final,
1299 &type_known_abstract,
1300 &use_declaring_class);
1301 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1302 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1303
1304 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1305 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1306 } else {
1307 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1308 use_declaring_class, can_assume_type_is_in_dex_cache,
1309 type_idx, rl_dest, rl_src);
1310 }
1311}
1312
Vladimir Marko22fe45d2015-03-18 11:33:58 +00001313void Mir2Lir::GenCheckCast(int opt_flags, uint32_t insn_idx, uint32_t type_idx,
1314 RegLocation rl_src) {
1315 if ((opt_flags & MIR_IGNORE_CHECK_CAST) != 0) {
1316 // Compiler analysis proved that this check-cast would never cause an exception.
1317 return;
1318 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001319 bool type_known_final, type_known_abstract, use_declaring_class;
1320 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1321 *cu_->dex_file,
1322 type_idx,
1323 &type_known_final,
1324 &type_known_abstract,
1325 &use_declaring_class);
1326 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1327 // of the exception throw path.
1328 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001329 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 // Verifier type analysis proved this check cast would never cause an exception.
1331 return;
1332 }
1333 FlushAllRegs();
1334 // May generate a call - use explicit registers
1335 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001336 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 if (needs_access_check) {
1338 // Check we have access to type_idx and if not throw IllegalAccessError,
1339 // returns Class* in kRet0
1340 // InitializeTypeAndVerifyAccess(idx, method)
Vladimir Marko20f85592015-03-19 10:07:02 +00001341 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001342 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001344 RegStorage method_reg = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001345 LoadRefDisp(method_reg, ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001346 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 } else {
1348 // Load dex cache entry into class_reg (kArg2)
Vladimir Marko05792b92015-08-03 11:56:49 +01001349 LoadTypeFromCache(type_idx, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1351 // Need to test presence of type in dex cache at runtime
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001352 GenIfNullUseHelperImm(class_reg, kQuickInitializeType, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 }
1354 }
1355 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001356 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001357
1358 // Slow path for the case where the classes are not equal. In this case we need
1359 // to call a helper function to do the check.
1360 class SlowPath : public LIRSlowPath {
1361 public:
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001362 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load)
1363 : LIRSlowPath(m2l, fromfast, cont), load_(load) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001364 }
1365
1366 void Compile() {
1367 GenerateTargetLabel();
1368
1369 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001370 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1371 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001372 }
Andreas Gampe98430592014-07-27 19:44:50 -07001373 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1374 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001375 m2l_->OpUnconditionalBranch(cont_);
1376 }
1377
1378 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001379 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001380 };
1381
1382 if (type_known_abstract) {
1383 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001384 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001385 LIR* cont = NewLIR0(kPseudoTargetLabel);
1386 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1387 } else {
1388 // Harder, more common case. We need to generate a forward branch over the load
1389 // if the target is null. If it's non-null we perform the load and branch to the
1390 // slow path if the classes are not equal.
1391
1392 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001393 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001394 /* load object->klass_ */
1395 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001396 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1397 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001398
Andreas Gampeccc60262014-07-04 18:02:38 -07001399 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001400 LIR* cont = NewLIR0(kPseudoTargetLabel);
1401
1402 // Add the slow path that will not perform load since this is already done.
1403 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1404
1405 // Set the null check to branch to the continuation.
1406 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407 }
1408}
1409
1410void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001411 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 RegLocation rl_result;
1413 if (cu_->instruction_set == kThumb2) {
1414 /*
1415 * NOTE: This is the one place in the code in which we might have
1416 * as many as six live temporary registers. There are 5 in the normal
1417 * set for Arm. Until we have spill capabilities, temporarily add
1418 * lr to the temp set. It is safe to do this locally, but note that
1419 * lr is used explicitly elsewhere in the code generator and cannot
1420 * normally be used as a general temp register.
1421 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001422 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1423 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 }
1425 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1426 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1427 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1428 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001429 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1430 RegStorage t_reg = AllocTemp();
1431 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1432 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1433 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 FreeTemp(t_reg);
1435 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001436 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1437 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 }
1439 /*
1440 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1441 * following StoreValueWide might need to allocate a temp register.
1442 * To further work around the lack of a spill capability, explicitly
1443 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1444 * Remove when spill is functional.
1445 */
1446 FreeRegLocTemps(rl_result, rl_src1);
1447 FreeRegLocTemps(rl_result, rl_src2);
1448 StoreValueWide(rl_dest, rl_result);
1449 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001450 Clobber(TargetReg(kLr, kNotWide));
1451 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 }
1453}
1454
Andreas Gampe98430592014-07-27 19:44:50 -07001455void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1456 RegLocation rl_src1, RegLocation rl_shift) {
1457 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 switch (opcode) {
1459 case Instruction::SHL_LONG:
1460 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001461 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001462 break;
1463 case Instruction::SHR_LONG:
1464 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001465 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 break;
1467 case Instruction::USHR_LONG:
1468 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001469 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001470 break;
1471 default:
1472 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001473 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 }
Andreas Gampe98430592014-07-27 19:44:50 -07001475 FlushAllRegs(); /* Send everything to home location */
1476 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001477 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 StoreValueWide(rl_dest, rl_result);
1479}
1480
1481
1482void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001483 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001484 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001485 OpKind op = kOpBkpt;
1486 bool is_div_rem = false;
1487 bool check_zero = false;
1488 bool unary = false;
1489 RegLocation rl_result;
1490 bool shift_op = false;
1491 switch (opcode) {
1492 case Instruction::NEG_INT:
1493 op = kOpNeg;
1494 unary = true;
1495 break;
1496 case Instruction::NOT_INT:
1497 op = kOpMvn;
1498 unary = true;
1499 break;
1500 case Instruction::ADD_INT:
1501 case Instruction::ADD_INT_2ADDR:
1502 op = kOpAdd;
1503 break;
1504 case Instruction::SUB_INT:
1505 case Instruction::SUB_INT_2ADDR:
1506 op = kOpSub;
1507 break;
1508 case Instruction::MUL_INT:
1509 case Instruction::MUL_INT_2ADDR:
1510 op = kOpMul;
1511 break;
1512 case Instruction::DIV_INT:
1513 case Instruction::DIV_INT_2ADDR:
1514 check_zero = true;
1515 op = kOpDiv;
1516 is_div_rem = true;
1517 break;
1518 /* NOTE: returns in kArg1 */
1519 case Instruction::REM_INT:
1520 case Instruction::REM_INT_2ADDR:
1521 check_zero = true;
1522 op = kOpRem;
1523 is_div_rem = true;
1524 break;
1525 case Instruction::AND_INT:
1526 case Instruction::AND_INT_2ADDR:
1527 op = kOpAnd;
1528 break;
1529 case Instruction::OR_INT:
1530 case Instruction::OR_INT_2ADDR:
1531 op = kOpOr;
1532 break;
1533 case Instruction::XOR_INT:
1534 case Instruction::XOR_INT_2ADDR:
1535 op = kOpXor;
1536 break;
1537 case Instruction::SHL_INT:
1538 case Instruction::SHL_INT_2ADDR:
1539 shift_op = true;
1540 op = kOpLsl;
1541 break;
1542 case Instruction::SHR_INT:
1543 case Instruction::SHR_INT_2ADDR:
1544 shift_op = true;
1545 op = kOpAsr;
1546 break;
1547 case Instruction::USHR_INT:
1548 case Instruction::USHR_INT_2ADDR:
1549 shift_op = true;
1550 op = kOpLsr;
1551 break;
1552 default:
1553 LOG(FATAL) << "Invalid word arith op: " << opcode;
1554 }
1555 if (!is_div_rem) {
1556 if (unary) {
1557 rl_src1 = LoadValue(rl_src1, kCoreReg);
1558 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001559 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001561 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001562 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001563 RegStorage t_reg = AllocTemp();
1564 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001565 rl_src1 = LoadValue(rl_src1, kCoreReg);
1566 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001567 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 FreeTemp(t_reg);
1569 } else {
1570 rl_src1 = LoadValue(rl_src1, kCoreReg);
1571 rl_src2 = LoadValue(rl_src2, kCoreReg);
1572 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001573 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 }
1575 }
1576 StoreValue(rl_dest, rl_result);
1577 } else {
Dave Allison70202782013-10-22 17:52:19 -07001578 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 +01001579 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1580 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 rl_src1 = LoadValue(rl_src1, kCoreReg);
1582 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001583 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001584 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 }
buzbee2700f7e2014-03-07 09:46:20 -08001586 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001587 done = true;
1588 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001589 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001590 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001591 // Use ARM SDIV instruction for division. For remainder we also need to
1592 // calculate using a MUL and subtract.
1593 rl_src1 = LoadValue(rl_src1, kCoreReg);
1594 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001595 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001596 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001597 }
buzbee2700f7e2014-03-07 09:46:20 -08001598 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001599 done = true;
1600 }
1601 }
1602
1603 // If we haven't already generated the code use the callout function.
1604 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001606 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001607 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001608 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001609 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001610 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 }
Dave Allison70202782013-10-22 17:52:19 -07001612 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001613 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001615 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616 else
1617 rl_result = GetReturnAlt();
1618 }
1619 StoreValue(rl_dest, rl_result);
1620 }
1621}
1622
1623/*
1624 * The following are the first-level codegen routines that analyze the format
1625 * of each bytecode then either dispatch special purpose codegen routines
1626 * or produce corresponding Thumb instructions directly.
1627 */
1628
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001630static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 x &= x - 1;
1632 return (x & (x - 1)) == 0;
1633}
1634
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1636// and store the result in 'rl_dest'.
Andreas Gamped500b532015-01-16 22:09:55 -08001637bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode ATTRIBUTE_UNUSED, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001638 RegLocation rl_src, RegLocation rl_dest, int lit) {
Andreas Gamped500b532015-01-16 22:09:55 -08001639 if ((lit < 2) || (!IsPowerOfTwo(lit))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 return false;
1641 }
Andreas Gampe7e499922015-01-06 08:28:12 -08001642 int k = CTZ(lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 if (k >= 30) {
1644 // Avoid special cases.
1645 return false;
1646 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 rl_src = LoadValue(rl_src, kCoreReg);
1648 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001649 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001650 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 if (lit == 2) {
1652 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001653 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1654 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1655 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001657 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001659 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1660 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 }
1662 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001663 RegStorage t_reg1 = AllocTemp();
1664 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001666 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1667 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001669 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001671 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001673 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001675 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 }
1677 }
1678 StoreValue(rl_dest, rl_result);
1679 return true;
1680}
1681
1682// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1683// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001684bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001685 if (lit < 0) {
1686 return false;
1687 }
1688 if (lit == 0) {
1689 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1690 LoadConstant(rl_result.reg, 0);
1691 StoreValue(rl_dest, rl_result);
1692 return true;
1693 }
1694 if (lit == 1) {
1695 rl_src = LoadValue(rl_src, kCoreReg);
1696 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1697 OpRegCopy(rl_result.reg, rl_src.reg);
1698 StoreValue(rl_dest, rl_result);
1699 return true;
1700 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001701 // There is RegRegRegShift on Arm, so check for more special cases
1702 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001703 return EasyMultiply(rl_src, rl_dest, lit);
1704 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 // Can we simplify this multiplication?
1706 bool power_of_two = false;
1707 bool pop_count_le2 = false;
1708 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001709 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 power_of_two = true;
1711 } else if (IsPopCountLE2(lit)) {
1712 pop_count_le2 = true;
1713 } else if (IsPowerOfTwo(lit + 1)) {
1714 power_of_two_minus_one = true;
1715 } else {
1716 return false;
1717 }
1718 rl_src = LoadValue(rl_src, kCoreReg);
1719 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1720 if (power_of_two) {
1721 // Shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001722 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, CTZ(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 } else if (pop_count_le2) {
1724 // Shift and add and shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001725 int first_bit = CTZ(lit);
1726 int second_bit = CTZ(lit ^ (1 << first_bit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1728 } else {
1729 // Reverse subtract: (src << (shift + 1)) - src.
1730 DCHECK(power_of_two_minus_one);
Andreas Gampe7e499922015-01-06 08:28:12 -08001731 // TUNING: rsb dst, src, src lsl#CTZ(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001732 RegStorage t_reg = AllocTemp();
Andreas Gampe7e499922015-01-06 08:28:12 -08001733 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, CTZ(lit + 1));
buzbee2700f7e2014-03-07 09:46:20 -08001734 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001735 }
1736 StoreValue(rl_dest, rl_result);
1737 return true;
1738}
1739
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001740// Returns true if it generates instructions.
1741bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1742 RegLocation rl_src2) {
1743 if (!rl_src2.is_const ||
1744 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1745 return false;
1746 }
1747
1748 if (!rl_src2.wide) {
1749 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1750 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1751 // Generate multiply by reciprocal instead of div.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001752 float recip = 1.0f/bit_cast<float, int32_t>(divisor);
1753 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<int32_t, float>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001754 return true;
1755 }
1756 } else {
1757 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1758 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1759 // Generate multiply by reciprocal instead of div.
1760 double recip = 1.0/bit_cast<double, int64_t>(divisor);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001761 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<int64_t, double>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001762 return true;
1763 }
1764 }
1765 return false;
1766}
1767
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001769 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001770 RegLocation rl_result;
1771 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1772 int shift_op = false;
1773 bool is_div = false;
1774
1775 switch (opcode) {
1776 case Instruction::RSUB_INT_LIT8:
1777 case Instruction::RSUB_INT: {
1778 rl_src = LoadValue(rl_src, kCoreReg);
1779 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1780 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001781 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001783 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1784 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001785 }
1786 StoreValue(rl_dest, rl_result);
1787 return;
1788 }
1789
1790 case Instruction::SUB_INT:
1791 case Instruction::SUB_INT_2ADDR:
1792 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001793 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001794 case Instruction::ADD_INT:
1795 case Instruction::ADD_INT_2ADDR:
1796 case Instruction::ADD_INT_LIT8:
1797 case Instruction::ADD_INT_LIT16:
1798 op = kOpAdd;
1799 break;
1800 case Instruction::MUL_INT:
1801 case Instruction::MUL_INT_2ADDR:
1802 case Instruction::MUL_INT_LIT8:
1803 case Instruction::MUL_INT_LIT16: {
1804 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1805 return;
1806 }
1807 op = kOpMul;
1808 break;
1809 }
1810 case Instruction::AND_INT:
1811 case Instruction::AND_INT_2ADDR:
1812 case Instruction::AND_INT_LIT8:
1813 case Instruction::AND_INT_LIT16:
1814 op = kOpAnd;
1815 break;
1816 case Instruction::OR_INT:
1817 case Instruction::OR_INT_2ADDR:
1818 case Instruction::OR_INT_LIT8:
1819 case Instruction::OR_INT_LIT16:
1820 op = kOpOr;
1821 break;
1822 case Instruction::XOR_INT:
1823 case Instruction::XOR_INT_2ADDR:
1824 case Instruction::XOR_INT_LIT8:
1825 case Instruction::XOR_INT_LIT16:
1826 op = kOpXor;
1827 break;
1828 case Instruction::SHL_INT_LIT8:
1829 case Instruction::SHL_INT:
1830 case Instruction::SHL_INT_2ADDR:
1831 lit &= 31;
1832 shift_op = true;
1833 op = kOpLsl;
1834 break;
1835 case Instruction::SHR_INT_LIT8:
1836 case Instruction::SHR_INT:
1837 case Instruction::SHR_INT_2ADDR:
1838 lit &= 31;
1839 shift_op = true;
1840 op = kOpAsr;
1841 break;
1842 case Instruction::USHR_INT_LIT8:
1843 case Instruction::USHR_INT:
1844 case Instruction::USHR_INT_2ADDR:
1845 lit &= 31;
1846 shift_op = true;
1847 op = kOpLsr;
1848 break;
1849
1850 case Instruction::DIV_INT:
1851 case Instruction::DIV_INT_2ADDR:
1852 case Instruction::DIV_INT_LIT8:
1853 case Instruction::DIV_INT_LIT16:
1854 case Instruction::REM_INT:
1855 case Instruction::REM_INT_2ADDR:
1856 case Instruction::REM_INT_LIT8:
1857 case Instruction::REM_INT_LIT16: {
1858 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001859 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001860 return;
1861 }
buzbee11b63d12013-08-27 07:34:17 -07001862 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001863 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001864 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001865 (opcode == Instruction::DIV_INT_LIT16)) {
1866 is_div = true;
1867 } else {
1868 is_div = false;
1869 }
buzbee11b63d12013-08-27 07:34:17 -07001870 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1871 return;
1872 }
Dave Allison70202782013-10-22 17:52:19 -07001873
1874 bool done = false;
Maja Gagic6ea651f2015-02-24 16:55:04 +01001875 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1876 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001878 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001879 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001880 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001881 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1882 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001883 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001884 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001885 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001886 // Use ARM SDIV instruction for division. For remainder we also need to
1887 // calculate using a MUL and subtract.
1888 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001889 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001890 done = true;
1891 }
1892 }
1893
1894 if (!done) {
1895 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001896 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1897 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001898 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001899 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001900 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001901 else
1902 rl_result = GetReturnAlt();
1903 }
1904 StoreValue(rl_dest, rl_result);
1905 return;
1906 }
1907 default:
1908 LOG(FATAL) << "Unexpected opcode " << opcode;
1909 }
1910 rl_src = LoadValue(rl_src, kCoreReg);
1911 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001912 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001913 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001914 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001916 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001917 }
1918 StoreValue(rl_dest, rl_result);
1919}
1920
Andreas Gampe98430592014-07-27 19:44:50 -07001921void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001922 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 RegLocation rl_result;
1924 OpKind first_op = kOpBkpt;
1925 OpKind second_op = kOpBkpt;
1926 bool call_out = false;
1927 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001928 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1929 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001930
1931 switch (opcode) {
1932 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001933 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1934 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001935 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001936 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001937 RegStorage t_reg = AllocTemp();
1938 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1939 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1940 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1941 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001942 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001943 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1944 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001945 }
Andreas Gampe98430592014-07-27 19:44:50 -07001946 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001947 return;
1948 case Instruction::ADD_LONG:
1949 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 first_op = kOpAdd;
1951 second_op = kOpAdc;
1952 break;
1953 case Instruction::SUB_LONG:
1954 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955 first_op = kOpSub;
1956 second_op = kOpSbc;
1957 break;
1958 case Instruction::MUL_LONG:
1959 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07001960 call_out = true;
1961 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1962 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001963 break;
1964 case Instruction::DIV_LONG:
1965 case Instruction::DIV_LONG_2ADDR:
1966 call_out = true;
1967 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001968 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1969 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001970 break;
1971 case Instruction::REM_LONG:
1972 case Instruction::REM_LONG_2ADDR:
1973 call_out = true;
1974 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001975 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07001977 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
1978 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001979 break;
1980 case Instruction::AND_LONG_2ADDR:
1981 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 first_op = kOpAnd;
1983 second_op = kOpAnd;
1984 break;
1985 case Instruction::OR_LONG:
1986 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001987 first_op = kOpOr;
1988 second_op = kOpOr;
1989 break;
1990 case Instruction::XOR_LONG:
1991 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001992 first_op = kOpXor;
1993 second_op = kOpXor;
1994 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001995 default:
1996 LOG(FATAL) << "Invalid long arith op";
1997 }
1998 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07001999 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002000 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002001 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002002 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002003 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2004 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2005 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2006 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002007 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2008 GenDivZeroCheckWide(r_tmp2);
2009 }
Andreas Gampe98430592014-07-27 19:44:50 -07002010 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002012 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002014 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 }
2016 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002017 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2018 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019 else
Andreas Gampe98430592014-07-27 19:44:50 -07002020 rl_result = GetReturnWideAlt();
2021 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002022 }
2023}
2024
Mark Mendelle87f9b52014-04-30 14:13:18 -04002025void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2026 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2027 LoadConstantNoClobber(rl_result.reg, value);
2028 StoreValue(rl_dest, rl_result);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002029}
2030
Andreas Gampe98430592014-07-27 19:44:50 -07002031void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
Vladimir Markofac10702015-04-22 11:51:52 +01002032 RegLocation rl_src, RegisterClass return_reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002033 /*
2034 * Don't optimize the register usage since it calls out to support
2035 * functions
2036 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002037
Brian Carlstrom7940e442013-07-12 13:46:57 -07002038 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002039 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 if (rl_dest.wide) {
Vladimir Markofac10702015-04-22 11:51:52 +01002041 RegLocation rl_result = GetReturnWide(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 StoreValueWide(rl_dest, rl_result);
2043 } else {
Vladimir Markofac10702015-04-22 11:51:52 +01002044 RegLocation rl_result = GetReturn(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002045 StoreValue(rl_dest, rl_result);
2046 }
2047}
2048
Vladimir Marko6ce3eba2015-02-16 13:05:59 +00002049class Mir2Lir::SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002050 public:
2051 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00002052 : LIRSlowPath(m2l, branch, cont) {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002053 }
2054
2055 void Compile() OVERRIDE {
2056 m2l_->ResetRegPool();
2057 m2l_->ResetDefTracking();
2058 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002059 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002060 if (cont_ != nullptr) {
2061 m2l_->OpUnconditionalBranch(cont_);
2062 }
2063 }
2064};
2065
Brian Carlstrom7940e442013-07-12 13:46:57 -07002066/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002067void Mir2Lir::GenSuspendTest(int opt_flags) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002068 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2069 return;
2070 }
Dave Allison69dfe512014-07-11 17:11:58 +00002071 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002072 FlushAllRegs();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002073 LIR* branch = OpTestSuspend(nullptr);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002074 LIR* cont = NewLIR0(kPseudoTargetLabel);
2075 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002076 } else {
Dave Allisonb373e092014-02-20 16:06:36 -08002077 FlushAllRegs(); // TODO: needed?
2078 LIR* inst = CheckSuspendUsingLoad();
2079 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002081}
2082
2083/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002084void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002085 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2086 OpUnconditionalBranch(target);
2087 return;
2088 }
Dave Allison69dfe512014-07-11 17:11:58 +00002089 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002090 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002091 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002092 LIR* branch = OpUnconditionalBranch(nullptr);
2093 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002094 } else {
2095 // For the implicit suspend check, just perform the trigger
2096 // load and branch to the target.
Dave Allisonb373e092014-02-20 16:06:36 -08002097 FlushAllRegs();
2098 LIR* inst = CheckSuspendUsingLoad();
2099 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002102}
2103
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002104/* Call out to helper assembly routine that will null check obj and then lock it. */
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002105void Mir2Lir::GenMonitorEnter(int opt_flags ATTRIBUTE_UNUSED, RegLocation rl_src) {
2106 // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002107 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002108 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002109}
2110
2111/* Call out to helper assembly routine that will null check obj and then unlock it. */
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002112void Mir2Lir::GenMonitorExit(int opt_flags ATTRIBUTE_UNUSED, RegLocation rl_src) {
2113 // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002114 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002115 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002116}
2117
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002118/* Generic code for generating a wide constant into a VR. */
2119void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2120 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002121 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002122 StoreValueWide(rl_dest, rl_result);
2123}
2124
Andreas Gampe48971b32014-08-06 10:09:01 -07002125void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002126 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2127 DCHECK(bb != nullptr);
2128 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002129 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002130 const uint16_t entries = table[1];
2131 // Chained cmp-and-branch.
2132 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002133 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002134 rl_src = LoadValue(rl_src, kCoreReg);
2135 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002136 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002137 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002138 // Switch to using a temp and add.
2139 break;
2140 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002141 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2142 DCHECK(successor_block_info != nullptr);
2143 int case_block_id = successor_block_info->block;
2144 DCHECK_EQ(starting_key + i, successor_block_info->key);
2145 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002146 }
2147 if (i < entries) {
2148 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2149 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2150 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002151 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002152 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2153 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2154 DCHECK(successor_block_info != nullptr);
2155 int case_block_id = successor_block_info->block;
2156 DCHECK_EQ(starting_key + i, successor_block_info->key);
2157 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002158 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2159 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002160 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2161 DCHECK(successor_block_info != nullptr);
2162 int case_block_id = successor_block_info->block;
2163 DCHECK_EQ(starting_key + i, successor_block_info->key);
2164 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002165 } else {
2166 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002167 for (; i < entries; ++i, ++succ_bb_iter) {
2168 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2169 DCHECK(successor_block_info != nullptr);
2170 int case_block_id = successor_block_info->block;
2171 DCHECK_EQ(starting_key + i, successor_block_info->key);
2172 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002173 }
2174 }
2175 }
2176}
2177
2178void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002179 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002180 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002181 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002182 }
2183
2184 const uint16_t entries = table[1];
2185 if (entries <= kSmallSwitchThreshold) {
2186 GenSmallPackedSwitch(mir, table_offset, rl_src);
2187 } else {
2188 // Use the backend-specific implementation.
2189 GenLargePackedSwitch(mir, table_offset, rl_src);
2190 }
2191}
2192
2193void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002194 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2195 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002196 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002197 const uint16_t entries = table[1];
2198 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002199 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002200 int i = 0;
2201 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2202 int case_block_id = successor_block_info->block;
2203 int key = successor_block_info->key;
2204 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2205 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002206 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002207 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002208}
2209
2210void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002211 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002212 if (cu_->verbose) {
2213 DumpSparseSwitchTable(table);
2214 }
2215
2216 const uint16_t entries = table[1];
2217 if (entries <= kSmallSwitchThreshold) {
2218 GenSmallSparseSwitch(mir, table_offset, rl_src);
2219 } else {
2220 // Use the backend-specific implementation.
2221 GenLargeSparseSwitch(mir, table_offset, rl_src);
2222 }
2223}
2224
Fred Shih37f05ef2014-07-16 18:38:08 -07002225bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2226 switch (size) {
2227 case kReference:
2228 return type == Primitive::kPrimNot;
2229 case k64:
2230 case kDouble:
2231 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2232 case k32:
2233 case kSingle:
2234 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2235 case kSignedHalf:
2236 return type == Primitive::kPrimShort;
2237 case kUnsignedHalf:
2238 return type == Primitive::kPrimChar;
2239 case kSignedByte:
2240 return type == Primitive::kPrimByte;
2241 case kUnsignedByte:
2242 return type == Primitive::kPrimBoolean;
2243 case kWord: // Intentional fallthrough.
2244 default:
2245 return false; // There are no sane types with this op size.
2246 }
2247}
2248
Brian Carlstrom7940e442013-07-12 13:46:57 -07002249} // namespace art