blob: 5da72147b0f7cb471d4ce3abd6b877036e151904 [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;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08001107 bool finalizable;
1108 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx,
1109 *dex_file,
1110 type_idx,
1111 &finalizable)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001112 bool is_type_initialized;
1113 bool use_direct_type_ptr;
1114 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001115 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001116 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001117 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1118 &direct_type_ptr, &is_finalizable) &&
1119 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001120 // The fast path.
1121 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001122 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001123 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001124 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001125 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001126 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001127 }
1128 } else {
1129 // Use the direct pointer.
1130 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001131 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001132 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001133 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001134 }
1135 }
1136 } else {
1137 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001138 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001139 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001141 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 }
Andreas Gampe98430592014-07-27 19:44:50 -07001143 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144}
1145
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001146void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001148 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149}
1150
1151// For final classes there are no sub-classes to check and so we can answer the instance-of
1152// question with simple comparisons.
1153void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1154 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001155 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001156 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001157
buzbeea0cd2d72014-06-01 09:33:49 -07001158 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001160 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001161 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001163 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 }
1165 LoadConstant(result_reg, 0); // assume false
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001166 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167
buzbeea0cd2d72014-06-01 09:33:49 -07001168 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1169 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001172 RegStorage r_method = LoadCurrMethodWithHint(check_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001173 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001174 kNotVolatile);
1175 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1176 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001178 LoadTypeFromCache(type_idx, check_class);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001179 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1180 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 }
1182
buzbee695d13a2014-04-19 13:32:20 -07001183 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 if (cu_->instruction_set == kThumb2) {
1185 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001186 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001188 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001190 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 }
1192 LIR* target = NewLIR0(kPseudoTargetLabel);
1193 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 FreeTemp(object_class);
1195 FreeTemp(check_class);
1196 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001197 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 FreeTemp(result_reg);
1199 }
1200 StoreValue(rl_dest, rl_result);
1201}
1202
1203void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1204 bool type_known_abstract, bool use_declaring_class,
1205 bool can_assume_type_is_in_dex_cache,
1206 uint32_t type_idx, RegLocation rl_dest,
1207 RegLocation rl_src) {
1208 FlushAllRegs();
1209 // May generate a call - use explicit registers
1210 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001211 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001212 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1213 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 if (needs_access_check) {
1215 // Check we have access to type_idx and if not throw IllegalAccessError,
1216 // returns Class* in kArg0
Vladimir Marko20f85592015-03-19 10:07:02 +00001217 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001218 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1219 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001221 RegStorage r_method = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Serguei Katkov9ee45192014-07-17 14:39:03 +07001222 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Mathieu Chartiere401d142015-04-22 13:56:20 -07001223 LoadRefDisp(r_method, ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001224 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001226 if (can_assume_type_is_in_dex_cache) {
1227 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001228 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001229 }
1230
Vladimir Marko05792b92015-08-03 11:56:49 +01001231 // Load dex cache entry into class_reg (kArg2)
1232 LoadTypeFromCache(type_idx, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 if (!can_assume_type_is_in_dex_cache) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001234 GenIfNullUseHelperImm(class_reg, kQuickInitializeType, type_idx);
Andreas Gampe90969af2014-07-15 23:02:11 -07001235
1236 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001237 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 }
1239 }
1240 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001241 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001242 if (!IsSameReg(rl_result.reg, ref_reg)) {
1243 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001244 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001246 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247
1248 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001249 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001251 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1252 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001254 LIR* branchover = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001256 // rl_result == ref == class.
1257 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001258 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 } else {
1260 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001261 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001262 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 if (!type_known_abstract) {
1264 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001265 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001266 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001267 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001269 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001270 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001271 if (it != nullptr) {
1272 OpEndIT(it);
1273 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 FreeTemp(r_tgt);
1275 } else {
1276 if (!type_known_abstract) {
1277 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001278 LoadConstant(rl_result.reg, 1); // assume true
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001279 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001281
Serguei Katkov9ee45192014-07-17 14:39:03 +07001282 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001283 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 }
1285 }
1286 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001287 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 /* branch targets here */
1289 LIR* target = NewLIR0(kPseudoTargetLabel);
1290 StoreValue(rl_dest, rl_result);
1291 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001292 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 branchover->target = target;
1294 }
1295}
1296
1297void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1298 bool type_known_final, type_known_abstract, use_declaring_class;
1299 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1300 *cu_->dex_file,
1301 type_idx,
1302 &type_known_final,
1303 &type_known_abstract,
1304 &use_declaring_class);
1305 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1306 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1307
1308 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1309 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1310 } else {
1311 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1312 use_declaring_class, can_assume_type_is_in_dex_cache,
1313 type_idx, rl_dest, rl_src);
1314 }
1315}
1316
Vladimir Marko22fe45d2015-03-18 11:33:58 +00001317void Mir2Lir::GenCheckCast(int opt_flags, uint32_t insn_idx, uint32_t type_idx,
1318 RegLocation rl_src) {
1319 if ((opt_flags & MIR_IGNORE_CHECK_CAST) != 0) {
1320 // Compiler analysis proved that this check-cast would never cause an exception.
1321 return;
1322 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 bool type_known_final, type_known_abstract, use_declaring_class;
1324 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1325 *cu_->dex_file,
1326 type_idx,
1327 &type_known_final,
1328 &type_known_abstract,
1329 &use_declaring_class);
1330 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1331 // of the exception throw path.
1332 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001333 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 // Verifier type analysis proved this check cast would never cause an exception.
1335 return;
1336 }
1337 FlushAllRegs();
1338 // May generate a call - use explicit registers
1339 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001340 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 if (needs_access_check) {
1342 // Check we have access to type_idx and if not throw IllegalAccessError,
1343 // returns Class* in kRet0
1344 // InitializeTypeAndVerifyAccess(idx, method)
Vladimir Marko20f85592015-03-19 10:07:02 +00001345 CallRuntimeHelperImmMethod(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001346 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 } else if (use_declaring_class) {
Vladimir Marko20f85592015-03-19 10:07:02 +00001348 RegStorage method_reg = LoadCurrMethodWithHint(TargetReg(kArg1, kRef));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001349 LoadRefDisp(method_reg, ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001350 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 } else {
1352 // Load dex cache entry into class_reg (kArg2)
Vladimir Marko05792b92015-08-03 11:56:49 +01001353 LoadTypeFromCache(type_idx, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1355 // Need to test presence of type in dex cache at runtime
Vladimir Marko5ea536a2015-04-20 20:11:30 +01001356 GenIfNullUseHelperImm(class_reg, kQuickInitializeType, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 }
1358 }
1359 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001360 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001361
1362 // Slow path for the case where the classes are not equal. In this case we need
1363 // to call a helper function to do the check.
1364 class SlowPath : public LIRSlowPath {
1365 public:
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001366 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load)
1367 : LIRSlowPath(m2l, fromfast, cont), load_(load) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001368 }
1369
1370 void Compile() {
1371 GenerateTargetLabel();
1372
1373 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001374 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1375 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001376 }
Andreas Gampe98430592014-07-27 19:44:50 -07001377 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1378 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001379 m2l_->OpUnconditionalBranch(cont_);
1380 }
1381
1382 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001383 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001384 };
1385
1386 if (type_known_abstract) {
1387 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001388 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001389 LIR* cont = NewLIR0(kPseudoTargetLabel);
1390 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1391 } else {
1392 // Harder, more common case. We need to generate a forward branch over the load
1393 // if the target is null. If it's non-null we perform the load and branch to the
1394 // slow path if the classes are not equal.
1395
1396 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001397 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001398 /* load object->klass_ */
1399 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001400 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1401 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001402
Andreas Gampeccc60262014-07-04 18:02:38 -07001403 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001404 LIR* cont = NewLIR0(kPseudoTargetLabel);
1405
1406 // Add the slow path that will not perform load since this is already done.
1407 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1408
1409 // Set the null check to branch to the continuation.
1410 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411 }
1412}
1413
1414void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001415 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 RegLocation rl_result;
1417 if (cu_->instruction_set == kThumb2) {
1418 /*
1419 * NOTE: This is the one place in the code in which we might have
1420 * as many as six live temporary registers. There are 5 in the normal
1421 * set for Arm. Until we have spill capabilities, temporarily add
1422 * lr to the temp set. It is safe to do this locally, but note that
1423 * lr is used explicitly elsewhere in the code generator and cannot
1424 * normally be used as a general temp register.
1425 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001426 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1427 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 }
1429 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1430 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1431 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1432 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001433 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1434 RegStorage t_reg = AllocTemp();
1435 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1436 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1437 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 FreeTemp(t_reg);
1439 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001440 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1441 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 }
1443 /*
1444 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1445 * following StoreValueWide might need to allocate a temp register.
1446 * To further work around the lack of a spill capability, explicitly
1447 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1448 * Remove when spill is functional.
1449 */
1450 FreeRegLocTemps(rl_result, rl_src1);
1451 FreeRegLocTemps(rl_result, rl_src2);
1452 StoreValueWide(rl_dest, rl_result);
1453 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001454 Clobber(TargetReg(kLr, kNotWide));
1455 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 }
1457}
1458
Andreas Gampe98430592014-07-27 19:44:50 -07001459void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1460 RegLocation rl_src1, RegLocation rl_shift) {
1461 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001462 switch (opcode) {
1463 case Instruction::SHL_LONG:
1464 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001465 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 break;
1467 case Instruction::SHR_LONG:
1468 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001469 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001470 break;
1471 case Instruction::USHR_LONG:
1472 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001473 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 break;
1475 default:
1476 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001477 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 }
Andreas Gampe98430592014-07-27 19:44:50 -07001479 FlushAllRegs(); /* Send everything to home location */
1480 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001481 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 StoreValueWide(rl_dest, rl_result);
1483}
1484
1485
1486void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001487 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001488 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001489 OpKind op = kOpBkpt;
1490 bool is_div_rem = false;
1491 bool check_zero = false;
1492 bool unary = false;
1493 RegLocation rl_result;
1494 bool shift_op = false;
1495 switch (opcode) {
1496 case Instruction::NEG_INT:
1497 op = kOpNeg;
1498 unary = true;
1499 break;
1500 case Instruction::NOT_INT:
1501 op = kOpMvn;
1502 unary = true;
1503 break;
1504 case Instruction::ADD_INT:
1505 case Instruction::ADD_INT_2ADDR:
1506 op = kOpAdd;
1507 break;
1508 case Instruction::SUB_INT:
1509 case Instruction::SUB_INT_2ADDR:
1510 op = kOpSub;
1511 break;
1512 case Instruction::MUL_INT:
1513 case Instruction::MUL_INT_2ADDR:
1514 op = kOpMul;
1515 break;
1516 case Instruction::DIV_INT:
1517 case Instruction::DIV_INT_2ADDR:
1518 check_zero = true;
1519 op = kOpDiv;
1520 is_div_rem = true;
1521 break;
1522 /* NOTE: returns in kArg1 */
1523 case Instruction::REM_INT:
1524 case Instruction::REM_INT_2ADDR:
1525 check_zero = true;
1526 op = kOpRem;
1527 is_div_rem = true;
1528 break;
1529 case Instruction::AND_INT:
1530 case Instruction::AND_INT_2ADDR:
1531 op = kOpAnd;
1532 break;
1533 case Instruction::OR_INT:
1534 case Instruction::OR_INT_2ADDR:
1535 op = kOpOr;
1536 break;
1537 case Instruction::XOR_INT:
1538 case Instruction::XOR_INT_2ADDR:
1539 op = kOpXor;
1540 break;
1541 case Instruction::SHL_INT:
1542 case Instruction::SHL_INT_2ADDR:
1543 shift_op = true;
1544 op = kOpLsl;
1545 break;
1546 case Instruction::SHR_INT:
1547 case Instruction::SHR_INT_2ADDR:
1548 shift_op = true;
1549 op = kOpAsr;
1550 break;
1551 case Instruction::USHR_INT:
1552 case Instruction::USHR_INT_2ADDR:
1553 shift_op = true;
1554 op = kOpLsr;
1555 break;
1556 default:
1557 LOG(FATAL) << "Invalid word arith op: " << opcode;
1558 }
1559 if (!is_div_rem) {
1560 if (unary) {
1561 rl_src1 = LoadValue(rl_src1, kCoreReg);
1562 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001563 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001565 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001566 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001567 RegStorage t_reg = AllocTemp();
1568 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 rl_src1 = LoadValue(rl_src1, kCoreReg);
1570 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001571 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001572 FreeTemp(t_reg);
1573 } else {
1574 rl_src1 = LoadValue(rl_src1, kCoreReg);
1575 rl_src2 = LoadValue(rl_src2, kCoreReg);
1576 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001577 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 }
1579 }
1580 StoreValue(rl_dest, rl_result);
1581 } else {
Dave Allison70202782013-10-22 17:52:19 -07001582 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 +01001583 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1584 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 rl_src1 = LoadValue(rl_src1, kCoreReg);
1586 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001587 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001588 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001589 }
buzbee2700f7e2014-03-07 09:46:20 -08001590 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001591 done = true;
1592 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001593 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001594 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001595 // Use ARM SDIV instruction for division. For remainder we also need to
1596 // calculate using a MUL and subtract.
1597 rl_src1 = LoadValue(rl_src1, kCoreReg);
1598 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001599 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001600 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001601 }
buzbee2700f7e2014-03-07 09:46:20 -08001602 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001603 done = true;
1604 }
1605 }
1606
1607 // If we haven't already generated the code use the callout function.
1608 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001610 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001611 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001612 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001613 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001614 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 }
Dave Allison70202782013-10-22 17:52:19 -07001616 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001617 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001619 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 else
1621 rl_result = GetReturnAlt();
1622 }
1623 StoreValue(rl_dest, rl_result);
1624 }
1625}
1626
1627/*
1628 * The following are the first-level codegen routines that analyze the format
1629 * of each bytecode then either dispatch special purpose codegen routines
1630 * or produce corresponding Thumb instructions directly.
1631 */
1632
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001634static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635 x &= x - 1;
1636 return (x & (x - 1)) == 0;
1637}
1638
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1640// and store the result in 'rl_dest'.
Andreas Gamped500b532015-01-16 22:09:55 -08001641bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode ATTRIBUTE_UNUSED, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001642 RegLocation rl_src, RegLocation rl_dest, int lit) {
Andreas Gamped500b532015-01-16 22:09:55 -08001643 if ((lit < 2) || (!IsPowerOfTwo(lit))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 return false;
1645 }
Andreas Gampe7e499922015-01-06 08:28:12 -08001646 int k = CTZ(lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 if (k >= 30) {
1648 // Avoid special cases.
1649 return false;
1650 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 rl_src = LoadValue(rl_src, kCoreReg);
1652 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001653 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001654 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 if (lit == 2) {
1656 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001657 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1658 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1659 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001661 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001663 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1664 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 }
1666 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001667 RegStorage t_reg1 = AllocTemp();
1668 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001670 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1671 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001673 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001675 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001677 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001679 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001680 }
1681 }
1682 StoreValue(rl_dest, rl_result);
1683 return true;
1684}
1685
1686// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1687// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001688bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001689 if (lit < 0) {
1690 return false;
1691 }
1692 if (lit == 0) {
1693 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1694 LoadConstant(rl_result.reg, 0);
1695 StoreValue(rl_dest, rl_result);
1696 return true;
1697 }
1698 if (lit == 1) {
1699 rl_src = LoadValue(rl_src, kCoreReg);
1700 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1701 OpRegCopy(rl_result.reg, rl_src.reg);
1702 StoreValue(rl_dest, rl_result);
1703 return true;
1704 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001705 // There is RegRegRegShift on Arm, so check for more special cases
1706 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001707 return EasyMultiply(rl_src, rl_dest, lit);
1708 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 // Can we simplify this multiplication?
1710 bool power_of_two = false;
1711 bool pop_count_le2 = false;
1712 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001713 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 power_of_two = true;
1715 } else if (IsPopCountLE2(lit)) {
1716 pop_count_le2 = true;
1717 } else if (IsPowerOfTwo(lit + 1)) {
1718 power_of_two_minus_one = true;
1719 } else {
1720 return false;
1721 }
1722 rl_src = LoadValue(rl_src, kCoreReg);
1723 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1724 if (power_of_two) {
1725 // Shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001726 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, CTZ(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 } else if (pop_count_le2) {
1728 // Shift and add and shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001729 int first_bit = CTZ(lit);
1730 int second_bit = CTZ(lit ^ (1 << first_bit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1732 } else {
1733 // Reverse subtract: (src << (shift + 1)) - src.
1734 DCHECK(power_of_two_minus_one);
Andreas Gampe7e499922015-01-06 08:28:12 -08001735 // TUNING: rsb dst, src, src lsl#CTZ(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001736 RegStorage t_reg = AllocTemp();
Andreas Gampe7e499922015-01-06 08:28:12 -08001737 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, CTZ(lit + 1));
buzbee2700f7e2014-03-07 09:46:20 -08001738 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 }
1740 StoreValue(rl_dest, rl_result);
1741 return true;
1742}
1743
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001744// Returns true if it generates instructions.
1745bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1746 RegLocation rl_src2) {
1747 if (!rl_src2.is_const ||
1748 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1749 return false;
1750 }
1751
1752 if (!rl_src2.wide) {
1753 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1754 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1755 // Generate multiply by reciprocal instead of div.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001756 float recip = 1.0f/bit_cast<float, int32_t>(divisor);
1757 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<int32_t, float>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001758 return true;
1759 }
1760 } else {
1761 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1762 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1763 // Generate multiply by reciprocal instead of div.
1764 double recip = 1.0/bit_cast<double, int64_t>(divisor);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001765 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<int64_t, double>(recip));
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001766 return true;
1767 }
1768 }
1769 return false;
1770}
1771
Brian Carlstrom7940e442013-07-12 13:46:57 -07001772void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001773 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001774 RegLocation rl_result;
1775 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1776 int shift_op = false;
1777 bool is_div = false;
1778
1779 switch (opcode) {
1780 case Instruction::RSUB_INT_LIT8:
1781 case Instruction::RSUB_INT: {
1782 rl_src = LoadValue(rl_src, kCoreReg);
1783 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1784 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001785 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001787 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1788 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001789 }
1790 StoreValue(rl_dest, rl_result);
1791 return;
1792 }
1793
1794 case Instruction::SUB_INT:
1795 case Instruction::SUB_INT_2ADDR:
1796 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001797 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001798 case Instruction::ADD_INT:
1799 case Instruction::ADD_INT_2ADDR:
1800 case Instruction::ADD_INT_LIT8:
1801 case Instruction::ADD_INT_LIT16:
1802 op = kOpAdd;
1803 break;
1804 case Instruction::MUL_INT:
1805 case Instruction::MUL_INT_2ADDR:
1806 case Instruction::MUL_INT_LIT8:
1807 case Instruction::MUL_INT_LIT16: {
1808 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1809 return;
1810 }
1811 op = kOpMul;
1812 break;
1813 }
1814 case Instruction::AND_INT:
1815 case Instruction::AND_INT_2ADDR:
1816 case Instruction::AND_INT_LIT8:
1817 case Instruction::AND_INT_LIT16:
1818 op = kOpAnd;
1819 break;
1820 case Instruction::OR_INT:
1821 case Instruction::OR_INT_2ADDR:
1822 case Instruction::OR_INT_LIT8:
1823 case Instruction::OR_INT_LIT16:
1824 op = kOpOr;
1825 break;
1826 case Instruction::XOR_INT:
1827 case Instruction::XOR_INT_2ADDR:
1828 case Instruction::XOR_INT_LIT8:
1829 case Instruction::XOR_INT_LIT16:
1830 op = kOpXor;
1831 break;
1832 case Instruction::SHL_INT_LIT8:
1833 case Instruction::SHL_INT:
1834 case Instruction::SHL_INT_2ADDR:
1835 lit &= 31;
1836 shift_op = true;
1837 op = kOpLsl;
1838 break;
1839 case Instruction::SHR_INT_LIT8:
1840 case Instruction::SHR_INT:
1841 case Instruction::SHR_INT_2ADDR:
1842 lit &= 31;
1843 shift_op = true;
1844 op = kOpAsr;
1845 break;
1846 case Instruction::USHR_INT_LIT8:
1847 case Instruction::USHR_INT:
1848 case Instruction::USHR_INT_2ADDR:
1849 lit &= 31;
1850 shift_op = true;
1851 op = kOpLsr;
1852 break;
1853
1854 case Instruction::DIV_INT:
1855 case Instruction::DIV_INT_2ADDR:
1856 case Instruction::DIV_INT_LIT8:
1857 case Instruction::DIV_INT_LIT16:
1858 case Instruction::REM_INT:
1859 case Instruction::REM_INT_2ADDR:
1860 case Instruction::REM_INT_LIT8:
1861 case Instruction::REM_INT_LIT16: {
1862 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001863 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001864 return;
1865 }
buzbee11b63d12013-08-27 07:34:17 -07001866 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001867 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001868 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001869 (opcode == Instruction::DIV_INT_LIT16)) {
1870 is_div = true;
1871 } else {
1872 is_div = false;
1873 }
buzbee11b63d12013-08-27 07:34:17 -07001874 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1875 return;
1876 }
Dave Allison70202782013-10-22 17:52:19 -07001877
1878 bool done = false;
Maja Gagic6ea651f2015-02-24 16:55:04 +01001879 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1880 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001881 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001882 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001883 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001884 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001885 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1886 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001887 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001888 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001889 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001890 // Use ARM SDIV instruction for division. For remainder we also need to
1891 // calculate using a MUL and subtract.
1892 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001893 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001894 done = true;
1895 }
1896 }
1897
1898 if (!done) {
1899 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001900 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1901 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001902 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001903 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001904 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001905 else
1906 rl_result = GetReturnAlt();
1907 }
1908 StoreValue(rl_dest, rl_result);
1909 return;
1910 }
1911 default:
1912 LOG(FATAL) << "Unexpected opcode " << opcode;
1913 }
1914 rl_src = LoadValue(rl_src, kCoreReg);
1915 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001916 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001917 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001918 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001919 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001920 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001921 }
1922 StoreValue(rl_dest, rl_result);
1923}
1924
Andreas Gampe98430592014-07-27 19:44:50 -07001925void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001926 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001927 RegLocation rl_result;
1928 OpKind first_op = kOpBkpt;
1929 OpKind second_op = kOpBkpt;
1930 bool call_out = false;
1931 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001932 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1933 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934
1935 switch (opcode) {
1936 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001937 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1938 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001939 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001940 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001941 RegStorage t_reg = AllocTemp();
1942 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1943 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1944 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1945 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001946 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001947 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1948 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001949 }
Andreas Gampe98430592014-07-27 19:44:50 -07001950 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001951 return;
1952 case Instruction::ADD_LONG:
1953 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 first_op = kOpAdd;
1955 second_op = kOpAdc;
1956 break;
1957 case Instruction::SUB_LONG:
1958 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 first_op = kOpSub;
1960 second_op = kOpSbc;
1961 break;
1962 case Instruction::MUL_LONG:
1963 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07001964 call_out = true;
1965 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1966 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 break;
1968 case Instruction::DIV_LONG:
1969 case Instruction::DIV_LONG_2ADDR:
1970 call_out = true;
1971 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001972 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1973 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 break;
1975 case Instruction::REM_LONG:
1976 case Instruction::REM_LONG_2ADDR:
1977 call_out = true;
1978 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001979 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07001981 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
1982 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 break;
1984 case Instruction::AND_LONG_2ADDR:
1985 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001986 first_op = kOpAnd;
1987 second_op = kOpAnd;
1988 break;
1989 case Instruction::OR_LONG:
1990 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001991 first_op = kOpOr;
1992 second_op = kOpOr;
1993 break;
1994 case Instruction::XOR_LONG:
1995 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 first_op = kOpXor;
1997 second_op = kOpXor;
1998 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999 default:
2000 LOG(FATAL) << "Invalid long arith op";
2001 }
2002 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002003 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002005 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002007 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2008 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2009 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2010 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002011 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2012 GenDivZeroCheckWide(r_tmp2);
2013 }
Andreas Gampe98430592014-07-27 19:44:50 -07002014 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002016 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002018 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019 }
2020 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002021 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2022 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002023 else
Andreas Gampe98430592014-07-27 19:44:50 -07002024 rl_result = GetReturnWideAlt();
2025 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002026 }
2027}
2028
Mark Mendelle87f9b52014-04-30 14:13:18 -04002029void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2030 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2031 LoadConstantNoClobber(rl_result.reg, value);
2032 StoreValue(rl_dest, rl_result);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002033}
2034
Andreas Gampe98430592014-07-27 19:44:50 -07002035void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
Vladimir Markofac10702015-04-22 11:51:52 +01002036 RegLocation rl_src, RegisterClass return_reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002037 /*
2038 * Don't optimize the register usage since it calls out to support
2039 * functions
2040 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002041
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002043 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002044 if (rl_dest.wide) {
Vladimir Markofac10702015-04-22 11:51:52 +01002045 RegLocation rl_result = GetReturnWide(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 StoreValueWide(rl_dest, rl_result);
2047 } else {
Vladimir Markofac10702015-04-22 11:51:52 +01002048 RegLocation rl_result = GetReturn(return_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002049 StoreValue(rl_dest, rl_result);
2050 }
2051}
2052
Vladimir Marko6ce3eba2015-02-16 13:05:59 +00002053class Mir2Lir::SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002054 public:
2055 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00002056 : LIRSlowPath(m2l, branch, cont) {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002057 }
2058
2059 void Compile() OVERRIDE {
2060 m2l_->ResetRegPool();
2061 m2l_->ResetDefTracking();
2062 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002063 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002064 if (cont_ != nullptr) {
2065 m2l_->OpUnconditionalBranch(cont_);
2066 }
2067 }
2068};
2069
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002071void Mir2Lir::GenSuspendTest(int opt_flags) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002072 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2073 return;
2074 }
Dave Allison69dfe512014-07-11 17:11:58 +00002075 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002076 FlushAllRegs();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002077 LIR* branch = OpTestSuspend(nullptr);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002078 LIR* cont = NewLIR0(kPseudoTargetLabel);
2079 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002080 } else {
Dave Allisonb373e092014-02-20 16:06:36 -08002081 FlushAllRegs(); // TODO: needed?
2082 LIR* inst = CheckSuspendUsingLoad();
2083 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002085}
2086
2087/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002088void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002089 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2090 OpUnconditionalBranch(target);
2091 return;
2092 }
Dave Allison69dfe512014-07-11 17:11:58 +00002093 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002094 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002095 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002096 LIR* branch = OpUnconditionalBranch(nullptr);
2097 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002098 } else {
2099 // For the implicit suspend check, just perform the trigger
2100 // load and branch to the target.
Dave Allisonb373e092014-02-20 16:06:36 -08002101 FlushAllRegs();
2102 LIR* inst = CheckSuspendUsingLoad();
2103 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002105 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002106}
2107
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002108/* Call out to helper assembly routine that will null check obj and then lock it. */
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002109void Mir2Lir::GenMonitorEnter(int opt_flags ATTRIBUTE_UNUSED, RegLocation rl_src) {
2110 // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002111 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002112 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002113}
2114
2115/* Call out to helper assembly routine that will null check obj and then unlock it. */
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002116void Mir2Lir::GenMonitorExit(int opt_flags ATTRIBUTE_UNUSED, RegLocation rl_src) {
2117 // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002118 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002119 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002120}
2121
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002122/* Generic code for generating a wide constant into a VR. */
2123void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2124 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002125 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002126 StoreValueWide(rl_dest, rl_result);
2127}
2128
Andreas Gampe48971b32014-08-06 10:09:01 -07002129void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002130 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2131 DCHECK(bb != nullptr);
2132 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002133 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002134 const uint16_t entries = table[1];
2135 // Chained cmp-and-branch.
2136 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002137 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002138 rl_src = LoadValue(rl_src, kCoreReg);
2139 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002140 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002141 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002142 // Switch to using a temp and add.
2143 break;
2144 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002145 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2146 DCHECK(successor_block_info != nullptr);
2147 int case_block_id = successor_block_info->block;
2148 DCHECK_EQ(starting_key + i, successor_block_info->key);
2149 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002150 }
2151 if (i < entries) {
2152 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2153 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2154 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002155 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002156 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2157 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2158 DCHECK(successor_block_info != nullptr);
2159 int case_block_id = successor_block_info->block;
2160 DCHECK_EQ(starting_key + i, successor_block_info->key);
2161 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002162 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2163 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002164 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2165 DCHECK(successor_block_info != nullptr);
2166 int case_block_id = successor_block_info->block;
2167 DCHECK_EQ(starting_key + i, successor_block_info->key);
2168 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002169 } else {
2170 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002171 for (; i < entries; ++i, ++succ_bb_iter) {
2172 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2173 DCHECK(successor_block_info != nullptr);
2174 int case_block_id = successor_block_info->block;
2175 DCHECK_EQ(starting_key + i, successor_block_info->key);
2176 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002177 }
2178 }
2179 }
2180}
2181
2182void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002183 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002184 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002185 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002186 }
2187
2188 const uint16_t entries = table[1];
2189 if (entries <= kSmallSwitchThreshold) {
2190 GenSmallPackedSwitch(mir, table_offset, rl_src);
2191 } else {
2192 // Use the backend-specific implementation.
2193 GenLargePackedSwitch(mir, table_offset, rl_src);
2194 }
2195}
2196
2197void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002198 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2199 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002200 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002201 const uint16_t entries = table[1];
2202 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002203 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002204 int i = 0;
2205 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2206 int case_block_id = successor_block_info->block;
2207 int key = successor_block_info->key;
2208 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2209 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002210 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002211 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002212}
2213
2214void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002215 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002216 if (cu_->verbose) {
2217 DumpSparseSwitchTable(table);
2218 }
2219
2220 const uint16_t entries = table[1];
2221 if (entries <= kSmallSwitchThreshold) {
2222 GenSmallSparseSwitch(mir, table_offset, rl_src);
2223 } else {
2224 // Use the backend-specific implementation.
2225 GenLargeSparseSwitch(mir, table_offset, rl_src);
2226 }
2227}
2228
Fred Shih37f05ef2014-07-16 18:38:08 -07002229bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2230 switch (size) {
2231 case kReference:
2232 return type == Primitive::kPrimNot;
2233 case k64:
2234 case kDouble:
2235 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2236 case k32:
2237 case kSingle:
2238 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2239 case kSignedHalf:
2240 return type == Primitive::kPrimShort;
2241 case kUnsignedHalf:
2242 return type == Primitive::kPrimChar;
2243 case kSignedByte:
2244 return type == Primitive::kPrimByte;
2245 case kUnsignedByte:
2246 return type == Primitive::kPrimBoolean;
2247 case kWord: // Intentional fallthrough.
2248 default:
2249 return false; // There are no sane types with this op size.
2250 }
2251}
2252
Brian Carlstrom7940e442013-07-12 13:46:57 -07002253} // namespace art