blob: 455f4e338db8b31b4fab944d812d10f0bf201175 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 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 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010029#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000030#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000031#include "handle.h"
32#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000033#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010034#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000035#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000036#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010037#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000039#include "utils/array_ref.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010040#include "utils/intrusive_forward_list.h"
Vladimir Marko372f10e2016-05-17 16:30:10 +010041#include "utils/transform_array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
43namespace art {
44
David Brazdil1abb4192015-02-17 18:33:36 +000045class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000051class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000057class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010059class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010060class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010063class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000064class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000065
Mathieu Chartier736b5602015-09-02 14:54:11 -070066namespace mirror {
67class DexCache;
68} // namespace mirror
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070static const int kDefaultNumberOfBlocks = 8;
71static const int kDefaultNumberOfSuccessors = 2;
72static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010073static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010074static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076
Roland Levillain5b5b9312016-03-22 14:57:31 +000077// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
78static constexpr int32_t kMaxIntShiftDistance = 0x1f;
79// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
80static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000081
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070083static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010085static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
86
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060087static constexpr uint32_t kNoDexPc = -1;
88
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010089inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
90 // For the purposes of the compiler, the dex files must actually be the same object
91 // if we want to safely treat them as the same. This is especially important for JIT
92 // as custom class loaders can open the same underlying file (or memory) multiple
93 // times and provide different class resolution but no two class loaders should ever
94 // use the same DexFile object - doing so is an unsupported hack that can lead to
95 // all sorts of weird failures.
96 return &lhs == &rhs;
97}
98
Dave Allison20dfc792014-06-16 20:44:29 -070099enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700100 // All types.
101 kCondEQ, // ==
102 kCondNE, // !=
103 // Signed integers and floating-point numbers.
104 kCondLT, // <
105 kCondLE, // <=
106 kCondGT, // >
107 kCondGE, // >=
108 // Unsigned integers.
109 kCondB, // <
110 kCondBE, // <=
111 kCondA, // >
112 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700113};
114
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000115enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000116 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000117 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000118 kAnalysisFailThrowCatchLoop,
119 kAnalysisFailAmbiguousArrayOp,
120 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000121};
122
Vladimir Markof9f64412015-09-02 14:05:49 +0100123class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124 public:
125 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
126
127 void AddInstruction(HInstruction* instruction);
128 void RemoveInstruction(HInstruction* instruction);
129
David Brazdilc3d743f2015-04-22 13:40:50 +0100130 // Insert `instruction` before/after an existing instruction `cursor`.
131 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
132 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
133
Roland Levillain6b469232014-09-25 10:10:38 +0100134 // Return true if this list contains `instruction`.
135 bool Contains(HInstruction* instruction) const;
136
Roland Levillainccc07a92014-09-16 14:48:16 +0100137 // Return true if `instruction1` is found before `instruction2` in
138 // this instruction list and false otherwise. Abort if none
139 // of these instructions is found.
140 bool FoundBefore(const HInstruction* instruction1,
141 const HInstruction* instruction2) const;
142
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000143 bool IsEmpty() const { return first_instruction_ == nullptr; }
144 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
145
146 // Update the block of all instructions to be `block`.
147 void SetBlockOfInstructions(HBasicBlock* block) const;
148
149 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000150 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000151 void Add(const HInstructionList& instruction_list);
152
David Brazdil2d7352b2015-04-20 14:52:42 +0100153 // Return the number of instructions in the list. This is an expensive operation.
154 size_t CountSize() const;
155
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100156 private:
157 HInstruction* first_instruction_;
158 HInstruction* last_instruction_;
159
160 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000161 friend class HGraph;
162 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100163 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100164 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100165
166 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
167};
168
David Brazdil4833f5a2015-12-16 10:37:39 +0000169class ReferenceTypeInfo : ValueObject {
170 public:
171 typedef Handle<mirror::Class> TypeHandle;
172
Vladimir Markoa1de9182016-02-25 11:37:38 +0000173 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
174
175 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000176 return ReferenceTypeInfo(type_handle, is_exact);
177 }
178
179 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
180
Vladimir Markof39745e2016-01-26 12:16:55 +0000181 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000182 return handle.GetReference() != nullptr;
183 }
184
Vladimir Marko456307a2016-04-19 14:12:13 +0000185 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000186 return IsValidHandle(type_handle_);
187 }
188
189 bool IsExact() const { return is_exact_; }
190
191 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
192 DCHECK(IsValid());
193 return GetTypeHandle()->IsObjectClass();
194 }
195
196 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
197 DCHECK(IsValid());
198 return GetTypeHandle()->IsStringClass();
199 }
200
201 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
202 DCHECK(IsValid());
203 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
204 }
205
206 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
207 DCHECK(IsValid());
208 return GetTypeHandle()->IsInterface();
209 }
210
211 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
212 DCHECK(IsValid());
213 return GetTypeHandle()->IsArrayClass();
214 }
215
216 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
217 DCHECK(IsValid());
218 return GetTypeHandle()->IsPrimitiveArray();
219 }
220
221 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
222 DCHECK(IsValid());
223 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
224 }
225
226 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
227 DCHECK(IsValid());
228 if (!IsExact()) return false;
229 if (!IsArrayClass()) return false;
230 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
231 }
232
233 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
234 DCHECK(IsValid());
235 if (!IsExact()) return false;
236 if (!IsArrayClass()) return false;
237 if (!rti.IsArrayClass()) return false;
238 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
239 rti.GetTypeHandle()->GetComponentType());
240 }
241
242 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
243
244 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
245 DCHECK(IsValid());
246 DCHECK(rti.IsValid());
247 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
248 }
249
250 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
251 DCHECK(IsValid());
252 DCHECK(rti.IsValid());
253 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
254 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
255 }
256
257 // Returns true if the type information provide the same amount of details.
258 // Note that it does not mean that the instructions have the same actual type
259 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000260 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000261 if (!IsValid() && !rti.IsValid()) {
262 // Invalid types are equal.
263 return true;
264 }
265 if (!IsValid() || !rti.IsValid()) {
266 // One is valid, the other not.
267 return false;
268 }
269 return IsExact() == rti.IsExact()
270 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
271 }
272
273 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000274 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
275 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
276 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000277
278 // The class of the object.
279 TypeHandle type_handle_;
280 // Whether or not the type is exact or a superclass of the actual type.
281 // Whether or not we have any information about this type.
282 bool is_exact_;
283};
284
285std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
286
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100288class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100290 HGraph(ArenaAllocator* arena,
291 const DexFile& dex_file,
292 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100293 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700294 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100295 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100296 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000297 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100298 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000299 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100300 blocks_(arena->Adapter(kArenaAllocBlockList)),
301 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
302 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700303 entry_block_(nullptr),
304 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100305 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100306 number_of_vregs_(0),
307 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000308 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400309 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000310 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000311 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000312 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000313 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100314 dex_file_(dex_file),
315 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100316 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100317 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100318 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100321 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
322 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
323 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
324 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000325 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000326 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
327 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100328 blocks_.reserve(kDefaultNumberOfBlocks);
329 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
David Brazdilbadd8262016-02-02 16:28:56 +0000331 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
332 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
333
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000334 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100335 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
336
David Brazdil69ba7b72015-06-23 18:27:30 +0100337 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000338 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100339
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000340 HBasicBlock* GetEntryBlock() const { return entry_block_; }
341 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100342 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000343
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000344 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
345 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000346
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100348
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100349 void ComputeDominanceInformation();
350 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000351 void ClearLoopInformation();
352 void FindBackEdges(ArenaBitVector* visited);
353 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100354 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100355 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
David Brazdil4833f5a2015-12-16 10:37:39 +0000357 // Analyze all natural loops in this graph. Returns a code specifying that it
358 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000359 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000360 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100361
David Brazdilffee3d32015-07-06 11:48:53 +0100362 // Iterate over blocks to compute try block membership. Needs reverse post
363 // order and loop information.
364 void ComputeTryBlockInformation();
365
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000366 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000367 // Returns the instruction to replace the invoke expression or null if the
368 // invoke is for a void method. Note that the caller is responsible for replacing
369 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000370 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000371
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000372 // Update the loop and try membership of `block`, which was spawned from `reference`.
373 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
374 // should be the new back edge.
375 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
376 HBasicBlock* reference,
377 bool replace_if_back_edge);
378
Mingyao Yang3584bce2015-05-19 16:01:59 -0700379 // Need to add a couple of blocks to test if the loop body is entered and
380 // put deoptimization instructions, etc.
381 void TransformLoopHeaderForBCE(HBasicBlock* header);
382
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000383 // Removes `block` from the graph. Assumes `block` has been disconnected from
384 // other blocks and has no instructions or phis.
385 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000386
David Brazdilfc6a86a2015-06-26 10:33:45 +0000387 // Splits the edge between `block` and `successor` while preserving the
388 // indices in the predecessor/successor lists. If there are multiple edges
389 // between the blocks, the lowest indices are used.
390 // Returns the new block which is empty and has the same dex pc as `successor`.
391 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
392
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100393 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
394 void SimplifyLoop(HBasicBlock* header);
395
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396 int32_t GetNextInstructionId() {
397 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000398 return current_instruction_id_++;
399 }
400
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000401 int32_t GetCurrentInstructionId() const {
402 return current_instruction_id_;
403 }
404
405 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000406 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000407 current_instruction_id_ = id;
408 }
409
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100410 uint16_t GetMaximumNumberOfOutVRegs() const {
411 return maximum_number_of_out_vregs_;
412 }
413
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000414 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
415 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100416 }
417
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100418 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
419 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
420 }
421
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000422 void UpdateTemporariesVRegSlots(size_t slots) {
423 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100424 }
425
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000426 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100427 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000428 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100429 }
430
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100431 void SetNumberOfVRegs(uint16_t number_of_vregs) {
432 number_of_vregs_ = number_of_vregs;
433 }
434
435 uint16_t GetNumberOfVRegs() const {
436 return number_of_vregs_;
437 }
438
439 void SetNumberOfInVRegs(uint16_t value) {
440 number_of_in_vregs_ = value;
441 }
442
David Brazdildee58d62016-04-07 09:54:26 +0000443 uint16_t GetNumberOfInVRegs() const {
444 return number_of_in_vregs_;
445 }
446
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100447 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100448 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100449 return number_of_vregs_ - number_of_in_vregs_;
450 }
451
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100452 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100453 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100454 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100455
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100456 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100457 return linear_order_;
458 }
459
Mark Mendell1152c922015-04-24 17:06:35 -0400460 bool HasBoundsChecks() const {
461 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800462 }
463
Mark Mendell1152c922015-04-24 17:06:35 -0400464 void SetHasBoundsChecks(bool value) {
465 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800466 }
467
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100468 bool ShouldGenerateConstructorBarrier() const {
469 return should_generate_constructor_barrier_;
470 }
471
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000472 bool IsDebuggable() const { return debuggable_; }
473
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000475 // already, it is created and inserted into the graph. This method is only for
476 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600477 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000478
479 // TODO: This is problematic for the consistency of reference type propagation
480 // because it can be created anytime after the pass and thus it will be left
481 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600482 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000483
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600484 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
485 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000486 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600487 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
488 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000489 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600490 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
491 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000492 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600493 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
494 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000495 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000496
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100497 HCurrentMethod* GetCurrentMethod();
498
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100499 const DexFile& GetDexFile() const {
500 return dex_file_;
501 }
502
503 uint32_t GetMethodIdx() const {
504 return method_idx_;
505 }
506
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100507 InvokeType GetInvokeType() const {
508 return invoke_type_;
509 }
510
Mark Mendellc4701932015-04-10 13:18:51 -0400511 InstructionSet GetInstructionSet() const {
512 return instruction_set_;
513 }
514
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000515 bool IsCompilingOsr() const { return osr_; }
516
David Brazdil77a48ae2015-09-15 12:34:04 +0000517 bool HasTryCatch() const { return has_try_catch_; }
518 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100519
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000520 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
521 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
522
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100523 ArtMethod* GetArtMethod() const { return art_method_; }
524 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
525
Mark Mendellf6529172015-11-17 11:16:56 -0500526 // Returns an instruction with the opposite boolean value from 'cond'.
527 // The instruction has been inserted into the graph, either as a constant, or
528 // before cursor.
529 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
530
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000531 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
532
David Brazdil2d7352b2015-04-20 14:52:42 +0100533 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000534 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100535 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000537 template <class InstructionType, typename ValueType>
538 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 ArenaSafeMap<ValueType, InstructionType*>* cache,
540 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000541 // Try to find an existing constant of the given value.
542 InstructionType* constant = nullptr;
543 auto cached_constant = cache->find(value);
544 if (cached_constant != cache->end()) {
545 constant = cached_constant->second;
546 }
547
548 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100549 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000550 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600551 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000552 cache->Overwrite(value, constant);
553 InsertConstant(constant);
554 }
555 return constant;
556 }
557
David Brazdil8d5b8b22015-03-24 10:51:52 +0000558 void InsertConstant(HConstant* instruction);
559
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000560 // Cache a float constant into the graph. This method should only be
561 // called by the SsaBuilder when creating "equivalent" instructions.
562 void CacheFloatConstant(HFloatConstant* constant);
563
564 // See CacheFloatConstant comment.
565 void CacheDoubleConstant(HDoubleConstant* constant);
566
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000567 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568
569 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100570 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000571
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100572 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100573 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100575 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100576 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100577
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000578 HBasicBlock* entry_block_;
579 HBasicBlock* exit_block_;
580
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100581 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100582 uint16_t maximum_number_of_out_vregs_;
583
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100584 // The number of virtual registers in this method. Contains the parameters.
585 uint16_t number_of_vregs_;
586
587 // The number of virtual registers used by parameters of this method.
588 uint16_t number_of_in_vregs_;
589
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000590 // Number of vreg size slots that the temporaries use (used in baseline compiler).
591 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100592
Mark Mendell1152c922015-04-24 17:06:35 -0400593 // Has bounds checks. We can totally skip BCE if it's false.
594 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800595
David Brazdil77a48ae2015-09-15 12:34:04 +0000596 // Flag whether there are any try/catch blocks in the graph. We will skip
597 // try/catch-related passes if false.
598 bool has_try_catch_;
599
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000600 // Flag whether there are any irreducible loops in the graph.
601 bool has_irreducible_loops_;
602
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000603 // Indicates whether the graph should be compiled in a way that
604 // ensures full debuggability. If false, we can apply more
605 // aggressive optimizations that may limit the level of debugging.
606 const bool debuggable_;
607
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000608 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000609 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000610
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100611 // The dex file from which the method is from.
612 const DexFile& dex_file_;
613
614 // The method index in the dex file.
615 const uint32_t method_idx_;
616
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100617 // If inlined, this encodes how the callee is being invoked.
618 const InvokeType invoke_type_;
619
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100620 // Whether the graph has been transformed to SSA form. Only used
621 // in debug mode to ensure we are not using properties only valid
622 // for non-SSA form (like the number of temporaries).
623 bool in_ssa_form_;
624
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100625 const bool should_generate_constructor_barrier_;
626
Mathieu Chartiere401d142015-04-22 13:56:20 -0700627 const InstructionSet instruction_set_;
628
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000629 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000630 HNullConstant* cached_null_constant_;
631 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000633 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000635
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100636 HCurrentMethod* cached_current_method_;
637
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100638 // The ArtMethod this graph is for. Note that for AOT, it may be null,
639 // for example for methods whose declaring class could not be resolved
640 // (such as when the superclass could not be found).
641 ArtMethod* art_method_;
642
David Brazdil4833f5a2015-12-16 10:37:39 +0000643 // Keep the RTI of inexact Object to avoid having to pass stack handle
644 // collection pointer to passes which may create NullConstant.
645 ReferenceTypeInfo inexact_object_rti_;
646
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000647 // Whether we are compiling this graph for on stack replacement: this will
648 // make all loops seen as irreducible and emit special stack maps to mark
649 // compiled code entries which the interpreter can directly jump to.
650 const bool osr_;
651
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000652 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100653 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000654 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000655 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656 DISALLOW_COPY_AND_ASSIGN(HGraph);
657};
658
Vladimir Markof9f64412015-09-02 14:05:49 +0100659class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000660 public:
661 HLoopInformation(HBasicBlock* header, HGraph* graph)
662 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100663 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000664 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100665 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100666 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100667 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000668 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100669 back_edges_.reserve(kDefaultNumberOfBackEdges);
670 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100671
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000672 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100673 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000674
675 void Dump(std::ostream& os);
676
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100677 HBasicBlock* GetHeader() const {
678 return header_;
679 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000680
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000681 void SetHeader(HBasicBlock* block) {
682 header_ = block;
683 }
684
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100685 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
686 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
687 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
688
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000689 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100690 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000691 }
692
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100694 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100695 }
696
David Brazdil46e2a392015-03-16 17:31:52 +0000697 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100698 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 }
700
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000701 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000703 }
704
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100705 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100707 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 }
710
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100711 // Returns the lifetime position of the back edge that has the
712 // greatest lifetime position.
713 size_t GetLifetimeEnd() const;
714
715 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100716 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100717 }
718
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000719 // Finds blocks that are part of this loop.
720 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100721
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100722 // Returns whether this loop information contains `block`.
723 // Note that this loop information *must* be populated before entering this function.
724 bool Contains(const HBasicBlock& block) const;
725
726 // Returns whether this loop information is an inner loop of `other`.
727 // Note that `other` *must* be populated before entering this function.
728 bool IsIn(const HLoopInformation& other) const;
729
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800730 // Returns true if instruction is not defined within this loop.
731 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700732
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100733 const ArenaBitVector& GetBlocks() const { return blocks_; }
734
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000736 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000737
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000738 void ClearAllBlocks() {
739 blocks_.ClearAllBits();
740 }
741
David Brazdil3f4a5222016-05-06 12:46:21 +0100742 bool HasBackEdgeNotDominatedByHeader() const;
743
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100744 bool IsPopulated() const {
745 return blocks_.GetHighestBitSet() != -1;
746 }
747
Anton Shaminf89381f2016-05-16 16:44:13 +0600748 bool DominatesAllBackEdges(HBasicBlock* block);
749
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000750 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100751 // Internal recursive implementation of `Populate`.
752 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100753 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100754
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000755 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100756 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000757 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100758 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100759 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100760 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000761
762 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
763};
764
David Brazdilec16f792015-08-19 15:04:01 +0100765// Stores try/catch information for basic blocks.
766// Note that HGraph is constructed so that catch blocks cannot simultaneously
767// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100768class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100769 public:
770 // Try block information constructor.
771 explicit TryCatchInformation(const HTryBoundary& try_entry)
772 : try_entry_(&try_entry),
773 catch_dex_file_(nullptr),
774 catch_type_index_(DexFile::kDexNoIndex16) {
775 DCHECK(try_entry_ != nullptr);
776 }
777
778 // Catch block information constructor.
779 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
780 : try_entry_(nullptr),
781 catch_dex_file_(&dex_file),
782 catch_type_index_(catch_type_index) {}
783
784 bool IsTryBlock() const { return try_entry_ != nullptr; }
785
786 const HTryBoundary& GetTryEntry() const {
787 DCHECK(IsTryBlock());
788 return *try_entry_;
789 }
790
791 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
792
793 bool IsCatchAllTypeIndex() const {
794 DCHECK(IsCatchBlock());
795 return catch_type_index_ == DexFile::kDexNoIndex16;
796 }
797
798 uint16_t GetCatchTypeIndex() const {
799 DCHECK(IsCatchBlock());
800 return catch_type_index_;
801 }
802
803 const DexFile& GetCatchDexFile() const {
804 DCHECK(IsCatchBlock());
805 return *catch_dex_file_;
806 }
807
808 private:
809 // One of possibly several TryBoundary instructions entering the block's try.
810 // Only set for try blocks.
811 const HTryBoundary* try_entry_;
812
813 // Exception type information. Only set for catch blocks.
814 const DexFile* catch_dex_file_;
815 const uint16_t catch_type_index_;
816};
817
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100818static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100819static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100820
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000821// A block in a method. Contains the list of instructions represented
822// as a double linked list. Each block knows its predecessors and
823// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100824
Vladimir Markof9f64412015-09-02 14:05:49 +0100825class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600827 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000828 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000829 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
830 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000831 loop_information_(nullptr),
832 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000833 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100834 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100835 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100836 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000837 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000838 try_catch_information_(nullptr) {
839 predecessors_.reserve(kDefaultNumberOfPredecessors);
840 successors_.reserve(kDefaultNumberOfSuccessors);
841 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
842 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000843
Vladimir Marko60584552015-09-03 13:35:12 +0000844 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100845 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846 }
847
Vladimir Marko60584552015-09-03 13:35:12 +0000848 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850 }
851
David Brazdild26a4112015-11-10 11:07:31 +0000852 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
853 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
854
Vladimir Marko60584552015-09-03 13:35:12 +0000855 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
856 return ContainsElement(successors_, block, start_from);
857 }
858
859 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100860 return dominated_blocks_;
861 }
862
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100863 bool IsEntryBlock() const {
864 return graph_->GetEntryBlock() == this;
865 }
866
867 bool IsExitBlock() const {
868 return graph_->GetExitBlock() == this;
869 }
870
David Brazdil46e2a392015-03-16 17:31:52 +0000871 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000872 bool IsSingleTryBoundary() const;
873
874 // Returns true if this block emits nothing but a jump.
875 bool IsSingleJump() const {
876 HLoopInformation* loop_info = GetLoopInformation();
877 return (IsSingleGoto() || IsSingleTryBoundary())
878 // Back edges generate a suspend check.
879 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
880 }
David Brazdil46e2a392015-03-16 17:31:52 +0000881
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882 void AddBackEdge(HBasicBlock* back_edge) {
883 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000884 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000885 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100886 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887 loop_information_->AddBackEdge(back_edge);
888 }
889
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000892
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100893 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600895 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897 HBasicBlock* GetDominator() const { return dominator_; }
898 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000899 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
900
901 void RemoveDominatedBlock(HBasicBlock* block) {
902 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100903 }
Vladimir Marko60584552015-09-03 13:35:12 +0000904
905 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
906 ReplaceElement(dominated_blocks_, existing, new_block);
907 }
908
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100909 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000910
911 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100912 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000913 }
914
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100915 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
916 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100917 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100918 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100919 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
920 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000921
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000922 HInstruction* GetFirstInstructionDisregardMoves() const;
923
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000925 successors_.push_back(block);
926 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 }
928
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100929 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
930 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100931 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000932 new_block->predecessors_.push_back(this);
933 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000934 }
935
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000936 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
937 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000938 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000939 new_block->successors_.push_back(this);
940 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000941 }
942
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100943 // Insert `this` between `predecessor` and `successor. This method
944 // preserves the indicies, and will update the first edge found between
945 // `predecessor` and `successor`.
946 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
947 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100948 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000949 successor->predecessors_[predecessor_index] = this;
950 predecessor->successors_[successor_index] = this;
951 successors_.push_back(successor);
952 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100953 }
954
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100955 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000956 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100957 }
958
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000959 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000960 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000961 }
962
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100963 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100965 }
966
967 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 predecessors_.push_back(block);
969 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100970 }
971
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100972 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000973 DCHECK_EQ(predecessors_.size(), 2u);
974 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100975 }
976
David Brazdil769c9e52015-04-27 13:54:09 +0100977 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000978 DCHECK_EQ(successors_.size(), 2u);
979 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100980 }
981
David Brazdilfc6a86a2015-06-26 10:33:45 +0000982 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000983 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100984 }
985
David Brazdilfc6a86a2015-06-26 10:33:45 +0000986 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000987 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100988 }
989
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000991 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100992 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000993 }
994
995 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000996 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100997 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000998 }
999
1000 // Returns whether the first occurrence of `predecessor` in the list of
1001 // predecessors is at index `idx`.
1002 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001003 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004 return GetPredecessorIndexOf(predecessor) == idx;
1005 }
1006
David Brazdild7558da2015-09-22 13:04:14 +01001007 // Create a new block between this block and its predecessors. The new block
1008 // is added to the graph, all predecessor edges are relinked to it and an edge
1009 // is created to `this`. Returns the new empty block. Reverse post order or
1010 // loop and try/catch information are not updated.
1011 HBasicBlock* CreateImmediateDominator();
1012
David Brazdilfc6a86a2015-06-26 10:33:45 +00001013 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001014 // created, latter block. Note that this method will add the block to the
1015 // graph, create a Goto at the end of the former block and will create an edge
1016 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001017 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001018 HBasicBlock* SplitBefore(HInstruction* cursor);
1019
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001020 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001021 // created block. Note that this method just updates raw block information,
1022 // like predecessors, successors, dominators, and instruction list. It does not
1023 // update the graph, reverse post order, loop information, nor make sure the
1024 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001025 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1026
1027 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1028 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029
1030 // Merge `other` at the end of `this`. Successors and dominated blocks of
1031 // `other` are changed to be successors and dominated blocks of `this`. Note
1032 // that this method does not update the graph, reverse post order, loop
1033 // information, nor make sure the blocks are consistent (for example ending
1034 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001035 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001036
1037 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1038 // of `this` are moved to `other`.
1039 // Note that this method does not update the graph, reverse post order, loop
1040 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001041 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001042 void ReplaceWith(HBasicBlock* other);
1043
David Brazdil2d7352b2015-04-20 14:52:42 +01001044 // Merge `other` at the end of `this`. This method updates loops, reverse post
1045 // order, links to predecessors, successors, dominators and deletes the block
1046 // from the graph. The two blocks must be successive, i.e. `this` the only
1047 // predecessor of `other` and vice versa.
1048 void MergeWith(HBasicBlock* other);
1049
1050 // Disconnects `this` from all its predecessors, successors and dominator,
1051 // removes it from all loops it is included in and eventually from the graph.
1052 // The block must not dominate any other block. Predecessors and successors
1053 // are safely updated.
1054 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001055
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001057 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001058 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001059 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001060 // Replace instruction `initial` with `replacement` within this block.
1061 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1062 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001064 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001065 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1066 // instruction list. With 'ensure_safety' set to true, it verifies that the
1067 // instruction is not in use and removes it from the use lists of its inputs.
1068 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1069 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001070 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001071
1072 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001073 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001074 }
1075
Roland Levillain6b879dd2014-09-22 17:13:44 +01001076 bool IsLoopPreHeaderFirstPredecessor() const {
1077 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001078 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001079 }
1080
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001081 bool IsFirstPredecessorBackEdge() const {
1082 DCHECK(IsLoopHeader());
1083 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1084 }
1085
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001086 HLoopInformation* GetLoopInformation() const {
1087 return loop_information_;
1088 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001089
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001090 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001091 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001093 void SetInLoop(HLoopInformation* info) {
1094 if (IsLoopHeader()) {
1095 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001096 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001097 loop_information_ = info;
1098 } else if (loop_information_->Contains(*info->GetHeader())) {
1099 // Block is currently part of an outer loop. Make it part of this inner loop.
1100 // Note that a non loop header having a loop information means this loop information
1101 // has already been populated
1102 loop_information_ = info;
1103 } else {
1104 // Block is part of an inner loop. Do not update the loop information.
1105 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1106 // at this point, because this method is being called while populating `info`.
1107 }
1108 }
1109
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001110 // Raw update of the loop information.
1111 void SetLoopInformation(HLoopInformation* info) {
1112 loop_information_ = info;
1113 }
1114
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001115 bool IsInLoop() const { return loop_information_ != nullptr; }
1116
David Brazdilec16f792015-08-19 15:04:01 +01001117 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1118
1119 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1120 try_catch_information_ = try_catch_information;
1121 }
1122
1123 bool IsTryBlock() const {
1124 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1125 }
1126
1127 bool IsCatchBlock() const {
1128 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1129 }
David Brazdilffee3d32015-07-06 11:48:53 +01001130
1131 // Returns the try entry that this block's successors should have. They will
1132 // be in the same try, unless the block ends in a try boundary. In that case,
1133 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001134 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001135
David Brazdild7558da2015-09-22 13:04:14 +01001136 bool HasThrowingInstructions() const;
1137
David Brazdila4b8c212015-05-07 09:59:30 +01001138 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001139 bool Dominates(HBasicBlock* block) const;
1140
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001141 size_t GetLifetimeStart() const { return lifetime_start_; }
1142 size_t GetLifetimeEnd() const { return lifetime_end_; }
1143
1144 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1145 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1146
David Brazdil8d5b8b22015-03-24 10:51:52 +00001147 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001148 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001149 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001150 bool HasSinglePhi() const;
1151
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001152 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001153 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001154 ArenaVector<HBasicBlock*> predecessors_;
1155 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156 HInstructionList instructions_;
1157 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001158 HLoopInformation* loop_information_;
1159 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001160 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001161 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001162 // The dex program counter of the first instruction of this block.
1163 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001164 size_t lifetime_start_;
1165 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001166 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001167
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001168 friend class HGraph;
1169 friend class HInstruction;
1170
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001171 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1172};
1173
David Brazdilb2bd1c52015-03-25 11:17:37 +00001174// Iterates over the LoopInformation of all loops which contain 'block'
1175// from the innermost to the outermost.
1176class HLoopInformationOutwardIterator : public ValueObject {
1177 public:
1178 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1179 : current_(block.GetLoopInformation()) {}
1180
1181 bool Done() const { return current_ == nullptr; }
1182
1183 void Advance() {
1184 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001185 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001186 }
1187
1188 HLoopInformation* Current() const {
1189 DCHECK(!Done());
1190 return current_;
1191 }
1192
1193 private:
1194 HLoopInformation* current_;
1195
1196 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1197};
1198
Alexandre Ramesef20f712015-06-09 10:29:30 +01001199#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001200 M(Above, Condition) \
1201 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001203 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001204 M(ArrayGet, Instruction) \
1205 M(ArrayLength, Instruction) \
1206 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001207 M(Below, Condition) \
1208 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001209 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001211 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001212 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001213 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001214 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001215 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001217 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001218 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001219 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001220 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001221 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001222 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001223 M(Exit, Instruction) \
1224 M(FloatConstant, Constant) \
1225 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001226 M(GreaterThan, Condition) \
1227 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001228 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(InstanceFieldGet, Instruction) \
1230 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001231 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001232 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001233 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001234 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001235 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(LessThan, Condition) \
1238 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001239 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001240 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001241 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001242 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001243 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001244 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001245 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001246 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001247 M(Neg, UnaryOperation) \
1248 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001250 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001251 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001252 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001253 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001254 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001255 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001256 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001257 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001258 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001259 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001260 M(Return, Instruction) \
1261 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001262 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001263 M(Shl, BinaryOperation) \
1264 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001265 M(StaticFieldGet, Instruction) \
1266 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267 M(UnresolvedInstanceFieldGet, Instruction) \
1268 M(UnresolvedInstanceFieldSet, Instruction) \
1269 M(UnresolvedStaticFieldGet, Instruction) \
1270 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001271 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001272 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001273 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001274 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001275 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001276 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001277 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001278 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001279
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001280/*
1281 * Instructions, shared across several (not all) architectures.
1282 */
1283#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1284#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1285#else
1286#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001287 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001288 M(MultiplyAccumulate, Instruction)
1289#endif
1290
Vladimir Markob4536b72015-11-24 13:45:23 +00001291#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001292#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001293#else
1294#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1295 M(ArmDexCacheArraysBase, Instruction)
1296#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001297
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001298#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001299#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001300#else
1301#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001302 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001303 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001304#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001305
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001306#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1307
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001308#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1309
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001310#ifndef ART_ENABLE_CODEGEN_x86
1311#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1312#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001313#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1314 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001315 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001316 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001317 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001318#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001319
1320#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1321
1322#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1323 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001324 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001325 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1326 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001327 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001328 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001329 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1330 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1331
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001332#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1333 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001334 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001335 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001336 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001337 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001338
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001339#define FOR_EACH_INSTRUCTION(M) \
1340 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1341 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1342
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001344FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1345#undef FORWARD_DECLARATION
1346
Vladimir Marko372f10e2016-05-17 16:30:10 +01001347#define DECLARE_INSTRUCTION(type) \
1348 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1349 const char* DebugName() const OVERRIDE { return #type; } \
1350 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1351 return other->Is##type(); \
1352 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001353 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001354
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001355#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1356 bool Is##type() const { return As##type() != nullptr; } \
1357 const H##type* As##type() const { return this; } \
1358 H##type* As##type() { return this; }
1359
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001360template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001361class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001362 public:
David Brazdiled596192015-01-23 10:39:45 +00001363 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001364 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001365 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001366
Vladimir Marko46817b82016-03-29 12:21:58 +01001367 // Hook for the IntrusiveForwardList<>.
1368 // TODO: Hide this better.
1369 IntrusiveForwardListHook hook;
1370
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001371 private:
David Brazdiled596192015-01-23 10:39:45 +00001372 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001373 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001374
1375 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001376 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001377
Vladimir Marko46817b82016-03-29 12:21:58 +01001378 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001379
1380 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1381};
1382
David Brazdiled596192015-01-23 10:39:45 +00001383template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001384using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001385
David Brazdil1abb4192015-02-17 18:33:36 +00001386// This class is used by HEnvironment and HInstruction classes to record the
1387// instructions they use and pointers to the corresponding HUseListNodes kept
1388// by the used instructions.
1389template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001390class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001391 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001392 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1393 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001394
Vladimir Marko46817b82016-03-29 12:21:58 +01001395 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1396 : HUserRecord(old_record.instruction_, before_use_node) {}
1397 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1398 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001399 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001400 }
1401
1402 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001403 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1404 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001405
1406 private:
1407 // Instruction used by the user.
1408 HInstruction* instruction_;
1409
Vladimir Marko46817b82016-03-29 12:21:58 +01001410 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1411 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001412};
1413
Aart Bik854a02b2015-07-14 16:07:00 -07001414/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001415 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001416 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001417 * For write/read dependences on fields/arrays, the dependence analysis uses
1418 * type disambiguation (e.g. a float field write cannot modify the value of an
1419 * integer field read) and the access type (e.g. a reference array write cannot
1420 * modify the value of a reference field read [although it may modify the
1421 * reference fetch prior to reading the field, which is represented by its own
1422 * write/read dependence]). The analysis makes conservative points-to
1423 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1424 * the same, and any reference read depends on any reference read without
1425 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001426 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001427 * The internal representation uses 38-bit and is described in the table below.
1428 * The first line indicates the side effect, and for field/array accesses the
1429 * second line indicates the type of the access (in the order of the
1430 * Primitive::Type enum).
1431 * The two numbered lines below indicate the bit position in the bitfield (read
1432 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001433 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1435 * +-------------+---------+---------+--------------+---------+---------+
1436 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1437 * | 3 |333333322|222222221| 1 |111111110|000000000|
1438 * | 7 |654321098|765432109| 8 |765432109|876543210|
1439 *
1440 * Note that, to ease the implementation, 'changes' bits are least significant
1441 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001442 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001443class SideEffects : public ValueObject {
1444 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001445 SideEffects() : flags_(0) {}
1446
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001447 static SideEffects None() {
1448 return SideEffects(0);
1449 }
1450
1451 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001452 return SideEffects(kAllChangeBits | kAllDependOnBits);
1453 }
1454
1455 static SideEffects AllChanges() {
1456 return SideEffects(kAllChangeBits);
1457 }
1458
1459 static SideEffects AllDependencies() {
1460 return SideEffects(kAllDependOnBits);
1461 }
1462
1463 static SideEffects AllExceptGCDependency() {
1464 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1465 }
1466
1467 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001468 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001469 }
1470
Aart Bik34c3ba92015-07-20 14:08:59 -07001471 static SideEffects AllWrites() {
1472 return SideEffects(kAllWrites);
1473 }
1474
1475 static SideEffects AllReads() {
1476 return SideEffects(kAllReads);
1477 }
1478
1479 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1480 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001481 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001482 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001483 }
1484
Aart Bik854a02b2015-07-14 16:07:00 -07001485 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001486 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001487 }
1488
Aart Bik34c3ba92015-07-20 14:08:59 -07001489 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1490 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001491 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001492 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001493 }
1494
1495 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001496 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001497 }
1498
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 static SideEffects CanTriggerGC() {
1500 return SideEffects(1ULL << kCanTriggerGCBit);
1501 }
1502
1503 static SideEffects DependsOnGC() {
1504 return SideEffects(1ULL << kDependsOnGCBit);
1505 }
1506
Aart Bik854a02b2015-07-14 16:07:00 -07001507 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001508 SideEffects Union(SideEffects other) const {
1509 return SideEffects(flags_ | other.flags_);
1510 }
1511
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001512 SideEffects Exclusion(SideEffects other) const {
1513 return SideEffects(flags_ & ~other.flags_);
1514 }
1515
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001516 void Add(SideEffects other) {
1517 flags_ |= other.flags_;
1518 }
1519
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001520 bool Includes(SideEffects other) const {
1521 return (other.flags_ & flags_) == other.flags_;
1522 }
1523
1524 bool HasSideEffects() const {
1525 return (flags_ & kAllChangeBits);
1526 }
1527
1528 bool HasDependencies() const {
1529 return (flags_ & kAllDependOnBits);
1530 }
1531
1532 // Returns true if there are no side effects or dependencies.
1533 bool DoesNothing() const {
1534 return flags_ == 0;
1535 }
1536
Aart Bik854a02b2015-07-14 16:07:00 -07001537 // Returns true if something is written.
1538 bool DoesAnyWrite() const {
1539 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001540 }
1541
Aart Bik854a02b2015-07-14 16:07:00 -07001542 // Returns true if something is read.
1543 bool DoesAnyRead() const {
1544 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001545 }
1546
Aart Bik854a02b2015-07-14 16:07:00 -07001547 // Returns true if potentially everything is written and read
1548 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001549 bool DoesAllReadWrite() const {
1550 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1551 }
1552
Aart Bik854a02b2015-07-14 16:07:00 -07001553 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001554 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001555 }
1556
Roland Levillain0d5a2812015-11-13 10:07:31 +00001557 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001558 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001559 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1560 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001561 }
1562
1563 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001565 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001566 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001567 for (int s = kLastBit; s >= 0; s--) {
1568 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1569 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1570 // This is a bit for the GC side effect.
1571 if (current_bit_is_set) {
1572 flags += "GC";
1573 }
Aart Bik854a02b2015-07-14 16:07:00 -07001574 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001575 } else {
1576 // This is a bit for the array/field analysis.
1577 // The underscore character stands for the 'can trigger GC' bit.
1578 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1579 if (current_bit_is_set) {
1580 flags += kDebug[s];
1581 }
1582 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1583 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1584 flags += "|";
1585 }
1586 }
Aart Bik854a02b2015-07-14 16:07:00 -07001587 }
1588 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001589 }
1590
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001592
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001593 private:
1594 static constexpr int kFieldArrayAnalysisBits = 9;
1595
1596 static constexpr int kFieldWriteOffset = 0;
1597 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1598 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1599 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1600
1601 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1602
1603 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1604 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1605 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1606 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1607
1608 static constexpr int kLastBit = kDependsOnGCBit;
1609 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1610
1611 // Aliases.
1612
1613 static_assert(kChangeBits == kDependOnBits,
1614 "the 'change' bits should match the 'depend on' bits.");
1615
1616 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1617 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1618 static constexpr uint64_t kAllWrites =
1619 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1620 static constexpr uint64_t kAllReads =
1621 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001622
Aart Bik854a02b2015-07-14 16:07:00 -07001623 // Translates type to bit flag.
1624 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1625 CHECK_NE(type, Primitive::kPrimVoid);
1626 const uint64_t one = 1;
1627 const int shift = type; // 0-based consecutive enum
1628 DCHECK_LE(kFieldWriteOffset, shift);
1629 DCHECK_LT(shift, kArrayWriteOffset);
1630 return one << (type + offset);
1631 }
1632
1633 // Private constructor on direct flags value.
1634 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1635
1636 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001637};
1638
David Brazdiled596192015-01-23 10:39:45 +00001639// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001640class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001641 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001642 HEnvironment(ArenaAllocator* arena,
1643 size_t number_of_vregs,
1644 const DexFile& dex_file,
1645 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001646 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001647 InvokeType invoke_type,
1648 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001649 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1650 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001651 parent_(nullptr),
1652 dex_file_(dex_file),
1653 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001654 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001655 invoke_type_(invoke_type),
1656 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001657 }
1658
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001659 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001660 : HEnvironment(arena,
1661 to_copy.Size(),
1662 to_copy.GetDexFile(),
1663 to_copy.GetMethodIdx(),
1664 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001665 to_copy.GetInvokeType(),
1666 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001667
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001668 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001669 if (parent_ != nullptr) {
1670 parent_->SetAndCopyParentChain(allocator, parent);
1671 } else {
1672 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1673 parent_->CopyFrom(parent);
1674 if (parent->GetParent() != nullptr) {
1675 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1676 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001677 }
David Brazdiled596192015-01-23 10:39:45 +00001678 }
1679
Vladimir Marko71bf8092015-09-15 15:33:14 +01001680 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001681 void CopyFrom(HEnvironment* environment);
1682
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001683 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1684 // input to the loop phi instead. This is for inserting instructions that
1685 // require an environment (like HDeoptimization) in the loop pre-header.
1686 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001687
1688 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001689 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001690 }
1691
1692 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001693 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001694 }
1695
David Brazdil1abb4192015-02-17 18:33:36 +00001696 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001697
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001698 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001699
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001700 HEnvironment* GetParent() const { return parent_; }
1701
1702 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001703 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001704 }
1705
1706 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001707 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001708 }
1709
1710 uint32_t GetDexPc() const {
1711 return dex_pc_;
1712 }
1713
1714 uint32_t GetMethodIdx() const {
1715 return method_idx_;
1716 }
1717
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001718 InvokeType GetInvokeType() const {
1719 return invoke_type_;
1720 }
1721
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001722 const DexFile& GetDexFile() const {
1723 return dex_file_;
1724 }
1725
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001726 HInstruction* GetHolder() const {
1727 return holder_;
1728 }
1729
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001730
1731 bool IsFromInlinedInvoke() const {
1732 return GetParent() != nullptr;
1733 }
1734
David Brazdiled596192015-01-23 10:39:45 +00001735 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001736 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1737 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001738 HEnvironment* parent_;
1739 const DexFile& dex_file_;
1740 const uint32_t method_idx_;
1741 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001742 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001743
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001744 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001745 HInstruction* const holder_;
1746
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001747 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001748
1749 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1750};
1751
Vladimir Markof9f64412015-09-02 14:05:49 +01001752class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001753 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001754 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001755 : previous_(nullptr),
1756 next_(nullptr),
1757 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001758 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001759 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001760 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001761 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001762 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001763 locations_(nullptr),
1764 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001765 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001766 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001767 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1768 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1769 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001770
Dave Allison20dfc792014-06-16 20:44:29 -07001771 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001772
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001773#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001774 enum InstructionKind {
1775 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1776 };
1777#undef DECLARE_KIND
1778
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001779 HInstruction* GetNext() const { return next_; }
1780 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001781
Calin Juravle77520bc2015-01-12 18:45:46 +00001782 HInstruction* GetNextDisregardingMoves() const;
1783 HInstruction* GetPreviousDisregardingMoves() const;
1784
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001785 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001786 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001787 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001788 bool IsInBlock() const { return block_ != nullptr; }
1789 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001790 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1791 bool IsIrreducibleLoopHeaderPhi() const {
1792 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1793 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001794
Vladimir Marko372f10e2016-05-17 16:30:10 +01001795 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1796
1797 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1798 // One virtual method is enough, just const_cast<> and then re-add the const.
1799 return ArrayRef<const HUserRecord<HInstruction*>>(
1800 const_cast<HInstruction*>(this)->GetInputRecords());
1801 }
1802
1803 auto GetInputs() {
1804 return MakeTransformArrayRef(
1805 GetInputRecords(),
1806 [](HUserRecord<HInstruction*>& record) -> HInstruction* {
1807 return record.GetInstruction();
1808 });
1809 }
1810
1811 auto GetInputs() const {
1812 return MakeTransformArrayRef(
1813 GetInputRecords(),
1814 [](const HUserRecord<HInstruction*>& record) -> const HInstruction* {
1815 return record.GetInstruction();
1816 });
1817 }
1818
1819 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001820 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001821
Vladimir Marko372f10e2016-05-17 16:30:10 +01001822 void SetRawInputAt(size_t index, HInstruction* input) {
1823 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1824 }
1825
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826 virtual void Accept(HGraphVisitor* visitor) = 0;
1827 virtual const char* DebugName() const = 0;
1828
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1830
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001831 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001832
1833 uint32_t GetDexPc() const { return dex_pc_; }
1834
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001835 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001836
Roland Levillaine161a2a2014-10-03 12:45:18 +01001837 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001838 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001839
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001840 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001841 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842
Calin Juravle10e244f2015-01-26 18:54:32 +00001843 // Does not apply for all instructions, but having this at top level greatly
1844 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001845 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001846 virtual bool CanBeNull() const {
1847 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1848 return true;
1849 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001850
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001851 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001852 return false;
1853 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001854
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001855 virtual bool IsActualObject() const {
1856 return GetType() == Primitive::kPrimNot;
1857 }
1858
Calin Juravle2e768302015-07-28 14:41:11 +00001859 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001860
Calin Juravle61d544b2015-02-23 16:46:57 +00001861 ReferenceTypeInfo GetReferenceTypeInfo() const {
1862 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001863 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001864 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001865 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001866
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001867 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001868 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001869 // Note: fixup_end remains valid across push_front().
1870 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1871 HUseListNode<HInstruction*>* new_node =
1872 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1873 uses_.push_front(*new_node);
1874 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001875 }
1876
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001877 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001878 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001879 // Note: env_fixup_end remains valid across push_front().
1880 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1881 HUseListNode<HEnvironment*>* new_node =
1882 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1883 env_uses_.push_front(*new_node);
1884 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001885 }
1886
David Brazdil1abb4192015-02-17 18:33:36 +00001887 void RemoveAsUserOfInput(size_t input) {
1888 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001889 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1890 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1891 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001892 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001893
Vladimir Marko372f10e2016-05-17 16:30:10 +01001894 void RemoveAsUserOfAllInputs() {
1895 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1896 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1897 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1898 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1899 }
1900 }
1901
David Brazdil1abb4192015-02-17 18:33:36 +00001902 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1903 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001904
Vladimir Marko46817b82016-03-29 12:21:58 +01001905 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1906 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1907 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001908 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001909 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001910 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001911
Roland Levillain6c82d402014-10-13 16:10:27 +01001912 // Does this instruction strictly dominate `other_instruction`?
1913 // Returns false if this instruction and `other_instruction` are the same.
1914 // Aborts if this instruction and `other_instruction` are both phis.
1915 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001916
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001917 int GetId() const { return id_; }
1918 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001919
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001920 int GetSsaIndex() const { return ssa_index_; }
1921 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1922 bool HasSsaIndex() const { return ssa_index_ != -1; }
1923
1924 bool HasEnvironment() const { return environment_ != nullptr; }
1925 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001926 // Set the `environment_` field. Raw because this method does not
1927 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001928 void SetRawEnvironment(HEnvironment* environment) {
1929 DCHECK(environment_ == nullptr);
1930 DCHECK_EQ(environment->GetHolder(), this);
1931 environment_ = environment;
1932 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001933
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001934 void InsertRawEnvironment(HEnvironment* environment) {
1935 DCHECK(environment_ != nullptr);
1936 DCHECK_EQ(environment->GetHolder(), this);
1937 DCHECK(environment->GetParent() == nullptr);
1938 environment->parent_ = environment_;
1939 environment_ = environment;
1940 }
1941
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001942 void RemoveEnvironment();
1943
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001944 // Set the environment of this instruction, copying it from `environment`. While
1945 // copying, the uses lists are being updated.
1946 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001948 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001950 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001951 if (environment->GetParent() != nullptr) {
1952 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1953 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001954 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001956 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1957 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001959 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001960 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001961 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001962 if (environment->GetParent() != nullptr) {
1963 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1964 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001965 }
1966
Nicolas Geoffray39468442014-09-02 15:17:15 +01001967 // Returns the number of entries in the environment. Typically, that is the
1968 // number of dex registers in a method. It could be more in case of inlining.
1969 size_t EnvironmentSize() const;
1970
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001971 LocationSummary* GetLocations() const { return locations_; }
1972 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001975 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001976
Alexandre Rames188d4312015-04-09 18:30:21 +01001977 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1978 // uses of this instruction by `other` are *not* updated.
1979 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1980 ReplaceWith(other);
1981 other->ReplaceInput(this, use_index);
1982 }
1983
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001984 // Move `this` instruction before `cursor`.
1985 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001986
Vladimir Markofb337ea2015-11-25 15:25:10 +00001987 // Move `this` before its first user and out of any loops. If there is no
1988 // out-of-loop user that dominates all other users, move the instruction
1989 // to the end of the out-of-loop common dominator of the user's blocks.
1990 //
1991 // This can be used only on non-throwing instructions with no side effects that
1992 // have at least one use but no environment uses.
1993 void MoveBeforeFirstUserAndOutOfLoops();
1994
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001995#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 bool Is##type() const; \
1997 const H##type* As##type() const; \
1998 H##type* As##type();
1999
2000 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2001#undef INSTRUCTION_TYPE_CHECK
2002
2003#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002004 bool Is##type() const { return (As##type() != nullptr); } \
2005 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002006 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008#undef INSTRUCTION_TYPE_CHECK
2009
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002010 // Returns whether the instruction can be moved within the graph.
2011 virtual bool CanBeMoved() const { return false; }
2012
2013 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002014 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002015 return false;
2016 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002017
2018 // Returns whether any data encoded in the two instructions is equal.
2019 // This method does not look at the inputs. Both instructions must be
2020 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002021 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002022 return false;
2023 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002024
2025 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002026 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002027 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002028 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002029
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002030 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2031 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2032 // the virtual function because the __attribute__((__pure__)) doesn't really
2033 // apply the strong requirement for virtual functions, preventing optimizations.
2034 InstructionKind GetKind() const PURE;
2035 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002036
2037 virtual size_t ComputeHashCode() const {
2038 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002039 for (const HInstruction* input : GetInputs()) {
2040 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002041 }
2042 return result;
2043 }
2044
2045 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002046 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002047 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002048
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002049 size_t GetLifetimePosition() const { return lifetime_position_; }
2050 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2051 LiveInterval* GetLiveInterval() const { return live_interval_; }
2052 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2053 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2054
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002055 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2056
2057 // Returns whether the code generation of the instruction will require to have access
2058 // to the current method. Such instructions are:
2059 // (1): Instructions that require an environment, as calling the runtime requires
2060 // to walk the stack and have the current method stored at a specific stack address.
2061 // (2): Object literals like classes and strings, that are loaded from the dex cache
2062 // fields of the current method.
2063 bool NeedsCurrentMethod() const {
2064 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2065 }
2066
Vladimir Markodc151b22015-10-15 18:02:30 +01002067 // Returns whether the code generation of the instruction will require to have access
2068 // to the dex cache of the current method's declaring class via the current method.
2069 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002070
Mark Mendellc4701932015-04-10 13:18:51 -04002071 // Does this instruction have any use in an environment before
2072 // control flow hits 'other'?
2073 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2074
2075 // Remove all references to environment uses of this instruction.
2076 // The caller must ensure that this is safe to do.
2077 void RemoveEnvironmentUsers();
2078
Vladimir Markoa1de9182016-02-25 11:37:38 +00002079 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2080 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002081
David Brazdil1abb4192015-02-17 18:33:36 +00002082 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002083 // If set, the machine code for this instruction is assumed to be generated by
2084 // its users. Used by liveness analysis to compute use positions accordingly.
2085 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2086 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2087 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2088 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2089
Vladimir Marko372f10e2016-05-17 16:30:10 +01002090 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2091 return GetInputRecords()[i];
2092 }
2093
2094 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2095 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2096 input_records[index] = input;
2097 }
David Brazdil1abb4192015-02-17 18:33:36 +00002098
Vladimir Markoa1de9182016-02-25 11:37:38 +00002099 uint32_t GetPackedFields() const {
2100 return packed_fields_;
2101 }
2102
2103 template <size_t flag>
2104 bool GetPackedFlag() const {
2105 return (packed_fields_ & (1u << flag)) != 0u;
2106 }
2107
2108 template <size_t flag>
2109 void SetPackedFlag(bool value = true) {
2110 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2111 }
2112
2113 template <typename BitFieldType>
2114 typename BitFieldType::value_type GetPackedField() const {
2115 return BitFieldType::Decode(packed_fields_);
2116 }
2117
2118 template <typename BitFieldType>
2119 void SetPackedField(typename BitFieldType::value_type value) {
2120 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2121 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2122 }
2123
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002124 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002125 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2126 auto before_use_node = uses_.before_begin();
2127 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2128 HInstruction* user = use_node->GetUser();
2129 size_t input_index = use_node->GetIndex();
2130 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2131 before_use_node = use_node;
2132 }
2133 }
2134
2135 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2136 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2137 if (next != uses_.end()) {
2138 HInstruction* next_user = next->GetUser();
2139 size_t next_index = next->GetIndex();
2140 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2141 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2142 }
2143 }
2144
2145 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2146 auto before_env_use_node = env_uses_.before_begin();
2147 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2148 HEnvironment* user = env_use_node->GetUser();
2149 size_t input_index = env_use_node->GetIndex();
2150 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2151 before_env_use_node = env_use_node;
2152 }
2153 }
2154
2155 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2156 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2157 if (next != env_uses_.end()) {
2158 HEnvironment* next_user = next->GetUser();
2159 size_t next_index = next->GetIndex();
2160 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2161 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2162 }
2163 }
David Brazdil1abb4192015-02-17 18:33:36 +00002164
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002165 HInstruction* previous_;
2166 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002167 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002168 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002169
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002170 // An instruction gets an id when it is added to the graph.
2171 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002172 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002173 int id_;
2174
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002175 // When doing liveness analysis, instructions that have uses get an SSA index.
2176 int ssa_index_;
2177
Vladimir Markoa1de9182016-02-25 11:37:38 +00002178 // Packed fields.
2179 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002180
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002181 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002182 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002183
2184 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002185 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002186
Nicolas Geoffray39468442014-09-02 15:17:15 +01002187 // The environment associated with this instruction. Not null if the instruction
2188 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002189 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002190
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002191 // Set by the code generator.
2192 LocationSummary* locations_;
2193
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002194 // Set by the liveness analysis.
2195 LiveInterval* live_interval_;
2196
2197 // Set by the liveness analysis, this is the position in a linear
2198 // order of blocks where this instruction's live interval start.
2199 size_t lifetime_position_;
2200
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002201 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002202
Vladimir Markoa1de9182016-02-25 11:37:38 +00002203 // The reference handle part of the reference type info.
2204 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002205 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002206 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002207
David Brazdil1abb4192015-02-17 18:33:36 +00002208 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002210 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002211 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002212 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213
2214 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2215};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002216std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217
2218class HInstructionIterator : public ValueObject {
2219 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002220 explicit HInstructionIterator(const HInstructionList& instructions)
2221 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002222 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223 }
2224
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002225 bool Done() const { return instruction_ == nullptr; }
2226 HInstruction* Current() const { return instruction_; }
2227 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002229 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230 }
2231
2232 private:
2233 HInstruction* instruction_;
2234 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002235
2236 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002237};
2238
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002239class HBackwardInstructionIterator : public ValueObject {
2240 public:
2241 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2242 : instruction_(instructions.last_instruction_) {
2243 next_ = Done() ? nullptr : instruction_->GetPrevious();
2244 }
2245
2246 bool Done() const { return instruction_ == nullptr; }
2247 HInstruction* Current() const { return instruction_; }
2248 void Advance() {
2249 instruction_ = next_;
2250 next_ = Done() ? nullptr : instruction_->GetPrevious();
2251 }
2252
2253 private:
2254 HInstruction* instruction_;
2255 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002256
2257 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002258};
2259
Vladimir Markof9f64412015-09-02 14:05:49 +01002260template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002261class HTemplateInstruction: public HInstruction {
2262 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002263 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002264 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002265 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002266
Vladimir Marko372f10e2016-05-17 16:30:10 +01002267 using HInstruction::GetInputRecords; // Keep the const version visible.
2268 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2269 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002270 }
2271
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002272 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002273 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002274
2275 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276};
2277
Vladimir Markof9f64412015-09-02 14:05:49 +01002278// HTemplateInstruction specialization for N=0.
2279template<>
2280class HTemplateInstruction<0>: public HInstruction {
2281 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002282 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 : HInstruction(side_effects, dex_pc) {}
2284
Vladimir Markof9f64412015-09-02 14:05:49 +01002285 virtual ~HTemplateInstruction() {}
2286
Vladimir Marko372f10e2016-05-17 16:30:10 +01002287 using HInstruction::GetInputRecords; // Keep the const version visible.
2288 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2289 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002290 }
2291
2292 private:
2293 friend class SsaBuilder;
2294};
2295
Dave Allison20dfc792014-06-16 20:44:29 -07002296template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002297class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002298 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002299 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002300 : HTemplateInstruction<N>(side_effects, dex_pc) {
2301 this->template SetPackedField<TypeField>(type);
2302 }
Dave Allison20dfc792014-06-16 20:44:29 -07002303 virtual ~HExpression() {}
2304
Vladimir Markoa1de9182016-02-25 11:37:38 +00002305 Primitive::Type GetType() const OVERRIDE {
2306 return TypeField::Decode(this->GetPackedFields());
2307 }
Dave Allison20dfc792014-06-16 20:44:29 -07002308
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002309 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002310 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2311 static constexpr size_t kFieldTypeSize =
2312 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2313 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2314 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2315 "Too many packed fields.");
2316 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002317};
2318
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002319// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2320// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002321class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002322 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002323 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2324 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002325
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002326 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002328 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002329
2330 private:
2331 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2332};
2333
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002334// Represents dex's RETURN opcodes. A HReturn is a control flow
2335// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002336class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002337 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002338 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2339 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002340 SetRawInputAt(0, value);
2341 }
2342
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002343 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002344
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002345 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002346
2347 private:
2348 DISALLOW_COPY_AND_ASSIGN(HReturn);
2349};
2350
Vladimir Markofcb503c2016-05-18 12:48:17 +01002351class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002352 public:
2353 HPhi(ArenaAllocator* arena,
2354 uint32_t reg_number,
2355 size_t number_of_inputs,
2356 Primitive::Type type,
2357 uint32_t dex_pc = kNoDexPc)
2358 : HInstruction(SideEffects::None(), dex_pc),
2359 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2360 reg_number_(reg_number) {
2361 SetPackedField<TypeField>(ToPhiType(type));
2362 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2363 // Phis are constructed live and marked dead if conflicting or unused.
2364 // Individual steps of SsaBuilder should assume that if a phi has been
2365 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2366 SetPackedFlag<kFlagIsLive>(true);
2367 SetPackedFlag<kFlagCanBeNull>(true);
2368 }
2369
2370 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2371 static Primitive::Type ToPhiType(Primitive::Type type) {
2372 return Primitive::PrimitiveKind(type);
2373 }
2374
2375 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2376
Vladimir Marko372f10e2016-05-17 16:30:10 +01002377 using HInstruction::GetInputRecords; // Keep the const version visible.
2378 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2379 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2380 }
David Brazdildee58d62016-04-07 09:54:26 +00002381
2382 void AddInput(HInstruction* input);
2383 void RemoveInputAt(size_t index);
2384
2385 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2386 void SetType(Primitive::Type new_type) {
2387 // Make sure that only valid type changes occur. The following are allowed:
2388 // (1) int -> float/ref (primitive type propagation),
2389 // (2) long -> double (primitive type propagation).
2390 DCHECK(GetType() == new_type ||
2391 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2392 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2393 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2394 SetPackedField<TypeField>(new_type);
2395 }
2396
2397 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2398 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2399
2400 uint32_t GetRegNumber() const { return reg_number_; }
2401
2402 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2403 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2404 bool IsDead() const { return !IsLive(); }
2405 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2406
2407 bool IsVRegEquivalentOf(HInstruction* other) const {
2408 return other != nullptr
2409 && other->IsPhi()
2410 && other->AsPhi()->GetBlock() == GetBlock()
2411 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2412 }
2413
2414 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2415 // An equivalent phi is a phi having the same dex register and type.
2416 // It assumes that phis with the same dex register are adjacent.
2417 HPhi* GetNextEquivalentPhiWithSameType() {
2418 HInstruction* next = GetNext();
2419 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2420 if (next->GetType() == GetType()) {
2421 return next->AsPhi();
2422 }
2423 next = next->GetNext();
2424 }
2425 return nullptr;
2426 }
2427
2428 DECLARE_INSTRUCTION(Phi);
2429
David Brazdildee58d62016-04-07 09:54:26 +00002430 private:
2431 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2432 static constexpr size_t kFieldTypeSize =
2433 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2434 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2435 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2436 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2437 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2438 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2439
Vladimir Marko372f10e2016-05-17 16:30:10 +01002440 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002441 const uint32_t reg_number_;
2442
2443 DISALLOW_COPY_AND_ASSIGN(HPhi);
2444};
2445
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002446// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002447// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002448// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002449class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002450 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002451 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002452
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002453 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002455 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002456
2457 private:
2458 DISALLOW_COPY_AND_ASSIGN(HExit);
2459};
2460
2461// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002462class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002463 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002464 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002465
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002466 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002467
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002468 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002469 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002470 }
2471
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002472 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HGoto);
2476};
2477
Roland Levillain9867bc72015-08-05 10:21:34 +01002478class HConstant : public HExpression<0> {
2479 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2481 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002482
2483 bool CanBeMoved() const OVERRIDE { return true; }
2484
Roland Levillain1a653882016-03-18 18:05:57 +00002485 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002486 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002487 // Is this constant 0 in the arithmetic sense?
2488 virtual bool IsArithmeticZero() const { return false; }
2489 // Is this constant a 0-bit pattern?
2490 virtual bool IsZeroBitPattern() const { return false; }
2491 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002492 virtual bool IsOne() const { return false; }
2493
David Brazdil77a48ae2015-09-15 12:34:04 +00002494 virtual uint64_t GetValueAsUint64() const = 0;
2495
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002496 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002497
2498 private:
2499 DISALLOW_COPY_AND_ASSIGN(HConstant);
2500};
2501
Vladimir Markofcb503c2016-05-18 12:48:17 +01002502class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002503 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002504 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002505 return true;
2506 }
2507
David Brazdil77a48ae2015-09-15 12:34:04 +00002508 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2509
Roland Levillain9867bc72015-08-05 10:21:34 +01002510 size_t ComputeHashCode() const OVERRIDE { return 0; }
2511
Roland Levillain1a653882016-03-18 18:05:57 +00002512 // The null constant representation is a 0-bit pattern.
2513 virtual bool IsZeroBitPattern() const { return true; }
2514
Roland Levillain9867bc72015-08-05 10:21:34 +01002515 DECLARE_INSTRUCTION(NullConstant);
2516
2517 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002518 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002519
2520 friend class HGraph;
2521 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2522};
2523
2524// Constants of the type int. Those can be from Dex instructions, or
2525// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002526class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002527 public:
2528 int32_t GetValue() const { return value_; }
2529
David Brazdil9f389d42015-10-01 14:32:56 +01002530 uint64_t GetValueAsUint64() const OVERRIDE {
2531 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2532 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002533
Vladimir Marko372f10e2016-05-17 16:30:10 +01002534 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002535 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002536 return other->AsIntConstant()->value_ == value_;
2537 }
2538
2539 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2540
2541 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002542 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2543 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002544 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2545
Roland Levillain1a653882016-03-18 18:05:57 +00002546 // Integer constants are used to encode Boolean values as well,
2547 // where 1 means true and 0 means false.
2548 bool IsTrue() const { return GetValue() == 1; }
2549 bool IsFalse() const { return GetValue() == 0; }
2550
Roland Levillain9867bc72015-08-05 10:21:34 +01002551 DECLARE_INSTRUCTION(IntConstant);
2552
2553 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002554 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2555 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2556 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2557 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002558
2559 const int32_t value_;
2560
2561 friend class HGraph;
2562 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2563 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2564 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2565};
2566
Vladimir Markofcb503c2016-05-18 12:48:17 +01002567class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002568 public:
2569 int64_t GetValue() const { return value_; }
2570
David Brazdil77a48ae2015-09-15 12:34:04 +00002571 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2572
Vladimir Marko372f10e2016-05-17 16:30:10 +01002573 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002574 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002575 return other->AsLongConstant()->value_ == value_;
2576 }
2577
2578 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2579
2580 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002581 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2582 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002583 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2584
2585 DECLARE_INSTRUCTION(LongConstant);
2586
2587 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002588 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2589 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002590
2591 const int64_t value_;
2592
2593 friend class HGraph;
2594 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2595};
Dave Allison20dfc792014-06-16 20:44:29 -07002596
Vladimir Markofcb503c2016-05-18 12:48:17 +01002597class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002598 public:
2599 float GetValue() const { return value_; }
2600
2601 uint64_t GetValueAsUint64() const OVERRIDE {
2602 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2603 }
2604
Vladimir Marko372f10e2016-05-17 16:30:10 +01002605 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002606 DCHECK(other->IsFloatConstant()) << other->DebugName();
2607 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2608 }
2609
2610 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2611
2612 bool IsMinusOne() const OVERRIDE {
2613 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2614 }
Roland Levillain1a653882016-03-18 18:05:57 +00002615 bool IsArithmeticZero() const OVERRIDE {
2616 return std::fpclassify(value_) == FP_ZERO;
2617 }
2618 bool IsArithmeticPositiveZero() const {
2619 return IsArithmeticZero() && !std::signbit(value_);
2620 }
2621 bool IsArithmeticNegativeZero() const {
2622 return IsArithmeticZero() && std::signbit(value_);
2623 }
2624 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002625 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002626 }
2627 bool IsOne() const OVERRIDE {
2628 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2629 }
2630 bool IsNaN() const {
2631 return std::isnan(value_);
2632 }
2633
2634 DECLARE_INSTRUCTION(FloatConstant);
2635
2636 private:
2637 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2638 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2639 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2640 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2641
2642 const float value_;
2643
2644 // Only the SsaBuilder and HGraph can create floating-point constants.
2645 friend class SsaBuilder;
2646 friend class HGraph;
2647 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2648};
2649
Vladimir Markofcb503c2016-05-18 12:48:17 +01002650class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002651 public:
2652 double GetValue() const { return value_; }
2653
2654 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2655
Vladimir Marko372f10e2016-05-17 16:30:10 +01002656 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002657 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2658 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2659 }
2660
2661 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2662
2663 bool IsMinusOne() const OVERRIDE {
2664 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2665 }
Roland Levillain1a653882016-03-18 18:05:57 +00002666 bool IsArithmeticZero() const OVERRIDE {
2667 return std::fpclassify(value_) == FP_ZERO;
2668 }
2669 bool IsArithmeticPositiveZero() const {
2670 return IsArithmeticZero() && !std::signbit(value_);
2671 }
2672 bool IsArithmeticNegativeZero() const {
2673 return IsArithmeticZero() && std::signbit(value_);
2674 }
2675 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002676 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002677 }
2678 bool IsOne() const OVERRIDE {
2679 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2680 }
2681 bool IsNaN() const {
2682 return std::isnan(value_);
2683 }
2684
2685 DECLARE_INSTRUCTION(DoubleConstant);
2686
2687 private:
2688 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2689 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2690 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2691 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2692
2693 const double value_;
2694
2695 // Only the SsaBuilder and HGraph can create floating-point constants.
2696 friend class SsaBuilder;
2697 friend class HGraph;
2698 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2699};
2700
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002701// Conditional branch. A block ending with an HIf instruction must have
2702// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002703class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002704 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002705 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2706 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002707 SetRawInputAt(0, input);
2708 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002709
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002710 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002711
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002712 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002713 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002714 }
2715
2716 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002717 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002718 }
2719
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002720 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002721
2722 private:
2723 DISALLOW_COPY_AND_ASSIGN(HIf);
2724};
2725
David Brazdilfc6a86a2015-06-26 10:33:45 +00002726
2727// Abstract instruction which marks the beginning and/or end of a try block and
2728// links it to the respective exception handlers. Behaves the same as a Goto in
2729// non-exceptional control flow.
2730// Normal-flow successor is stored at index zero, exception handlers under
2731// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002732class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002733 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002734 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002735 kEntry,
2736 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002737 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002738 };
2739
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002741 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2742 SetPackedField<BoundaryKindField>(kind);
2743 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002744
2745 bool IsControlFlow() const OVERRIDE { return true; }
2746
2747 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002748 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002749
David Brazdild26a4112015-11-10 11:07:31 +00002750 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2751 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2752 }
2753
David Brazdilfc6a86a2015-06-26 10:33:45 +00002754 // Returns whether `handler` is among its exception handlers (non-zero index
2755 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002756 bool HasExceptionHandler(const HBasicBlock& handler) const {
2757 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002758 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002759 }
2760
2761 // If not present already, adds `handler` to its block's list of exception
2762 // handlers.
2763 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002764 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002765 GetBlock()->AddSuccessor(handler);
2766 }
2767 }
2768
Vladimir Markoa1de9182016-02-25 11:37:38 +00002769 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2770 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002771
David Brazdilffee3d32015-07-06 11:48:53 +01002772 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2773
David Brazdilfc6a86a2015-06-26 10:33:45 +00002774 DECLARE_INSTRUCTION(TryBoundary);
2775
2776 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002777 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2778 static constexpr size_t kFieldBoundaryKindSize =
2779 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2780 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2781 kFieldBoundaryKind + kFieldBoundaryKindSize;
2782 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2783 "Too many packed fields.");
2784 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002785
2786 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2787};
2788
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002789// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002790class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002791 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002792 // We set CanTriggerGC to prevent any intermediate address to be live
2793 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002794 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002795 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002796 SetRawInputAt(0, cond);
2797 }
2798
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002799 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002800 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002801 return true;
2802 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002803 bool NeedsEnvironment() const OVERRIDE { return true; }
2804 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002805
2806 DECLARE_INSTRUCTION(Deoptimize);
2807
2808 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002809 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2810};
2811
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002812// Represents the ArtMethod that was passed as a first argument to
2813// the method. It is used by instructions that depend on it, like
2814// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002815class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2818 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002819
2820 DECLARE_INSTRUCTION(CurrentMethod);
2821
2822 private:
2823 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2824};
2825
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002826// Fetches an ArtMethod from the virtual table or the interface method table
2827// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002828class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002829 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002830 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002831 kVTable,
2832 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002833 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002834 };
2835 HClassTableGet(HInstruction* cls,
2836 Primitive::Type type,
2837 TableKind kind,
2838 size_t index,
2839 uint32_t dex_pc)
2840 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002841 index_(index) {
2842 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002843 SetRawInputAt(0, cls);
2844 }
2845
2846 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002847 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002848 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002849 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002850 }
2851
Vladimir Markoa1de9182016-02-25 11:37:38 +00002852 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002853 size_t GetIndex() const { return index_; }
2854
2855 DECLARE_INSTRUCTION(ClassTableGet);
2856
2857 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002858 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2859 static constexpr size_t kFieldTableKindSize =
2860 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2861 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2862 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2863 "Too many packed fields.");
2864 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2865
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002866 // The index of the ArtMethod in the table.
2867 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002868
2869 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2870};
2871
Mark Mendellfe57faa2015-09-18 09:26:15 -04002872// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2873// have one successor for each entry in the switch table, and the final successor
2874// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002875class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002876 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002877 HPackedSwitch(int32_t start_value,
2878 uint32_t num_entries,
2879 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002880 uint32_t dex_pc = kNoDexPc)
2881 : HTemplateInstruction(SideEffects::None(), dex_pc),
2882 start_value_(start_value),
2883 num_entries_(num_entries) {
2884 SetRawInputAt(0, input);
2885 }
2886
2887 bool IsControlFlow() const OVERRIDE { return true; }
2888
2889 int32_t GetStartValue() const { return start_value_; }
2890
Vladimir Marko211c2112015-09-24 16:52:33 +01002891 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002892
2893 HBasicBlock* GetDefaultBlock() const {
2894 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002895 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002896 }
2897 DECLARE_INSTRUCTION(PackedSwitch);
2898
2899 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002900 const int32_t start_value_;
2901 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002902
2903 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2904};
2905
Roland Levillain88cb1752014-10-20 16:36:47 +01002906class HUnaryOperation : public HExpression<1> {
2907 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002908 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2909 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002910 SetRawInputAt(0, input);
2911 }
2912
2913 HInstruction* GetInput() const { return InputAt(0); }
2914 Primitive::Type GetResultType() const { return GetType(); }
2915
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002916 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002917 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002918 return true;
2919 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002920
Roland Levillain31dd3d62016-02-16 12:21:02 +00002921 // Try to statically evaluate `this` and return a HConstant
2922 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002923 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002924 HConstant* TryStaticEvaluation() const;
2925
2926 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002927 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2928 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002929 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2930 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002931
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002932 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002933
2934 private:
2935 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2936};
2937
Dave Allison20dfc792014-06-16 20:44:29 -07002938class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002939 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002940 HBinaryOperation(Primitive::Type result_type,
2941 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002942 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002943 SideEffects side_effects = SideEffects::None(),
2944 uint32_t dex_pc = kNoDexPc)
2945 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002946 SetRawInputAt(0, left);
2947 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002948 }
2949
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950 HInstruction* GetLeft() const { return InputAt(0); }
2951 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002952 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002953
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002954 virtual bool IsCommutative() const { return false; }
2955
2956 // Put constant on the right.
2957 // Returns whether order is changed.
2958 bool OrderInputsWithConstantOnTheRight() {
2959 HInstruction* left = InputAt(0);
2960 HInstruction* right = InputAt(1);
2961 if (left->IsConstant() && !right->IsConstant()) {
2962 ReplaceInput(right, 0);
2963 ReplaceInput(left, 1);
2964 return true;
2965 }
2966 return false;
2967 }
2968
2969 // Order inputs by instruction id, but favor constant on the right side.
2970 // This helps GVN for commutative ops.
2971 void OrderInputs() {
2972 DCHECK(IsCommutative());
2973 HInstruction* left = InputAt(0);
2974 HInstruction* right = InputAt(1);
2975 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2976 return;
2977 }
2978 if (OrderInputsWithConstantOnTheRight()) {
2979 return;
2980 }
2981 // Order according to instruction id.
2982 if (left->GetId() > right->GetId()) {
2983 ReplaceInput(right, 0);
2984 ReplaceInput(left, 1);
2985 }
2986 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002987
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002988 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002989 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002990 return true;
2991 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002992
Roland Levillain31dd3d62016-02-16 12:21:02 +00002993 // Try to statically evaluate `this` and return a HConstant
2994 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002995 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002996 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002997
2998 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002999 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3000 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003001 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3002 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003003 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003004 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3005 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003006 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3007 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003008 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3009 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003010 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003011 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3012 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003013
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003014 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003015 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003016 HConstant* GetConstantRight() const;
3017
3018 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003019 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003020 HInstruction* GetLeastConstantLeft() const;
3021
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003022 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003023
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003024 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003025 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3026};
3027
Mark Mendellc4701932015-04-10 13:18:51 -04003028// The comparison bias applies for floating point operations and indicates how NaN
3029// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003030enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003031 kNoBias, // bias is not applicable (i.e. for long operation)
3032 kGtBias, // return 1 for NaN comparisons
3033 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003034 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003035};
3036
Roland Levillain31dd3d62016-02-16 12:21:02 +00003037std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3038
Dave Allison20dfc792014-06-16 20:44:29 -07003039class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003040 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003041 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003042 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3043 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3044 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003045
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003046 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003047 // `instruction`, and disregard moves in between.
3048 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003049
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003050 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003051
3052 virtual IfCondition GetCondition() const = 0;
3053
Mark Mendellc4701932015-04-10 13:18:51 -04003054 virtual IfCondition GetOppositeCondition() const = 0;
3055
Vladimir Markoa1de9182016-02-25 11:37:38 +00003056 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003057 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3058
Vladimir Markoa1de9182016-02-25 11:37:38 +00003059 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3060 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003061
Vladimir Marko372f10e2016-05-17 16:30:10 +01003062 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003063 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003064 }
3065
Roland Levillain4fa13f62015-07-06 18:11:54 +01003066 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003067 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003068 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003069 if (if_cond == kCondNE) {
3070 return true;
3071 } else if (if_cond == kCondEQ) {
3072 return false;
3073 }
3074 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003075 }
3076
3077 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003079 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003080 if (if_cond == kCondEQ) {
3081 return true;
3082 } else if (if_cond == kCondNE) {
3083 return false;
3084 }
3085 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003086 }
3087
Roland Levillain31dd3d62016-02-16 12:21:02 +00003088 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003089 // Needed if we merge a HCompare into a HCondition.
3090 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3091 static constexpr size_t kFieldComparisonBiasSize =
3092 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3093 static constexpr size_t kNumberOfConditionPackedBits =
3094 kFieldComparisonBias + kFieldComparisonBiasSize;
3095 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3096 using ComparisonBiasField =
3097 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3098
Roland Levillain31dd3d62016-02-16 12:21:02 +00003099 template <typename T>
3100 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3101
3102 template <typename T>
3103 int32_t CompareFP(T x, T y) const {
3104 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3105 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3106 // Handle the bias.
3107 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3108 }
3109
3110 // Return an integer constant containing the result of a condition evaluated at compile time.
3111 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3112 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3113 }
3114
Dave Allison20dfc792014-06-16 20:44:29 -07003115 private:
3116 DISALLOW_COPY_AND_ASSIGN(HCondition);
3117};
3118
3119// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003120class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003121 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003122 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3123 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003124
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003125 bool IsCommutative() const OVERRIDE { return true; }
3126
Vladimir Marko9e23df52015-11-10 17:14:35 +00003127 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3128 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003129 return MakeConstantCondition(true, GetDexPc());
3130 }
3131 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3132 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3133 }
3134 // In the following Evaluate methods, a HCompare instruction has
3135 // been merged into this HEqual instruction; evaluate it as
3136 // `Compare(x, y) == 0`.
3137 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3138 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3139 GetDexPc());
3140 }
3141 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3142 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3143 }
3144 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3145 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003146 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003147
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003148 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003149
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003150 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003151 return kCondEQ;
3152 }
3153
Mark Mendellc4701932015-04-10 13:18:51 -04003154 IfCondition GetOppositeCondition() const OVERRIDE {
3155 return kCondNE;
3156 }
3157
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003158 private:
Aart Bike9f37602015-10-09 11:15:55 -07003159 template <typename T> bool Compute(T x, T y) const { return x == y; }
3160
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003161 DISALLOW_COPY_AND_ASSIGN(HEqual);
3162};
3163
Vladimir Markofcb503c2016-05-18 12:48:17 +01003164class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003165 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003166 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3167 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003168
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003169 bool IsCommutative() const OVERRIDE { return true; }
3170
Vladimir Marko9e23df52015-11-10 17:14:35 +00003171 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3172 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003173 return MakeConstantCondition(false, GetDexPc());
3174 }
3175 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3176 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3177 }
3178 // In the following Evaluate methods, a HCompare instruction has
3179 // been merged into this HNotEqual instruction; evaluate it as
3180 // `Compare(x, y) != 0`.
3181 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3182 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3183 }
3184 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3185 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3186 }
3187 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3188 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003189 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003190
Dave Allison20dfc792014-06-16 20:44:29 -07003191 DECLARE_INSTRUCTION(NotEqual);
3192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003193 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003194 return kCondNE;
3195 }
3196
Mark Mendellc4701932015-04-10 13:18:51 -04003197 IfCondition GetOppositeCondition() const OVERRIDE {
3198 return kCondEQ;
3199 }
3200
Dave Allison20dfc792014-06-16 20:44:29 -07003201 private:
Aart Bike9f37602015-10-09 11:15:55 -07003202 template <typename T> bool Compute(T x, T y) const { return x != y; }
3203
Dave Allison20dfc792014-06-16 20:44:29 -07003204 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3205};
3206
Vladimir Markofcb503c2016-05-18 12:48:17 +01003207class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003208 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003209 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3210 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003211
Roland Levillain9867bc72015-08-05 10:21:34 +01003212 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003213 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003214 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003215 // In the following Evaluate methods, a HCompare instruction has
3216 // been merged into this HLessThan instruction; evaluate it as
3217 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003218 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003219 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3220 }
3221 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3222 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3223 }
3224 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3225 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003226 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003227
Dave Allison20dfc792014-06-16 20:44:29 -07003228 DECLARE_INSTRUCTION(LessThan);
3229
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003230 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003231 return kCondLT;
3232 }
3233
Mark Mendellc4701932015-04-10 13:18:51 -04003234 IfCondition GetOppositeCondition() const OVERRIDE {
3235 return kCondGE;
3236 }
3237
Dave Allison20dfc792014-06-16 20:44:29 -07003238 private:
Aart Bike9f37602015-10-09 11:15:55 -07003239 template <typename T> bool Compute(T x, T y) const { return x < y; }
3240
Dave Allison20dfc792014-06-16 20:44:29 -07003241 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3242};
3243
Vladimir Markofcb503c2016-05-18 12:48:17 +01003244class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003245 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003246 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3247 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003248
Roland Levillain9867bc72015-08-05 10:21:34 +01003249 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003250 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003251 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003252 // In the following Evaluate methods, a HCompare instruction has
3253 // been merged into this HLessThanOrEqual instruction; evaluate it as
3254 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003255 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003256 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3257 }
3258 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3259 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3260 }
3261 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3262 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003263 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003264
Dave Allison20dfc792014-06-16 20:44:29 -07003265 DECLARE_INSTRUCTION(LessThanOrEqual);
3266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003267 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003268 return kCondLE;
3269 }
3270
Mark Mendellc4701932015-04-10 13:18:51 -04003271 IfCondition GetOppositeCondition() const OVERRIDE {
3272 return kCondGT;
3273 }
3274
Dave Allison20dfc792014-06-16 20:44:29 -07003275 private:
Aart Bike9f37602015-10-09 11:15:55 -07003276 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3277
Dave Allison20dfc792014-06-16 20:44:29 -07003278 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3279};
3280
Vladimir Markofcb503c2016-05-18 12:48:17 +01003281class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003282 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003283 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3284 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003285
Roland Levillain9867bc72015-08-05 10:21:34 +01003286 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003287 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003288 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003289 // In the following Evaluate methods, a HCompare instruction has
3290 // been merged into this HGreaterThan instruction; evaluate it as
3291 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003292 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003293 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3294 }
3295 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3296 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3297 }
3298 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3299 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003300 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003301
Dave Allison20dfc792014-06-16 20:44:29 -07003302 DECLARE_INSTRUCTION(GreaterThan);
3303
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003304 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003305 return kCondGT;
3306 }
3307
Mark Mendellc4701932015-04-10 13:18:51 -04003308 IfCondition GetOppositeCondition() const OVERRIDE {
3309 return kCondLE;
3310 }
3311
Dave Allison20dfc792014-06-16 20:44:29 -07003312 private:
Aart Bike9f37602015-10-09 11:15:55 -07003313 template <typename T> bool Compute(T x, T y) const { return x > y; }
3314
Dave Allison20dfc792014-06-16 20:44:29 -07003315 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3316};
3317
Vladimir Markofcb503c2016-05-18 12:48:17 +01003318class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003320 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3321 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003322
Roland Levillain9867bc72015-08-05 10:21:34 +01003323 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003324 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003325 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003326 // In the following Evaluate methods, a HCompare instruction has
3327 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3328 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003329 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003330 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3331 }
3332 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3333 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3334 }
3335 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3336 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003337 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003338
Dave Allison20dfc792014-06-16 20:44:29 -07003339 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3340
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003341 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003342 return kCondGE;
3343 }
3344
Mark Mendellc4701932015-04-10 13:18:51 -04003345 IfCondition GetOppositeCondition() const OVERRIDE {
3346 return kCondLT;
3347 }
3348
Dave Allison20dfc792014-06-16 20:44:29 -07003349 private:
Aart Bike9f37602015-10-09 11:15:55 -07003350 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3351
Dave Allison20dfc792014-06-16 20:44:29 -07003352 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3353};
3354
Vladimir Markofcb503c2016-05-18 12:48:17 +01003355class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003356 public:
3357 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3358 : HCondition(first, second, dex_pc) {}
3359
3360 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003361 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003362 }
3363 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003364 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3365 }
3366 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3367 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3368 LOG(FATAL) << DebugName() << " is not defined for float values";
3369 UNREACHABLE();
3370 }
3371 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3372 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3373 LOG(FATAL) << DebugName() << " is not defined for double values";
3374 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003375 }
3376
3377 DECLARE_INSTRUCTION(Below);
3378
3379 IfCondition GetCondition() const OVERRIDE {
3380 return kCondB;
3381 }
3382
3383 IfCondition GetOppositeCondition() const OVERRIDE {
3384 return kCondAE;
3385 }
3386
3387 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003388 template <typename T> bool Compute(T x, T y) const {
3389 return MakeUnsigned(x) < MakeUnsigned(y);
3390 }
Aart Bike9f37602015-10-09 11:15:55 -07003391
3392 DISALLOW_COPY_AND_ASSIGN(HBelow);
3393};
3394
Vladimir Markofcb503c2016-05-18 12:48:17 +01003395class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003396 public:
3397 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3398 : HCondition(first, second, dex_pc) {}
3399
3400 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003401 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003402 }
3403 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003404 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3405 }
3406 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3407 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3408 LOG(FATAL) << DebugName() << " is not defined for float values";
3409 UNREACHABLE();
3410 }
3411 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3412 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3413 LOG(FATAL) << DebugName() << " is not defined for double values";
3414 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003415 }
3416
3417 DECLARE_INSTRUCTION(BelowOrEqual);
3418
3419 IfCondition GetCondition() const OVERRIDE {
3420 return kCondBE;
3421 }
3422
3423 IfCondition GetOppositeCondition() const OVERRIDE {
3424 return kCondA;
3425 }
3426
3427 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 template <typename T> bool Compute(T x, T y) const {
3429 return MakeUnsigned(x) <= MakeUnsigned(y);
3430 }
Aart Bike9f37602015-10-09 11:15:55 -07003431
3432 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3433};
3434
Vladimir Markofcb503c2016-05-18 12:48:17 +01003435class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003436 public:
3437 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3438 : HCondition(first, second, dex_pc) {}
3439
3440 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003441 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003442 }
3443 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003444 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3445 }
3446 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3447 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3448 LOG(FATAL) << DebugName() << " is not defined for float values";
3449 UNREACHABLE();
3450 }
3451 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3452 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3453 LOG(FATAL) << DebugName() << " is not defined for double values";
3454 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003455 }
3456
3457 DECLARE_INSTRUCTION(Above);
3458
3459 IfCondition GetCondition() const OVERRIDE {
3460 return kCondA;
3461 }
3462
3463 IfCondition GetOppositeCondition() const OVERRIDE {
3464 return kCondBE;
3465 }
3466
3467 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003468 template <typename T> bool Compute(T x, T y) const {
3469 return MakeUnsigned(x) > MakeUnsigned(y);
3470 }
Aart Bike9f37602015-10-09 11:15:55 -07003471
3472 DISALLOW_COPY_AND_ASSIGN(HAbove);
3473};
3474
Vladimir Markofcb503c2016-05-18 12:48:17 +01003475class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003476 public:
3477 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3478 : HCondition(first, second, dex_pc) {}
3479
3480 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003481 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003482 }
3483 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003484 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3485 }
3486 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3487 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3488 LOG(FATAL) << DebugName() << " is not defined for float values";
3489 UNREACHABLE();
3490 }
3491 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3492 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3493 LOG(FATAL) << DebugName() << " is not defined for double values";
3494 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003495 }
3496
3497 DECLARE_INSTRUCTION(AboveOrEqual);
3498
3499 IfCondition GetCondition() const OVERRIDE {
3500 return kCondAE;
3501 }
3502
3503 IfCondition GetOppositeCondition() const OVERRIDE {
3504 return kCondB;
3505 }
3506
3507 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003508 template <typename T> bool Compute(T x, T y) const {
3509 return MakeUnsigned(x) >= MakeUnsigned(y);
3510 }
Aart Bike9f37602015-10-09 11:15:55 -07003511
3512 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3513};
Dave Allison20dfc792014-06-16 20:44:29 -07003514
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003515// Instruction to check how two inputs compare to each other.
3516// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003517class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003518 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003519 // Note that `comparison_type` is the type of comparison performed
3520 // between the comparison's inputs, not the type of the instantiated
3521 // HCompare instruction (which is always Primitive::kPrimInt).
3522 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003523 HInstruction* first,
3524 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003525 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003526 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003527 : HBinaryOperation(Primitive::kPrimInt,
3528 first,
3529 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003530 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003531 dex_pc) {
3532 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003533 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3534 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003535 }
3536
Roland Levillain9867bc72015-08-05 10:21:34 +01003537 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003538 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3539
3540 template <typename T>
3541 int32_t ComputeFP(T x, T y) const {
3542 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3543 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3544 // Handle the bias.
3545 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3546 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003547
Roland Levillain9867bc72015-08-05 10:21:34 +01003548 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3550 // reach this code path when processing a freshly built HIR
3551 // graph. However HCompare integer instructions can be synthesized
3552 // by the instruction simplifier to implement IntegerCompare and
3553 // IntegerSignum intrinsics, so we have to handle this case.
3554 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003555 }
3556 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003557 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3558 }
3559 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3560 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3561 }
3562 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3563 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003564 }
3565
Vladimir Marko372f10e2016-05-17 16:30:10 +01003566 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003567 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003568 }
3569
Vladimir Markoa1de9182016-02-25 11:37:38 +00003570 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003571
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003573 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 bool IsGtBias() const {
3575 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003576 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003578
Roland Levillain1693a1f2016-03-15 14:57:31 +00003579 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3580 // Comparisons do not require a runtime call in any back end.
3581 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003582 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003583
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003584 DECLARE_INSTRUCTION(Compare);
3585
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003587 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3588 static constexpr size_t kFieldComparisonBiasSize =
3589 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3590 static constexpr size_t kNumberOfComparePackedBits =
3591 kFieldComparisonBias + kFieldComparisonBiasSize;
3592 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3593 using ComparisonBiasField =
3594 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3595
Roland Levillain31dd3d62016-02-16 12:21:02 +00003596 // Return an integer constant containing the result of a comparison evaluated at compile time.
3597 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3598 DCHECK(value == -1 || value == 0 || value == 1) << value;
3599 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3600 }
3601
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003602 private:
3603 DISALLOW_COPY_AND_ASSIGN(HCompare);
3604};
3605
Vladimir Markofcb503c2016-05-18 12:48:17 +01003606class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003607 public:
3608 HNewInstance(HInstruction* cls,
3609 HCurrentMethod* current_method,
3610 uint32_t dex_pc,
3611 uint16_t type_index,
3612 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003613 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003614 bool finalizable,
3615 QuickEntrypointEnum entrypoint)
3616 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3617 type_index_(type_index),
3618 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003619 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003620 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003621 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003622 SetRawInputAt(0, cls);
3623 SetRawInputAt(1, current_method);
3624 }
3625
3626 uint16_t GetTypeIndex() const { return type_index_; }
3627 const DexFile& GetDexFile() const { return dex_file_; }
3628
3629 // Calls runtime so needs an environment.
3630 bool NeedsEnvironment() const OVERRIDE { return true; }
3631
Mingyao Yang062157f2016-03-02 10:15:36 -08003632 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3633 bool CanThrow() const OVERRIDE { return true; }
3634
3635 // Needs to call into runtime to make sure it's instantiable/accessible.
3636 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003637
Vladimir Markoa1de9182016-02-25 11:37:38 +00003638 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003639
3640 bool CanBeNull() const OVERRIDE { return false; }
3641
3642 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3643
3644 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3645 entrypoint_ = entrypoint;
3646 }
3647
3648 bool IsStringAlloc() const;
3649
3650 DECLARE_INSTRUCTION(NewInstance);
3651
3652 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003653 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3654 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003655 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3656 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3657 "Too many packed fields.");
3658
David Brazdil6de19382016-01-08 17:37:10 +00003659 const uint16_t type_index_;
3660 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003661 QuickEntrypointEnum entrypoint_;
3662
3663 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3664};
3665
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003666enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003667#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3668 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003669#include "intrinsics_list.h"
3670 kNone,
3671 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3672#undef INTRINSICS_LIST
3673#undef OPTIMIZING_INTRINSICS
3674};
3675std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3676
Agi Csaki05f20562015-08-19 14:58:14 -07003677enum IntrinsicNeedsEnvironmentOrCache {
3678 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3679 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003680};
3681
Aart Bik5d75afe2015-12-14 11:57:01 -08003682enum IntrinsicSideEffects {
3683 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3684 kReadSideEffects, // Intrinsic may read heap memory.
3685 kWriteSideEffects, // Intrinsic may write heap memory.
3686 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3687};
3688
3689enum IntrinsicExceptions {
3690 kNoThrow, // Intrinsic does not throw any exceptions.
3691 kCanThrow // Intrinsic may throw exceptions.
3692};
3693
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003694class HInvoke : public HInstruction {
3695 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003696 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003697
Vladimir Marko372f10e2016-05-17 16:30:10 +01003698 using HInstruction::GetInputRecords; // Keep the const version visible.
3699 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3700 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3701 }
3702
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003703 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003704 SetRawInputAt(index, argument);
3705 }
3706
Roland Levillain3e3d7332015-04-28 11:00:54 +01003707 // Return the number of arguments. This number can be lower than
3708 // the number of inputs returned by InputCount(), as some invoke
3709 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3710 // inputs at the end of their list of inputs.
3711 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3712
Vladimir Markoa1de9182016-02-25 11:37:38 +00003713 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003714
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003715 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003716 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003717
Vladimir Markoa1de9182016-02-25 11:37:38 +00003718 InvokeType GetOriginalInvokeType() const {
3719 return GetPackedField<OriginalInvokeTypeField>();
3720 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003721
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003722 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003723 return intrinsic_;
3724 }
3725
Aart Bik5d75afe2015-12-14 11:57:01 -08003726 void SetIntrinsic(Intrinsics intrinsic,
3727 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3728 IntrinsicSideEffects side_effects,
3729 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003730
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003731 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003732 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003733 }
3734
Vladimir Markoa1de9182016-02-25 11:37:38 +00003735 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003736
3737 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3738
Vladimir Marko372f10e2016-05-17 16:30:10 +01003739 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003740 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3741 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003742
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003743 uint32_t* GetIntrinsicOptimizations() {
3744 return &intrinsic_optimizations_;
3745 }
3746
3747 const uint32_t* GetIntrinsicOptimizations() const {
3748 return &intrinsic_optimizations_;
3749 }
3750
3751 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3752
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003753 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003754
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003755 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003756 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3757 static constexpr size_t kFieldOriginalInvokeTypeSize =
3758 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3759 static constexpr size_t kFieldReturnType =
3760 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3761 static constexpr size_t kFieldReturnTypeSize =
3762 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3763 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3764 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3765 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3766 using OriginalInvokeTypeField =
3767 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3768 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3769
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003770 HInvoke(ArenaAllocator* arena,
3771 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003772 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003773 Primitive::Type return_type,
3774 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003775 uint32_t dex_method_index,
3776 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003777 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003778 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003779 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003780 inputs_(number_of_arguments + number_of_other_inputs,
3781 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003782 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003783 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003784 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003785 SetPackedField<ReturnTypeField>(return_type);
3786 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3787 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003788 }
3789
Vladimir Markoa1de9182016-02-25 11:37:38 +00003790 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003791
Roland Levillain3e3d7332015-04-28 11:00:54 +01003792 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003793 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003794 const uint32_t dex_method_index_;
3795 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003796
3797 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3798 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003799
3800 private:
3801 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3802};
3803
Vladimir Markofcb503c2016-05-18 12:48:17 +01003804class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003805 public:
3806 HInvokeUnresolved(ArenaAllocator* arena,
3807 uint32_t number_of_arguments,
3808 Primitive::Type return_type,
3809 uint32_t dex_pc,
3810 uint32_t dex_method_index,
3811 InvokeType invoke_type)
3812 : HInvoke(arena,
3813 number_of_arguments,
3814 0u /* number_of_other_inputs */,
3815 return_type,
3816 dex_pc,
3817 dex_method_index,
3818 invoke_type) {
3819 }
3820
3821 DECLARE_INSTRUCTION(InvokeUnresolved);
3822
3823 private:
3824 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3825};
3826
Vladimir Markofcb503c2016-05-18 12:48:17 +01003827class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003828 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003829 // Requirements of this method call regarding the class
3830 // initialization (clinit) check of its declaring class.
3831 enum class ClinitCheckRequirement {
3832 kNone, // Class already initialized.
3833 kExplicit, // Static call having explicit clinit check as last input.
3834 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003835 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003836 };
3837
Vladimir Marko58155012015-08-19 12:49:41 +00003838 // Determines how to load the target ArtMethod*.
3839 enum class MethodLoadKind {
3840 // Use a String init ArtMethod* loaded from Thread entrypoints.
3841 kStringInit,
3842
3843 // Use the method's own ArtMethod* loaded by the register allocator.
3844 kRecursive,
3845
3846 // Use ArtMethod* at a known address, embed the direct address in the code.
3847 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3848 kDirectAddress,
3849
3850 // Use ArtMethod* at an address that will be known at link time, embed the direct
3851 // address in the code. If the image is relocatable, emit .patch_oat entry.
3852 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3853 // the image relocatable or not.
3854 kDirectAddressWithFixup,
3855
Vladimir Markoa1de9182016-02-25 11:37:38 +00003856 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003857 // Used when we need to use the dex cache, for example for invoke-static that
3858 // may cause class initialization (the entry may point to a resolution method),
3859 // and we know that we can access the dex cache arrays using a PC-relative load.
3860 kDexCachePcRelative,
3861
3862 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3863 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3864 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3865 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3866 kDexCacheViaMethod,
3867 };
3868
3869 // Determines the location of the code pointer.
3870 enum class CodePtrLocation {
3871 // Recursive call, use local PC-relative call instruction.
3872 kCallSelf,
3873
3874 // Use PC-relative call instruction patched at link time.
3875 // Used for calls within an oat file, boot->boot or app->app.
3876 kCallPCRelative,
3877
3878 // Call to a known target address, embed the direct address in code.
3879 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3880 kCallDirect,
3881
3882 // Call to a target address that will be known at link time, embed the direct
3883 // address in code. If the image is relocatable, emit .patch_oat entry.
3884 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3885 // the image relocatable or not.
3886 kCallDirectWithFixup,
3887
3888 // Use code pointer from the ArtMethod*.
3889 // Used when we don't know the target code. This is also the last-resort-kind used when
3890 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3891 kCallArtMethod,
3892 };
3893
3894 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003895 MethodLoadKind method_load_kind;
3896 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003897 // The method load data holds
3898 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3899 // Note that there are multiple string init methods, each having its own offset.
3900 // - the method address for kDirectAddress
3901 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003902 uint64_t method_load_data;
3903 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003904 };
3905
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003906 HInvokeStaticOrDirect(ArenaAllocator* arena,
3907 uint32_t number_of_arguments,
3908 Primitive::Type return_type,
3909 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003910 uint32_t method_index,
3911 MethodReference target_method,
3912 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003913 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003914 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003915 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003916 : HInvoke(arena,
3917 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003918 // There is potentially one extra argument for the HCurrentMethod node, and
3919 // potentially one other if the clinit check is explicit, and potentially
3920 // one other if the method is a string factory.
3921 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003922 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003923 return_type,
3924 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003925 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003926 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003927 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003928 dispatch_info_(dispatch_info) {
3929 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3930 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3931 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003932
Vladimir Markodc151b22015-10-15 18:02:30 +01003933 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003934 bool had_current_method_input = HasCurrentMethodInput();
3935 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3936
3937 // Using the current method is the default and once we find a better
3938 // method load kind, we should not go back to using the current method.
3939 DCHECK(had_current_method_input || !needs_current_method_input);
3940
3941 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003942 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3943 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003944 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003945 dispatch_info_ = dispatch_info;
3946 }
3947
Vladimir Markoc53c0792015-11-19 15:48:33 +00003948 void AddSpecialInput(HInstruction* input) {
3949 // We allow only one special input.
3950 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3951 DCHECK(InputCount() == GetSpecialInputIndex() ||
3952 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3953 InsertInputAt(GetSpecialInputIndex(), input);
3954 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003955
Vladimir Marko372f10e2016-05-17 16:30:10 +01003956 using HInstruction::GetInputRecords; // Keep the const version visible.
3957 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3958 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3959 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3960 DCHECK(!input_records.empty());
3961 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3962 HInstruction* last_input = input_records.back().GetInstruction();
3963 // Note: `last_input` may be null during arguments setup.
3964 if (last_input != nullptr) {
3965 // `last_input` is the last input of a static invoke marked as having
3966 // an explicit clinit check. It must either be:
3967 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3968 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3969 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3970 }
3971 }
3972 return input_records;
3973 }
3974
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003975 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003976 // We access the method via the dex cache so we can't do an implicit null check.
3977 // TODO: for intrinsics we can generate implicit null checks.
3978 return false;
3979 }
3980
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003981 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003982 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003983 }
3984
Vladimir Markoc53c0792015-11-19 15:48:33 +00003985 // Get the index of the special input, if any.
3986 //
David Brazdil6de19382016-01-08 17:37:10 +00003987 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3988 // method pointer; otherwise there may be one platform-specific special input,
3989 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003990 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003991 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003992
Vladimir Markoa1de9182016-02-25 11:37:38 +00003993 InvokeType GetOptimizedInvokeType() const {
3994 return GetPackedField<OptimizedInvokeTypeField>();
3995 }
3996
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003997 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003998 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003999 }
4000
Vladimir Marko58155012015-08-19 12:49:41 +00004001 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4002 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4003 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004004 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004005 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004006 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004007 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004008 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4009 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004010 bool HasCurrentMethodInput() const {
4011 // This function can be called only after the invoke has been fully initialized by the builder.
4012 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004013 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004014 return true;
4015 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004016 DCHECK(InputCount() == GetSpecialInputIndex() ||
4017 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004018 return false;
4019 }
4020 }
Vladimir Marko58155012015-08-19 12:49:41 +00004021 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4022 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004023 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004024
4025 int32_t GetStringInitOffset() const {
4026 DCHECK(IsStringInit());
4027 return dispatch_info_.method_load_data;
4028 }
4029
4030 uint64_t GetMethodAddress() const {
4031 DCHECK(HasMethodAddress());
4032 return dispatch_info_.method_load_data;
4033 }
4034
4035 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004036 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004037 return dispatch_info_.method_load_data;
4038 }
4039
4040 uint64_t GetDirectCodePtr() const {
4041 DCHECK(HasDirectCodePtr());
4042 return dispatch_info_.direct_code_ptr;
4043 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004044
Vladimir Markoa1de9182016-02-25 11:37:38 +00004045 ClinitCheckRequirement GetClinitCheckRequirement() const {
4046 return GetPackedField<ClinitCheckRequirementField>();
4047 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004048
Roland Levillain4c0eb422015-04-24 16:43:49 +01004049 // Is this instruction a call to a static method?
4050 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004051 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004052 }
4053
Vladimir Markofbb184a2015-11-13 14:47:00 +00004054 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4055 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4056 // instruction; only relevant for static calls with explicit clinit check.
4057 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004058 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004059 size_t last_input_index = inputs_.size() - 1u;
4060 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004061 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004062 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004063 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004064 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004065 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004066 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004067 }
4068
4069 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004070 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004071 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004072 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004073 }
4074
4075 // Is this a call to a static method whose declaring class has an
4076 // implicit intialization check requirement?
4077 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004078 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004079 }
4080
Vladimir Markob554b5a2015-11-06 12:57:55 +00004081 // Does this method load kind need the current method as an input?
4082 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4083 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4084 }
4085
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004086 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004087
Roland Levillain4c0eb422015-04-24 16:43:49 +01004088 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004089 void InsertInputAt(size_t index, HInstruction* input);
4090 void RemoveInputAt(size_t index);
4091
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004092 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004093 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4094 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4095 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4096 static constexpr size_t kFieldClinitCheckRequirement =
4097 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4098 static constexpr size_t kFieldClinitCheckRequirementSize =
4099 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4100 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4101 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4102 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4103 "Too many packed fields.");
4104 using OptimizedInvokeTypeField =
4105 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4106 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4107 kFieldClinitCheckRequirement,
4108 kFieldClinitCheckRequirementSize>;
4109
Vladimir Marko58155012015-08-19 12:49:41 +00004110 // The target method may refer to different dex file or method index than the original
4111 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4112 // in derived class to increase visibility.
4113 MethodReference target_method_;
4114 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004115
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004116 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004117};
Vladimir Markof64242a2015-12-01 14:58:23 +00004118std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004119std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004120
Vladimir Markofcb503c2016-05-18 12:48:17 +01004121class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004122 public:
4123 HInvokeVirtual(ArenaAllocator* arena,
4124 uint32_t number_of_arguments,
4125 Primitive::Type return_type,
4126 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004127 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004128 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004129 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004130 vtable_index_(vtable_index) {}
4131
Calin Juravle641547a2015-04-21 22:08:51 +01004132 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004133 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004134 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004135 }
4136
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004137 uint32_t GetVTableIndex() const { return vtable_index_; }
4138
4139 DECLARE_INSTRUCTION(InvokeVirtual);
4140
4141 private:
4142 const uint32_t vtable_index_;
4143
4144 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4145};
4146
Vladimir Markofcb503c2016-05-18 12:48:17 +01004147class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004148 public:
4149 HInvokeInterface(ArenaAllocator* arena,
4150 uint32_t number_of_arguments,
4151 Primitive::Type return_type,
4152 uint32_t dex_pc,
4153 uint32_t dex_method_index,
4154 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004155 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004156 imt_index_(imt_index) {}
4157
Calin Juravle641547a2015-04-21 22:08:51 +01004158 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004159 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004160 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004161 }
4162
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004163 uint32_t GetImtIndex() const { return imt_index_; }
4164 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4165
4166 DECLARE_INSTRUCTION(InvokeInterface);
4167
4168 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004169 const uint32_t imt_index_;
4170
4171 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4172};
4173
Vladimir Markofcb503c2016-05-18 12:48:17 +01004174class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004175 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004176 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004177 : HUnaryOperation(result_type, input, dex_pc) {
4178 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4179 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004180
Roland Levillain9867bc72015-08-05 10:21:34 +01004181 template <typename T> T Compute(T x) const { return -x; }
4182
4183 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 }
4186 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004187 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004188 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004189 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4190 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4191 }
4192 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4193 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4194 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004195
Roland Levillain88cb1752014-10-20 16:36:47 +01004196 DECLARE_INSTRUCTION(Neg);
4197
4198 private:
4199 DISALLOW_COPY_AND_ASSIGN(HNeg);
4200};
4201
Vladimir Markofcb503c2016-05-18 12:48:17 +01004202class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004204 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004205 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004206 uint32_t dex_pc,
4207 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004208 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004209 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004211 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004212 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004213 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004214 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004215 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004216 }
4217
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004219 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004220
4221 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004222 bool NeedsEnvironment() const OVERRIDE { return true; }
4223
Mingyao Yang0c365e62015-03-31 15:09:29 -07004224 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4225 bool CanThrow() const OVERRIDE { return true; }
4226
Calin Juravle10e244f2015-01-26 18:54:32 +00004227 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004228
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004229 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4230
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004231 DECLARE_INSTRUCTION(NewArray);
4232
4233 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004234 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004235 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004236 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004237
4238 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4239};
4240
Vladimir Markofcb503c2016-05-18 12:48:17 +01004241class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004242 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 HAdd(Primitive::Type result_type,
4244 HInstruction* left,
4245 HInstruction* right,
4246 uint32_t dex_pc = kNoDexPc)
4247 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004248
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004249 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004250
Roland Levillain9867bc72015-08-05 10:21:34 +01004251 template <typename T> T Compute(T x, T y) const { return x + y; }
4252
4253 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004254 return GetBlock()->GetGraph()->GetIntConstant(
4255 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004256 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004257 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 return GetBlock()->GetGraph()->GetLongConstant(
4259 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004260 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004261 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4262 return GetBlock()->GetGraph()->GetFloatConstant(
4263 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4264 }
4265 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4266 return GetBlock()->GetGraph()->GetDoubleConstant(
4267 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4268 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004269
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004270 DECLARE_INSTRUCTION(Add);
4271
4272 private:
4273 DISALLOW_COPY_AND_ASSIGN(HAdd);
4274};
4275
Vladimir Markofcb503c2016-05-18 12:48:17 +01004276class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004278 HSub(Primitive::Type result_type,
4279 HInstruction* left,
4280 HInstruction* right,
4281 uint32_t dex_pc = kNoDexPc)
4282 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004283
Roland Levillain9867bc72015-08-05 10:21:34 +01004284 template <typename T> T Compute(T x, T y) const { return x - y; }
4285
4286 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004287 return GetBlock()->GetGraph()->GetIntConstant(
4288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004289 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004290 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 return GetBlock()->GetGraph()->GetLongConstant(
4292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004293 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004294 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4295 return GetBlock()->GetGraph()->GetFloatConstant(
4296 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4297 }
4298 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4299 return GetBlock()->GetGraph()->GetDoubleConstant(
4300 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4301 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004302
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004303 DECLARE_INSTRUCTION(Sub);
4304
4305 private:
4306 DISALLOW_COPY_AND_ASSIGN(HSub);
4307};
4308
Vladimir Markofcb503c2016-05-18 12:48:17 +01004309class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 HMul(Primitive::Type result_type,
4312 HInstruction* left,
4313 HInstruction* right,
4314 uint32_t dex_pc = kNoDexPc)
4315 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004316
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004317 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004318
Roland Levillain9867bc72015-08-05 10:21:34 +01004319 template <typename T> T Compute(T x, T y) const { return x * y; }
4320
4321 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004322 return GetBlock()->GetGraph()->GetIntConstant(
4323 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004324 }
4325 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004326 return GetBlock()->GetGraph()->GetLongConstant(
4327 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004328 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004329 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4330 return GetBlock()->GetGraph()->GetFloatConstant(
4331 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4332 }
4333 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4334 return GetBlock()->GetGraph()->GetDoubleConstant(
4335 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4336 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004337
4338 DECLARE_INSTRUCTION(Mul);
4339
4340 private:
4341 DISALLOW_COPY_AND_ASSIGN(HMul);
4342};
4343
Vladimir Markofcb503c2016-05-18 12:48:17 +01004344class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004345 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004346 HDiv(Primitive::Type result_type,
4347 HInstruction* left,
4348 HInstruction* right,
4349 uint32_t dex_pc)
4350 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004351
Roland Levillain9867bc72015-08-05 10:21:34 +01004352 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004353 T ComputeIntegral(T x, T y) const {
4354 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 // Our graph structure ensures we never have 0 for `y` during
4356 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004357 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004358 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004359 return (y == -1) ? -x : x / y;
4360 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004361
Roland Levillain31dd3d62016-02-16 12:21:02 +00004362 template <typename T>
4363 T ComputeFP(T x, T y) const {
4364 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4365 return x / y;
4366 }
4367
Roland Levillain9867bc72015-08-05 10:21:34 +01004368 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004369 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004370 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004371 }
4372 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004374 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4375 }
4376 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4377 return GetBlock()->GetGraph()->GetFloatConstant(
4378 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4379 }
4380 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4381 return GetBlock()->GetGraph()->GetDoubleConstant(
4382 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004383 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004384
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004385 static SideEffects SideEffectsForArchRuntimeCalls() {
4386 // The generated code can use a runtime call.
4387 return SideEffects::CanTriggerGC();
4388 }
4389
Calin Juravle7c4954d2014-10-28 16:57:40 +00004390 DECLARE_INSTRUCTION(Div);
4391
4392 private:
4393 DISALLOW_COPY_AND_ASSIGN(HDiv);
4394};
4395
Vladimir Markofcb503c2016-05-18 12:48:17 +01004396class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004397 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004398 HRem(Primitive::Type result_type,
4399 HInstruction* left,
4400 HInstruction* right,
4401 uint32_t dex_pc)
4402 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004403
Roland Levillain9867bc72015-08-05 10:21:34 +01004404 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004405 T ComputeIntegral(T x, T y) const {
4406 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004407 // Our graph structure ensures we never have 0 for `y` during
4408 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004409 DCHECK_NE(y, 0);
4410 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4411 return (y == -1) ? 0 : x % y;
4412 }
4413
Roland Levillain31dd3d62016-02-16 12:21:02 +00004414 template <typename T>
4415 T ComputeFP(T x, T y) const {
4416 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4417 return std::fmod(x, y);
4418 }
4419
Roland Levillain9867bc72015-08-05 10:21:34 +01004420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004421 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004422 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004423 }
4424 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004426 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004427 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004428 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4429 return GetBlock()->GetGraph()->GetFloatConstant(
4430 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4431 }
4432 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4433 return GetBlock()->GetGraph()->GetDoubleConstant(
4434 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4435 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004436
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004437 static SideEffects SideEffectsForArchRuntimeCalls() {
4438 return SideEffects::CanTriggerGC();
4439 }
4440
Calin Juravlebacfec32014-11-14 15:54:36 +00004441 DECLARE_INSTRUCTION(Rem);
4442
4443 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004444 DISALLOW_COPY_AND_ASSIGN(HRem);
4445};
4446
Vladimir Markofcb503c2016-05-18 12:48:17 +01004447class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004448 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004449 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4450 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004451 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004452 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004453 SetRawInputAt(0, value);
4454 }
4455
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004456 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4457
Calin Juravled0d48522014-11-04 16:40:20 +00004458 bool CanBeMoved() const OVERRIDE { return true; }
4459
Vladimir Marko372f10e2016-05-17 16:30:10 +01004460 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004461 return true;
4462 }
4463
4464 bool NeedsEnvironment() const OVERRIDE { return true; }
4465 bool CanThrow() const OVERRIDE { return true; }
4466
Calin Juravled0d48522014-11-04 16:40:20 +00004467 DECLARE_INSTRUCTION(DivZeroCheck);
4468
4469 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004470 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4471};
4472
Vladimir Markofcb503c2016-05-18 12:48:17 +01004473class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004475 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004476 HInstruction* value,
4477 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004478 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004479 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4480 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4481 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004482 }
4483
Roland Levillain5b5b9312016-03-22 14:57:31 +00004484 template <typename T>
4485 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4486 return value << (distance & max_shift_distance);
4487 }
4488
4489 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004490 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004491 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004493 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004494 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004495 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004496 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004497 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4498 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4499 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4500 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004501 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4503 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004504 LOG(FATAL) << DebugName() << " is not defined for float values";
4505 UNREACHABLE();
4506 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004507 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4508 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004509 LOG(FATAL) << DebugName() << " is not defined for double values";
4510 UNREACHABLE();
4511 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004512
4513 DECLARE_INSTRUCTION(Shl);
4514
4515 private:
4516 DISALLOW_COPY_AND_ASSIGN(HShl);
4517};
4518
Vladimir Markofcb503c2016-05-18 12:48:17 +01004519class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004520 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004521 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 HInstruction* value,
4523 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004524 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004525 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4526 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4527 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 }
4529
Roland Levillain5b5b9312016-03-22 14:57:31 +00004530 template <typename T>
4531 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4532 return value >> (distance & max_shift_distance);
4533 }
4534
4535 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004536 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004537 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004538 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004539 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004541 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4544 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4545 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4546 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4549 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004550 LOG(FATAL) << DebugName() << " is not defined for float values";
4551 UNREACHABLE();
4552 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004553 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4554 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004555 LOG(FATAL) << DebugName() << " is not defined for double values";
4556 UNREACHABLE();
4557 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004558
4559 DECLARE_INSTRUCTION(Shr);
4560
4561 private:
4562 DISALLOW_COPY_AND_ASSIGN(HShr);
4563};
4564
Vladimir Markofcb503c2016-05-18 12:48:17 +01004565class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004566 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004567 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004568 HInstruction* value,
4569 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004570 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004571 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4572 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4573 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004574 }
4575
Roland Levillain5b5b9312016-03-22 14:57:31 +00004576 template <typename T>
4577 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4578 typedef typename std::make_unsigned<T>::type V;
4579 V ux = static_cast<V>(value);
4580 return static_cast<T>(ux >> (distance & max_shift_distance));
4581 }
4582
4583 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004584 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004585 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004586 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004591 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4592 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4593 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4594 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004595 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4597 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004598 LOG(FATAL) << DebugName() << " is not defined for float values";
4599 UNREACHABLE();
4600 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004601 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4602 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004603 LOG(FATAL) << DebugName() << " is not defined for double values";
4604 UNREACHABLE();
4605 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004606
4607 DECLARE_INSTRUCTION(UShr);
4608
4609 private:
4610 DISALLOW_COPY_AND_ASSIGN(HUShr);
4611};
4612
Vladimir Markofcb503c2016-05-18 12:48:17 +01004613class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004614 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004615 HAnd(Primitive::Type result_type,
4616 HInstruction* left,
4617 HInstruction* right,
4618 uint32_t dex_pc = kNoDexPc)
4619 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004620
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004621 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004622
Roland Levillaine53bd812016-02-24 14:54:18 +00004623 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004624
4625 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004626 return GetBlock()->GetGraph()->GetIntConstant(
4627 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004628 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004629 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 return GetBlock()->GetGraph()->GetLongConstant(
4631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004632 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004633 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4634 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4635 LOG(FATAL) << DebugName() << " is not defined for float values";
4636 UNREACHABLE();
4637 }
4638 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4639 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4640 LOG(FATAL) << DebugName() << " is not defined for double values";
4641 UNREACHABLE();
4642 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004643
4644 DECLARE_INSTRUCTION(And);
4645
4646 private:
4647 DISALLOW_COPY_AND_ASSIGN(HAnd);
4648};
4649
Vladimir Markofcb503c2016-05-18 12:48:17 +01004650class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 HOr(Primitive::Type result_type,
4653 HInstruction* left,
4654 HInstruction* right,
4655 uint32_t dex_pc = kNoDexPc)
4656 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004657
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004658 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004659
Roland Levillaine53bd812016-02-24 14:54:18 +00004660 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004661
4662 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 return GetBlock()->GetGraph()->GetIntConstant(
4664 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004665 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004666 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 return GetBlock()->GetGraph()->GetLongConstant(
4668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004670 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4671 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4672 LOG(FATAL) << DebugName() << " is not defined for float values";
4673 UNREACHABLE();
4674 }
4675 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4676 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4677 LOG(FATAL) << DebugName() << " is not defined for double values";
4678 UNREACHABLE();
4679 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004680
4681 DECLARE_INSTRUCTION(Or);
4682
4683 private:
4684 DISALLOW_COPY_AND_ASSIGN(HOr);
4685};
4686
Vladimir Markofcb503c2016-05-18 12:48:17 +01004687class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004688 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004689 HXor(Primitive::Type result_type,
4690 HInstruction* left,
4691 HInstruction* right,
4692 uint32_t dex_pc = kNoDexPc)
4693 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004694
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004695 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004696
Roland Levillaine53bd812016-02-24 14:54:18 +00004697 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004698
4699 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004700 return GetBlock()->GetGraph()->GetIntConstant(
4701 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004702 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 return GetBlock()->GetGraph()->GetLongConstant(
4705 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004706 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004707 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4708 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4709 LOG(FATAL) << DebugName() << " is not defined for float values";
4710 UNREACHABLE();
4711 }
4712 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4713 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4714 LOG(FATAL) << DebugName() << " is not defined for double values";
4715 UNREACHABLE();
4716 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004717
4718 DECLARE_INSTRUCTION(Xor);
4719
4720 private:
4721 DISALLOW_COPY_AND_ASSIGN(HXor);
4722};
4723
Vladimir Markofcb503c2016-05-18 12:48:17 +01004724class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004725 public:
4726 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004727 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004728 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4729 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004730 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004731
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 template <typename T>
4733 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4734 typedef typename std::make_unsigned<T>::type V;
4735 V ux = static_cast<V>(value);
4736 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 return static_cast<T>(ux);
4738 } else {
4739 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 return static_cast<T>(ux >> (distance & max_shift_value)) |
4741 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742 }
4743 }
4744
Roland Levillain5b5b9312016-03-22 14:57:31 +00004745 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004746 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004747 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004750 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004751 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004752 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4754 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4755 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4756 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004757 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4759 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004760 LOG(FATAL) << DebugName() << " is not defined for float values";
4761 UNREACHABLE();
4762 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4764 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004765 LOG(FATAL) << DebugName() << " is not defined for double values";
4766 UNREACHABLE();
4767 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004768
4769 DECLARE_INSTRUCTION(Ror);
4770
4771 private:
4772 DISALLOW_COPY_AND_ASSIGN(HRor);
4773};
4774
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004775// The value of a parameter in this method. Its location depends on
4776// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004777class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004778 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004779 HParameterValue(const DexFile& dex_file,
4780 uint16_t type_index,
4781 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004782 Primitive::Type parameter_type,
4783 bool is_this = false)
4784 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004785 dex_file_(dex_file),
4786 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004787 index_(index) {
4788 SetPackedFlag<kFlagIsThis>(is_this);
4789 SetPackedFlag<kFlagCanBeNull>(!is_this);
4790 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004791
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004792 const DexFile& GetDexFile() const { return dex_file_; }
4793 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004794 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004795 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004796
Vladimir Markoa1de9182016-02-25 11:37:38 +00004797 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4798 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004799
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004800 DECLARE_INSTRUCTION(ParameterValue);
4801
4802 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004803 // Whether or not the parameter value corresponds to 'this' argument.
4804 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4805 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4806 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4807 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4808 "Too many packed fields.");
4809
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004810 const DexFile& dex_file_;
4811 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004812 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004813 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004814 const uint8_t index_;
4815
4816 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4817};
4818
Vladimir Markofcb503c2016-05-18 12:48:17 +01004819class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004821 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4822 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004824 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004825 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004826 return true;
4827 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004828
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 template <typename T> T Compute(T x) const { return ~x; }
4830
4831 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
4834 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004837 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4838 LOG(FATAL) << DebugName() << " is not defined for float values";
4839 UNREACHABLE();
4840 }
4841 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4842 LOG(FATAL) << DebugName() << " is not defined for double values";
4843 UNREACHABLE();
4844 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004845
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004846 DECLARE_INSTRUCTION(Not);
4847
4848 private:
4849 DISALLOW_COPY_AND_ASSIGN(HNot);
4850};
4851
Vladimir Markofcb503c2016-05-18 12:48:17 +01004852class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004853 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004854 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4855 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004856
4857 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004858 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004859 return true;
4860 }
4861
Roland Levillain9867bc72015-08-05 10:21:34 +01004862 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004863 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004864 return !x;
4865 }
4866
Roland Levillain9867bc72015-08-05 10:21:34 +01004867 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004868 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004869 }
4870 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4871 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004872 UNREACHABLE();
4873 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004874 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4875 LOG(FATAL) << DebugName() << " is not defined for float values";
4876 UNREACHABLE();
4877 }
4878 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4879 LOG(FATAL) << DebugName() << " is not defined for double values";
4880 UNREACHABLE();
4881 }
David Brazdil66d126e2015-04-03 16:02:44 +01004882
4883 DECLARE_INSTRUCTION(BooleanNot);
4884
4885 private:
4886 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4887};
4888
Vladimir Markofcb503c2016-05-18 12:48:17 +01004889class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004890 public:
4891 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004892 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004893 : HExpression(result_type,
4894 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4895 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004896 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004897 // Invariant: We should never generate a conversion to a Boolean value.
4898 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004899 }
4900
4901 HInstruction* GetInput() const { return InputAt(0); }
4902 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4903 Primitive::Type GetResultType() const { return GetType(); }
4904
4905 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004906 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4907 return true;
4908 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004909
Mark Mendelle82549b2015-05-06 10:55:34 -04004910 // Try to statically evaluate the conversion and return a HConstant
4911 // containing the result. If the input cannot be converted, return nullptr.
4912 HConstant* TryStaticEvaluation() const;
4913
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004914 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4915 Primitive::Type result_type) {
4916 // Some architectures may not require the 'GC' side effects, but at this point
4917 // in the compilation process we do not know what architecture we will
4918 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004919 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4920 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004921 return SideEffects::CanTriggerGC();
4922 }
4923 return SideEffects::None();
4924 }
4925
Roland Levillaindff1f282014-11-05 14:15:05 +00004926 DECLARE_INSTRUCTION(TypeConversion);
4927
4928 private:
4929 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4930};
4931
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004932static constexpr uint32_t kNoRegNumber = -1;
4933
Vladimir Markofcb503c2016-05-18 12:48:17 +01004934class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004935 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004936 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4937 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004938 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004939 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004940 SetRawInputAt(0, value);
4941 }
4942
Calin Juravle10e244f2015-01-26 18:54:32 +00004943 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004944 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004945 return true;
4946 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004947
Calin Juravle10e244f2015-01-26 18:54:32 +00004948 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004949
Calin Juravle10e244f2015-01-26 18:54:32 +00004950 bool CanThrow() const OVERRIDE { return true; }
4951
4952 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004953
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954
4955 DECLARE_INSTRUCTION(NullCheck);
4956
4957 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004958 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4959};
4960
4961class FieldInfo : public ValueObject {
4962 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004963 FieldInfo(MemberOffset field_offset,
4964 Primitive::Type field_type,
4965 bool is_volatile,
4966 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004967 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004968 const DexFile& dex_file,
4969 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004970 : field_offset_(field_offset),
4971 field_type_(field_type),
4972 is_volatile_(is_volatile),
4973 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004974 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004975 dex_file_(dex_file),
4976 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977
4978 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004979 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004980 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004981 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004982 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004983 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004984 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985
4986 private:
4987 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004988 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004989 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004990 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004991 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004992 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004993 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004994};
4995
Vladimir Markofcb503c2016-05-18 12:48:17 +01004996class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004997 public:
4998 HInstanceFieldGet(HInstruction* value,
4999 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005000 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005001 bool is_volatile,
5002 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005003 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005004 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005005 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005006 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005007 : HExpression(field_type,
5008 SideEffects::FieldReadOfType(field_type, is_volatile),
5009 dex_pc),
5010 field_info_(field_offset,
5011 field_type,
5012 is_volatile,
5013 field_idx,
5014 declaring_class_def_index,
5015 dex_file,
5016 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005017 SetRawInputAt(0, value);
5018 }
5019
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005020 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005021
Vladimir Marko372f10e2016-05-17 16:30:10 +01005022 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5023 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005024 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005025 }
5026
Calin Juravle641547a2015-04-21 22:08:51 +01005027 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5028 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005029 }
5030
5031 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005032 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5033 }
5034
Calin Juravle52c48962014-12-16 17:02:57 +00005035 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005036 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005037 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005038 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039
5040 DECLARE_INSTRUCTION(InstanceFieldGet);
5041
5042 private:
5043 const FieldInfo field_info_;
5044
5045 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5046};
5047
Vladimir Markofcb503c2016-05-18 12:48:17 +01005048class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005049 public:
5050 HInstanceFieldSet(HInstruction* object,
5051 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005052 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005053 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005054 bool is_volatile,
5055 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005056 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005057 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005058 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005059 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005060 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5061 dex_pc),
5062 field_info_(field_offset,
5063 field_type,
5064 is_volatile,
5065 field_idx,
5066 declaring_class_def_index,
5067 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005068 dex_cache) {
5069 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070 SetRawInputAt(0, object);
5071 SetRawInputAt(1, value);
5072 }
5073
Calin Juravle641547a2015-04-21 22:08:51 +01005074 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5075 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005076 }
5077
Calin Juravle52c48962014-12-16 17:02:57 +00005078 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005080 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005081 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005082 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005083 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5084 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005085
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086 DECLARE_INSTRUCTION(InstanceFieldSet);
5087
5088 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005089 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5090 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5091 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5092 "Too many packed fields.");
5093
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005094 const FieldInfo field_info_;
5095
5096 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5097};
5098
Vladimir Markofcb503c2016-05-18 12:48:17 +01005099class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005101 HArrayGet(HInstruction* array,
5102 HInstruction* index,
5103 Primitive::Type type,
5104 uint32_t dex_pc,
5105 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005106 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005107 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 SetRawInputAt(0, array);
5109 SetRawInputAt(1, index);
5110 }
5111
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005112 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005113 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005114 return true;
5115 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005116 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005117 // TODO: We can be smarter here.
5118 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5119 // which generates the implicit null check. There are cases when these can be removed
5120 // to produce better code. If we ever add optimizations to do so we should allow an
5121 // implicit check here (as long as the address falls in the first page).
5122 return false;
5123 }
5124
David Brazdil4833f5a2015-12-16 10:37:39 +00005125 bool IsEquivalentOf(HArrayGet* other) const {
5126 bool result = (GetDexPc() == other->GetDexPc());
5127 if (kIsDebugBuild && result) {
5128 DCHECK_EQ(GetBlock(), other->GetBlock());
5129 DCHECK_EQ(GetArray(), other->GetArray());
5130 DCHECK_EQ(GetIndex(), other->GetIndex());
5131 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005132 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005133 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005134 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5135 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005136 }
5137 }
5138 return result;
5139 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005140
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005141 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5142
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005143 HInstruction* GetArray() const { return InputAt(0); }
5144 HInstruction* GetIndex() const { return InputAt(1); }
5145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 DECLARE_INSTRUCTION(ArrayGet);
5147
5148 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005149 // We treat a String as an array, creating the HArrayGet from String.charAt()
5150 // intrinsic in the instruction simplifier. We can always determine whether
5151 // a particular HArrayGet is actually a String.charAt() by looking at the type
5152 // of the input but that requires holding the mutator lock, so we prefer to use
5153 // a flag, so that code generators don't need to do the locking.
5154 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5155 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5156 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5157 "Too many packed fields.");
5158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5160};
5161
Vladimir Markofcb503c2016-05-18 12:48:17 +01005162class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 public:
5164 HArraySet(HInstruction* array,
5165 HInstruction* index,
5166 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005167 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005168 uint32_t dex_pc)
5169 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005170 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5171 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5172 SetPackedFlag<kFlagValueCanBeNull>(true);
5173 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174 SetRawInputAt(0, array);
5175 SetRawInputAt(1, index);
5176 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005177 // Make a best guess now, may be refined during SSA building.
5178 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 }
5180
Calin Juravle77520bc2015-01-12 18:45:46 +00005181 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005182 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005183 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184 }
5185
Mingyao Yang81014cb2015-06-02 03:16:27 -07005186 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005187 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005188
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005189 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005190 // TODO: Same as for ArrayGet.
5191 return false;
5192 }
5193
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005194 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005195 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005196 }
5197
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005198 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005199 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005200 }
5201
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005202 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005204 }
5205
Vladimir Markoa1de9182016-02-25 11:37:38 +00005206 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5207 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5208 bool StaticTypeOfArrayIsObjectArray() const {
5209 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5210 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005211
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005212 HInstruction* GetArray() const { return InputAt(0); }
5213 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 HInstruction* GetValue() const { return InputAt(2); }
5215
5216 Primitive::Type GetComponentType() const {
5217 // The Dex format does not type floating point index operations. Since the
5218 // `expected_component_type_` is set during building and can therefore not
5219 // be correct, we also check what is the value type. If it is a floating
5220 // point type, we must use that type.
5221 Primitive::Type value_type = GetValue()->GetType();
5222 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5223 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005224 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005225 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005226
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005227 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005228 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005229 }
5230
Aart Bik18b36ab2016-04-13 16:41:35 -07005231 void ComputeSideEffects() {
5232 Primitive::Type type = GetComponentType();
5233 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5234 SideEffectsForArchRuntimeCalls(type)));
5235 }
5236
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005237 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5238 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5239 }
5240
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 DECLARE_INSTRUCTION(ArraySet);
5242
5243 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005244 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5245 static constexpr size_t kFieldExpectedComponentTypeSize =
5246 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5247 static constexpr size_t kFlagNeedsTypeCheck =
5248 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5249 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005250 // Cached information for the reference_type_info_ so that codegen
5251 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005252 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5253 static constexpr size_t kNumberOfArraySetPackedBits =
5254 kFlagStaticTypeOfArrayIsObjectArray + 1;
5255 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5256 using ExpectedComponentTypeField =
5257 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258
5259 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5260};
5261
Vladimir Markofcb503c2016-05-18 12:48:17 +01005262class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005264 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005265 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005266 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005267 // Note that arrays do not change length, so the instruction does not
5268 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 SetRawInputAt(0, array);
5270 }
5271
Calin Juravle77520bc2015-01-12 18:45:46 +00005272 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005273 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005274 return true;
5275 }
Calin Juravle641547a2015-04-21 22:08:51 +01005276 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5277 return obj == InputAt(0);
5278 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005279
Vladimir Markodce016e2016-04-28 13:10:02 +01005280 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5281
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 DECLARE_INSTRUCTION(ArrayLength);
5283
5284 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005285 // We treat a String as an array, creating the HArrayLength from String.length()
5286 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5287 // determine whether a particular HArrayLength is actually a String.length() by
5288 // looking at the type of the input but that requires holding the mutator lock, so
5289 // we prefer to use a flag, so that code generators don't need to do the locking.
5290 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5291 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5292 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5293 "Too many packed fields.");
5294
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5296};
5297
Vladimir Markofcb503c2016-05-18 12:48:17 +01005298class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005300 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5301 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005302 HBoundsCheck(HInstruction* index,
5303 HInstruction* length,
5304 uint32_t dex_pc,
5305 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5306 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5307 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005308 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309 SetRawInputAt(0, index);
5310 SetRawInputAt(1, length);
5311 }
5312
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005313 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005314 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005315 return true;
5316 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005317
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005318 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005320 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005321
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005322 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5323 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5324
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005325 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326
5327 DECLARE_INSTRUCTION(BoundsCheck);
5328
5329 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005330 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5331 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5332 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5333 // so we need to create an HEnvironment which will be translated to an InlineInfo
5334 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5335 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5336 // from the invoke as we don't want to look again at the dex bytecode.
5337 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5340};
5341
Vladimir Markofcb503c2016-05-18 12:48:17 +01005342class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005343 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005344 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005345 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005346
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005347 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005348 return true;
5349 }
5350
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005351 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5352 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005353
5354 DECLARE_INSTRUCTION(SuspendCheck);
5355
5356 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005357 // Only used for code generation, in order to share the same slow path between back edges
5358 // of a same loop.
5359 SlowPathCode* slow_path_;
5360
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005361 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5362};
5363
David Srbecky0cf44932015-12-09 14:09:59 +00005364// Pseudo-instruction which provides the native debugger with mapping information.
5365// It ensures that we can generate line number and local variables at this point.
5366class HNativeDebugInfo : public HTemplateInstruction<0> {
5367 public:
5368 explicit HNativeDebugInfo(uint32_t dex_pc)
5369 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5370
5371 bool NeedsEnvironment() const OVERRIDE {
5372 return true;
5373 }
5374
5375 DECLARE_INSTRUCTION(NativeDebugInfo);
5376
5377 private:
5378 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5379};
5380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005381/**
5382 * Instruction to load a Class object.
5383 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005384class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005386 // Determines how to load the Class.
5387 enum class LoadKind {
5388 // Use the Class* from the method's own ArtMethod*.
5389 kReferrersClass,
5390
5391 // Use boot image Class* address that will be known at link time.
5392 // Used for boot image classes referenced by boot image code in non-PIC mode.
5393 kBootImageLinkTimeAddress,
5394
5395 // Use PC-relative boot image Class* address that will be known at link time.
5396 // Used for boot image classes referenced by boot image code in PIC mode.
5397 kBootImageLinkTimePcRelative,
5398
5399 // Use a known boot image Class* address, embedded in the code by the codegen.
5400 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5401 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5402 // GetIncludePatchInformation().
5403 kBootImageAddress,
5404
5405 // Load from the resolved types array at an absolute address.
5406 // Used for classes outside the boot image referenced by JIT-compiled code.
5407 kDexCacheAddress,
5408
5409 // Load from resolved types array in the dex cache using a PC-relative load.
5410 // Used for classes outside boot image when we know that we can access
5411 // the dex cache arrays using a PC-relative load.
5412 kDexCachePcRelative,
5413
5414 // Load from resolved types array accessed through the class loaded from
5415 // the compiled method's own ArtMethod*. This is the default access type when
5416 // all other types are unavailable.
5417 kDexCacheViaMethod,
5418
5419 kLast = kDexCacheViaMethod
5420 };
5421
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005422 HLoadClass(HCurrentMethod* current_method,
5423 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005424 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005425 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005426 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005427 bool needs_access_check,
5428 bool is_in_dex_cache)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005429 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5430 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005431 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005432 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005433 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005434 // Referrers class should not need access check. We never inline unverified
5435 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005436 DCHECK(!is_referrers_class || !needs_access_check);
5437
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005438 SetPackedField<LoadKindField>(
5439 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005440 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5441 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5442 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005443 }
5444
5445 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5446 DCHECK(HasAddress(load_kind));
5447 load_data_.address = address;
5448 SetLoadKindInternal(load_kind);
5449 }
5450
5451 void SetLoadKindWithTypeReference(LoadKind load_kind,
5452 const DexFile& dex_file,
5453 uint32_t type_index) {
5454 DCHECK(HasTypeReference(load_kind));
5455 DCHECK(IsSameDexFile(dex_file_, dex_file));
5456 DCHECK_EQ(type_index_, type_index);
5457 SetLoadKindInternal(load_kind);
5458 }
5459
5460 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5461 const DexFile& dex_file,
5462 uint32_t element_index) {
5463 DCHECK(HasDexCacheReference(load_kind));
5464 DCHECK(IsSameDexFile(dex_file_, dex_file));
5465 load_data_.dex_cache_element_index = element_index;
5466 SetLoadKindInternal(load_kind);
5467 }
5468
5469 LoadKind GetLoadKind() const {
5470 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005471 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005472
5473 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005474
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005475 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005476
5477 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5478
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005479 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005480
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005481 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005482 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005483 }
5484
Calin Juravle0ba218d2015-05-19 18:46:01 +01005485 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005486 // The entrypoint the code generator is going to call does not do
5487 // clinit of the class.
5488 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005489 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005490 }
5491
5492 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005493 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005494 (!IsReferrersClass() && !IsInDexCache()) ||
5495 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005496 }
5497
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005498
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005499 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005500 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005501 }
5502
Calin Juravleacf735c2015-02-12 15:25:22 +00005503 ReferenceTypeInfo GetLoadedClassRTI() {
5504 return loaded_class_rti_;
5505 }
5506
5507 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5508 // Make sure we only set exact types (the loaded class should never be merged).
5509 DCHECK(rti.IsExact());
5510 loaded_class_rti_ = rti;
5511 }
5512
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005513 uint32_t GetTypeIndex() const { return type_index_; }
5514 const DexFile& GetDexFile() const { return dex_file_; }
5515
5516 uint32_t GetDexCacheElementOffset() const;
5517
5518 uint64_t GetAddress() const {
5519 DCHECK(HasAddress(GetLoadKind()));
5520 return load_data_.address;
5521 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005522
Vladimir Markoa1de9182016-02-25 11:37:38 +00005523 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005524
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005525 static SideEffects SideEffectsForArchRuntimeCalls() {
5526 return SideEffects::CanTriggerGC();
5527 }
5528
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005529 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5531 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5532 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005533
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005534 void MarkInDexCache() {
5535 SetPackedFlag<kFlagIsInDexCache>(true);
5536 DCHECK(!NeedsEnvironment());
5537 RemoveEnvironment();
5538 SetSideEffects(SideEffects::None());
5539 }
5540
5541 void AddSpecialInput(HInstruction* special_input);
5542
5543 using HInstruction::GetInputRecords; // Keep the const version visible.
5544 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5545 return ArrayRef<HUserRecord<HInstruction*>>(
5546 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5547 }
5548
5549 Primitive::Type GetType() const OVERRIDE {
5550 return Primitive::kPrimNot;
5551 }
5552
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005553 DECLARE_INSTRUCTION(LoadClass);
5554
5555 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005556 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005557 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005558 // Whether this instruction must generate the initialization check.
5559 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005560 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005561 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5562 static constexpr size_t kFieldLoadKindSize =
5563 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5564 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005565 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005566 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5567
5568 static bool HasTypeReference(LoadKind load_kind) {
5569 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5570 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5571 load_kind == LoadKind::kDexCacheViaMethod ||
5572 load_kind == LoadKind::kReferrersClass;
5573 }
5574
5575 static bool HasAddress(LoadKind load_kind) {
5576 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5577 }
5578
5579 static bool HasDexCacheReference(LoadKind load_kind) {
5580 return load_kind == LoadKind::kDexCachePcRelative;
5581 }
5582
5583 void SetLoadKindInternal(LoadKind load_kind);
5584
5585 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5586 // For other load kinds it's empty or possibly some architecture-specific instruction
5587 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5588 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005589
5590 const uint16_t type_index_;
5591 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005592
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005593 union {
5594 uint32_t dex_cache_element_index; // Only for dex cache reference.
5595 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5596 } load_data_;
5597
Calin Juravleacf735c2015-02-12 15:25:22 +00005598 ReferenceTypeInfo loaded_class_rti_;
5599
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5601};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005602std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5603
5604// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5605inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5606 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5607 return load_data_.dex_cache_element_index;
5608}
5609
5610// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5611inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
5612 // The special input is used for PC-relative loads on some architectures.
5613 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5614 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5615 DCHECK(special_input_.GetInstruction() == nullptr);
5616 special_input_ = HUserRecord<HInstruction*>(special_input);
5617 special_input->AddUseAt(this, 0);
5618}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005619
Vladimir Marko372f10e2016-05-17 16:30:10 +01005620class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005621 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005622 // Determines how to load the String.
5623 enum class LoadKind {
5624 // Use boot image String* address that will be known at link time.
5625 // Used for boot image strings referenced by boot image code in non-PIC mode.
5626 kBootImageLinkTimeAddress,
5627
5628 // Use PC-relative boot image String* address that will be known at link time.
5629 // Used for boot image strings referenced by boot image code in PIC mode.
5630 kBootImageLinkTimePcRelative,
5631
5632 // Use a known boot image String* address, embedded in the code by the codegen.
5633 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5634 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5635 // GetIncludePatchInformation().
5636 kBootImageAddress,
5637
5638 // Load from the resolved strings array at an absolute address.
5639 // Used for strings outside the boot image referenced by JIT-compiled code.
5640 kDexCacheAddress,
5641
5642 // Load from resolved strings array in the dex cache using a PC-relative load.
5643 // Used for strings outside boot image when we know that we can access
5644 // the dex cache arrays using a PC-relative load.
5645 kDexCachePcRelative,
5646
5647 // Load from resolved strings array accessed through the class loaded from
5648 // the compiled method's own ArtMethod*. This is the default access type when
5649 // all other types are unavailable.
5650 kDexCacheViaMethod,
5651
5652 kLast = kDexCacheViaMethod
5653 };
5654
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005655 HLoadString(HCurrentMethod* current_method,
5656 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005657 const DexFile& dex_file,
5658 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005659 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5660 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005661 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005662 SetPackedFlag<kFlagIsInDexCache>(false);
5663 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5664 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005665 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005666
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005667 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5668 DCHECK(HasAddress(load_kind));
5669 load_data_.address = address;
5670 SetLoadKindInternal(load_kind);
5671 }
5672
5673 void SetLoadKindWithStringReference(LoadKind load_kind,
5674 const DexFile& dex_file,
5675 uint32_t string_index) {
5676 DCHECK(HasStringReference(load_kind));
5677 load_data_.ref.dex_file = &dex_file;
5678 string_index_ = string_index;
5679 SetLoadKindInternal(load_kind);
5680 }
5681
5682 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5683 const DexFile& dex_file,
5684 uint32_t element_index) {
5685 DCHECK(HasDexCacheReference(load_kind));
5686 load_data_.ref.dex_file = &dex_file;
5687 load_data_.ref.dex_cache_element_index = element_index;
5688 SetLoadKindInternal(load_kind);
5689 }
5690
5691 LoadKind GetLoadKind() const {
5692 return GetPackedField<LoadKindField>();
5693 }
5694
5695 const DexFile& GetDexFile() const;
5696
5697 uint32_t GetStringIndex() const {
5698 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5699 return string_index_;
5700 }
5701
5702 uint32_t GetDexCacheElementOffset() const;
5703
5704 uint64_t GetAddress() const {
5705 DCHECK(HasAddress(GetLoadKind()));
5706 return load_data_.address;
5707 }
5708
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005709 bool CanBeMoved() const OVERRIDE { return true; }
5710
Vladimir Marko372f10e2016-05-17 16:30:10 +01005711 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005712
5713 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5714
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005715 // Will call the runtime if we need to load the string through
5716 // the dex cache and the string is not guaranteed to be there yet.
5717 bool NeedsEnvironment() const OVERRIDE {
5718 LoadKind load_kind = GetLoadKind();
5719 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5720 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5721 load_kind == LoadKind::kBootImageAddress) {
5722 return false;
5723 }
5724 return !IsInDexCache();
5725 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005726
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005727 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5728 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5729 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005730
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005731 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005732 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005733
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005734 static SideEffects SideEffectsForArchRuntimeCalls() {
5735 return SideEffects::CanTriggerGC();
5736 }
5737
Vladimir Markoa1de9182016-02-25 11:37:38 +00005738 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5739
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005740 void MarkInDexCache() {
5741 SetPackedFlag<kFlagIsInDexCache>(true);
5742 DCHECK(!NeedsEnvironment());
5743 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005744 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005745 }
5746
Vladimir Marko372f10e2016-05-17 16:30:10 +01005747 void AddSpecialInput(HInstruction* special_input);
5748
5749 using HInstruction::GetInputRecords; // Keep the const version visible.
5750 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5751 return ArrayRef<HUserRecord<HInstruction*>>(
5752 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005753 }
5754
Vladimir Marko372f10e2016-05-17 16:30:10 +01005755 Primitive::Type GetType() const OVERRIDE {
5756 return Primitive::kPrimNot;
5757 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005758
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005759 DECLARE_INSTRUCTION(LoadString);
5760
5761 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005762 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005763 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5764 static constexpr size_t kFieldLoadKindSize =
5765 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5766 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005767 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005768 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005769
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005770 static bool HasStringReference(LoadKind load_kind) {
5771 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5772 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5773 load_kind == LoadKind::kDexCacheViaMethod;
5774 }
5775
5776 static bool HasAddress(LoadKind load_kind) {
5777 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5778 }
5779
5780 static bool HasDexCacheReference(LoadKind load_kind) {
5781 return load_kind == LoadKind::kDexCachePcRelative;
5782 }
5783
5784 void SetLoadKindInternal(LoadKind load_kind);
5785
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005786 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5787 // For other load kinds it's empty or possibly some architecture-specific instruction
5788 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005789 HUserRecord<HInstruction*> special_input_;
5790
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005791 // String index serves also as the hash code and it's also needed for slow-paths,
5792 // so it must not be overwritten with other load data.
5793 uint32_t string_index_;
5794
5795 union {
5796 struct {
5797 const DexFile* dex_file; // For string reference and dex cache reference.
5798 uint32_t dex_cache_element_index; // Only for dex cache reference.
5799 } ref;
5800 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5801 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005802
5803 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5804};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005805std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5806
5807// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5808inline const DexFile& HLoadString::GetDexFile() const {
5809 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5810 << GetLoadKind();
5811 return *load_data_.ref.dex_file;
5812}
5813
5814// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5815inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5816 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5817 return load_data_.ref.dex_cache_element_index;
5818}
5819
5820// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5821inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5822 // The special input is used for PC-relative loads on some architectures.
5823 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5824 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005825 // HLoadString::GetInputRecords() returns an empty array at this point,
5826 // so use the GetInputRecords() from the base class to set the input record.
5827 DCHECK(special_input_.GetInstruction() == nullptr);
5828 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005829 special_input->AddUseAt(this, 0);
5830}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005831
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005832/**
5833 * Performs an initialization check on its Class object input.
5834 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005835class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005836 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005837 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005838 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005839 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005840 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5841 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005842 SetRawInputAt(0, constant);
5843 }
5844
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005845 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005846 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005847 return true;
5848 }
5849
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005850 bool NeedsEnvironment() const OVERRIDE {
5851 // May call runtime to initialize the class.
5852 return true;
5853 }
5854
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005855 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005856
5857 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5858
5859 DECLARE_INSTRUCTION(ClinitCheck);
5860
5861 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005862 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5863};
5864
Vladimir Markofcb503c2016-05-18 12:48:17 +01005865class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005866 public:
5867 HStaticFieldGet(HInstruction* cls,
5868 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005869 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005870 bool is_volatile,
5871 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005872 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005873 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005874 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005875 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005876 : HExpression(field_type,
5877 SideEffects::FieldReadOfType(field_type, is_volatile),
5878 dex_pc),
5879 field_info_(field_offset,
5880 field_type,
5881 is_volatile,
5882 field_idx,
5883 declaring_class_def_index,
5884 dex_file,
5885 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005886 SetRawInputAt(0, cls);
5887 }
5888
Calin Juravle52c48962014-12-16 17:02:57 +00005889
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005890 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005891
Vladimir Marko372f10e2016-05-17 16:30:10 +01005892 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5893 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005894 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005895 }
5896
5897 size_t ComputeHashCode() const OVERRIDE {
5898 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5899 }
5900
Calin Juravle52c48962014-12-16 17:02:57 +00005901 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005902 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5903 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005904 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005905
5906 DECLARE_INSTRUCTION(StaticFieldGet);
5907
5908 private:
5909 const FieldInfo field_info_;
5910
5911 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5912};
5913
Vladimir Markofcb503c2016-05-18 12:48:17 +01005914class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005915 public:
5916 HStaticFieldSet(HInstruction* cls,
5917 HInstruction* value,
5918 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005919 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005920 bool is_volatile,
5921 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005922 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005923 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005924 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005925 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005926 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5927 dex_pc),
5928 field_info_(field_offset,
5929 field_type,
5930 is_volatile,
5931 field_idx,
5932 declaring_class_def_index,
5933 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005934 dex_cache) {
5935 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005936 SetRawInputAt(0, cls);
5937 SetRawInputAt(1, value);
5938 }
5939
Calin Juravle52c48962014-12-16 17:02:57 +00005940 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005941 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5942 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005943 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005944
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005945 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005946 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5947 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005948
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949 DECLARE_INSTRUCTION(StaticFieldSet);
5950
5951 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005952 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5953 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5954 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5955 "Too many packed fields.");
5956
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005957 const FieldInfo field_info_;
5958
5959 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5960};
5961
Vladimir Markofcb503c2016-05-18 12:48:17 +01005962class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005963 public:
5964 HUnresolvedInstanceFieldGet(HInstruction* obj,
5965 Primitive::Type field_type,
5966 uint32_t field_index,
5967 uint32_t dex_pc)
5968 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5969 field_index_(field_index) {
5970 SetRawInputAt(0, obj);
5971 }
5972
5973 bool NeedsEnvironment() const OVERRIDE { return true; }
5974 bool CanThrow() const OVERRIDE { return true; }
5975
5976 Primitive::Type GetFieldType() const { return GetType(); }
5977 uint32_t GetFieldIndex() const { return field_index_; }
5978
5979 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5980
5981 private:
5982 const uint32_t field_index_;
5983
5984 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5985};
5986
Vladimir Markofcb503c2016-05-18 12:48:17 +01005987class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005988 public:
5989 HUnresolvedInstanceFieldSet(HInstruction* obj,
5990 HInstruction* value,
5991 Primitive::Type field_type,
5992 uint32_t field_index,
5993 uint32_t dex_pc)
5994 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005995 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005996 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005997 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005998 SetRawInputAt(0, obj);
5999 SetRawInputAt(1, value);
6000 }
6001
6002 bool NeedsEnvironment() const OVERRIDE { return true; }
6003 bool CanThrow() const OVERRIDE { return true; }
6004
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006006 uint32_t GetFieldIndex() const { return field_index_; }
6007
6008 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6009
6010 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006011 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6012 static constexpr size_t kFieldFieldTypeSize =
6013 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6014 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6015 kFieldFieldType + kFieldFieldTypeSize;
6016 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6017 "Too many packed fields.");
6018 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6019
Calin Juravlee460d1d2015-09-29 04:52:17 +01006020 const uint32_t field_index_;
6021
6022 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6023};
6024
Vladimir Markofcb503c2016-05-18 12:48:17 +01006025class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006026 public:
6027 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6028 uint32_t field_index,
6029 uint32_t dex_pc)
6030 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6031 field_index_(field_index) {
6032 }
6033
6034 bool NeedsEnvironment() const OVERRIDE { return true; }
6035 bool CanThrow() const OVERRIDE { return true; }
6036
6037 Primitive::Type GetFieldType() const { return GetType(); }
6038 uint32_t GetFieldIndex() const { return field_index_; }
6039
6040 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6041
6042 private:
6043 const uint32_t field_index_;
6044
6045 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6046};
6047
Vladimir Markofcb503c2016-05-18 12:48:17 +01006048class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006049 public:
6050 HUnresolvedStaticFieldSet(HInstruction* value,
6051 Primitive::Type field_type,
6052 uint32_t field_index,
6053 uint32_t dex_pc)
6054 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006055 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006057 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006058 SetRawInputAt(0, value);
6059 }
6060
6061 bool NeedsEnvironment() const OVERRIDE { return true; }
6062 bool CanThrow() const OVERRIDE { return true; }
6063
Vladimir Markoa1de9182016-02-25 11:37:38 +00006064 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006065 uint32_t GetFieldIndex() const { return field_index_; }
6066
6067 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6068
6069 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6071 static constexpr size_t kFieldFieldTypeSize =
6072 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6073 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6074 kFieldFieldType + kFieldFieldTypeSize;
6075 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6076 "Too many packed fields.");
6077 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6078
Calin Juravlee460d1d2015-09-29 04:52:17 +01006079 const uint32_t field_index_;
6080
6081 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6082};
6083
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006084// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006085class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006086 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006087 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6088 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006089
David Brazdilbbd733e2015-08-18 17:48:17 +01006090 bool CanBeNull() const OVERRIDE { return false; }
6091
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006092 DECLARE_INSTRUCTION(LoadException);
6093
6094 private:
6095 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6096};
6097
David Brazdilcb1c0552015-08-04 16:22:25 +01006098// Implicit part of move-exception which clears thread-local exception storage.
6099// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006100class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006102 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6103 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006104
6105 DECLARE_INSTRUCTION(ClearException);
6106
6107 private:
6108 DISALLOW_COPY_AND_ASSIGN(HClearException);
6109};
6110
Vladimir Markofcb503c2016-05-18 12:48:17 +01006111class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006112 public:
6113 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006114 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006115 SetRawInputAt(0, exception);
6116 }
6117
6118 bool IsControlFlow() const OVERRIDE { return true; }
6119
6120 bool NeedsEnvironment() const OVERRIDE { return true; }
6121
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006122 bool CanThrow() const OVERRIDE { return true; }
6123
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006124
6125 DECLARE_INSTRUCTION(Throw);
6126
6127 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006128 DISALLOW_COPY_AND_ASSIGN(HThrow);
6129};
6130
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006131/**
6132 * Implementation strategies for the code generator of a HInstanceOf
6133 * or `HCheckCast`.
6134 */
6135enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006136 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137 kExactCheck, // Can do a single class compare.
6138 kClassHierarchyCheck, // Can just walk the super class chain.
6139 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6140 kInterfaceCheck, // No optimization yet when checking against an interface.
6141 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006142 kArrayCheck, // No optimization yet when checking against a generic array.
6143 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006144};
6145
Roland Levillain86503782016-02-11 19:07:30 +00006146std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6147
Vladimir Markofcb503c2016-05-18 12:48:17 +01006148class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006149 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006150 HInstanceOf(HInstruction* object,
6151 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006153 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006154 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006156 dex_pc) {
6157 SetPackedField<TypeCheckKindField>(check_kind);
6158 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006159 SetRawInputAt(0, object);
6160 SetRawInputAt(1, constant);
6161 }
6162
6163 bool CanBeMoved() const OVERRIDE { return true; }
6164
Vladimir Marko372f10e2016-05-17 16:30:10 +01006165 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006166 return true;
6167 }
6168
6169 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006170 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006171 }
6172
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006173 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006174 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6175 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6176 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6177 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006178
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006179 static bool CanCallRuntime(TypeCheckKind check_kind) {
6180 // Mips currently does runtime calls for any other checks.
6181 return check_kind != TypeCheckKind::kExactCheck;
6182 }
6183
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006185 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006186 }
6187
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006188 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006189
6190 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006191 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6192 static constexpr size_t kFieldTypeCheckKindSize =
6193 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6194 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6195 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6196 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6197 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006198
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006199 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6200};
6201
Vladimir Markofcb503c2016-05-18 12:48:17 +01006202class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006203 public:
David Brazdilf5552582015-12-27 13:36:12 +00006204 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006205 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006206 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6207 SetPackedFlag<kFlagUpperCanBeNull>(true);
6208 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006209 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006210 SetRawInputAt(0, input);
6211 }
6212
David Brazdilf5552582015-12-27 13:36:12 +00006213 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006214 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006215 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006216 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006217
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006218 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006219 DCHECK(GetUpperCanBeNull() || !can_be_null);
6220 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006221 }
6222
Vladimir Markoa1de9182016-02-25 11:37:38 +00006223 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006224
Calin Juravleb1498f62015-02-16 13:13:29 +00006225 DECLARE_INSTRUCTION(BoundType);
6226
6227 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006228 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6229 // is false then CanBeNull() cannot be true).
6230 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6231 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6232 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6233 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6234
Calin Juravleb1498f62015-02-16 13:13:29 +00006235 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006236 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6237 // It is used to bound the type in cases like:
6238 // if (x instanceof ClassX) {
6239 // // uper_bound_ will be ClassX
6240 // }
David Brazdilf5552582015-12-27 13:36:12 +00006241 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006242
6243 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6244};
6245
Vladimir Markofcb503c2016-05-18 12:48:17 +01006246class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006247 public:
6248 HCheckCast(HInstruction* object,
6249 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006250 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006251 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006252 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6253 SetPackedField<TypeCheckKindField>(check_kind);
6254 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006255 SetRawInputAt(0, object);
6256 SetRawInputAt(1, constant);
6257 }
6258
6259 bool CanBeMoved() const OVERRIDE { return true; }
6260
Vladimir Marko372f10e2016-05-17 16:30:10 +01006261 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006262 return true;
6263 }
6264
6265 bool NeedsEnvironment() const OVERRIDE {
6266 // Instruction may throw a CheckCastError.
6267 return true;
6268 }
6269
6270 bool CanThrow() const OVERRIDE { return true; }
6271
Vladimir Markoa1de9182016-02-25 11:37:38 +00006272 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6273 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6274 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6275 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006276
6277 DECLARE_INSTRUCTION(CheckCast);
6278
6279 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006280 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6281 static constexpr size_t kFieldTypeCheckKindSize =
6282 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6283 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6284 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6285 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6286 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006287
6288 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006289};
6290
Vladimir Markofcb503c2016-05-18 12:48:17 +01006291class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006293 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006294 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006295 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6296 SetPackedField<BarrierKindField>(barrier_kind);
6297 }
Calin Juravle27df7582015-04-17 19:12:31 +01006298
Vladimir Markoa1de9182016-02-25 11:37:38 +00006299 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006300
6301 DECLARE_INSTRUCTION(MemoryBarrier);
6302
6303 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006304 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6305 static constexpr size_t kFieldBarrierKindSize =
6306 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6307 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6308 kFieldBarrierKind + kFieldBarrierKindSize;
6309 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6310 "Too many packed fields.");
6311 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006312
6313 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6314};
6315
Vladimir Markofcb503c2016-05-18 12:48:17 +01006316class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006317 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006318 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006319 kEnter,
6320 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006321 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006322 };
6323
6324 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006325 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006326 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6327 dex_pc) {
6328 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006329 SetRawInputAt(0, object);
6330 }
6331
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006332 // Instruction may go into runtime, so we need an environment.
6333 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006334
6335 bool CanThrow() const OVERRIDE {
6336 // Verifier guarantees that monitor-exit cannot throw.
6337 // This is important because it allows the HGraphBuilder to remove
6338 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6339 return IsEnter();
6340 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006341
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6343 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006344
6345 DECLARE_INSTRUCTION(MonitorOperation);
6346
Calin Juravle52c48962014-12-16 17:02:57 +00006347 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006348 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6349 static constexpr size_t kFieldOperationKindSize =
6350 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6351 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6352 kFieldOperationKind + kFieldOperationKindSize;
6353 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6354 "Too many packed fields.");
6355 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006356
6357 private:
6358 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6359};
6360
Vladimir Markofcb503c2016-05-18 12:48:17 +01006361class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006362 public:
6363 HSelect(HInstruction* condition,
6364 HInstruction* true_value,
6365 HInstruction* false_value,
6366 uint32_t dex_pc)
6367 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6368 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6369
6370 // First input must be `true_value` or `false_value` to allow codegens to
6371 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6372 // that architectures which implement HSelect as a conditional move also
6373 // will not need to invert the condition.
6374 SetRawInputAt(0, false_value);
6375 SetRawInputAt(1, true_value);
6376 SetRawInputAt(2, condition);
6377 }
6378
6379 HInstruction* GetFalseValue() const { return InputAt(0); }
6380 HInstruction* GetTrueValue() const { return InputAt(1); }
6381 HInstruction* GetCondition() const { return InputAt(2); }
6382
6383 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006384 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6385 return true;
6386 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006387
6388 bool CanBeNull() const OVERRIDE {
6389 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6390 }
6391
6392 DECLARE_INSTRUCTION(Select);
6393
6394 private:
6395 DISALLOW_COPY_AND_ASSIGN(HSelect);
6396};
6397
Vladimir Markof9f64412015-09-02 14:05:49 +01006398class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006399 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006400 MoveOperands(Location source,
6401 Location destination,
6402 Primitive::Type type,
6403 HInstruction* instruction)
6404 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006405
6406 Location GetSource() const { return source_; }
6407 Location GetDestination() const { return destination_; }
6408
6409 void SetSource(Location value) { source_ = value; }
6410 void SetDestination(Location value) { destination_ = value; }
6411
6412 // The parallel move resolver marks moves as "in-progress" by clearing the
6413 // destination (but not the source).
6414 Location MarkPending() {
6415 DCHECK(!IsPending());
6416 Location dest = destination_;
6417 destination_ = Location::NoLocation();
6418 return dest;
6419 }
6420
6421 void ClearPending(Location dest) {
6422 DCHECK(IsPending());
6423 destination_ = dest;
6424 }
6425
6426 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006427 DCHECK(source_.IsValid() || destination_.IsInvalid());
6428 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006429 }
6430
6431 // True if this blocks a move from the given location.
6432 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006433 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006434 }
6435
6436 // A move is redundant if it's been eliminated, if its source and
6437 // destination are the same, or if its destination is unneeded.
6438 bool IsRedundant() const {
6439 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6440 }
6441
6442 // We clear both operands to indicate move that's been eliminated.
6443 void Eliminate() {
6444 source_ = destination_ = Location::NoLocation();
6445 }
6446
6447 bool IsEliminated() const {
6448 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6449 return source_.IsInvalid();
6450 }
6451
Alexey Frunze4dda3372015-06-01 18:31:49 -07006452 Primitive::Type GetType() const { return type_; }
6453
Nicolas Geoffray90218252015-04-15 11:56:51 +01006454 bool Is64BitMove() const {
6455 return Primitive::Is64BitType(type_);
6456 }
6457
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006458 HInstruction* GetInstruction() const { return instruction_; }
6459
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006460 private:
6461 Location source_;
6462 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006463 // The type this move is for.
6464 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006465 // The instruction this move is assocatied with. Null when this move is
6466 // for moving an input in the expected locations of user (including a phi user).
6467 // This is only used in debug mode, to ensure we do not connect interval siblings
6468 // in the same parallel move.
6469 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006470};
6471
Roland Levillainc9285912015-12-18 10:38:42 +00006472std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6473
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006474static constexpr size_t kDefaultNumberOfMoves = 4;
6475
Vladimir Markofcb503c2016-05-18 12:48:17 +01006476class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006477 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006478 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006479 : HTemplateInstruction(SideEffects::None(), dex_pc),
6480 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6481 moves_.reserve(kDefaultNumberOfMoves);
6482 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006483
Nicolas Geoffray90218252015-04-15 11:56:51 +01006484 void AddMove(Location source,
6485 Location destination,
6486 Primitive::Type type,
6487 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006488 DCHECK(source.IsValid());
6489 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006490 if (kIsDebugBuild) {
6491 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006492 for (const MoveOperands& move : moves_) {
6493 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006494 // Special case the situation where the move is for the spill slot
6495 // of the instruction.
6496 if ((GetPrevious() == instruction)
6497 || ((GetPrevious() == nullptr)
6498 && instruction->IsPhi()
6499 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006500 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006501 << "Doing parallel moves for the same instruction.";
6502 } else {
6503 DCHECK(false) << "Doing parallel moves for the same instruction.";
6504 }
6505 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006506 }
6507 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006508 for (const MoveOperands& move : moves_) {
6509 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006510 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006511 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006512 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006513 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006514 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006515 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006516 }
6517
Vladimir Marko225b6462015-09-28 12:17:40 +01006518 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006519 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006520 }
6521
Vladimir Marko225b6462015-09-28 12:17:40 +01006522 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006523
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006524 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006525
6526 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006527 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006528
6529 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6530};
6531
Mark Mendell0616ae02015-04-17 12:49:27 -04006532} // namespace art
6533
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006534#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6535#include "nodes_shared.h"
6536#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006537#ifdef ART_ENABLE_CODEGEN_arm
6538#include "nodes_arm.h"
6539#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006540#ifdef ART_ENABLE_CODEGEN_arm64
6541#include "nodes_arm64.h"
6542#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006543#ifdef ART_ENABLE_CODEGEN_x86
6544#include "nodes_x86.h"
6545#endif
6546
6547namespace art {
6548
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006549class HGraphVisitor : public ValueObject {
6550 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006551 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6552 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006554 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006555 virtual void VisitBasicBlock(HBasicBlock* block);
6556
Roland Levillain633021e2014-10-01 14:12:25 +01006557 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006558 void VisitInsertionOrder();
6559
Roland Levillain633021e2014-10-01 14:12:25 +01006560 // Visit the graph following dominator tree reverse post-order.
6561 void VisitReversePostOrder();
6562
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006563 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006564
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006565 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006566#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006567 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6568
6569 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6570
6571#undef DECLARE_VISIT_INSTRUCTION
6572
6573 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006574 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006575
6576 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6577};
6578
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006579class HGraphDelegateVisitor : public HGraphVisitor {
6580 public:
6581 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6582 virtual ~HGraphDelegateVisitor() {}
6583
6584 // Visit functions that delegate to to super class.
6585#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006586 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006587
6588 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6589
6590#undef DECLARE_VISIT_INSTRUCTION
6591
6592 private:
6593 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6594};
6595
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006596class HInsertionOrderIterator : public ValueObject {
6597 public:
6598 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6599
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006600 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006601 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006602 void Advance() { ++index_; }
6603
6604 private:
6605 const HGraph& graph_;
6606 size_t index_;
6607
6608 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6609};
6610
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006611class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006612 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006613 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6614 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006615 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006616 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006617
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006618 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6619 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006620 void Advance() { ++index_; }
6621
6622 private:
6623 const HGraph& graph_;
6624 size_t index_;
6625
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006626 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006627};
6628
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006629class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006630 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006631 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006632 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006633 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006634 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006635 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006636
6637 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006638 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006639 void Advance() { --index_; }
6640
6641 private:
6642 const HGraph& graph_;
6643 size_t index_;
6644
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006645 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006646};
6647
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006648class HLinearPostOrderIterator : public ValueObject {
6649 public:
6650 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006651 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006652
6653 bool Done() const { return index_ == 0; }
6654
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006655 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006656
6657 void Advance() {
6658 --index_;
6659 DCHECK_GE(index_, 0U);
6660 }
6661
6662 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006663 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006664 size_t index_;
6665
6666 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6667};
6668
6669class HLinearOrderIterator : public ValueObject {
6670 public:
6671 explicit HLinearOrderIterator(const HGraph& graph)
6672 : order_(graph.GetLinearOrder()), index_(0) {}
6673
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006674 bool Done() const { return index_ == order_.size(); }
6675 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006676 void Advance() { ++index_; }
6677
6678 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006679 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006680 size_t index_;
6681
6682 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6683};
6684
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006685// Iterator over the blocks that art part of the loop. Includes blocks part
6686// of an inner loop. The order in which the blocks are iterated is on their
6687// block id.
6688class HBlocksInLoopIterator : public ValueObject {
6689 public:
6690 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6691 : blocks_in_loop_(info.GetBlocks()),
6692 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6693 index_(0) {
6694 if (!blocks_in_loop_.IsBitSet(index_)) {
6695 Advance();
6696 }
6697 }
6698
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006699 bool Done() const { return index_ == blocks_.size(); }
6700 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006701 void Advance() {
6702 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006703 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006704 if (blocks_in_loop_.IsBitSet(index_)) {
6705 break;
6706 }
6707 }
6708 }
6709
6710 private:
6711 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006712 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006713 size_t index_;
6714
6715 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6716};
6717
Mingyao Yang3584bce2015-05-19 16:01:59 -07006718// Iterator over the blocks that art part of the loop. Includes blocks part
6719// of an inner loop. The order in which the blocks are iterated is reverse
6720// post order.
6721class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6722 public:
6723 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6724 : blocks_in_loop_(info.GetBlocks()),
6725 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6726 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006727 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006728 Advance();
6729 }
6730 }
6731
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006732 bool Done() const { return index_ == blocks_.size(); }
6733 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006734 void Advance() {
6735 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006736 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6737 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006738 break;
6739 }
6740 }
6741 }
6742
6743 private:
6744 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006745 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006746 size_t index_;
6747
6748 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6749};
6750
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006751inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006752 if (constant->IsIntConstant()) {
6753 return constant->AsIntConstant()->GetValue();
6754 } else if (constant->IsLongConstant()) {
6755 return constant->AsLongConstant()->GetValue();
6756 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006757 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006758 return 0;
6759 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006760}
6761
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006762#define INSTRUCTION_TYPE_CHECK(type, super) \
6763 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6764 inline const H##type* HInstruction::As##type() const { \
6765 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6766 } \
6767 inline H##type* HInstruction::As##type() { \
6768 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6769 }
6770
6771 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6772#undef INSTRUCTION_TYPE_CHECK
6773
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006774// Create space in `blocks` for adding `number_of_new_blocks` entries
6775// starting at location `at`. Blocks after `at` are moved accordingly.
6776inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6777 size_t number_of_new_blocks,
6778 size_t after) {
6779 DCHECK_LT(after, blocks->size());
6780 size_t old_size = blocks->size();
6781 size_t new_size = old_size + number_of_new_blocks;
6782 blocks->resize(new_size);
6783 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6784}
6785
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006786} // namespace art
6787
6788#endif // ART_COMPILER_OPTIMIZING_NODES_H_