blob: 23ac457568a13c014616e03da0c773d412c6bf66 [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
David Brazdil94ab38f2016-06-21 17:48:19 +0100175 static ReferenceTypeInfo Create(TypeHandle type_handle) SHARED_REQUIRES(Locks::mutator_lock_) {
176 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
177 }
178
Vladimir Markoa1de9182016-02-25 11:37:38 +0000179 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000180 return ReferenceTypeInfo(type_handle, is_exact);
181 }
182
183 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
184
Vladimir Markof39745e2016-01-26 12:16:55 +0000185 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000186 return handle.GetReference() != nullptr;
187 }
188
Vladimir Marko456307a2016-04-19 14:12:13 +0000189 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000190 return IsValidHandle(type_handle_);
191 }
192
193 bool IsExact() const { return is_exact_; }
194
195 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
196 DCHECK(IsValid());
197 return GetTypeHandle()->IsObjectClass();
198 }
199
200 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
201 DCHECK(IsValid());
202 return GetTypeHandle()->IsStringClass();
203 }
204
205 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
206 DCHECK(IsValid());
207 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
208 }
209
210 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
211 DCHECK(IsValid());
212 return GetTypeHandle()->IsInterface();
213 }
214
215 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
216 DCHECK(IsValid());
217 return GetTypeHandle()->IsArrayClass();
218 }
219
220 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
221 DCHECK(IsValid());
222 return GetTypeHandle()->IsPrimitiveArray();
223 }
224
225 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
226 DCHECK(IsValid());
227 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
228 }
229
230 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
231 DCHECK(IsValid());
232 if (!IsExact()) return false;
233 if (!IsArrayClass()) return false;
234 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
235 }
236
237 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
238 DCHECK(IsValid());
239 if (!IsExact()) return false;
240 if (!IsArrayClass()) return false;
241 if (!rti.IsArrayClass()) return false;
242 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
243 rti.GetTypeHandle()->GetComponentType());
244 }
245
246 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
247
248 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
249 DCHECK(IsValid());
250 DCHECK(rti.IsValid());
251 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
252 }
253
254 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
255 DCHECK(IsValid());
256 DCHECK(rti.IsValid());
257 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
258 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
259 }
260
261 // Returns true if the type information provide the same amount of details.
262 // Note that it does not mean that the instructions have the same actual type
263 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000264 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000265 if (!IsValid() && !rti.IsValid()) {
266 // Invalid types are equal.
267 return true;
268 }
269 if (!IsValid() || !rti.IsValid()) {
270 // One is valid, the other not.
271 return false;
272 }
273 return IsExact() == rti.IsExact()
274 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
275 }
276
277 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000278 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
279 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
280 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000281
282 // The class of the object.
283 TypeHandle type_handle_;
284 // Whether or not the type is exact or a superclass of the actual type.
285 // Whether or not we have any information about this type.
286 bool is_exact_;
287};
288
289std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
290
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000291// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100292class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000293 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100294 HGraph(ArenaAllocator* arena,
295 const DexFile& dex_file,
296 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100297 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100299 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100300 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000301 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100302 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000303 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100304 blocks_(arena->Adapter(kArenaAllocBlockList)),
305 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
306 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700307 entry_block_(nullptr),
308 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100309 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100310 number_of_vregs_(0),
311 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000312 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400313 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000314 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000315 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000316 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000317 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 dex_file_(dex_file),
319 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100320 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100321 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100322 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000324 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100325 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
326 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
327 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
328 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000329 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000330 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
331 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100332 blocks_.reserve(kDefaultNumberOfBlocks);
333 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334
David Brazdilbadd8262016-02-02 16:28:56 +0000335 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
336 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
337
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000338 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100339 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
340
David Brazdil69ba7b72015-06-23 18:27:30 +0100341 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000342 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100343
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000344 HBasicBlock* GetEntryBlock() const { return entry_block_; }
345 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100346 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000347
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000348 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
349 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000350
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100352
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100353 void ComputeDominanceInformation();
354 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000355 void ClearLoopInformation();
356 void FindBackEdges(ArenaBitVector* visited);
357 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100358 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100359 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360
David Brazdil4833f5a2015-12-16 10:37:39 +0000361 // Analyze all natural loops in this graph. Returns a code specifying that it
362 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000363 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000364 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365
David Brazdilffee3d32015-07-06 11:48:53 +0100366 // Iterate over blocks to compute try block membership. Needs reverse post
367 // order and loop information.
368 void ComputeTryBlockInformation();
369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000370 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000371 // Returns the instruction to replace the invoke expression or null if the
372 // invoke is for a void method. Note that the caller is responsible for replacing
373 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000374 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000375
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000376 // Update the loop and try membership of `block`, which was spawned from `reference`.
377 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
378 // should be the new back edge.
379 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
380 HBasicBlock* reference,
381 bool replace_if_back_edge);
382
Mingyao Yang3584bce2015-05-19 16:01:59 -0700383 // Need to add a couple of blocks to test if the loop body is entered and
384 // put deoptimization instructions, etc.
385 void TransformLoopHeaderForBCE(HBasicBlock* header);
386
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000387 // Removes `block` from the graph. Assumes `block` has been disconnected from
388 // other blocks and has no instructions or phis.
389 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000390
David Brazdilfc6a86a2015-06-26 10:33:45 +0000391 // Splits the edge between `block` and `successor` while preserving the
392 // indices in the predecessor/successor lists. If there are multiple edges
393 // between the blocks, the lowest indices are used.
394 // Returns the new block which is empty and has the same dex pc as `successor`.
395 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
396
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100397 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
398 void SimplifyLoop(HBasicBlock* header);
399
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000400 int32_t GetNextInstructionId() {
401 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000402 return current_instruction_id_++;
403 }
404
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000405 int32_t GetCurrentInstructionId() const {
406 return current_instruction_id_;
407 }
408
409 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000410 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000411 current_instruction_id_ = id;
412 }
413
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100414 uint16_t GetMaximumNumberOfOutVRegs() const {
415 return maximum_number_of_out_vregs_;
416 }
417
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
419 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100420 }
421
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100422 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
423 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
424 }
425
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000426 void UpdateTemporariesVRegSlots(size_t slots) {
427 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100428 }
429
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000430 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100431 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433 }
434
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100435 void SetNumberOfVRegs(uint16_t number_of_vregs) {
436 number_of_vregs_ = number_of_vregs;
437 }
438
439 uint16_t GetNumberOfVRegs() const {
440 return number_of_vregs_;
441 }
442
443 void SetNumberOfInVRegs(uint16_t value) {
444 number_of_in_vregs_ = value;
445 }
446
David Brazdildee58d62016-04-07 09:54:26 +0000447 uint16_t GetNumberOfInVRegs() const {
448 return number_of_in_vregs_;
449 }
450
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100451 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100452 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100453 return number_of_vregs_ - number_of_in_vregs_;
454 }
455
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100456 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100457 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100458 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100459
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100460 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100461 return linear_order_;
462 }
463
Mark Mendell1152c922015-04-24 17:06:35 -0400464 bool HasBoundsChecks() const {
465 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800466 }
467
Mark Mendell1152c922015-04-24 17:06:35 -0400468 void SetHasBoundsChecks(bool value) {
469 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800470 }
471
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100472 bool ShouldGenerateConstructorBarrier() const {
473 return should_generate_constructor_barrier_;
474 }
475
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000476 bool IsDebuggable() const { return debuggable_; }
477
David Brazdil8d5b8b22015-03-24 10:51:52 +0000478 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000479 // already, it is created and inserted into the graph. This method is only for
480 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600481 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000482
483 // TODO: This is problematic for the consistency of reference type propagation
484 // because it can be created anytime after the pass and thus it will be left
485 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600486 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000487
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600488 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
489 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000490 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600491 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
492 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000493 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600494 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
495 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000496 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600497 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
498 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000499 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000500
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100501 HCurrentMethod* GetCurrentMethod();
502
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100503 const DexFile& GetDexFile() const {
504 return dex_file_;
505 }
506
507 uint32_t GetMethodIdx() const {
508 return method_idx_;
509 }
510
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100511 InvokeType GetInvokeType() const {
512 return invoke_type_;
513 }
514
Mark Mendellc4701932015-04-10 13:18:51 -0400515 InstructionSet GetInstructionSet() const {
516 return instruction_set_;
517 }
518
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000519 bool IsCompilingOsr() const { return osr_; }
520
David Brazdil77a48ae2015-09-15 12:34:04 +0000521 bool HasTryCatch() const { return has_try_catch_; }
522 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100523
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000524 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
525 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
526
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100527 ArtMethod* GetArtMethod() const { return art_method_; }
528 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
529
Mark Mendellf6529172015-11-17 11:16:56 -0500530 // Returns an instruction with the opposite boolean value from 'cond'.
531 // The instruction has been inserted into the graph, either as a constant, or
532 // before cursor.
533 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
534
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000535 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
536
David Brazdil2d7352b2015-04-20 14:52:42 +0100537 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000538 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100539 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000540
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000541 template <class InstructionType, typename ValueType>
542 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600543 ArenaSafeMap<ValueType, InstructionType*>* cache,
544 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000545 // Try to find an existing constant of the given value.
546 InstructionType* constant = nullptr;
547 auto cached_constant = cache->find(value);
548 if (cached_constant != cache->end()) {
549 constant = cached_constant->second;
550 }
551
552 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100553 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000554 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600555 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000556 cache->Overwrite(value, constant);
557 InsertConstant(constant);
558 }
559 return constant;
560 }
561
David Brazdil8d5b8b22015-03-24 10:51:52 +0000562 void InsertConstant(HConstant* instruction);
563
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000564 // Cache a float constant into the graph. This method should only be
565 // called by the SsaBuilder when creating "equivalent" instructions.
566 void CacheFloatConstant(HFloatConstant* constant);
567
568 // See CacheFloatConstant comment.
569 void CacheDoubleConstant(HDoubleConstant* constant);
570
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000571 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572
573 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100574 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100576 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100577 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100579 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100580 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100581
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000582 HBasicBlock* entry_block_;
583 HBasicBlock* exit_block_;
584
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100585 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100586 uint16_t maximum_number_of_out_vregs_;
587
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100588 // The number of virtual registers in this method. Contains the parameters.
589 uint16_t number_of_vregs_;
590
591 // The number of virtual registers used by parameters of this method.
592 uint16_t number_of_in_vregs_;
593
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000594 // Number of vreg size slots that the temporaries use (used in baseline compiler).
595 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100596
Mark Mendell1152c922015-04-24 17:06:35 -0400597 // Has bounds checks. We can totally skip BCE if it's false.
598 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800599
David Brazdil77a48ae2015-09-15 12:34:04 +0000600 // Flag whether there are any try/catch blocks in the graph. We will skip
601 // try/catch-related passes if false.
602 bool has_try_catch_;
603
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000604 // Flag whether there are any irreducible loops in the graph.
605 bool has_irreducible_loops_;
606
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000607 // Indicates whether the graph should be compiled in a way that
608 // ensures full debuggability. If false, we can apply more
609 // aggressive optimizations that may limit the level of debugging.
610 const bool debuggable_;
611
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000612 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000613 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000614
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100615 // The dex file from which the method is from.
616 const DexFile& dex_file_;
617
618 // The method index in the dex file.
619 const uint32_t method_idx_;
620
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100621 // If inlined, this encodes how the callee is being invoked.
622 const InvokeType invoke_type_;
623
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100624 // Whether the graph has been transformed to SSA form. Only used
625 // in debug mode to ensure we are not using properties only valid
626 // for non-SSA form (like the number of temporaries).
627 bool in_ssa_form_;
628
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100629 const bool should_generate_constructor_barrier_;
630
Mathieu Chartiere401d142015-04-22 13:56:20 -0700631 const InstructionSet instruction_set_;
632
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000633 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000634 HNullConstant* cached_null_constant_;
635 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000636 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000637 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000638 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000639
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100640 HCurrentMethod* cached_current_method_;
641
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100642 // The ArtMethod this graph is for. Note that for AOT, it may be null,
643 // for example for methods whose declaring class could not be resolved
644 // (such as when the superclass could not be found).
645 ArtMethod* art_method_;
646
David Brazdil4833f5a2015-12-16 10:37:39 +0000647 // Keep the RTI of inexact Object to avoid having to pass stack handle
648 // collection pointer to passes which may create NullConstant.
649 ReferenceTypeInfo inexact_object_rti_;
650
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000651 // Whether we are compiling this graph for on stack replacement: this will
652 // make all loops seen as irreducible and emit special stack maps to mark
653 // compiled code entries which the interpreter can directly jump to.
654 const bool osr_;
655
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000656 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100657 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000658 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000659 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660 DISALLOW_COPY_AND_ASSIGN(HGraph);
661};
662
Vladimir Markof9f64412015-09-02 14:05:49 +0100663class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000664 public:
665 HLoopInformation(HBasicBlock* header, HGraph* graph)
666 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100667 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000668 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100669 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100671 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000672 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100673 back_edges_.reserve(kDefaultNumberOfBackEdges);
674 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100675
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000676 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100677 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000678
679 void Dump(std::ostream& os);
680
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 HBasicBlock* GetHeader() const {
682 return header_;
683 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000684
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000685 void SetHeader(HBasicBlock* block) {
686 header_ = block;
687 }
688
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100689 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
690 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
691 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
692
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100694 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 }
696
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100698 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 }
700
David Brazdil46e2a392015-03-16 17:31:52 +0000701 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 }
704
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000705 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100706 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000707 }
708
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100709 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100711 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100712 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100713 }
714
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100715 // Returns the lifetime position of the back edge that has the
716 // greatest lifetime position.
717 size_t GetLifetimeEnd() const;
718
719 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100721 }
722
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000723 // Finds blocks that are part of this loop.
724 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100725
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100726 // Returns whether this loop information contains `block`.
727 // Note that this loop information *must* be populated before entering this function.
728 bool Contains(const HBasicBlock& block) const;
729
730 // Returns whether this loop information is an inner loop of `other`.
731 // Note that `other` *must* be populated before entering this function.
732 bool IsIn(const HLoopInformation& other) const;
733
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800734 // Returns true if instruction is not defined within this loop.
735 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700736
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 const ArenaBitVector& GetBlocks() const { return blocks_; }
738
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000739 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000740 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000741
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000742 void ClearAllBlocks() {
743 blocks_.ClearAllBits();
744 }
745
David Brazdil3f4a5222016-05-06 12:46:21 +0100746 bool HasBackEdgeNotDominatedByHeader() const;
747
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100748 bool IsPopulated() const {
749 return blocks_.GetHighestBitSet() != -1;
750 }
751
Anton Shaminf89381f2016-05-16 16:44:13 +0600752 bool DominatesAllBackEdges(HBasicBlock* block);
753
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000754 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100755 // Internal recursive implementation of `Populate`.
756 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100757 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000759 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100760 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000761 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100762 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100763 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000765
766 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
767};
768
David Brazdilec16f792015-08-19 15:04:01 +0100769// Stores try/catch information for basic blocks.
770// Note that HGraph is constructed so that catch blocks cannot simultaneously
771// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100772class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100773 public:
774 // Try block information constructor.
775 explicit TryCatchInformation(const HTryBoundary& try_entry)
776 : try_entry_(&try_entry),
777 catch_dex_file_(nullptr),
778 catch_type_index_(DexFile::kDexNoIndex16) {
779 DCHECK(try_entry_ != nullptr);
780 }
781
782 // Catch block information constructor.
783 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
784 : try_entry_(nullptr),
785 catch_dex_file_(&dex_file),
786 catch_type_index_(catch_type_index) {}
787
788 bool IsTryBlock() const { return try_entry_ != nullptr; }
789
790 const HTryBoundary& GetTryEntry() const {
791 DCHECK(IsTryBlock());
792 return *try_entry_;
793 }
794
795 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
796
797 bool IsCatchAllTypeIndex() const {
798 DCHECK(IsCatchBlock());
799 return catch_type_index_ == DexFile::kDexNoIndex16;
800 }
801
802 uint16_t GetCatchTypeIndex() const {
803 DCHECK(IsCatchBlock());
804 return catch_type_index_;
805 }
806
807 const DexFile& GetCatchDexFile() const {
808 DCHECK(IsCatchBlock());
809 return *catch_dex_file_;
810 }
811
812 private:
813 // One of possibly several TryBoundary instructions entering the block's try.
814 // Only set for try blocks.
815 const HTryBoundary* try_entry_;
816
817 // Exception type information. Only set for catch blocks.
818 const DexFile* catch_dex_file_;
819 const uint16_t catch_type_index_;
820};
821
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100822static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100823static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100824
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000825// A block in a method. Contains the list of instructions represented
826// as a double linked list. Each block knows its predecessors and
827// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100828
Vladimir Markof9f64412015-09-02 14:05:49 +0100829class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600831 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000833 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
834 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000835 loop_information_(nullptr),
836 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000837 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100838 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100839 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100840 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000841 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000842 try_catch_information_(nullptr) {
843 predecessors_.reserve(kDefaultNumberOfPredecessors);
844 successors_.reserve(kDefaultNumberOfSuccessors);
845 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
846 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000847
Vladimir Marko60584552015-09-03 13:35:12 +0000848 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850 }
851
Vladimir Marko60584552015-09-03 13:35:12 +0000852 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100853 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000854 }
855
David Brazdild26a4112015-11-10 11:07:31 +0000856 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
857 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
858
Vladimir Marko60584552015-09-03 13:35:12 +0000859 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
860 return ContainsElement(successors_, block, start_from);
861 }
862
863 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100864 return dominated_blocks_;
865 }
866
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100867 bool IsEntryBlock() const {
868 return graph_->GetEntryBlock() == this;
869 }
870
871 bool IsExitBlock() const {
872 return graph_->GetExitBlock() == this;
873 }
874
David Brazdil46e2a392015-03-16 17:31:52 +0000875 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000876 bool IsSingleTryBoundary() const;
877
878 // Returns true if this block emits nothing but a jump.
879 bool IsSingleJump() const {
880 HLoopInformation* loop_info = GetLoopInformation();
881 return (IsSingleGoto() || IsSingleTryBoundary())
882 // Back edges generate a suspend check.
883 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
884 }
David Brazdil46e2a392015-03-16 17:31:52 +0000885
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000886 void AddBackEdge(HBasicBlock* back_edge) {
887 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100890 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000891 loop_information_->AddBackEdge(back_edge);
892 }
893
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100897 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000898 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600899 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000901 HBasicBlock* GetDominator() const { return dominator_; }
902 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000903 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
904
905 void RemoveDominatedBlock(HBasicBlock* block) {
906 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100907 }
Vladimir Marko60584552015-09-03 13:35:12 +0000908
909 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
910 ReplaceElement(dominated_blocks_, existing, new_block);
911 }
912
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100913 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000914
915 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100916 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000917 }
918
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100919 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
920 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100921 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100922 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100923 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
924 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000925
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000926 HInstruction* GetFirstInstructionDisregardMoves() const;
927
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000929 successors_.push_back(block);
930 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000931 }
932
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100933 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
934 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100935 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000936 new_block->predecessors_.push_back(this);
937 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000938 }
939
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000940 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
941 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000942 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000943 new_block->successors_.push_back(this);
944 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000945 }
946
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100947 // Insert `this` between `predecessor` and `successor. This method
948 // preserves the indicies, and will update the first edge found between
949 // `predecessor` and `successor`.
950 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
951 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100952 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000953 successor->predecessors_[predecessor_index] = this;
954 predecessor->successors_[successor_index] = this;
955 successors_.push_back(successor);
956 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100957 }
958
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100959 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000960 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100961 }
962
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000963 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000965 }
966
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100967 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 }
970
971 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000972 predecessors_.push_back(block);
973 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100974 }
975
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100976 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000977 DCHECK_EQ(predecessors_.size(), 2u);
978 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100979 }
980
David Brazdil769c9e52015-04-27 13:54:09 +0100981 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000982 DCHECK_EQ(successors_.size(), 2u);
983 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100984 }
985
David Brazdilfc6a86a2015-06-26 10:33:45 +0000986 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000987 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100988 }
989
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000991 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100992 }
993
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000995 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100996 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997 }
998
999 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001000 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001001 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001002 }
1003
1004 // Returns whether the first occurrence of `predecessor` in the list of
1005 // predecessors is at index `idx`.
1006 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001007 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001008 return GetPredecessorIndexOf(predecessor) == idx;
1009 }
1010
David Brazdild7558da2015-09-22 13:04:14 +01001011 // Create a new block between this block and its predecessors. The new block
1012 // is added to the graph, all predecessor edges are relinked to it and an edge
1013 // is created to `this`. Returns the new empty block. Reverse post order or
1014 // loop and try/catch information are not updated.
1015 HBasicBlock* CreateImmediateDominator();
1016
David Brazdilfc6a86a2015-06-26 10:33:45 +00001017 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001018 // created, latter block. Note that this method will add the block to the
1019 // graph, create a Goto at the end of the former block and will create an edge
1020 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001021 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001022 HBasicBlock* SplitBefore(HInstruction* cursor);
1023
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001024 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 // created block. Note that this method just updates raw block information,
1026 // like predecessors, successors, dominators, and instruction list. It does not
1027 // update the graph, reverse post order, loop information, nor make sure the
1028 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001029 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1030
1031 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1032 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001033
1034 // Merge `other` at the end of `this`. Successors and dominated blocks of
1035 // `other` are changed to be successors and dominated blocks of `this`. Note
1036 // that this method does not update the graph, reverse post order, loop
1037 // information, nor make sure the blocks are consistent (for example ending
1038 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001039 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001040
1041 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1042 // of `this` are moved to `other`.
1043 // Note that this method does not update the graph, reverse post order, loop
1044 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001045 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001046 void ReplaceWith(HBasicBlock* other);
1047
David Brazdil2d7352b2015-04-20 14:52:42 +01001048 // Merge `other` at the end of `this`. This method updates loops, reverse post
1049 // order, links to predecessors, successors, dominators and deletes the block
1050 // from the graph. The two blocks must be successive, i.e. `this` the only
1051 // predecessor of `other` and vice versa.
1052 void MergeWith(HBasicBlock* other);
1053
1054 // Disconnects `this` from all its predecessors, successors and dominator,
1055 // removes it from all loops it is included in and eventually from the graph.
1056 // The block must not dominate any other block. Predecessors and successors
1057 // are safely updated.
1058 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001059
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001061 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001062 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001063 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001064 // Replace instruction `initial` with `replacement` within this block.
1065 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1066 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001067 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001068 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001069 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1070 // instruction list. With 'ensure_safety' set to true, it verifies that the
1071 // instruction is not in use and removes it from the use lists of its inputs.
1072 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1073 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001074 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075
1076 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001077 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001078 }
1079
Roland Levillain6b879dd2014-09-22 17:13:44 +01001080 bool IsLoopPreHeaderFirstPredecessor() const {
1081 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001082 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001083 }
1084
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001085 bool IsFirstPredecessorBackEdge() const {
1086 DCHECK(IsLoopHeader());
1087 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1088 }
1089
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001090 HLoopInformation* GetLoopInformation() const {
1091 return loop_information_;
1092 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001095 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001097 void SetInLoop(HLoopInformation* info) {
1098 if (IsLoopHeader()) {
1099 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001100 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 loop_information_ = info;
1102 } else if (loop_information_->Contains(*info->GetHeader())) {
1103 // Block is currently part of an outer loop. Make it part of this inner loop.
1104 // Note that a non loop header having a loop information means this loop information
1105 // has already been populated
1106 loop_information_ = info;
1107 } else {
1108 // Block is part of an inner loop. Do not update the loop information.
1109 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1110 // at this point, because this method is being called while populating `info`.
1111 }
1112 }
1113
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 // Raw update of the loop information.
1115 void SetLoopInformation(HLoopInformation* info) {
1116 loop_information_ = info;
1117 }
1118
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001119 bool IsInLoop() const { return loop_information_ != nullptr; }
1120
David Brazdilec16f792015-08-19 15:04:01 +01001121 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1122
1123 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1124 try_catch_information_ = try_catch_information;
1125 }
1126
1127 bool IsTryBlock() const {
1128 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1129 }
1130
1131 bool IsCatchBlock() const {
1132 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1133 }
David Brazdilffee3d32015-07-06 11:48:53 +01001134
1135 // Returns the try entry that this block's successors should have. They will
1136 // be in the same try, unless the block ends in a try boundary. In that case,
1137 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001138 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001139
David Brazdild7558da2015-09-22 13:04:14 +01001140 bool HasThrowingInstructions() const;
1141
David Brazdila4b8c212015-05-07 09:59:30 +01001142 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001143 bool Dominates(HBasicBlock* block) const;
1144
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001145 size_t GetLifetimeStart() const { return lifetime_start_; }
1146 size_t GetLifetimeEnd() const { return lifetime_end_; }
1147
1148 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1149 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1150
David Brazdil8d5b8b22015-03-24 10:51:52 +00001151 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001152 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001153 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001154 bool HasSinglePhi() const;
1155
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001156 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001157 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001158 ArenaVector<HBasicBlock*> predecessors_;
1159 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001160 HInstructionList instructions_;
1161 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001162 HLoopInformation* loop_information_;
1163 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001164 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001165 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001166 // The dex program counter of the first instruction of this block.
1167 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001168 size_t lifetime_start_;
1169 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001170 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001172 friend class HGraph;
1173 friend class HInstruction;
1174
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001175 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1176};
1177
David Brazdilb2bd1c52015-03-25 11:17:37 +00001178// Iterates over the LoopInformation of all loops which contain 'block'
1179// from the innermost to the outermost.
1180class HLoopInformationOutwardIterator : public ValueObject {
1181 public:
1182 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1183 : current_(block.GetLoopInformation()) {}
1184
1185 bool Done() const { return current_ == nullptr; }
1186
1187 void Advance() {
1188 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001189 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001190 }
1191
1192 HLoopInformation* Current() const {
1193 DCHECK(!Done());
1194 return current_;
1195 }
1196
1197 private:
1198 HLoopInformation* current_;
1199
1200 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1201};
1202
Alexandre Ramesef20f712015-06-09 10:29:30 +01001203#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001204 M(Above, Condition) \
1205 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001207 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001208 M(ArrayGet, Instruction) \
1209 M(ArrayLength, Instruction) \
1210 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001211 M(Below, Condition) \
1212 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001213 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001215 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001216 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001217 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001218 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001219 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001220 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001221 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001222 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001223 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001224 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001226 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Exit, Instruction) \
1228 M(FloatConstant, Constant) \
1229 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001230 M(GreaterThan, Condition) \
1231 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001232 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(InstanceFieldGet, Instruction) \
1234 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001235 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001237 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001238 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001239 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001241 M(LessThan, Condition) \
1242 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001243 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001244 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001245 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001246 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001247 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001248 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001249 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001250 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001251 M(Neg, UnaryOperation) \
1252 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001253 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001254 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001255 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001256 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001257 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001258 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001259 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001260 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001261 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001263 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(Return, Instruction) \
1265 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001266 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001267 M(Shl, BinaryOperation) \
1268 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001269 M(StaticFieldGet, Instruction) \
1270 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 M(UnresolvedInstanceFieldGet, Instruction) \
1272 M(UnresolvedInstanceFieldSet, Instruction) \
1273 M(UnresolvedStaticFieldGet, Instruction) \
1274 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001275 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001276 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001277 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001278 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001279 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001280 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001281 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001282 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001283
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001284/*
1285 * Instructions, shared across several (not all) architectures.
1286 */
1287#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1288#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1289#else
1290#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001291 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001292 M(MultiplyAccumulate, Instruction) \
1293 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001294#endif
1295
Vladimir Markob4536b72015-11-24 13:45:23 +00001296#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001297#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001298#else
1299#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1300 M(ArmDexCacheArraysBase, Instruction)
1301#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001302
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001303#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001304#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001305#else
1306#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001307 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001308#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001309
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001310#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001312#else
1313#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1314 M(MipsComputeBaseMethodAddress, Instruction) \
1315 M(MipsDexCacheArraysBase, Instruction)
1316#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001317
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001318#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1319
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001320#ifndef ART_ENABLE_CODEGEN_x86
1321#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1322#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001323#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1324 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001325 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001326 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001327 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001328#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001329
1330#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1331
1332#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1333 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001334 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001335 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1336 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001337 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001338 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001339 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1340 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1341
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001342#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1343 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001346 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001347 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001348
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001349#define FOR_EACH_INSTRUCTION(M) \
1350 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1351 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1352
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001353#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001354FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1355#undef FORWARD_DECLARATION
1356
Vladimir Marko372f10e2016-05-17 16:30:10 +01001357#define DECLARE_INSTRUCTION(type) \
1358 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1359 const char* DebugName() const OVERRIDE { return #type; } \
1360 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1361 return other->Is##type(); \
1362 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001363 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001364
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001365#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1366 bool Is##type() const { return As##type() != nullptr; } \
1367 const H##type* As##type() const { return this; } \
1368 H##type* As##type() { return this; }
1369
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001370template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001371class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001372 public:
David Brazdiled596192015-01-23 10:39:45 +00001373 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001374 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001375 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001376
Vladimir Marko46817b82016-03-29 12:21:58 +01001377 // Hook for the IntrusiveForwardList<>.
1378 // TODO: Hide this better.
1379 IntrusiveForwardListHook hook;
1380
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381 private:
David Brazdiled596192015-01-23 10:39:45 +00001382 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001383 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001384
1385 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001386 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001387
Vladimir Marko46817b82016-03-29 12:21:58 +01001388 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001389
1390 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1391};
1392
David Brazdiled596192015-01-23 10:39:45 +00001393template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001394using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001395
David Brazdil1abb4192015-02-17 18:33:36 +00001396// This class is used by HEnvironment and HInstruction classes to record the
1397// instructions they use and pointers to the corresponding HUseListNodes kept
1398// by the used instructions.
1399template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001400class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001401 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001402 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1403 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001404
Vladimir Marko46817b82016-03-29 12:21:58 +01001405 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1406 : HUserRecord(old_record.instruction_, before_use_node) {}
1407 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1408 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001409 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001410 }
1411
1412 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001413 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1414 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001415
1416 private:
1417 // Instruction used by the user.
1418 HInstruction* instruction_;
1419
Vladimir Marko46817b82016-03-29 12:21:58 +01001420 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1421 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001422};
1423
Vladimir Markoe9004912016-06-16 16:50:52 +01001424// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1425// This is used for HInstruction::GetInputs() to return a container wrapper providing
1426// HInstruction* values even though the underlying container has HUserRecord<>s.
1427struct HInputExtractor {
1428 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1429 return record.GetInstruction();
1430 }
1431 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1432 return record.GetInstruction();
1433 }
1434};
1435
1436using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1437using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1438
Aart Bik854a02b2015-07-14 16:07:00 -07001439/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001440 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001441 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 * For write/read dependences on fields/arrays, the dependence analysis uses
1443 * type disambiguation (e.g. a float field write cannot modify the value of an
1444 * integer field read) and the access type (e.g. a reference array write cannot
1445 * modify the value of a reference field read [although it may modify the
1446 * reference fetch prior to reading the field, which is represented by its own
1447 * write/read dependence]). The analysis makes conservative points-to
1448 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1449 * the same, and any reference read depends on any reference read without
1450 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001451 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001452 * The internal representation uses 38-bit and is described in the table below.
1453 * The first line indicates the side effect, and for field/array accesses the
1454 * second line indicates the type of the access (in the order of the
1455 * Primitive::Type enum).
1456 * The two numbered lines below indicate the bit position in the bitfield (read
1457 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001458 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1460 * +-------------+---------+---------+--------------+---------+---------+
1461 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1462 * | 3 |333333322|222222221| 1 |111111110|000000000|
1463 * | 7 |654321098|765432109| 8 |765432109|876543210|
1464 *
1465 * Note that, to ease the implementation, 'changes' bits are least significant
1466 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001467 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001468class SideEffects : public ValueObject {
1469 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001470 SideEffects() : flags_(0) {}
1471
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001472 static SideEffects None() {
1473 return SideEffects(0);
1474 }
1475
1476 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001477 return SideEffects(kAllChangeBits | kAllDependOnBits);
1478 }
1479
1480 static SideEffects AllChanges() {
1481 return SideEffects(kAllChangeBits);
1482 }
1483
1484 static SideEffects AllDependencies() {
1485 return SideEffects(kAllDependOnBits);
1486 }
1487
1488 static SideEffects AllExceptGCDependency() {
1489 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1490 }
1491
1492 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001493 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494 }
1495
Aart Bik34c3ba92015-07-20 14:08:59 -07001496 static SideEffects AllWrites() {
1497 return SideEffects(kAllWrites);
1498 }
1499
1500 static SideEffects AllReads() {
1501 return SideEffects(kAllReads);
1502 }
1503
1504 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1505 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001506 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001507 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001508 }
1509
Aart Bik854a02b2015-07-14 16:07:00 -07001510 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001511 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001512 }
1513
Aart Bik34c3ba92015-07-20 14:08:59 -07001514 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1515 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001516 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001517 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001518 }
1519
1520 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001521 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001522 }
1523
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001524 static SideEffects CanTriggerGC() {
1525 return SideEffects(1ULL << kCanTriggerGCBit);
1526 }
1527
1528 static SideEffects DependsOnGC() {
1529 return SideEffects(1ULL << kDependsOnGCBit);
1530 }
1531
Aart Bik854a02b2015-07-14 16:07:00 -07001532 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001533 SideEffects Union(SideEffects other) const {
1534 return SideEffects(flags_ | other.flags_);
1535 }
1536
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001537 SideEffects Exclusion(SideEffects other) const {
1538 return SideEffects(flags_ & ~other.flags_);
1539 }
1540
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001541 void Add(SideEffects other) {
1542 flags_ |= other.flags_;
1543 }
1544
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001545 bool Includes(SideEffects other) const {
1546 return (other.flags_ & flags_) == other.flags_;
1547 }
1548
1549 bool HasSideEffects() const {
1550 return (flags_ & kAllChangeBits);
1551 }
1552
1553 bool HasDependencies() const {
1554 return (flags_ & kAllDependOnBits);
1555 }
1556
1557 // Returns true if there are no side effects or dependencies.
1558 bool DoesNothing() const {
1559 return flags_ == 0;
1560 }
1561
Aart Bik854a02b2015-07-14 16:07:00 -07001562 // Returns true if something is written.
1563 bool DoesAnyWrite() const {
1564 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001565 }
1566
Aart Bik854a02b2015-07-14 16:07:00 -07001567 // Returns true if something is read.
1568 bool DoesAnyRead() const {
1569 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001570 }
1571
Aart Bik854a02b2015-07-14 16:07:00 -07001572 // Returns true if potentially everything is written and read
1573 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001574 bool DoesAllReadWrite() const {
1575 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1576 }
1577
Aart Bik854a02b2015-07-14 16:07:00 -07001578 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001580 }
1581
Roland Levillain0d5a2812015-11-13 10:07:31 +00001582 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001583 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1585 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001586 }
1587
1588 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001589 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001590 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001591 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 for (int s = kLastBit; s >= 0; s--) {
1593 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1594 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1595 // This is a bit for the GC side effect.
1596 if (current_bit_is_set) {
1597 flags += "GC";
1598 }
Aart Bik854a02b2015-07-14 16:07:00 -07001599 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001600 } else {
1601 // This is a bit for the array/field analysis.
1602 // The underscore character stands for the 'can trigger GC' bit.
1603 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1604 if (current_bit_is_set) {
1605 flags += kDebug[s];
1606 }
1607 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1608 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1609 flags += "|";
1610 }
1611 }
Aart Bik854a02b2015-07-14 16:07:00 -07001612 }
1613 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001614 }
1615
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001617
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 private:
1619 static constexpr int kFieldArrayAnalysisBits = 9;
1620
1621 static constexpr int kFieldWriteOffset = 0;
1622 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1623 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1624 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1625
1626 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1627
1628 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1629 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1630 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1631 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1632
1633 static constexpr int kLastBit = kDependsOnGCBit;
1634 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1635
1636 // Aliases.
1637
1638 static_assert(kChangeBits == kDependOnBits,
1639 "the 'change' bits should match the 'depend on' bits.");
1640
1641 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1642 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1643 static constexpr uint64_t kAllWrites =
1644 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1645 static constexpr uint64_t kAllReads =
1646 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001647
Aart Bik854a02b2015-07-14 16:07:00 -07001648 // Translates type to bit flag.
1649 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1650 CHECK_NE(type, Primitive::kPrimVoid);
1651 const uint64_t one = 1;
1652 const int shift = type; // 0-based consecutive enum
1653 DCHECK_LE(kFieldWriteOffset, shift);
1654 DCHECK_LT(shift, kArrayWriteOffset);
1655 return one << (type + offset);
1656 }
1657
1658 // Private constructor on direct flags value.
1659 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1660
1661 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001662};
1663
David Brazdiled596192015-01-23 10:39:45 +00001664// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001665class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001666 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001667 HEnvironment(ArenaAllocator* arena,
1668 size_t number_of_vregs,
1669 const DexFile& dex_file,
1670 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001671 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001672 InvokeType invoke_type,
1673 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001674 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1675 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001676 parent_(nullptr),
1677 dex_file_(dex_file),
1678 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001679 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001680 invoke_type_(invoke_type),
1681 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001682 }
1683
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001684 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001685 : HEnvironment(arena,
1686 to_copy.Size(),
1687 to_copy.GetDexFile(),
1688 to_copy.GetMethodIdx(),
1689 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001690 to_copy.GetInvokeType(),
1691 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001692
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001693 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001694 if (parent_ != nullptr) {
1695 parent_->SetAndCopyParentChain(allocator, parent);
1696 } else {
1697 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1698 parent_->CopyFrom(parent);
1699 if (parent->GetParent() != nullptr) {
1700 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1701 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001702 }
David Brazdiled596192015-01-23 10:39:45 +00001703 }
1704
Vladimir Marko71bf8092015-09-15 15:33:14 +01001705 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001706 void CopyFrom(HEnvironment* environment);
1707
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001708 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1709 // input to the loop phi instead. This is for inserting instructions that
1710 // require an environment (like HDeoptimization) in the loop pre-header.
1711 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001712
1713 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001714 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001715 }
1716
1717 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001718 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001719 }
1720
David Brazdil1abb4192015-02-17 18:33:36 +00001721 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001722
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001723 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001724
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001725 HEnvironment* GetParent() const { return parent_; }
1726
1727 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001728 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001729 }
1730
1731 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001732 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001733 }
1734
1735 uint32_t GetDexPc() const {
1736 return dex_pc_;
1737 }
1738
1739 uint32_t GetMethodIdx() const {
1740 return method_idx_;
1741 }
1742
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001743 InvokeType GetInvokeType() const {
1744 return invoke_type_;
1745 }
1746
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001747 const DexFile& GetDexFile() const {
1748 return dex_file_;
1749 }
1750
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001751 HInstruction* GetHolder() const {
1752 return holder_;
1753 }
1754
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001755
1756 bool IsFromInlinedInvoke() const {
1757 return GetParent() != nullptr;
1758 }
1759
David Brazdiled596192015-01-23 10:39:45 +00001760 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001761 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1762 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001763 HEnvironment* parent_;
1764 const DexFile& dex_file_;
1765 const uint32_t method_idx_;
1766 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001767 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001768
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001769 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001770 HInstruction* const holder_;
1771
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001772 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001773
1774 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1775};
1776
Vladimir Markof9f64412015-09-02 14:05:49 +01001777class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001778 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001779 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001780 : previous_(nullptr),
1781 next_(nullptr),
1782 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001783 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001784 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001785 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001786 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001788 locations_(nullptr),
1789 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001791 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001792 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1793 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1794 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795
Dave Allison20dfc792014-06-16 20:44:29 -07001796 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001797
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001798#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001799 enum InstructionKind {
1800 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1801 };
1802#undef DECLARE_KIND
1803
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001804 HInstruction* GetNext() const { return next_; }
1805 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001806
Calin Juravle77520bc2015-01-12 18:45:46 +00001807 HInstruction* GetNextDisregardingMoves() const;
1808 HInstruction* GetPreviousDisregardingMoves() const;
1809
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001810 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001811 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001812 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001813 bool IsInBlock() const { return block_ != nullptr; }
1814 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001815 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1816 bool IsIrreducibleLoopHeaderPhi() const {
1817 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1818 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001819
Vladimir Marko372f10e2016-05-17 16:30:10 +01001820 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1821
1822 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1823 // One virtual method is enough, just const_cast<> and then re-add the const.
1824 return ArrayRef<const HUserRecord<HInstruction*>>(
1825 const_cast<HInstruction*>(this)->GetInputRecords());
1826 }
1827
Vladimir Markoe9004912016-06-16 16:50:52 +01001828 HInputsRef GetInputs() {
1829 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001830 }
1831
Vladimir Markoe9004912016-06-16 16:50:52 +01001832 HConstInputsRef GetInputs() const {
1833 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001834 }
1835
1836 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001837 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001838
Vladimir Marko372f10e2016-05-17 16:30:10 +01001839 void SetRawInputAt(size_t index, HInstruction* input) {
1840 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1841 }
1842
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001843 virtual void Accept(HGraphVisitor* visitor) = 0;
1844 virtual const char* DebugName() const = 0;
1845
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001846 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1847
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001849
1850 uint32_t GetDexPc() const { return dex_pc_; }
1851
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001852 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001853
Roland Levillaine161a2a2014-10-03 12:45:18 +01001854 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001855 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001856
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001857 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001858 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859
Calin Juravle10e244f2015-01-26 18:54:32 +00001860 // Does not apply for all instructions, but having this at top level greatly
1861 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001862 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001863 virtual bool CanBeNull() const {
1864 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1865 return true;
1866 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001867
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001868 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001869 return false;
1870 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001871
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001872 virtual bool IsActualObject() const {
1873 return GetType() == Primitive::kPrimNot;
1874 }
1875
Calin Juravle2e768302015-07-28 14:41:11 +00001876 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001877
Calin Juravle61d544b2015-02-23 16:46:57 +00001878 ReferenceTypeInfo GetReferenceTypeInfo() const {
1879 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001880 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001881 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001882 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001883
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001884 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001885 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001886 // Note: fixup_end remains valid across push_front().
1887 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1888 HUseListNode<HInstruction*>* new_node =
1889 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1890 uses_.push_front(*new_node);
1891 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001892 }
1893
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001894 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001895 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001896 // Note: env_fixup_end remains valid across push_front().
1897 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1898 HUseListNode<HEnvironment*>* new_node =
1899 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1900 env_uses_.push_front(*new_node);
1901 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001902 }
1903
David Brazdil1abb4192015-02-17 18:33:36 +00001904 void RemoveAsUserOfInput(size_t input) {
1905 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001906 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1907 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1908 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001909 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001910
Vladimir Marko372f10e2016-05-17 16:30:10 +01001911 void RemoveAsUserOfAllInputs() {
1912 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1913 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1914 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1915 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1916 }
1917 }
1918
David Brazdil1abb4192015-02-17 18:33:36 +00001919 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1920 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001921
Vladimir Marko46817b82016-03-29 12:21:58 +01001922 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1923 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1924 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001925 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001926 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001927 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001928
Roland Levillain6c82d402014-10-13 16:10:27 +01001929 // Does this instruction strictly dominate `other_instruction`?
1930 // Returns false if this instruction and `other_instruction` are the same.
1931 // Aborts if this instruction and `other_instruction` are both phis.
1932 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001933
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001934 int GetId() const { return id_; }
1935 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001936
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001937 int GetSsaIndex() const { return ssa_index_; }
1938 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1939 bool HasSsaIndex() const { return ssa_index_ != -1; }
1940
1941 bool HasEnvironment() const { return environment_ != nullptr; }
1942 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001943 // Set the `environment_` field. Raw because this method does not
1944 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 void SetRawEnvironment(HEnvironment* environment) {
1946 DCHECK(environment_ == nullptr);
1947 DCHECK_EQ(environment->GetHolder(), this);
1948 environment_ = environment;
1949 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001950
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001951 void InsertRawEnvironment(HEnvironment* environment) {
1952 DCHECK(environment_ != nullptr);
1953 DCHECK_EQ(environment->GetHolder(), this);
1954 DCHECK(environment->GetParent() == nullptr);
1955 environment->parent_ = environment_;
1956 environment_ = environment;
1957 }
1958
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001959 void RemoveEnvironment();
1960
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001961 // Set the environment of this instruction, copying it from `environment`. While
1962 // copying, the uses lists are being updated.
1963 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001964 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001965 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001966 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001967 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001968 if (environment->GetParent() != nullptr) {
1969 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1970 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001971 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001972
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001973 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1974 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001975 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001976 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001977 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001978 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001979 if (environment->GetParent() != nullptr) {
1980 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1981 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001982 }
1983
Nicolas Geoffray39468442014-09-02 15:17:15 +01001984 // Returns the number of entries in the environment. Typically, that is the
1985 // number of dex registers in a method. It could be more in case of inlining.
1986 size_t EnvironmentSize() const;
1987
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001988 LocationSummary* GetLocations() const { return locations_; }
1989 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001990
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001991 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001992 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001993
Alexandre Rames188d4312015-04-09 18:30:21 +01001994 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1995 // uses of this instruction by `other` are *not* updated.
1996 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1997 ReplaceWith(other);
1998 other->ReplaceInput(this, use_index);
1999 }
2000
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002001 // Move `this` instruction before `cursor`.
2002 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002003
Vladimir Markofb337ea2015-11-25 15:25:10 +00002004 // Move `this` before its first user and out of any loops. If there is no
2005 // out-of-loop user that dominates all other users, move the instruction
2006 // to the end of the out-of-loop common dominator of the user's blocks.
2007 //
2008 // This can be used only on non-throwing instructions with no side effects that
2009 // have at least one use but no environment uses.
2010 void MoveBeforeFirstUserAndOutOfLoops();
2011
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002012#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002013 bool Is##type() const; \
2014 const H##type* As##type() const; \
2015 H##type* As##type();
2016
2017 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2018#undef INSTRUCTION_TYPE_CHECK
2019
2020#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002021 bool Is##type() const { return (As##type() != nullptr); } \
2022 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002023 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025#undef INSTRUCTION_TYPE_CHECK
2026
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002027 // Returns whether the instruction can be moved within the graph.
2028 virtual bool CanBeMoved() const { return false; }
2029
2030 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002031 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002032 return false;
2033 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002034
2035 // Returns whether any data encoded in the two instructions is equal.
2036 // This method does not look at the inputs. Both instructions must be
2037 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002038 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002039 return false;
2040 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002041
2042 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002043 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002044 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002045 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002046
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002047 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2048 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2049 // the virtual function because the __attribute__((__pure__)) doesn't really
2050 // apply the strong requirement for virtual functions, preventing optimizations.
2051 InstructionKind GetKind() const PURE;
2052 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002053
2054 virtual size_t ComputeHashCode() const {
2055 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002056 for (const HInstruction* input : GetInputs()) {
2057 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002058 }
2059 return result;
2060 }
2061
2062 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002063 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002064 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002065
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002066 size_t GetLifetimePosition() const { return lifetime_position_; }
2067 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2068 LiveInterval* GetLiveInterval() const { return live_interval_; }
2069 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2070 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2071
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002072 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2073
2074 // Returns whether the code generation of the instruction will require to have access
2075 // to the current method. Such instructions are:
2076 // (1): Instructions that require an environment, as calling the runtime requires
2077 // to walk the stack and have the current method stored at a specific stack address.
2078 // (2): Object literals like classes and strings, that are loaded from the dex cache
2079 // fields of the current method.
2080 bool NeedsCurrentMethod() const {
2081 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2082 }
2083
Vladimir Markodc151b22015-10-15 18:02:30 +01002084 // Returns whether the code generation of the instruction will require to have access
2085 // to the dex cache of the current method's declaring class via the current method.
2086 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002087
Mark Mendellc4701932015-04-10 13:18:51 -04002088 // Does this instruction have any use in an environment before
2089 // control flow hits 'other'?
2090 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2091
2092 // Remove all references to environment uses of this instruction.
2093 // The caller must ensure that this is safe to do.
2094 void RemoveEnvironmentUsers();
2095
Vladimir Markoa1de9182016-02-25 11:37:38 +00002096 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2097 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002098
David Brazdil1abb4192015-02-17 18:33:36 +00002099 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002100 // If set, the machine code for this instruction is assumed to be generated by
2101 // its users. Used by liveness analysis to compute use positions accordingly.
2102 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2103 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2104 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2105 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2106
Vladimir Marko372f10e2016-05-17 16:30:10 +01002107 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2108 return GetInputRecords()[i];
2109 }
2110
2111 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2112 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2113 input_records[index] = input;
2114 }
David Brazdil1abb4192015-02-17 18:33:36 +00002115
Vladimir Markoa1de9182016-02-25 11:37:38 +00002116 uint32_t GetPackedFields() const {
2117 return packed_fields_;
2118 }
2119
2120 template <size_t flag>
2121 bool GetPackedFlag() const {
2122 return (packed_fields_ & (1u << flag)) != 0u;
2123 }
2124
2125 template <size_t flag>
2126 void SetPackedFlag(bool value = true) {
2127 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2128 }
2129
2130 template <typename BitFieldType>
2131 typename BitFieldType::value_type GetPackedField() const {
2132 return BitFieldType::Decode(packed_fields_);
2133 }
2134
2135 template <typename BitFieldType>
2136 void SetPackedField(typename BitFieldType::value_type value) {
2137 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2138 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2139 }
2140
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002141 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002142 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2143 auto before_use_node = uses_.before_begin();
2144 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2145 HInstruction* user = use_node->GetUser();
2146 size_t input_index = use_node->GetIndex();
2147 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2148 before_use_node = use_node;
2149 }
2150 }
2151
2152 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2153 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2154 if (next != uses_.end()) {
2155 HInstruction* next_user = next->GetUser();
2156 size_t next_index = next->GetIndex();
2157 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2158 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2159 }
2160 }
2161
2162 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2163 auto before_env_use_node = env_uses_.before_begin();
2164 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2165 HEnvironment* user = env_use_node->GetUser();
2166 size_t input_index = env_use_node->GetIndex();
2167 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2168 before_env_use_node = env_use_node;
2169 }
2170 }
2171
2172 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2173 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2174 if (next != env_uses_.end()) {
2175 HEnvironment* next_user = next->GetUser();
2176 size_t next_index = next->GetIndex();
2177 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2178 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2179 }
2180 }
David Brazdil1abb4192015-02-17 18:33:36 +00002181
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002182 HInstruction* previous_;
2183 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002184 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002185 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002187 // An instruction gets an id when it is added to the graph.
2188 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002189 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002190 int id_;
2191
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002192 // When doing liveness analysis, instructions that have uses get an SSA index.
2193 int ssa_index_;
2194
Vladimir Markoa1de9182016-02-25 11:37:38 +00002195 // Packed fields.
2196 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002197
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002198 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002199 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002200
2201 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002202 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203
Nicolas Geoffray39468442014-09-02 15:17:15 +01002204 // The environment associated with this instruction. Not null if the instruction
2205 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002206 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002207
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002208 // Set by the code generator.
2209 LocationSummary* locations_;
2210
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002211 // Set by the liveness analysis.
2212 LiveInterval* live_interval_;
2213
2214 // Set by the liveness analysis, this is the position in a linear
2215 // order of blocks where this instruction's live interval start.
2216 size_t lifetime_position_;
2217
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002218 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002219
Vladimir Markoa1de9182016-02-25 11:37:38 +00002220 // The reference handle part of the reference type info.
2221 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002222 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002223 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002224
David Brazdil1abb4192015-02-17 18:33:36 +00002225 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002227 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002228 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002229 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230
2231 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2232};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002233std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002234
2235class HInstructionIterator : public ValueObject {
2236 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002237 explicit HInstructionIterator(const HInstructionList& instructions)
2238 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002239 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002240 }
2241
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002242 bool Done() const { return instruction_ == nullptr; }
2243 HInstruction* Current() const { return instruction_; }
2244 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002245 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002246 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247 }
2248
2249 private:
2250 HInstruction* instruction_;
2251 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002252
2253 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002254};
2255
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002256class HBackwardInstructionIterator : public ValueObject {
2257 public:
2258 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2259 : instruction_(instructions.last_instruction_) {
2260 next_ = Done() ? nullptr : instruction_->GetPrevious();
2261 }
2262
2263 bool Done() const { return instruction_ == nullptr; }
2264 HInstruction* Current() const { return instruction_; }
2265 void Advance() {
2266 instruction_ = next_;
2267 next_ = Done() ? nullptr : instruction_->GetPrevious();
2268 }
2269
2270 private:
2271 HInstruction* instruction_;
2272 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002273
2274 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002275};
2276
Vladimir Markof9f64412015-09-02 14:05:49 +01002277template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002278class HTemplateInstruction: public HInstruction {
2279 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002280 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002281 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002282 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002283
Vladimir Marko372f10e2016-05-17 16:30:10 +01002284 using HInstruction::GetInputRecords; // Keep the const version visible.
2285 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2286 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002287 }
2288
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002290 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002291
2292 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293};
2294
Vladimir Markof9f64412015-09-02 14:05:49 +01002295// HTemplateInstruction specialization for N=0.
2296template<>
2297class HTemplateInstruction<0>: public HInstruction {
2298 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002299 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002300 : HInstruction(side_effects, dex_pc) {}
2301
Vladimir Markof9f64412015-09-02 14:05:49 +01002302 virtual ~HTemplateInstruction() {}
2303
Vladimir Marko372f10e2016-05-17 16:30:10 +01002304 using HInstruction::GetInputRecords; // Keep the const version visible.
2305 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2306 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002307 }
2308
2309 private:
2310 friend class SsaBuilder;
2311};
2312
Dave Allison20dfc792014-06-16 20:44:29 -07002313template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002314class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002315 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002316 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002317 : HTemplateInstruction<N>(side_effects, dex_pc) {
2318 this->template SetPackedField<TypeField>(type);
2319 }
Dave Allison20dfc792014-06-16 20:44:29 -07002320 virtual ~HExpression() {}
2321
Vladimir Markoa1de9182016-02-25 11:37:38 +00002322 Primitive::Type GetType() const OVERRIDE {
2323 return TypeField::Decode(this->GetPackedFields());
2324 }
Dave Allison20dfc792014-06-16 20:44:29 -07002325
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002326 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002327 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2328 static constexpr size_t kFieldTypeSize =
2329 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2330 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2331 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2332 "Too many packed fields.");
2333 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002334};
2335
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002336// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2337// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002338class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002340 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2341 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002342
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002343 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002344
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002345 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002346
2347 private:
2348 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2349};
2350
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002351// Represents dex's RETURN opcodes. A HReturn is a control flow
2352// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002353class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002354 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002355 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2356 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002357 SetRawInputAt(0, value);
2358 }
2359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002360 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002361
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002362 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002363
2364 private:
2365 DISALLOW_COPY_AND_ASSIGN(HReturn);
2366};
2367
Vladimir Markofcb503c2016-05-18 12:48:17 +01002368class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002369 public:
2370 HPhi(ArenaAllocator* arena,
2371 uint32_t reg_number,
2372 size_t number_of_inputs,
2373 Primitive::Type type,
2374 uint32_t dex_pc = kNoDexPc)
2375 : HInstruction(SideEffects::None(), dex_pc),
2376 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2377 reg_number_(reg_number) {
2378 SetPackedField<TypeField>(ToPhiType(type));
2379 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2380 // Phis are constructed live and marked dead if conflicting or unused.
2381 // Individual steps of SsaBuilder should assume that if a phi has been
2382 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2383 SetPackedFlag<kFlagIsLive>(true);
2384 SetPackedFlag<kFlagCanBeNull>(true);
2385 }
2386
2387 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2388 static Primitive::Type ToPhiType(Primitive::Type type) {
2389 return Primitive::PrimitiveKind(type);
2390 }
2391
2392 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2393
Vladimir Marko372f10e2016-05-17 16:30:10 +01002394 using HInstruction::GetInputRecords; // Keep the const version visible.
2395 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2396 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2397 }
David Brazdildee58d62016-04-07 09:54:26 +00002398
2399 void AddInput(HInstruction* input);
2400 void RemoveInputAt(size_t index);
2401
2402 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2403 void SetType(Primitive::Type new_type) {
2404 // Make sure that only valid type changes occur. The following are allowed:
2405 // (1) int -> float/ref (primitive type propagation),
2406 // (2) long -> double (primitive type propagation).
2407 DCHECK(GetType() == new_type ||
2408 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2409 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2410 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2411 SetPackedField<TypeField>(new_type);
2412 }
2413
2414 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2415 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2416
2417 uint32_t GetRegNumber() const { return reg_number_; }
2418
2419 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2420 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2421 bool IsDead() const { return !IsLive(); }
2422 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2423
Vladimir Markoe9004912016-06-16 16:50:52 +01002424 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002425 return other != nullptr
2426 && other->IsPhi()
2427 && other->AsPhi()->GetBlock() == GetBlock()
2428 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2429 }
2430
2431 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2432 // An equivalent phi is a phi having the same dex register and type.
2433 // It assumes that phis with the same dex register are adjacent.
2434 HPhi* GetNextEquivalentPhiWithSameType() {
2435 HInstruction* next = GetNext();
2436 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2437 if (next->GetType() == GetType()) {
2438 return next->AsPhi();
2439 }
2440 next = next->GetNext();
2441 }
2442 return nullptr;
2443 }
2444
2445 DECLARE_INSTRUCTION(Phi);
2446
David Brazdildee58d62016-04-07 09:54:26 +00002447 private:
2448 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2449 static constexpr size_t kFieldTypeSize =
2450 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2451 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2452 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2453 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2454 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2455 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2456
Vladimir Marko372f10e2016-05-17 16:30:10 +01002457 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002458 const uint32_t reg_number_;
2459
2460 DISALLOW_COPY_AND_ASSIGN(HPhi);
2461};
2462
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002463// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002464// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002465// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002466class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002467 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002470 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002472 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HExit);
2476};
2477
2478// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002479class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002480 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002481 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002482
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002483 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002485 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002486 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002487 }
2488
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002489 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002490
2491 private:
2492 DISALLOW_COPY_AND_ASSIGN(HGoto);
2493};
2494
Roland Levillain9867bc72015-08-05 10:21:34 +01002495class HConstant : public HExpression<0> {
2496 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002497 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2498 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002499
2500 bool CanBeMoved() const OVERRIDE { return true; }
2501
Roland Levillain1a653882016-03-18 18:05:57 +00002502 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002503 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002504 // Is this constant 0 in the arithmetic sense?
2505 virtual bool IsArithmeticZero() const { return false; }
2506 // Is this constant a 0-bit pattern?
2507 virtual bool IsZeroBitPattern() const { return false; }
2508 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002509 virtual bool IsOne() const { return false; }
2510
David Brazdil77a48ae2015-09-15 12:34:04 +00002511 virtual uint64_t GetValueAsUint64() const = 0;
2512
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002513 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002514
2515 private:
2516 DISALLOW_COPY_AND_ASSIGN(HConstant);
2517};
2518
Vladimir Markofcb503c2016-05-18 12:48:17 +01002519class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002520 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002521 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002522 return true;
2523 }
2524
David Brazdil77a48ae2015-09-15 12:34:04 +00002525 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2526
Roland Levillain9867bc72015-08-05 10:21:34 +01002527 size_t ComputeHashCode() const OVERRIDE { return 0; }
2528
Roland Levillain1a653882016-03-18 18:05:57 +00002529 // The null constant representation is a 0-bit pattern.
2530 virtual bool IsZeroBitPattern() const { return true; }
2531
Roland Levillain9867bc72015-08-05 10:21:34 +01002532 DECLARE_INSTRUCTION(NullConstant);
2533
2534 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002535 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002536
2537 friend class HGraph;
2538 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2539};
2540
2541// Constants of the type int. Those can be from Dex instructions, or
2542// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002543class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002544 public:
2545 int32_t GetValue() const { return value_; }
2546
David Brazdil9f389d42015-10-01 14:32:56 +01002547 uint64_t GetValueAsUint64() const OVERRIDE {
2548 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2549 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002550
Vladimir Marko372f10e2016-05-17 16:30:10 +01002551 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002552 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002553 return other->AsIntConstant()->value_ == value_;
2554 }
2555
2556 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2557
2558 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002559 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2560 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002561 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2562
Roland Levillain1a653882016-03-18 18:05:57 +00002563 // Integer constants are used to encode Boolean values as well,
2564 // where 1 means true and 0 means false.
2565 bool IsTrue() const { return GetValue() == 1; }
2566 bool IsFalse() const { return GetValue() == 0; }
2567
Roland Levillain9867bc72015-08-05 10:21:34 +01002568 DECLARE_INSTRUCTION(IntConstant);
2569
2570 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002571 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2572 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2573 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2574 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002575
2576 const int32_t value_;
2577
2578 friend class HGraph;
2579 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2580 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2581 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2582};
2583
Vladimir Markofcb503c2016-05-18 12:48:17 +01002584class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002585 public:
2586 int64_t GetValue() const { return value_; }
2587
David Brazdil77a48ae2015-09-15 12:34:04 +00002588 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2589
Vladimir Marko372f10e2016-05-17 16:30:10 +01002590 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002591 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002592 return other->AsLongConstant()->value_ == value_;
2593 }
2594
2595 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2596
2597 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002598 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2599 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002600 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2601
2602 DECLARE_INSTRUCTION(LongConstant);
2603
2604 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002605 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2606 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002607
2608 const int64_t value_;
2609
2610 friend class HGraph;
2611 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2612};
Dave Allison20dfc792014-06-16 20:44:29 -07002613
Vladimir Markofcb503c2016-05-18 12:48:17 +01002614class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002615 public:
2616 float GetValue() const { return value_; }
2617
2618 uint64_t GetValueAsUint64() const OVERRIDE {
2619 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2620 }
2621
Vladimir Marko372f10e2016-05-17 16:30:10 +01002622 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002623 DCHECK(other->IsFloatConstant()) << other->DebugName();
2624 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2625 }
2626
2627 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2628
2629 bool IsMinusOne() const OVERRIDE {
2630 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2631 }
Roland Levillain1a653882016-03-18 18:05:57 +00002632 bool IsArithmeticZero() const OVERRIDE {
2633 return std::fpclassify(value_) == FP_ZERO;
2634 }
2635 bool IsArithmeticPositiveZero() const {
2636 return IsArithmeticZero() && !std::signbit(value_);
2637 }
2638 bool IsArithmeticNegativeZero() const {
2639 return IsArithmeticZero() && std::signbit(value_);
2640 }
2641 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002642 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002643 }
2644 bool IsOne() const OVERRIDE {
2645 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2646 }
2647 bool IsNaN() const {
2648 return std::isnan(value_);
2649 }
2650
2651 DECLARE_INSTRUCTION(FloatConstant);
2652
2653 private:
2654 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2655 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2656 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2657 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2658
2659 const float value_;
2660
2661 // Only the SsaBuilder and HGraph can create floating-point constants.
2662 friend class SsaBuilder;
2663 friend class HGraph;
2664 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2665};
2666
Vladimir Markofcb503c2016-05-18 12:48:17 +01002667class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002668 public:
2669 double GetValue() const { return value_; }
2670
2671 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2672
Vladimir Marko372f10e2016-05-17 16:30:10 +01002673 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002674 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2675 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2676 }
2677
2678 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2679
2680 bool IsMinusOne() const OVERRIDE {
2681 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2682 }
Roland Levillain1a653882016-03-18 18:05:57 +00002683 bool IsArithmeticZero() const OVERRIDE {
2684 return std::fpclassify(value_) == FP_ZERO;
2685 }
2686 bool IsArithmeticPositiveZero() const {
2687 return IsArithmeticZero() && !std::signbit(value_);
2688 }
2689 bool IsArithmeticNegativeZero() const {
2690 return IsArithmeticZero() && std::signbit(value_);
2691 }
2692 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002693 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002694 }
2695 bool IsOne() const OVERRIDE {
2696 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2697 }
2698 bool IsNaN() const {
2699 return std::isnan(value_);
2700 }
2701
2702 DECLARE_INSTRUCTION(DoubleConstant);
2703
2704 private:
2705 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2706 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2707 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2708 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2709
2710 const double value_;
2711
2712 // Only the SsaBuilder and HGraph can create floating-point constants.
2713 friend class SsaBuilder;
2714 friend class HGraph;
2715 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2716};
2717
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002718// Conditional branch. A block ending with an HIf instruction must have
2719// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002720class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002721 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2723 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002724 SetRawInputAt(0, input);
2725 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002726
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002727 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002728
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002729 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002730 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002731 }
2732
2733 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002734 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002735 }
2736
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002737 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002738
2739 private:
2740 DISALLOW_COPY_AND_ASSIGN(HIf);
2741};
2742
David Brazdilfc6a86a2015-06-26 10:33:45 +00002743
2744// Abstract instruction which marks the beginning and/or end of a try block and
2745// links it to the respective exception handlers. Behaves the same as a Goto in
2746// non-exceptional control flow.
2747// Normal-flow successor is stored at index zero, exception handlers under
2748// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002749class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002750 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002751 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002752 kEntry,
2753 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002754 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002755 };
2756
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002758 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2759 SetPackedField<BoundaryKindField>(kind);
2760 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002761
2762 bool IsControlFlow() const OVERRIDE { return true; }
2763
2764 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002765 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002766
David Brazdild26a4112015-11-10 11:07:31 +00002767 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2768 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2769 }
2770
David Brazdilfc6a86a2015-06-26 10:33:45 +00002771 // Returns whether `handler` is among its exception handlers (non-zero index
2772 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002773 bool HasExceptionHandler(const HBasicBlock& handler) const {
2774 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002775 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002776 }
2777
2778 // If not present already, adds `handler` to its block's list of exception
2779 // handlers.
2780 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002781 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002782 GetBlock()->AddSuccessor(handler);
2783 }
2784 }
2785
Vladimir Markoa1de9182016-02-25 11:37:38 +00002786 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2787 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002788
David Brazdilffee3d32015-07-06 11:48:53 +01002789 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2790
David Brazdilfc6a86a2015-06-26 10:33:45 +00002791 DECLARE_INSTRUCTION(TryBoundary);
2792
2793 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002794 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2795 static constexpr size_t kFieldBoundaryKindSize =
2796 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2797 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2798 kFieldBoundaryKind + kFieldBoundaryKindSize;
2799 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2800 "Too many packed fields.");
2801 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002802
2803 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2804};
2805
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002806// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002807class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002808 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002809 // We set CanTriggerGC to prevent any intermediate address to be live
2810 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002811 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002812 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002813 SetRawInputAt(0, cond);
2814 }
2815
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002816 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002817 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002818 return true;
2819 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002820 bool NeedsEnvironment() const OVERRIDE { return true; }
2821 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002822
2823 DECLARE_INSTRUCTION(Deoptimize);
2824
2825 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002826 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2827};
2828
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002829// Represents the ArtMethod that was passed as a first argument to
2830// the method. It is used by instructions that depend on it, like
2831// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002832class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002833 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002834 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2835 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002836
2837 DECLARE_INSTRUCTION(CurrentMethod);
2838
2839 private:
2840 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2841};
2842
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002843// Fetches an ArtMethod from the virtual table or the interface method table
2844// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002845class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002846 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002847 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002848 kVTable,
2849 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002850 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002851 };
2852 HClassTableGet(HInstruction* cls,
2853 Primitive::Type type,
2854 TableKind kind,
2855 size_t index,
2856 uint32_t dex_pc)
2857 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002858 index_(index) {
2859 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002860 SetRawInputAt(0, cls);
2861 }
2862
2863 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002864 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002865 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002866 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002867 }
2868
Vladimir Markoa1de9182016-02-25 11:37:38 +00002869 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002870 size_t GetIndex() const { return index_; }
2871
2872 DECLARE_INSTRUCTION(ClassTableGet);
2873
2874 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002875 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2876 static constexpr size_t kFieldTableKindSize =
2877 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2878 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2879 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2880 "Too many packed fields.");
2881 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2882
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002883 // The index of the ArtMethod in the table.
2884 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002885
2886 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2887};
2888
Mark Mendellfe57faa2015-09-18 09:26:15 -04002889// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2890// have one successor for each entry in the switch table, and the final successor
2891// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002892class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002893 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002894 HPackedSwitch(int32_t start_value,
2895 uint32_t num_entries,
2896 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002897 uint32_t dex_pc = kNoDexPc)
2898 : HTemplateInstruction(SideEffects::None(), dex_pc),
2899 start_value_(start_value),
2900 num_entries_(num_entries) {
2901 SetRawInputAt(0, input);
2902 }
2903
2904 bool IsControlFlow() const OVERRIDE { return true; }
2905
2906 int32_t GetStartValue() const { return start_value_; }
2907
Vladimir Marko211c2112015-09-24 16:52:33 +01002908 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002909
2910 HBasicBlock* GetDefaultBlock() const {
2911 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002912 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002913 }
2914 DECLARE_INSTRUCTION(PackedSwitch);
2915
2916 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002917 const int32_t start_value_;
2918 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002919
2920 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2921};
2922
Roland Levillain88cb1752014-10-20 16:36:47 +01002923class HUnaryOperation : public HExpression<1> {
2924 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002925 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2926 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002927 SetRawInputAt(0, input);
2928 }
2929
2930 HInstruction* GetInput() const { return InputAt(0); }
2931 Primitive::Type GetResultType() const { return GetType(); }
2932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002933 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002934 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002935 return true;
2936 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002937
Roland Levillain31dd3d62016-02-16 12:21:02 +00002938 // Try to statically evaluate `this` and return a HConstant
2939 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002940 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002941 HConstant* TryStaticEvaluation() const;
2942
2943 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002944 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2945 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002946 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2947 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002948
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002949 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002950
2951 private:
2952 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2953};
2954
Dave Allison20dfc792014-06-16 20:44:29 -07002955class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002956 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957 HBinaryOperation(Primitive::Type result_type,
2958 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002959 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002960 SideEffects side_effects = SideEffects::None(),
2961 uint32_t dex_pc = kNoDexPc)
2962 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002963 SetRawInputAt(0, left);
2964 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002965 }
2966
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002967 HInstruction* GetLeft() const { return InputAt(0); }
2968 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002969 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002970
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002971 virtual bool IsCommutative() const { return false; }
2972
2973 // Put constant on the right.
2974 // Returns whether order is changed.
2975 bool OrderInputsWithConstantOnTheRight() {
2976 HInstruction* left = InputAt(0);
2977 HInstruction* right = InputAt(1);
2978 if (left->IsConstant() && !right->IsConstant()) {
2979 ReplaceInput(right, 0);
2980 ReplaceInput(left, 1);
2981 return true;
2982 }
2983 return false;
2984 }
2985
2986 // Order inputs by instruction id, but favor constant on the right side.
2987 // This helps GVN for commutative ops.
2988 void OrderInputs() {
2989 DCHECK(IsCommutative());
2990 HInstruction* left = InputAt(0);
2991 HInstruction* right = InputAt(1);
2992 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2993 return;
2994 }
2995 if (OrderInputsWithConstantOnTheRight()) {
2996 return;
2997 }
2998 // Order according to instruction id.
2999 if (left->GetId() > right->GetId()) {
3000 ReplaceInput(right, 0);
3001 ReplaceInput(left, 1);
3002 }
3003 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003004
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003005 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003006 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003007 return true;
3008 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003009
Roland Levillain31dd3d62016-02-16 12:21:02 +00003010 // Try to statically evaluate `this` and return a HConstant
3011 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003012 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003013 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003014
3015 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003016 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3017 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003018 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3019 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003020 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003021 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3022 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003023 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3024 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003025 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3026 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003027 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3029 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003030
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003031 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003032 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003033 HConstant* GetConstantRight() const;
3034
3035 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003036 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003037 HInstruction* GetLeastConstantLeft() const;
3038
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003039 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003040
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003041 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003042 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3043};
3044
Mark Mendellc4701932015-04-10 13:18:51 -04003045// The comparison bias applies for floating point operations and indicates how NaN
3046// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003047enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003048 kNoBias, // bias is not applicable (i.e. for long operation)
3049 kGtBias, // return 1 for NaN comparisons
3050 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003051 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003052};
3053
Roland Levillain31dd3d62016-02-16 12:21:02 +00003054std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3055
Dave Allison20dfc792014-06-16 20:44:29 -07003056class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003058 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003059 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3060 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3061 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003062
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003063 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003064 // `instruction`, and disregard moves in between.
3065 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003066
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003067 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003068
3069 virtual IfCondition GetCondition() const = 0;
3070
Mark Mendellc4701932015-04-10 13:18:51 -04003071 virtual IfCondition GetOppositeCondition() const = 0;
3072
Vladimir Markoa1de9182016-02-25 11:37:38 +00003073 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003074 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3075
Vladimir Markoa1de9182016-02-25 11:37:38 +00003076 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3077 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003078
Vladimir Marko372f10e2016-05-17 16:30:10 +01003079 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003080 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003081 }
3082
Roland Levillain4fa13f62015-07-06 18:11:54 +01003083 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003085 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003086 if (if_cond == kCondNE) {
3087 return true;
3088 } else if (if_cond == kCondEQ) {
3089 return false;
3090 }
3091 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003092 }
3093
3094 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003095 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003096 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003097 if (if_cond == kCondEQ) {
3098 return true;
3099 } else if (if_cond == kCondNE) {
3100 return false;
3101 }
3102 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003103 }
3104
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003106 // Needed if we merge a HCompare into a HCondition.
3107 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3108 static constexpr size_t kFieldComparisonBiasSize =
3109 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3110 static constexpr size_t kNumberOfConditionPackedBits =
3111 kFieldComparisonBias + kFieldComparisonBiasSize;
3112 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3113 using ComparisonBiasField =
3114 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3115
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116 template <typename T>
3117 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3118
3119 template <typename T>
3120 int32_t CompareFP(T x, T y) const {
3121 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3122 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3123 // Handle the bias.
3124 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3125 }
3126
3127 // Return an integer constant containing the result of a condition evaluated at compile time.
3128 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3129 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3130 }
3131
Dave Allison20dfc792014-06-16 20:44:29 -07003132 private:
3133 DISALLOW_COPY_AND_ASSIGN(HCondition);
3134};
3135
3136// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003137class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003138 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003139 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3140 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003141
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003142 bool IsCommutative() const OVERRIDE { return true; }
3143
Vladimir Marko9e23df52015-11-10 17:14:35 +00003144 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3145 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003146 return MakeConstantCondition(true, GetDexPc());
3147 }
3148 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3149 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3150 }
3151 // In the following Evaluate methods, a HCompare instruction has
3152 // been merged into this HEqual instruction; evaluate it as
3153 // `Compare(x, y) == 0`.
3154 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3155 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3156 GetDexPc());
3157 }
3158 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3159 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3160 }
3161 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3162 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003163 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003164
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003165 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003166
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003167 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003168 return kCondEQ;
3169 }
3170
Mark Mendellc4701932015-04-10 13:18:51 -04003171 IfCondition GetOppositeCondition() const OVERRIDE {
3172 return kCondNE;
3173 }
3174
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003175 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003176 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003177
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003178 DISALLOW_COPY_AND_ASSIGN(HEqual);
3179};
3180
Vladimir Markofcb503c2016-05-18 12:48:17 +01003181class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003183 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3184 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003185
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003186 bool IsCommutative() const OVERRIDE { return true; }
3187
Vladimir Marko9e23df52015-11-10 17:14:35 +00003188 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3189 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003190 return MakeConstantCondition(false, GetDexPc());
3191 }
3192 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3193 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3194 }
3195 // In the following Evaluate methods, a HCompare instruction has
3196 // been merged into this HNotEqual instruction; evaluate it as
3197 // `Compare(x, y) != 0`.
3198 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3199 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3200 }
3201 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3202 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3203 }
3204 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3205 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003206 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003207
Dave Allison20dfc792014-06-16 20:44:29 -07003208 DECLARE_INSTRUCTION(NotEqual);
3209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003210 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003211 return kCondNE;
3212 }
3213
Mark Mendellc4701932015-04-10 13:18:51 -04003214 IfCondition GetOppositeCondition() const OVERRIDE {
3215 return kCondEQ;
3216 }
3217
Dave Allison20dfc792014-06-16 20:44:29 -07003218 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003219 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003220
Dave Allison20dfc792014-06-16 20:44:29 -07003221 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3222};
3223
Vladimir Markofcb503c2016-05-18 12:48:17 +01003224class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003226 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3227 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003228
Roland Levillain9867bc72015-08-05 10:21:34 +01003229 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003230 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003231 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003232 // In the following Evaluate methods, a HCompare instruction has
3233 // been merged into this HLessThan instruction; evaluate it as
3234 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003235 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3237 }
3238 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3239 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3240 }
3241 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3242 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003243 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003244
Dave Allison20dfc792014-06-16 20:44:29 -07003245 DECLARE_INSTRUCTION(LessThan);
3246
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003247 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003248 return kCondLT;
3249 }
3250
Mark Mendellc4701932015-04-10 13:18:51 -04003251 IfCondition GetOppositeCondition() const OVERRIDE {
3252 return kCondGE;
3253 }
3254
Dave Allison20dfc792014-06-16 20:44:29 -07003255 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003256 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003257
Dave Allison20dfc792014-06-16 20:44:29 -07003258 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3259};
3260
Vladimir Markofcb503c2016-05-18 12:48:17 +01003261class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003263 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3264 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003265
Roland Levillain9867bc72015-08-05 10:21:34 +01003266 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003267 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003268 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003269 // In the following Evaluate methods, a HCompare instruction has
3270 // been merged into this HLessThanOrEqual instruction; evaluate it as
3271 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003272 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3274 }
3275 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3276 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3277 }
3278 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3279 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003280 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003281
Dave Allison20dfc792014-06-16 20:44:29 -07003282 DECLARE_INSTRUCTION(LessThanOrEqual);
3283
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003284 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003285 return kCondLE;
3286 }
3287
Mark Mendellc4701932015-04-10 13:18:51 -04003288 IfCondition GetOppositeCondition() const OVERRIDE {
3289 return kCondGT;
3290 }
3291
Dave Allison20dfc792014-06-16 20:44:29 -07003292 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003293 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003294
Dave Allison20dfc792014-06-16 20:44:29 -07003295 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3296};
3297
Vladimir Markofcb503c2016-05-18 12:48:17 +01003298class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003299 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003300 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3301 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003302
Roland Levillain9867bc72015-08-05 10:21:34 +01003303 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003305 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003306 // In the following Evaluate methods, a HCompare instruction has
3307 // been merged into this HGreaterThan instruction; evaluate it as
3308 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003309 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3311 }
3312 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3313 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3314 }
3315 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3316 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003318
Dave Allison20dfc792014-06-16 20:44:29 -07003319 DECLARE_INSTRUCTION(GreaterThan);
3320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003321 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003322 return kCondGT;
3323 }
3324
Mark Mendellc4701932015-04-10 13:18:51 -04003325 IfCondition GetOppositeCondition() const OVERRIDE {
3326 return kCondLE;
3327 }
3328
Dave Allison20dfc792014-06-16 20:44:29 -07003329 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003330 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003331
Dave Allison20dfc792014-06-16 20:44:29 -07003332 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3333};
3334
Vladimir Markofcb503c2016-05-18 12:48:17 +01003335class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003337 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3338 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003339
Roland Levillain9867bc72015-08-05 10:21:34 +01003340 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003341 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003342 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003343 // In the following Evaluate methods, a HCompare instruction has
3344 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3345 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003346 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003347 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3348 }
3349 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3350 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3351 }
3352 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3353 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003354 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003355
Dave Allison20dfc792014-06-16 20:44:29 -07003356 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3357
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003358 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003359 return kCondGE;
3360 }
3361
Mark Mendellc4701932015-04-10 13:18:51 -04003362 IfCondition GetOppositeCondition() const OVERRIDE {
3363 return kCondLT;
3364 }
3365
Dave Allison20dfc792014-06-16 20:44:29 -07003366 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003367 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003368
Dave Allison20dfc792014-06-16 20:44:29 -07003369 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3370};
3371
Vladimir Markofcb503c2016-05-18 12:48:17 +01003372class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003373 public:
3374 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3375 : HCondition(first, second, dex_pc) {}
3376
3377 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003378 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003379 }
3380 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003381 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3382 }
3383 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3384 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3385 LOG(FATAL) << DebugName() << " is not defined for float values";
3386 UNREACHABLE();
3387 }
3388 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3389 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3390 LOG(FATAL) << DebugName() << " is not defined for double values";
3391 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003392 }
3393
3394 DECLARE_INSTRUCTION(Below);
3395
3396 IfCondition GetCondition() const OVERRIDE {
3397 return kCondB;
3398 }
3399
3400 IfCondition GetOppositeCondition() const OVERRIDE {
3401 return kCondAE;
3402 }
3403
3404 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003405 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003406 return MakeUnsigned(x) < MakeUnsigned(y);
3407 }
Aart Bike9f37602015-10-09 11:15:55 -07003408
3409 DISALLOW_COPY_AND_ASSIGN(HBelow);
3410};
3411
Vladimir Markofcb503c2016-05-18 12:48:17 +01003412class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003413 public:
3414 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3415 : HCondition(first, second, dex_pc) {}
3416
3417 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003418 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003419 }
3420 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3422 }
3423 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3424 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3425 LOG(FATAL) << DebugName() << " is not defined for float values";
3426 UNREACHABLE();
3427 }
3428 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3429 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3430 LOG(FATAL) << DebugName() << " is not defined for double values";
3431 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003432 }
3433
3434 DECLARE_INSTRUCTION(BelowOrEqual);
3435
3436 IfCondition GetCondition() const OVERRIDE {
3437 return kCondBE;
3438 }
3439
3440 IfCondition GetOppositeCondition() const OVERRIDE {
3441 return kCondA;
3442 }
3443
3444 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003445 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003446 return MakeUnsigned(x) <= MakeUnsigned(y);
3447 }
Aart Bike9f37602015-10-09 11:15:55 -07003448
3449 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3450};
3451
Vladimir Markofcb503c2016-05-18 12:48:17 +01003452class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003453 public:
3454 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3455 : HCondition(first, second, dex_pc) {}
3456
3457 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003459 }
3460 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003461 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3462 }
3463 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3464 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3465 LOG(FATAL) << DebugName() << " is not defined for float values";
3466 UNREACHABLE();
3467 }
3468 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3469 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3470 LOG(FATAL) << DebugName() << " is not defined for double values";
3471 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003472 }
3473
3474 DECLARE_INSTRUCTION(Above);
3475
3476 IfCondition GetCondition() const OVERRIDE {
3477 return kCondA;
3478 }
3479
3480 IfCondition GetOppositeCondition() const OVERRIDE {
3481 return kCondBE;
3482 }
3483
3484 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003485 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003486 return MakeUnsigned(x) > MakeUnsigned(y);
3487 }
Aart Bike9f37602015-10-09 11:15:55 -07003488
3489 DISALLOW_COPY_AND_ASSIGN(HAbove);
3490};
3491
Vladimir Markofcb503c2016-05-18 12:48:17 +01003492class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003493 public:
3494 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3495 : HCondition(first, second, dex_pc) {}
3496
3497 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003499 }
3500 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003501 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3502 }
3503 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3504 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3505 LOG(FATAL) << DebugName() << " is not defined for float values";
3506 UNREACHABLE();
3507 }
3508 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3509 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3510 LOG(FATAL) << DebugName() << " is not defined for double values";
3511 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003512 }
3513
3514 DECLARE_INSTRUCTION(AboveOrEqual);
3515
3516 IfCondition GetCondition() const OVERRIDE {
3517 return kCondAE;
3518 }
3519
3520 IfCondition GetOppositeCondition() const OVERRIDE {
3521 return kCondB;
3522 }
3523
3524 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003525 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003526 return MakeUnsigned(x) >= MakeUnsigned(y);
3527 }
Aart Bike9f37602015-10-09 11:15:55 -07003528
3529 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3530};
Dave Allison20dfc792014-06-16 20:44:29 -07003531
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003532// Instruction to check how two inputs compare to each other.
3533// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003534class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003535 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003536 // Note that `comparison_type` is the type of comparison performed
3537 // between the comparison's inputs, not the type of the instantiated
3538 // HCompare instruction (which is always Primitive::kPrimInt).
3539 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003540 HInstruction* first,
3541 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003542 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003543 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003544 : HBinaryOperation(Primitive::kPrimInt,
3545 first,
3546 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003547 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003548 dex_pc) {
3549 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003550 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3551 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003552 }
3553
Roland Levillain9867bc72015-08-05 10:21:34 +01003554 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003555 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3556
3557 template <typename T>
3558 int32_t ComputeFP(T x, T y) const {
3559 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3560 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3561 // Handle the bias.
3562 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3563 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003564
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003566 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3567 // reach this code path when processing a freshly built HIR
3568 // graph. However HCompare integer instructions can be synthesized
3569 // by the instruction simplifier to implement IntegerCompare and
3570 // IntegerSignum intrinsics, so we have to handle this case.
3571 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003572 }
3573 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3575 }
3576 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3577 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3578 }
3579 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3580 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003581 }
3582
Vladimir Marko372f10e2016-05-17 16:30:10 +01003583 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003584 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003585 }
3586
Vladimir Markoa1de9182016-02-25 11:37:38 +00003587 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003588
Roland Levillain31dd3d62016-02-16 12:21:02 +00003589 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003590 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 bool IsGtBias() const {
3592 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003593 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003594 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003595
Roland Levillain1693a1f2016-03-15 14:57:31 +00003596 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3597 // Comparisons do not require a runtime call in any back end.
3598 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003599 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003600
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003601 DECLARE_INSTRUCTION(Compare);
3602
Roland Levillain31dd3d62016-02-16 12:21:02 +00003603 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003604 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3605 static constexpr size_t kFieldComparisonBiasSize =
3606 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3607 static constexpr size_t kNumberOfComparePackedBits =
3608 kFieldComparisonBias + kFieldComparisonBiasSize;
3609 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3610 using ComparisonBiasField =
3611 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3612
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 // Return an integer constant containing the result of a comparison evaluated at compile time.
3614 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3615 DCHECK(value == -1 || value == 0 || value == 1) << value;
3616 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3617 }
3618
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003619 private:
3620 DISALLOW_COPY_AND_ASSIGN(HCompare);
3621};
3622
Vladimir Markofcb503c2016-05-18 12:48:17 +01003623class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003624 public:
3625 HNewInstance(HInstruction* cls,
3626 HCurrentMethod* current_method,
3627 uint32_t dex_pc,
3628 uint16_t type_index,
3629 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003630 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003631 bool finalizable,
3632 QuickEntrypointEnum entrypoint)
3633 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3634 type_index_(type_index),
3635 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003636 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003637 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003638 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003639 SetRawInputAt(0, cls);
3640 SetRawInputAt(1, current_method);
3641 }
3642
3643 uint16_t GetTypeIndex() const { return type_index_; }
3644 const DexFile& GetDexFile() const { return dex_file_; }
3645
3646 // Calls runtime so needs an environment.
3647 bool NeedsEnvironment() const OVERRIDE { return true; }
3648
Mingyao Yang062157f2016-03-02 10:15:36 -08003649 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3650 bool CanThrow() const OVERRIDE { return true; }
3651
3652 // Needs to call into runtime to make sure it's instantiable/accessible.
3653 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003654
Vladimir Markoa1de9182016-02-25 11:37:38 +00003655 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003656
3657 bool CanBeNull() const OVERRIDE { return false; }
3658
3659 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3660
3661 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3662 entrypoint_ = entrypoint;
3663 }
3664
3665 bool IsStringAlloc() const;
3666
3667 DECLARE_INSTRUCTION(NewInstance);
3668
3669 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003670 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3671 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003672 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3673 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3674 "Too many packed fields.");
3675
David Brazdil6de19382016-01-08 17:37:10 +00003676 const uint16_t type_index_;
3677 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003678 QuickEntrypointEnum entrypoint_;
3679
3680 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3681};
3682
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003683enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003684#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3685 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003686#include "intrinsics_list.h"
3687 kNone,
3688 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3689#undef INTRINSICS_LIST
3690#undef OPTIMIZING_INTRINSICS
3691};
3692std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3693
Agi Csaki05f20562015-08-19 14:58:14 -07003694enum IntrinsicNeedsEnvironmentOrCache {
3695 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3696 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003697};
3698
Aart Bik5d75afe2015-12-14 11:57:01 -08003699enum IntrinsicSideEffects {
3700 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3701 kReadSideEffects, // Intrinsic may read heap memory.
3702 kWriteSideEffects, // Intrinsic may write heap memory.
3703 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3704};
3705
3706enum IntrinsicExceptions {
3707 kNoThrow, // Intrinsic does not throw any exceptions.
3708 kCanThrow // Intrinsic may throw exceptions.
3709};
3710
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003711class HInvoke : public HInstruction {
3712 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003713 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003714
Vladimir Marko372f10e2016-05-17 16:30:10 +01003715 using HInstruction::GetInputRecords; // Keep the const version visible.
3716 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3717 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3718 }
3719
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003720 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003721 SetRawInputAt(index, argument);
3722 }
3723
Roland Levillain3e3d7332015-04-28 11:00:54 +01003724 // Return the number of arguments. This number can be lower than
3725 // the number of inputs returned by InputCount(), as some invoke
3726 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3727 // inputs at the end of their list of inputs.
3728 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3729
Vladimir Markoa1de9182016-02-25 11:37:38 +00003730 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003731
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003732 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003733 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003734
Vladimir Markoa1de9182016-02-25 11:37:38 +00003735 InvokeType GetOriginalInvokeType() const {
3736 return GetPackedField<OriginalInvokeTypeField>();
3737 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003738
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003739 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003740 return intrinsic_;
3741 }
3742
Aart Bik5d75afe2015-12-14 11:57:01 -08003743 void SetIntrinsic(Intrinsics intrinsic,
3744 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3745 IntrinsicSideEffects side_effects,
3746 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003747
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003748 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003749 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003750 }
3751
Vladimir Markoa1de9182016-02-25 11:37:38 +00003752 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003753
3754 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3755
Vladimir Marko372f10e2016-05-17 16:30:10 +01003756 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003757 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3758 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003759
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003760 uint32_t* GetIntrinsicOptimizations() {
3761 return &intrinsic_optimizations_;
3762 }
3763
3764 const uint32_t* GetIntrinsicOptimizations() const {
3765 return &intrinsic_optimizations_;
3766 }
3767
3768 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3769
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003770 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003771
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003772 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003773 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3774 static constexpr size_t kFieldOriginalInvokeTypeSize =
3775 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3776 static constexpr size_t kFieldReturnType =
3777 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3778 static constexpr size_t kFieldReturnTypeSize =
3779 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3780 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3781 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3782 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3783 using OriginalInvokeTypeField =
3784 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3785 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3786
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003787 HInvoke(ArenaAllocator* arena,
3788 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003789 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003790 Primitive::Type return_type,
3791 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003792 uint32_t dex_method_index,
3793 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003794 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003795 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003796 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003797 inputs_(number_of_arguments + number_of_other_inputs,
3798 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003799 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003800 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003801 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003802 SetPackedField<ReturnTypeField>(return_type);
3803 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3804 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003805 }
3806
Vladimir Markoa1de9182016-02-25 11:37:38 +00003807 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003808
Roland Levillain3e3d7332015-04-28 11:00:54 +01003809 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003810 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003811 const uint32_t dex_method_index_;
3812 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003813
3814 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3815 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003816
3817 private:
3818 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3819};
3820
Vladimir Markofcb503c2016-05-18 12:48:17 +01003821class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003822 public:
3823 HInvokeUnresolved(ArenaAllocator* arena,
3824 uint32_t number_of_arguments,
3825 Primitive::Type return_type,
3826 uint32_t dex_pc,
3827 uint32_t dex_method_index,
3828 InvokeType invoke_type)
3829 : HInvoke(arena,
3830 number_of_arguments,
3831 0u /* number_of_other_inputs */,
3832 return_type,
3833 dex_pc,
3834 dex_method_index,
3835 invoke_type) {
3836 }
3837
3838 DECLARE_INSTRUCTION(InvokeUnresolved);
3839
3840 private:
3841 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3842};
3843
Vladimir Markofcb503c2016-05-18 12:48:17 +01003844class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003845 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003846 // Requirements of this method call regarding the class
3847 // initialization (clinit) check of its declaring class.
3848 enum class ClinitCheckRequirement {
3849 kNone, // Class already initialized.
3850 kExplicit, // Static call having explicit clinit check as last input.
3851 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003852 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003853 };
3854
Vladimir Marko58155012015-08-19 12:49:41 +00003855 // Determines how to load the target ArtMethod*.
3856 enum class MethodLoadKind {
3857 // Use a String init ArtMethod* loaded from Thread entrypoints.
3858 kStringInit,
3859
3860 // Use the method's own ArtMethod* loaded by the register allocator.
3861 kRecursive,
3862
3863 // Use ArtMethod* at a known address, embed the direct address in the code.
3864 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3865 kDirectAddress,
3866
3867 // Use ArtMethod* at an address that will be known at link time, embed the direct
3868 // address in the code. If the image is relocatable, emit .patch_oat entry.
3869 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3870 // the image relocatable or not.
3871 kDirectAddressWithFixup,
3872
Vladimir Markoa1de9182016-02-25 11:37:38 +00003873 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003874 // Used when we need to use the dex cache, for example for invoke-static that
3875 // may cause class initialization (the entry may point to a resolution method),
3876 // and we know that we can access the dex cache arrays using a PC-relative load.
3877 kDexCachePcRelative,
3878
3879 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3880 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3881 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3882 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3883 kDexCacheViaMethod,
3884 };
3885
3886 // Determines the location of the code pointer.
3887 enum class CodePtrLocation {
3888 // Recursive call, use local PC-relative call instruction.
3889 kCallSelf,
3890
3891 // Use PC-relative call instruction patched at link time.
3892 // Used for calls within an oat file, boot->boot or app->app.
3893 kCallPCRelative,
3894
3895 // Call to a known target address, embed the direct address in code.
3896 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3897 kCallDirect,
3898
3899 // Call to a target address that will be known at link time, embed the direct
3900 // address in code. If the image is relocatable, emit .patch_oat entry.
3901 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3902 // the image relocatable or not.
3903 kCallDirectWithFixup,
3904
3905 // Use code pointer from the ArtMethod*.
3906 // Used when we don't know the target code. This is also the last-resort-kind used when
3907 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3908 kCallArtMethod,
3909 };
3910
3911 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003912 MethodLoadKind method_load_kind;
3913 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003914 // The method load data holds
3915 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3916 // Note that there are multiple string init methods, each having its own offset.
3917 // - the method address for kDirectAddress
3918 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003919 uint64_t method_load_data;
3920 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003921 };
3922
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003923 HInvokeStaticOrDirect(ArenaAllocator* arena,
3924 uint32_t number_of_arguments,
3925 Primitive::Type return_type,
3926 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003927 uint32_t method_index,
3928 MethodReference target_method,
3929 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003930 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003931 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003932 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003933 : HInvoke(arena,
3934 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003935 // There is potentially one extra argument for the HCurrentMethod node, and
3936 // potentially one other if the clinit check is explicit, and potentially
3937 // one other if the method is a string factory.
3938 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003939 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003940 return_type,
3941 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003942 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003943 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003944 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003945 dispatch_info_(dispatch_info) {
3946 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3947 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3948 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003949
Vladimir Markodc151b22015-10-15 18:02:30 +01003950 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003951 bool had_current_method_input = HasCurrentMethodInput();
3952 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3953
3954 // Using the current method is the default and once we find a better
3955 // method load kind, we should not go back to using the current method.
3956 DCHECK(had_current_method_input || !needs_current_method_input);
3957
3958 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003959 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3960 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003961 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003962 dispatch_info_ = dispatch_info;
3963 }
3964
Vladimir Markoc53c0792015-11-19 15:48:33 +00003965 void AddSpecialInput(HInstruction* input) {
3966 // We allow only one special input.
3967 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3968 DCHECK(InputCount() == GetSpecialInputIndex() ||
3969 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3970 InsertInputAt(GetSpecialInputIndex(), input);
3971 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003972
Vladimir Marko372f10e2016-05-17 16:30:10 +01003973 using HInstruction::GetInputRecords; // Keep the const version visible.
3974 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3975 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3976 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3977 DCHECK(!input_records.empty());
3978 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3979 HInstruction* last_input = input_records.back().GetInstruction();
3980 // Note: `last_input` may be null during arguments setup.
3981 if (last_input != nullptr) {
3982 // `last_input` is the last input of a static invoke marked as having
3983 // an explicit clinit check. It must either be:
3984 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3985 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3986 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3987 }
3988 }
3989 return input_records;
3990 }
3991
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003992 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003993 // We access the method via the dex cache so we can't do an implicit null check.
3994 // TODO: for intrinsics we can generate implicit null checks.
3995 return false;
3996 }
3997
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003998 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003999 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004000 }
4001
Vladimir Markoc53c0792015-11-19 15:48:33 +00004002 // Get the index of the special input, if any.
4003 //
David Brazdil6de19382016-01-08 17:37:10 +00004004 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4005 // method pointer; otherwise there may be one platform-specific special input,
4006 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004007 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004008 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004009
Vladimir Markoa1de9182016-02-25 11:37:38 +00004010 InvokeType GetOptimizedInvokeType() const {
4011 return GetPackedField<OptimizedInvokeTypeField>();
4012 }
4013
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004014 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004015 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004016 }
4017
Vladimir Marko58155012015-08-19 12:49:41 +00004018 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4019 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4020 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004021 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004022 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004023 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004024 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004025 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4026 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004027 bool HasCurrentMethodInput() const {
4028 // This function can be called only after the invoke has been fully initialized by the builder.
4029 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004030 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004031 return true;
4032 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004033 DCHECK(InputCount() == GetSpecialInputIndex() ||
4034 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004035 return false;
4036 }
4037 }
Vladimir Marko58155012015-08-19 12:49:41 +00004038 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4039 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004040 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004041
4042 int32_t GetStringInitOffset() const {
4043 DCHECK(IsStringInit());
4044 return dispatch_info_.method_load_data;
4045 }
4046
4047 uint64_t GetMethodAddress() const {
4048 DCHECK(HasMethodAddress());
4049 return dispatch_info_.method_load_data;
4050 }
4051
4052 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004053 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004054 return dispatch_info_.method_load_data;
4055 }
4056
4057 uint64_t GetDirectCodePtr() const {
4058 DCHECK(HasDirectCodePtr());
4059 return dispatch_info_.direct_code_ptr;
4060 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004061
Vladimir Markoa1de9182016-02-25 11:37:38 +00004062 ClinitCheckRequirement GetClinitCheckRequirement() const {
4063 return GetPackedField<ClinitCheckRequirementField>();
4064 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004065
Roland Levillain4c0eb422015-04-24 16:43:49 +01004066 // Is this instruction a call to a static method?
4067 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004068 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004069 }
4070
Vladimir Markofbb184a2015-11-13 14:47:00 +00004071 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4072 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4073 // instruction; only relevant for static calls with explicit clinit check.
4074 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004075 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004076 size_t last_input_index = inputs_.size() - 1u;
4077 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004078 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004079 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004080 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004081 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004082 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004083 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004084 }
4085
4086 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004087 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004088 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004089 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004090 }
4091
4092 // Is this a call to a static method whose declaring class has an
4093 // implicit intialization check requirement?
4094 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004095 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004096 }
4097
Vladimir Markob554b5a2015-11-06 12:57:55 +00004098 // Does this method load kind need the current method as an input?
4099 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4100 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4101 }
4102
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004103 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004104
Roland Levillain4c0eb422015-04-24 16:43:49 +01004105 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004106 void InsertInputAt(size_t index, HInstruction* input);
4107 void RemoveInputAt(size_t index);
4108
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004109 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004110 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4111 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4112 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4113 static constexpr size_t kFieldClinitCheckRequirement =
4114 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4115 static constexpr size_t kFieldClinitCheckRequirementSize =
4116 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4117 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4118 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4119 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4120 "Too many packed fields.");
4121 using OptimizedInvokeTypeField =
4122 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4123 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4124 kFieldClinitCheckRequirement,
4125 kFieldClinitCheckRequirementSize>;
4126
Vladimir Marko58155012015-08-19 12:49:41 +00004127 // The target method may refer to different dex file or method index than the original
4128 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4129 // in derived class to increase visibility.
4130 MethodReference target_method_;
4131 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004132
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004133 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004134};
Vladimir Markof64242a2015-12-01 14:58:23 +00004135std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004136std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004137
Vladimir Markofcb503c2016-05-18 12:48:17 +01004138class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004139 public:
4140 HInvokeVirtual(ArenaAllocator* arena,
4141 uint32_t number_of_arguments,
4142 Primitive::Type return_type,
4143 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004144 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004145 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004146 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004147 vtable_index_(vtable_index) {}
4148
Calin Juravle641547a2015-04-21 22:08:51 +01004149 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004150 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004151 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004152 }
4153
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004154 uint32_t GetVTableIndex() const { return vtable_index_; }
4155
4156 DECLARE_INSTRUCTION(InvokeVirtual);
4157
4158 private:
4159 const uint32_t vtable_index_;
4160
4161 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4162};
4163
Vladimir Markofcb503c2016-05-18 12:48:17 +01004164class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004165 public:
4166 HInvokeInterface(ArenaAllocator* arena,
4167 uint32_t number_of_arguments,
4168 Primitive::Type return_type,
4169 uint32_t dex_pc,
4170 uint32_t dex_method_index,
4171 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004172 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004173 imt_index_(imt_index) {}
4174
Calin Juravle641547a2015-04-21 22:08:51 +01004175 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004176 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004177 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004178 }
4179
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004180 uint32_t GetImtIndex() const { return imt_index_; }
4181 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4182
4183 DECLARE_INSTRUCTION(InvokeInterface);
4184
4185 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004186 const uint32_t imt_index_;
4187
4188 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4189};
4190
Vladimir Markofcb503c2016-05-18 12:48:17 +01004191class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004192 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004193 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004194 : HUnaryOperation(result_type, input, dex_pc) {
4195 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4196 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004197
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004198 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004199
4200 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004201 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004202 }
4203 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004204 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004205 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004206 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4207 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4208 }
4209 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4210 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4211 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004212
Roland Levillain88cb1752014-10-20 16:36:47 +01004213 DECLARE_INSTRUCTION(Neg);
4214
4215 private:
4216 DISALLOW_COPY_AND_ASSIGN(HNeg);
4217};
4218
Vladimir Markofcb503c2016-05-18 12:48:17 +01004219class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004220 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004221 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004222 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004223 uint32_t dex_pc,
4224 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004225 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004226 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004228 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004229 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004230 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004231 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004232 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004233 }
4234
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004235 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004236 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004237
4238 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004239 bool NeedsEnvironment() const OVERRIDE { return true; }
4240
Mingyao Yang0c365e62015-03-31 15:09:29 -07004241 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4242 bool CanThrow() const OVERRIDE { return true; }
4243
Calin Juravle10e244f2015-01-26 18:54:32 +00004244 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004245
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004246 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4247
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004248 DECLARE_INSTRUCTION(NewArray);
4249
4250 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004251 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004252 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004253 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004254
4255 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4256};
4257
Vladimir Markofcb503c2016-05-18 12:48:17 +01004258class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004259 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004260 HAdd(Primitive::Type result_type,
4261 HInstruction* left,
4262 HInstruction* right,
4263 uint32_t dex_pc = kNoDexPc)
4264 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004265
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004266 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004267
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004268 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004269
4270 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004271 return GetBlock()->GetGraph()->GetIntConstant(
4272 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004273 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004274 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004275 return GetBlock()->GetGraph()->GetLongConstant(
4276 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004277 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004278 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4279 return GetBlock()->GetGraph()->GetFloatConstant(
4280 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4281 }
4282 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4283 return GetBlock()->GetGraph()->GetDoubleConstant(
4284 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4285 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004286
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004287 DECLARE_INSTRUCTION(Add);
4288
4289 private:
4290 DISALLOW_COPY_AND_ASSIGN(HAdd);
4291};
4292
Vladimir Markofcb503c2016-05-18 12:48:17 +01004293class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004294 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004295 HSub(Primitive::Type result_type,
4296 HInstruction* left,
4297 HInstruction* right,
4298 uint32_t dex_pc = kNoDexPc)
4299 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004300
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004301 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004302
4303 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 return GetBlock()->GetGraph()->GetIntConstant(
4305 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004306 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004307 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004308 return GetBlock()->GetGraph()->GetLongConstant(
4309 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004310 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004311 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4312 return GetBlock()->GetGraph()->GetFloatConstant(
4313 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4314 }
4315 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4316 return GetBlock()->GetGraph()->GetDoubleConstant(
4317 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4318 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004319
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004320 DECLARE_INSTRUCTION(Sub);
4321
4322 private:
4323 DISALLOW_COPY_AND_ASSIGN(HSub);
4324};
4325
Vladimir Markofcb503c2016-05-18 12:48:17 +01004326class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004327 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004328 HMul(Primitive::Type result_type,
4329 HInstruction* left,
4330 HInstruction* right,
4331 uint32_t dex_pc = kNoDexPc)
4332 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004334 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004335
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004336 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004337
4338 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004339 return GetBlock()->GetGraph()->GetIntConstant(
4340 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004341 }
4342 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004343 return GetBlock()->GetGraph()->GetLongConstant(
4344 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004345 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004346 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4347 return GetBlock()->GetGraph()->GetFloatConstant(
4348 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4349 }
4350 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4351 return GetBlock()->GetGraph()->GetDoubleConstant(
4352 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4353 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004354
4355 DECLARE_INSTRUCTION(Mul);
4356
4357 private:
4358 DISALLOW_COPY_AND_ASSIGN(HMul);
4359};
4360
Vladimir Markofcb503c2016-05-18 12:48:17 +01004361class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004362 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004363 HDiv(Primitive::Type result_type,
4364 HInstruction* left,
4365 HInstruction* right,
4366 uint32_t dex_pc)
4367 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004368
Roland Levillain9867bc72015-08-05 10:21:34 +01004369 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004370 T ComputeIntegral(T x, T y) const {
4371 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004372 // Our graph structure ensures we never have 0 for `y` during
4373 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004374 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004375 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004376 return (y == -1) ? -x : x / y;
4377 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004378
Roland Levillain31dd3d62016-02-16 12:21:02 +00004379 template <typename T>
4380 T ComputeFP(T x, T y) const {
4381 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4382 return x / y;
4383 }
4384
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004386 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004387 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004388 }
4389 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004390 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004391 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4392 }
4393 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4394 return GetBlock()->GetGraph()->GetFloatConstant(
4395 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4396 }
4397 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4398 return GetBlock()->GetGraph()->GetDoubleConstant(
4399 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004400 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004401
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004402 static SideEffects SideEffectsForArchRuntimeCalls() {
4403 // The generated code can use a runtime call.
4404 return SideEffects::CanTriggerGC();
4405 }
4406
Calin Juravle7c4954d2014-10-28 16:57:40 +00004407 DECLARE_INSTRUCTION(Div);
4408
4409 private:
4410 DISALLOW_COPY_AND_ASSIGN(HDiv);
4411};
4412
Vladimir Markofcb503c2016-05-18 12:48:17 +01004413class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004414 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004415 HRem(Primitive::Type result_type,
4416 HInstruction* left,
4417 HInstruction* right,
4418 uint32_t dex_pc)
4419 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004420
Roland Levillain9867bc72015-08-05 10:21:34 +01004421 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004422 T ComputeIntegral(T x, T y) const {
4423 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004424 // Our graph structure ensures we never have 0 for `y` during
4425 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004426 DCHECK_NE(y, 0);
4427 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4428 return (y == -1) ? 0 : x % y;
4429 }
4430
Roland Levillain31dd3d62016-02-16 12:21:02 +00004431 template <typename T>
4432 T ComputeFP(T x, T y) const {
4433 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4434 return std::fmod(x, y);
4435 }
4436
Roland Levillain9867bc72015-08-05 10:21:34 +01004437 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004438 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004439 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004440 }
4441 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004442 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004443 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004444 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004445 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4446 return GetBlock()->GetGraph()->GetFloatConstant(
4447 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4448 }
4449 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4450 return GetBlock()->GetGraph()->GetDoubleConstant(
4451 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4452 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004453
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004454 static SideEffects SideEffectsForArchRuntimeCalls() {
4455 return SideEffects::CanTriggerGC();
4456 }
4457
Calin Juravlebacfec32014-11-14 15:54:36 +00004458 DECLARE_INSTRUCTION(Rem);
4459
4460 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004461 DISALLOW_COPY_AND_ASSIGN(HRem);
4462};
4463
Vladimir Markofcb503c2016-05-18 12:48:17 +01004464class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004465 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004466 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4467 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004468 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004469 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004470 SetRawInputAt(0, value);
4471 }
4472
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004473 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4474
Calin Juravled0d48522014-11-04 16:40:20 +00004475 bool CanBeMoved() const OVERRIDE { return true; }
4476
Vladimir Marko372f10e2016-05-17 16:30:10 +01004477 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004478 return true;
4479 }
4480
4481 bool NeedsEnvironment() const OVERRIDE { return true; }
4482 bool CanThrow() const OVERRIDE { return true; }
4483
Calin Juravled0d48522014-11-04 16:40:20 +00004484 DECLARE_INSTRUCTION(DivZeroCheck);
4485
4486 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004487 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4488};
4489
Vladimir Markofcb503c2016-05-18 12:48:17 +01004490class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004491 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004492 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004493 HInstruction* value,
4494 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004496 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4497 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4498 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 }
4500
Roland Levillain5b5b9312016-03-22 14:57:31 +00004501 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004502 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004503 return value << (distance & max_shift_distance);
4504 }
4505
4506 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004507 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004508 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004511 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004512 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004514 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4515 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4516 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4517 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4520 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004521 LOG(FATAL) << DebugName() << " is not defined for float values";
4522 UNREACHABLE();
4523 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4525 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004526 LOG(FATAL) << DebugName() << " is not defined for double values";
4527 UNREACHABLE();
4528 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004529
4530 DECLARE_INSTRUCTION(Shl);
4531
4532 private:
4533 DISALLOW_COPY_AND_ASSIGN(HShl);
4534};
4535
Vladimir Markofcb503c2016-05-18 12:48:17 +01004536class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004538 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004539 HInstruction* value,
4540 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004542 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4543 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4544 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004545 }
4546
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004548 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 return value >> (distance & max_shift_distance);
4550 }
4551
4552 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004553 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004557 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004558 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004559 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004560 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4561 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4562 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4563 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004564 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4566 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004567 LOG(FATAL) << DebugName() << " is not defined for float values";
4568 UNREACHABLE();
4569 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4571 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004572 LOG(FATAL) << DebugName() << " is not defined for double values";
4573 UNREACHABLE();
4574 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004575
4576 DECLARE_INSTRUCTION(Shr);
4577
4578 private:
4579 DISALLOW_COPY_AND_ASSIGN(HShr);
4580};
4581
Vladimir Markofcb503c2016-05-18 12:48:17 +01004582class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004583 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004584 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004585 HInstruction* value,
4586 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004587 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004588 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4589 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4590 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004591 }
4592
Roland Levillain5b5b9312016-03-22 14:57:31 +00004593 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004594 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 typedef typename std::make_unsigned<T>::type V;
4596 V ux = static_cast<V>(value);
4597 return static_cast<T>(ux >> (distance & max_shift_distance));
4598 }
4599
4600 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004601 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004602 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004603 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004604 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004605 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004606 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004607 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004608 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4609 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4610 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4611 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004612 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004613 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4614 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004615 LOG(FATAL) << DebugName() << " is not defined for float values";
4616 UNREACHABLE();
4617 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004618 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4619 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004620 LOG(FATAL) << DebugName() << " is not defined for double values";
4621 UNREACHABLE();
4622 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004623
4624 DECLARE_INSTRUCTION(UShr);
4625
4626 private:
4627 DISALLOW_COPY_AND_ASSIGN(HUShr);
4628};
4629
Vladimir Markofcb503c2016-05-18 12:48:17 +01004630class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 HAnd(Primitive::Type result_type,
4633 HInstruction* left,
4634 HInstruction* right,
4635 uint32_t dex_pc = kNoDexPc)
4636 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004637
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004638 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004639
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004640 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004641
4642 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 return GetBlock()->GetGraph()->GetIntConstant(
4644 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004647 return GetBlock()->GetGraph()->GetLongConstant(
4648 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004649 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004650 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4651 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4652 LOG(FATAL) << DebugName() << " is not defined for float values";
4653 UNREACHABLE();
4654 }
4655 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4656 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4657 LOG(FATAL) << DebugName() << " is not defined for double values";
4658 UNREACHABLE();
4659 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004660
4661 DECLARE_INSTRUCTION(And);
4662
4663 private:
4664 DISALLOW_COPY_AND_ASSIGN(HAnd);
4665};
4666
Vladimir Markofcb503c2016-05-18 12:48:17 +01004667class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 HOr(Primitive::Type result_type,
4670 HInstruction* left,
4671 HInstruction* right,
4672 uint32_t dex_pc = kNoDexPc)
4673 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004674
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004675 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004676
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004677 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004678
4679 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 return GetBlock()->GetGraph()->GetIntConstant(
4681 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004683 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004684 return GetBlock()->GetGraph()->GetLongConstant(
4685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004686 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004687 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4688 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4689 LOG(FATAL) << DebugName() << " is not defined for float values";
4690 UNREACHABLE();
4691 }
4692 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4693 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4694 LOG(FATAL) << DebugName() << " is not defined for double values";
4695 UNREACHABLE();
4696 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004697
4698 DECLARE_INSTRUCTION(Or);
4699
4700 private:
4701 DISALLOW_COPY_AND_ASSIGN(HOr);
4702};
4703
Vladimir Markofcb503c2016-05-18 12:48:17 +01004704class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004705 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004706 HXor(Primitive::Type result_type,
4707 HInstruction* left,
4708 HInstruction* right,
4709 uint32_t dex_pc = kNoDexPc)
4710 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004711
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004712 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004713
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004714 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004715
4716 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004717 return GetBlock()->GetGraph()->GetIntConstant(
4718 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004720 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004721 return GetBlock()->GetGraph()->GetLongConstant(
4722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004723 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004724 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4725 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4726 LOG(FATAL) << DebugName() << " is not defined for float values";
4727 UNREACHABLE();
4728 }
4729 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4730 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4731 LOG(FATAL) << DebugName() << " is not defined for double values";
4732 UNREACHABLE();
4733 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004734
4735 DECLARE_INSTRUCTION(Xor);
4736
4737 private:
4738 DISALLOW_COPY_AND_ASSIGN(HXor);
4739};
4740
Vladimir Markofcb503c2016-05-18 12:48:17 +01004741class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742 public:
4743 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004744 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004745 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4746 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004747 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004750 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004751 typedef typename std::make_unsigned<T>::type V;
4752 V ux = static_cast<V>(value);
4753 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004754 return static_cast<T>(ux);
4755 } else {
4756 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 return static_cast<T>(ux >> (distance & max_shift_value)) |
4758 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004759 }
4760 }
4761
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004763 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004765 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004766 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004767 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004768 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004769 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4771 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4772 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4773 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004774 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4776 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004777 LOG(FATAL) << DebugName() << " is not defined for float values";
4778 UNREACHABLE();
4779 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4781 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004782 LOG(FATAL) << DebugName() << " is not defined for double values";
4783 UNREACHABLE();
4784 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004785
4786 DECLARE_INSTRUCTION(Ror);
4787
4788 private:
4789 DISALLOW_COPY_AND_ASSIGN(HRor);
4790};
4791
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004792// The value of a parameter in this method. Its location depends on
4793// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004794class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004795 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004796 HParameterValue(const DexFile& dex_file,
4797 uint16_t type_index,
4798 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 Primitive::Type parameter_type,
4800 bool is_this = false)
4801 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004802 dex_file_(dex_file),
4803 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004804 index_(index) {
4805 SetPackedFlag<kFlagIsThis>(is_this);
4806 SetPackedFlag<kFlagCanBeNull>(!is_this);
4807 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004808
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004809 const DexFile& GetDexFile() const { return dex_file_; }
4810 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004811 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004812 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004813
Vladimir Markoa1de9182016-02-25 11:37:38 +00004814 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4815 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004816
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004817 DECLARE_INSTRUCTION(ParameterValue);
4818
4819 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004820 // Whether or not the parameter value corresponds to 'this' argument.
4821 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4822 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4823 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4824 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4825 "Too many packed fields.");
4826
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004827 const DexFile& dex_file_;
4828 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004829 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004830 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004831 const uint8_t index_;
4832
4833 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4834};
4835
Vladimir Markofcb503c2016-05-18 12:48:17 +01004836class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004837 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004838 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4839 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004840
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004841 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004842 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004843 return true;
4844 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004845
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004846 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004847
4848 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004849 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004850 }
4851 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004852 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004853 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004854 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4855 LOG(FATAL) << DebugName() << " is not defined for float values";
4856 UNREACHABLE();
4857 }
4858 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4859 LOG(FATAL) << DebugName() << " is not defined for double values";
4860 UNREACHABLE();
4861 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004862
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004863 DECLARE_INSTRUCTION(Not);
4864
4865 private:
4866 DISALLOW_COPY_AND_ASSIGN(HNot);
4867};
4868
Vladimir Markofcb503c2016-05-18 12:48:17 +01004869class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004870 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4872 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004873
4874 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004875 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004876 return true;
4877 }
4878
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004879 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004880 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004881 return !x;
4882 }
4883
Roland Levillain9867bc72015-08-05 10:21:34 +01004884 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004885 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004886 }
4887 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4888 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004889 UNREACHABLE();
4890 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004891 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4892 LOG(FATAL) << DebugName() << " is not defined for float values";
4893 UNREACHABLE();
4894 }
4895 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4896 LOG(FATAL) << DebugName() << " is not defined for double values";
4897 UNREACHABLE();
4898 }
David Brazdil66d126e2015-04-03 16:02:44 +01004899
4900 DECLARE_INSTRUCTION(BooleanNot);
4901
4902 private:
4903 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4904};
4905
Vladimir Markofcb503c2016-05-18 12:48:17 +01004906class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004907 public:
4908 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004909 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004910 : HExpression(result_type,
4911 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4912 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004913 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004914 // Invariant: We should never generate a conversion to a Boolean value.
4915 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004916 }
4917
4918 HInstruction* GetInput() const { return InputAt(0); }
4919 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4920 Primitive::Type GetResultType() const { return GetType(); }
4921
4922 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004923 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4924 return true;
4925 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004926
Mark Mendelle82549b2015-05-06 10:55:34 -04004927 // Try to statically evaluate the conversion and return a HConstant
4928 // containing the result. If the input cannot be converted, return nullptr.
4929 HConstant* TryStaticEvaluation() const;
4930
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004931 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4932 Primitive::Type result_type) {
4933 // Some architectures may not require the 'GC' side effects, but at this point
4934 // in the compilation process we do not know what architecture we will
4935 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004936 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4937 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004938 return SideEffects::CanTriggerGC();
4939 }
4940 return SideEffects::None();
4941 }
4942
Roland Levillaindff1f282014-11-05 14:15:05 +00004943 DECLARE_INSTRUCTION(TypeConversion);
4944
4945 private:
4946 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4947};
4948
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004949static constexpr uint32_t kNoRegNumber = -1;
4950
Vladimir Markofcb503c2016-05-18 12:48:17 +01004951class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004952 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004953 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4954 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004955 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004956 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004957 SetRawInputAt(0, value);
4958 }
4959
Calin Juravle10e244f2015-01-26 18:54:32 +00004960 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004961 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004962 return true;
4963 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004964
Calin Juravle10e244f2015-01-26 18:54:32 +00004965 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004966
Calin Juravle10e244f2015-01-26 18:54:32 +00004967 bool CanThrow() const OVERRIDE { return true; }
4968
4969 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004970
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004971
4972 DECLARE_INSTRUCTION(NullCheck);
4973
4974 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004975 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4976};
4977
4978class FieldInfo : public ValueObject {
4979 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004980 FieldInfo(MemberOffset field_offset,
4981 Primitive::Type field_type,
4982 bool is_volatile,
4983 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004984 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004985 const DexFile& dex_file,
4986 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004987 : field_offset_(field_offset),
4988 field_type_(field_type),
4989 is_volatile_(is_volatile),
4990 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004991 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004992 dex_file_(dex_file),
4993 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004994
4995 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004996 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004997 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004998 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004999 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005000 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005001 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005002
5003 private:
5004 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005005 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005006 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005007 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005008 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005009 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005010 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005011};
5012
Vladimir Markofcb503c2016-05-18 12:48:17 +01005013class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005014 public:
5015 HInstanceFieldGet(HInstruction* value,
5016 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005017 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005018 bool is_volatile,
5019 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005020 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005021 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005022 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005023 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005024 : HExpression(field_type,
5025 SideEffects::FieldReadOfType(field_type, is_volatile),
5026 dex_pc),
5027 field_info_(field_offset,
5028 field_type,
5029 is_volatile,
5030 field_idx,
5031 declaring_class_def_index,
5032 dex_file,
5033 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005034 SetRawInputAt(0, value);
5035 }
5036
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005037 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005038
Vladimir Marko372f10e2016-05-17 16:30:10 +01005039 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5040 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005041 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005042 }
5043
Calin Juravle641547a2015-04-21 22:08:51 +01005044 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005045 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005046 }
5047
5048 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005049 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5050 }
5051
Calin Juravle52c48962014-12-16 17:02:57 +00005052 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005054 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005055 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005056
5057 DECLARE_INSTRUCTION(InstanceFieldGet);
5058
5059 private:
5060 const FieldInfo field_info_;
5061
5062 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5063};
5064
Vladimir Markofcb503c2016-05-18 12:48:17 +01005065class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066 public:
5067 HInstanceFieldSet(HInstruction* object,
5068 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005069 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005070 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005071 bool is_volatile,
5072 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005073 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005074 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005075 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005076 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005077 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5078 dex_pc),
5079 field_info_(field_offset,
5080 field_type,
5081 is_volatile,
5082 field_idx,
5083 declaring_class_def_index,
5084 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005085 dex_cache) {
5086 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005087 SetRawInputAt(0, object);
5088 SetRawInputAt(1, value);
5089 }
5090
Calin Juravle641547a2015-04-21 22:08:51 +01005091 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005092 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005093 }
5094
Calin Juravle52c48962014-12-16 17:02:57 +00005095 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005096 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005097 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005098 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005099 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005100 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5101 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005102
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005103 DECLARE_INSTRUCTION(InstanceFieldSet);
5104
5105 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005106 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5107 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5108 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5109 "Too many packed fields.");
5110
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111 const FieldInfo field_info_;
5112
5113 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5114};
5115
Vladimir Markofcb503c2016-05-18 12:48:17 +01005116class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005117 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005118 HArrayGet(HInstruction* array,
5119 HInstruction* index,
5120 Primitive::Type type,
5121 uint32_t dex_pc,
5122 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005123 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005124 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125 SetRawInputAt(0, array);
5126 SetRawInputAt(1, index);
5127 }
5128
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005129 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005130 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005131 return true;
5132 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005133 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005134 // TODO: We can be smarter here.
5135 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5136 // which generates the implicit null check. There are cases when these can be removed
5137 // to produce better code. If we ever add optimizations to do so we should allow an
5138 // implicit check here (as long as the address falls in the first page).
5139 return false;
5140 }
5141
David Brazdil4833f5a2015-12-16 10:37:39 +00005142 bool IsEquivalentOf(HArrayGet* other) const {
5143 bool result = (GetDexPc() == other->GetDexPc());
5144 if (kIsDebugBuild && result) {
5145 DCHECK_EQ(GetBlock(), other->GetBlock());
5146 DCHECK_EQ(GetArray(), other->GetArray());
5147 DCHECK_EQ(GetIndex(), other->GetIndex());
5148 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005149 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005150 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005151 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5152 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005153 }
5154 }
5155 return result;
5156 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005157
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005158 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5159
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005160 HInstruction* GetArray() const { return InputAt(0); }
5161 HInstruction* GetIndex() const { return InputAt(1); }
5162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 DECLARE_INSTRUCTION(ArrayGet);
5164
5165 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005166 // We treat a String as an array, creating the HArrayGet from String.charAt()
5167 // intrinsic in the instruction simplifier. We can always determine whether
5168 // a particular HArrayGet is actually a String.charAt() by looking at the type
5169 // of the input but that requires holding the mutator lock, so we prefer to use
5170 // a flag, so that code generators don't need to do the locking.
5171 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5172 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5173 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5174 "Too many packed fields.");
5175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005176 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5177};
5178
Vladimir Markofcb503c2016-05-18 12:48:17 +01005179class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005180 public:
5181 HArraySet(HInstruction* array,
5182 HInstruction* index,
5183 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005184 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005185 uint32_t dex_pc)
5186 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005187 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5188 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5189 SetPackedFlag<kFlagValueCanBeNull>(true);
5190 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191 SetRawInputAt(0, array);
5192 SetRawInputAt(1, index);
5193 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005194 // Make a best guess now, may be refined during SSA building.
5195 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196 }
5197
Calin Juravle77520bc2015-01-12 18:45:46 +00005198 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005199 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005200 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005201 }
5202
Mingyao Yang81014cb2015-06-02 03:16:27 -07005203 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005204 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005205
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005206 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005207 // TODO: Same as for ArrayGet.
5208 return false;
5209 }
5210
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005211 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005212 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005213 }
5214
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005215 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005216 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005217 }
5218
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005219 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005220 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005221 }
5222
Vladimir Markoa1de9182016-02-25 11:37:38 +00005223 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5224 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5225 bool StaticTypeOfArrayIsObjectArray() const {
5226 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5227 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005228
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005229 HInstruction* GetArray() const { return InputAt(0); }
5230 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005231 HInstruction* GetValue() const { return InputAt(2); }
5232
5233 Primitive::Type GetComponentType() const {
5234 // The Dex format does not type floating point index operations. Since the
5235 // `expected_component_type_` is set during building and can therefore not
5236 // be correct, we also check what is the value type. If it is a floating
5237 // point type, we must use that type.
5238 Primitive::Type value_type = GetValue()->GetType();
5239 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5240 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005241 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005242 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005243
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005244 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005245 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005246 }
5247
Aart Bik18b36ab2016-04-13 16:41:35 -07005248 void ComputeSideEffects() {
5249 Primitive::Type type = GetComponentType();
5250 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5251 SideEffectsForArchRuntimeCalls(type)));
5252 }
5253
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005254 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5255 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5256 }
5257
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258 DECLARE_INSTRUCTION(ArraySet);
5259
5260 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005261 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5262 static constexpr size_t kFieldExpectedComponentTypeSize =
5263 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5264 static constexpr size_t kFlagNeedsTypeCheck =
5265 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5266 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005267 // Cached information for the reference_type_info_ so that codegen
5268 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005269 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5270 static constexpr size_t kNumberOfArraySetPackedBits =
5271 kFlagStaticTypeOfArrayIsObjectArray + 1;
5272 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5273 using ExpectedComponentTypeField =
5274 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275
5276 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5277};
5278
Vladimir Markofcb503c2016-05-18 12:48:17 +01005279class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005281 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005282 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005283 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005284 // Note that arrays do not change length, so the instruction does not
5285 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 SetRawInputAt(0, array);
5287 }
5288
Calin Juravle77520bc2015-01-12 18:45:46 +00005289 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005290 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005291 return true;
5292 }
Calin Juravle641547a2015-04-21 22:08:51 +01005293 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5294 return obj == InputAt(0);
5295 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005296
Vladimir Markodce016e2016-04-28 13:10:02 +01005297 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5298
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 DECLARE_INSTRUCTION(ArrayLength);
5300
5301 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005302 // We treat a String as an array, creating the HArrayLength from String.length()
5303 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5304 // determine whether a particular HArrayLength is actually a String.length() by
5305 // looking at the type of the input but that requires holding the mutator lock, so
5306 // we prefer to use a flag, so that code generators don't need to do the locking.
5307 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5308 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5309 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5310 "Too many packed fields.");
5311
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005312 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5313};
5314
Vladimir Markofcb503c2016-05-18 12:48:17 +01005315class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005317 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5318 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005319 HBoundsCheck(HInstruction* index,
5320 HInstruction* length,
5321 uint32_t dex_pc,
5322 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5323 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5324 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005325 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 SetRawInputAt(0, index);
5327 SetRawInputAt(1, length);
5328 }
5329
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005330 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005331 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005332 return true;
5333 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005334
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005335 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005337 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005338
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005339 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5340 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5341
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005342 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343
5344 DECLARE_INSTRUCTION(BoundsCheck);
5345
5346 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005347 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5348 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5349 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5350 // so we need to create an HEnvironment which will be translated to an InlineInfo
5351 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5352 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5353 // from the invoke as we don't want to look again at the dex bytecode.
5354 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5355
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5357};
5358
Vladimir Markofcb503c2016-05-18 12:48:17 +01005359class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005360 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005361 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005362 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005364 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005365 return true;
5366 }
5367
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005368 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5369 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005370
5371 DECLARE_INSTRUCTION(SuspendCheck);
5372
5373 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005374 // Only used for code generation, in order to share the same slow path between back edges
5375 // of a same loop.
5376 SlowPathCode* slow_path_;
5377
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005378 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5379};
5380
David Srbecky0cf44932015-12-09 14:09:59 +00005381// Pseudo-instruction which provides the native debugger with mapping information.
5382// It ensures that we can generate line number and local variables at this point.
5383class HNativeDebugInfo : public HTemplateInstruction<0> {
5384 public:
5385 explicit HNativeDebugInfo(uint32_t dex_pc)
5386 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5387
5388 bool NeedsEnvironment() const OVERRIDE {
5389 return true;
5390 }
5391
5392 DECLARE_INSTRUCTION(NativeDebugInfo);
5393
5394 private:
5395 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5396};
5397
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005398/**
5399 * Instruction to load a Class object.
5400 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005401class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005402 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005403 // Determines how to load the Class.
5404 enum class LoadKind {
5405 // Use the Class* from the method's own ArtMethod*.
5406 kReferrersClass,
5407
5408 // Use boot image Class* address that will be known at link time.
5409 // Used for boot image classes referenced by boot image code in non-PIC mode.
5410 kBootImageLinkTimeAddress,
5411
5412 // Use PC-relative boot image Class* address that will be known at link time.
5413 // Used for boot image classes referenced by boot image code in PIC mode.
5414 kBootImageLinkTimePcRelative,
5415
5416 // Use a known boot image Class* address, embedded in the code by the codegen.
5417 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5418 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5419 // GetIncludePatchInformation().
5420 kBootImageAddress,
5421
5422 // Load from the resolved types array at an absolute address.
5423 // Used for classes outside the boot image referenced by JIT-compiled code.
5424 kDexCacheAddress,
5425
5426 // Load from resolved types array in the dex cache using a PC-relative load.
5427 // Used for classes outside boot image when we know that we can access
5428 // the dex cache arrays using a PC-relative load.
5429 kDexCachePcRelative,
5430
5431 // Load from resolved types array accessed through the class loaded from
5432 // the compiled method's own ArtMethod*. This is the default access type when
5433 // all other types are unavailable.
5434 kDexCacheViaMethod,
5435
5436 kLast = kDexCacheViaMethod
5437 };
5438
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005439 HLoadClass(HCurrentMethod* current_method,
5440 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005441 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005442 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005443 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005444 bool needs_access_check,
5445 bool is_in_dex_cache)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005446 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5447 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005448 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005449 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005450 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005451 // Referrers class should not need access check. We never inline unverified
5452 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005453 DCHECK(!is_referrers_class || !needs_access_check);
5454
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005455 SetPackedField<LoadKindField>(
5456 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005457 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5458 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5459 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005460 }
5461
5462 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5463 DCHECK(HasAddress(load_kind));
5464 load_data_.address = address;
5465 SetLoadKindInternal(load_kind);
5466 }
5467
5468 void SetLoadKindWithTypeReference(LoadKind load_kind,
5469 const DexFile& dex_file,
5470 uint32_t type_index) {
5471 DCHECK(HasTypeReference(load_kind));
5472 DCHECK(IsSameDexFile(dex_file_, dex_file));
5473 DCHECK_EQ(type_index_, type_index);
5474 SetLoadKindInternal(load_kind);
5475 }
5476
5477 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5478 const DexFile& dex_file,
5479 uint32_t element_index) {
5480 DCHECK(HasDexCacheReference(load_kind));
5481 DCHECK(IsSameDexFile(dex_file_, dex_file));
5482 load_data_.dex_cache_element_index = element_index;
5483 SetLoadKindInternal(load_kind);
5484 }
5485
5486 LoadKind GetLoadKind() const {
5487 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005488 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005489
5490 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005491
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005492 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005493
5494 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5495
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005496 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005497
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005498 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005499 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005500 }
5501
Calin Juravle0ba218d2015-05-19 18:46:01 +01005502 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005503 // The entrypoint the code generator is going to call does not do
5504 // clinit of the class.
5505 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005506 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005507 }
5508
5509 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005510 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005511 (!IsReferrersClass() && !IsInDexCache()) ||
5512 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005513 }
5514
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005515
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005516 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005517 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005518 }
5519
Calin Juravleacf735c2015-02-12 15:25:22 +00005520 ReferenceTypeInfo GetLoadedClassRTI() {
5521 return loaded_class_rti_;
5522 }
5523
5524 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5525 // Make sure we only set exact types (the loaded class should never be merged).
5526 DCHECK(rti.IsExact());
5527 loaded_class_rti_ = rti;
5528 }
5529
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005530 uint32_t GetTypeIndex() const { return type_index_; }
5531 const DexFile& GetDexFile() const { return dex_file_; }
5532
5533 uint32_t GetDexCacheElementOffset() const;
5534
5535 uint64_t GetAddress() const {
5536 DCHECK(HasAddress(GetLoadKind()));
5537 return load_data_.address;
5538 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005539
Vladimir Markoa1de9182016-02-25 11:37:38 +00005540 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005541
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005542 static SideEffects SideEffectsForArchRuntimeCalls() {
5543 return SideEffects::CanTriggerGC();
5544 }
5545
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005546 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005547 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5548 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5549 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005550
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551 void MarkInDexCache() {
5552 SetPackedFlag<kFlagIsInDexCache>(true);
5553 DCHECK(!NeedsEnvironment());
5554 RemoveEnvironment();
5555 SetSideEffects(SideEffects::None());
5556 }
5557
5558 void AddSpecialInput(HInstruction* special_input);
5559
5560 using HInstruction::GetInputRecords; // Keep the const version visible.
5561 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5562 return ArrayRef<HUserRecord<HInstruction*>>(
5563 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5564 }
5565
5566 Primitive::Type GetType() const OVERRIDE {
5567 return Primitive::kPrimNot;
5568 }
5569
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005570 DECLARE_INSTRUCTION(LoadClass);
5571
5572 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005573 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005575 // Whether this instruction must generate the initialization check.
5576 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005577 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005578 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5579 static constexpr size_t kFieldLoadKindSize =
5580 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5581 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005583 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5584
5585 static bool HasTypeReference(LoadKind load_kind) {
5586 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5587 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5588 load_kind == LoadKind::kDexCacheViaMethod ||
5589 load_kind == LoadKind::kReferrersClass;
5590 }
5591
5592 static bool HasAddress(LoadKind load_kind) {
5593 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5594 }
5595
5596 static bool HasDexCacheReference(LoadKind load_kind) {
5597 return load_kind == LoadKind::kDexCachePcRelative;
5598 }
5599
5600 void SetLoadKindInternal(LoadKind load_kind);
5601
5602 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5603 // For other load kinds it's empty or possibly some architecture-specific instruction
5604 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5605 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005606
5607 const uint16_t type_index_;
5608 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005609
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005610 union {
5611 uint32_t dex_cache_element_index; // Only for dex cache reference.
5612 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5613 } load_data_;
5614
Calin Juravleacf735c2015-02-12 15:25:22 +00005615 ReferenceTypeInfo loaded_class_rti_;
5616
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005617 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5618};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005619std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5620
5621// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5622inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5623 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5624 return load_data_.dex_cache_element_index;
5625}
5626
5627// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5628inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
5629 // The special input is used for PC-relative loads on some architectures.
5630 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5631 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5632 DCHECK(special_input_.GetInstruction() == nullptr);
5633 special_input_ = HUserRecord<HInstruction*>(special_input);
5634 special_input->AddUseAt(this, 0);
5635}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005636
Vladimir Marko372f10e2016-05-17 16:30:10 +01005637class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005638 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005639 // Determines how to load the String.
5640 enum class LoadKind {
5641 // Use boot image String* address that will be known at link time.
5642 // Used for boot image strings referenced by boot image code in non-PIC mode.
5643 kBootImageLinkTimeAddress,
5644
5645 // Use PC-relative boot image String* address that will be known at link time.
5646 // Used for boot image strings referenced by boot image code in PIC mode.
5647 kBootImageLinkTimePcRelative,
5648
5649 // Use a known boot image String* address, embedded in the code by the codegen.
5650 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5651 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5652 // GetIncludePatchInformation().
5653 kBootImageAddress,
5654
5655 // Load from the resolved strings array at an absolute address.
5656 // Used for strings outside the boot image referenced by JIT-compiled code.
5657 kDexCacheAddress,
5658
5659 // Load from resolved strings array in the dex cache using a PC-relative load.
5660 // Used for strings outside boot image when we know that we can access
5661 // the dex cache arrays using a PC-relative load.
5662 kDexCachePcRelative,
5663
5664 // Load from resolved strings array accessed through the class loaded from
5665 // the compiled method's own ArtMethod*. This is the default access type when
5666 // all other types are unavailable.
5667 kDexCacheViaMethod,
5668
5669 kLast = kDexCacheViaMethod
5670 };
5671
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005672 HLoadString(HCurrentMethod* current_method,
5673 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005674 const DexFile& dex_file,
5675 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005676 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5677 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005678 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005679 SetPackedFlag<kFlagIsInDexCache>(false);
5680 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5681 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005682 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005683
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005684 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5685 DCHECK(HasAddress(load_kind));
5686 load_data_.address = address;
5687 SetLoadKindInternal(load_kind);
5688 }
5689
5690 void SetLoadKindWithStringReference(LoadKind load_kind,
5691 const DexFile& dex_file,
5692 uint32_t string_index) {
5693 DCHECK(HasStringReference(load_kind));
5694 load_data_.ref.dex_file = &dex_file;
5695 string_index_ = string_index;
5696 SetLoadKindInternal(load_kind);
5697 }
5698
5699 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5700 const DexFile& dex_file,
5701 uint32_t element_index) {
5702 DCHECK(HasDexCacheReference(load_kind));
5703 load_data_.ref.dex_file = &dex_file;
5704 load_data_.ref.dex_cache_element_index = element_index;
5705 SetLoadKindInternal(load_kind);
5706 }
5707
5708 LoadKind GetLoadKind() const {
5709 return GetPackedField<LoadKindField>();
5710 }
5711
5712 const DexFile& GetDexFile() const;
5713
5714 uint32_t GetStringIndex() const {
5715 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5716 return string_index_;
5717 }
5718
5719 uint32_t GetDexCacheElementOffset() const;
5720
5721 uint64_t GetAddress() const {
5722 DCHECK(HasAddress(GetLoadKind()));
5723 return load_data_.address;
5724 }
5725
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005726 bool CanBeMoved() const OVERRIDE { return true; }
5727
Vladimir Marko372f10e2016-05-17 16:30:10 +01005728 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005729
5730 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5731
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005732 // Will call the runtime if we need to load the string through
5733 // the dex cache and the string is not guaranteed to be there yet.
5734 bool NeedsEnvironment() const OVERRIDE {
5735 LoadKind load_kind = GetLoadKind();
5736 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5737 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5738 load_kind == LoadKind::kBootImageAddress) {
5739 return false;
5740 }
5741 return !IsInDexCache();
5742 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005743
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005744 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5745 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5746 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005747
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005748 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005750
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005751 static SideEffects SideEffectsForArchRuntimeCalls() {
5752 return SideEffects::CanTriggerGC();
5753 }
5754
Vladimir Markoa1de9182016-02-25 11:37:38 +00005755 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5756
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005757 void MarkInDexCache() {
5758 SetPackedFlag<kFlagIsInDexCache>(true);
5759 DCHECK(!NeedsEnvironment());
5760 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005761 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005762 }
5763
Vladimir Marko372f10e2016-05-17 16:30:10 +01005764 void AddSpecialInput(HInstruction* special_input);
5765
5766 using HInstruction::GetInputRecords; // Keep the const version visible.
5767 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5768 return ArrayRef<HUserRecord<HInstruction*>>(
5769 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005770 }
5771
Vladimir Marko372f10e2016-05-17 16:30:10 +01005772 Primitive::Type GetType() const OVERRIDE {
5773 return Primitive::kPrimNot;
5774 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005775
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005776 DECLARE_INSTRUCTION(LoadString);
5777
5778 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005779 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005780 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5781 static constexpr size_t kFieldLoadKindSize =
5782 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5783 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005784 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005785 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005786
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005787 static bool HasStringReference(LoadKind load_kind) {
5788 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5789 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5790 load_kind == LoadKind::kDexCacheViaMethod;
5791 }
5792
5793 static bool HasAddress(LoadKind load_kind) {
5794 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5795 }
5796
5797 static bool HasDexCacheReference(LoadKind load_kind) {
5798 return load_kind == LoadKind::kDexCachePcRelative;
5799 }
5800
5801 void SetLoadKindInternal(LoadKind load_kind);
5802
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005803 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5804 // For other load kinds it's empty or possibly some architecture-specific instruction
5805 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005806 HUserRecord<HInstruction*> special_input_;
5807
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005808 // String index serves also as the hash code and it's also needed for slow-paths,
5809 // so it must not be overwritten with other load data.
5810 uint32_t string_index_;
5811
5812 union {
5813 struct {
5814 const DexFile* dex_file; // For string reference and dex cache reference.
5815 uint32_t dex_cache_element_index; // Only for dex cache reference.
5816 } ref;
5817 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5818 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005819
5820 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5821};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005822std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5823
5824// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5825inline const DexFile& HLoadString::GetDexFile() const {
5826 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5827 << GetLoadKind();
5828 return *load_data_.ref.dex_file;
5829}
5830
5831// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5832inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5833 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5834 return load_data_.ref.dex_cache_element_index;
5835}
5836
5837// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5838inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5839 // The special input is used for PC-relative loads on some architectures.
5840 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5841 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005842 // HLoadString::GetInputRecords() returns an empty array at this point,
5843 // so use the GetInputRecords() from the base class to set the input record.
5844 DCHECK(special_input_.GetInstruction() == nullptr);
5845 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005846 special_input->AddUseAt(this, 0);
5847}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005848
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005849/**
5850 * Performs an initialization check on its Class object input.
5851 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005852class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005853 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005854 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005855 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005856 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005857 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5858 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005859 SetRawInputAt(0, constant);
5860 }
5861
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005862 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005863 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005864 return true;
5865 }
5866
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005867 bool NeedsEnvironment() const OVERRIDE {
5868 // May call runtime to initialize the class.
5869 return true;
5870 }
5871
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005872 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873
5874 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5875
5876 DECLARE_INSTRUCTION(ClinitCheck);
5877
5878 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005879 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5880};
5881
Vladimir Markofcb503c2016-05-18 12:48:17 +01005882class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005883 public:
5884 HStaticFieldGet(HInstruction* cls,
5885 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005886 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005887 bool is_volatile,
5888 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005889 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005890 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005891 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005892 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005893 : HExpression(field_type,
5894 SideEffects::FieldReadOfType(field_type, is_volatile),
5895 dex_pc),
5896 field_info_(field_offset,
5897 field_type,
5898 is_volatile,
5899 field_idx,
5900 declaring_class_def_index,
5901 dex_file,
5902 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903 SetRawInputAt(0, cls);
5904 }
5905
Calin Juravle52c48962014-12-16 17:02:57 +00005906
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005907 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005908
Vladimir Marko372f10e2016-05-17 16:30:10 +01005909 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5910 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005911 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005912 }
5913
5914 size_t ComputeHashCode() const OVERRIDE {
5915 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5916 }
5917
Calin Juravle52c48962014-12-16 17:02:57 +00005918 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005919 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5920 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005921 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005922
5923 DECLARE_INSTRUCTION(StaticFieldGet);
5924
5925 private:
5926 const FieldInfo field_info_;
5927
5928 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5929};
5930
Vladimir Markofcb503c2016-05-18 12:48:17 +01005931class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005932 public:
5933 HStaticFieldSet(HInstruction* cls,
5934 HInstruction* value,
5935 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005936 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005937 bool is_volatile,
5938 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005939 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005940 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005941 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005942 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005943 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5944 dex_pc),
5945 field_info_(field_offset,
5946 field_type,
5947 is_volatile,
5948 field_idx,
5949 declaring_class_def_index,
5950 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005951 dex_cache) {
5952 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005953 SetRawInputAt(0, cls);
5954 SetRawInputAt(1, value);
5955 }
5956
Calin Juravle52c48962014-12-16 17:02:57 +00005957 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005958 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5959 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005960 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005962 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005963 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5964 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005965
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005966 DECLARE_INSTRUCTION(StaticFieldSet);
5967
5968 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5970 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5971 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5972 "Too many packed fields.");
5973
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005974 const FieldInfo field_info_;
5975
5976 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5977};
5978
Vladimir Markofcb503c2016-05-18 12:48:17 +01005979class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005980 public:
5981 HUnresolvedInstanceFieldGet(HInstruction* obj,
5982 Primitive::Type field_type,
5983 uint32_t field_index,
5984 uint32_t dex_pc)
5985 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5986 field_index_(field_index) {
5987 SetRawInputAt(0, obj);
5988 }
5989
5990 bool NeedsEnvironment() const OVERRIDE { return true; }
5991 bool CanThrow() const OVERRIDE { return true; }
5992
5993 Primitive::Type GetFieldType() const { return GetType(); }
5994 uint32_t GetFieldIndex() const { return field_index_; }
5995
5996 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5997
5998 private:
5999 const uint32_t field_index_;
6000
6001 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6002};
6003
Vladimir Markofcb503c2016-05-18 12:48:17 +01006004class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006005 public:
6006 HUnresolvedInstanceFieldSet(HInstruction* obj,
6007 HInstruction* value,
6008 Primitive::Type field_type,
6009 uint32_t field_index,
6010 uint32_t dex_pc)
6011 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006012 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006013 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006014 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006015 SetRawInputAt(0, obj);
6016 SetRawInputAt(1, value);
6017 }
6018
6019 bool NeedsEnvironment() const OVERRIDE { return true; }
6020 bool CanThrow() const OVERRIDE { return true; }
6021
Vladimir Markoa1de9182016-02-25 11:37:38 +00006022 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006023 uint32_t GetFieldIndex() const { return field_index_; }
6024
6025 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6026
6027 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6029 static constexpr size_t kFieldFieldTypeSize =
6030 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6031 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6032 kFieldFieldType + kFieldFieldTypeSize;
6033 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6034 "Too many packed fields.");
6035 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6036
Calin Juravlee460d1d2015-09-29 04:52:17 +01006037 const uint32_t field_index_;
6038
6039 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6040};
6041
Vladimir Markofcb503c2016-05-18 12:48:17 +01006042class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006043 public:
6044 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6045 uint32_t field_index,
6046 uint32_t dex_pc)
6047 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6048 field_index_(field_index) {
6049 }
6050
6051 bool NeedsEnvironment() const OVERRIDE { return true; }
6052 bool CanThrow() const OVERRIDE { return true; }
6053
6054 Primitive::Type GetFieldType() const { return GetType(); }
6055 uint32_t GetFieldIndex() const { return field_index_; }
6056
6057 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6058
6059 private:
6060 const uint32_t field_index_;
6061
6062 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6063};
6064
Vladimir Markofcb503c2016-05-18 12:48:17 +01006065class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006066 public:
6067 HUnresolvedStaticFieldSet(HInstruction* value,
6068 Primitive::Type field_type,
6069 uint32_t field_index,
6070 uint32_t dex_pc)
6071 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006072 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006073 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006074 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006075 SetRawInputAt(0, value);
6076 }
6077
6078 bool NeedsEnvironment() const OVERRIDE { return true; }
6079 bool CanThrow() const OVERRIDE { return true; }
6080
Vladimir Markoa1de9182016-02-25 11:37:38 +00006081 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006082 uint32_t GetFieldIndex() const { return field_index_; }
6083
6084 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6085
6086 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006087 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6088 static constexpr size_t kFieldFieldTypeSize =
6089 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6090 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6091 kFieldFieldType + kFieldFieldTypeSize;
6092 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6093 "Too many packed fields.");
6094 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6095
Calin Juravlee460d1d2015-09-29 04:52:17 +01006096 const uint32_t field_index_;
6097
6098 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6099};
6100
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006101// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006102class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006103 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006104 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6105 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006106
David Brazdilbbd733e2015-08-18 17:48:17 +01006107 bool CanBeNull() const OVERRIDE { return false; }
6108
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006109 DECLARE_INSTRUCTION(LoadException);
6110
6111 private:
6112 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6113};
6114
David Brazdilcb1c0552015-08-04 16:22:25 +01006115// Implicit part of move-exception which clears thread-local exception storage.
6116// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006117class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006119 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6120 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006121
6122 DECLARE_INSTRUCTION(ClearException);
6123
6124 private:
6125 DISALLOW_COPY_AND_ASSIGN(HClearException);
6126};
6127
Vladimir Markofcb503c2016-05-18 12:48:17 +01006128class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006129 public:
6130 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006131 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006132 SetRawInputAt(0, exception);
6133 }
6134
6135 bool IsControlFlow() const OVERRIDE { return true; }
6136
6137 bool NeedsEnvironment() const OVERRIDE { return true; }
6138
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006139 bool CanThrow() const OVERRIDE { return true; }
6140
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006141
6142 DECLARE_INSTRUCTION(Throw);
6143
6144 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006145 DISALLOW_COPY_AND_ASSIGN(HThrow);
6146};
6147
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148/**
6149 * Implementation strategies for the code generator of a HInstanceOf
6150 * or `HCheckCast`.
6151 */
6152enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006153 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154 kExactCheck, // Can do a single class compare.
6155 kClassHierarchyCheck, // Can just walk the super class chain.
6156 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6157 kInterfaceCheck, // No optimization yet when checking against an interface.
6158 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006159 kArrayCheck, // No optimization yet when checking against a generic array.
6160 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006161};
6162
Roland Levillain86503782016-02-11 19:07:30 +00006163std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6164
Vladimir Markofcb503c2016-05-18 12:48:17 +01006165class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006166 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006167 HInstanceOf(HInstruction* object,
6168 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006170 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006171 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006173 dex_pc) {
6174 SetPackedField<TypeCheckKindField>(check_kind);
6175 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006176 SetRawInputAt(0, object);
6177 SetRawInputAt(1, constant);
6178 }
6179
6180 bool CanBeMoved() const OVERRIDE { return true; }
6181
Vladimir Marko372f10e2016-05-17 16:30:10 +01006182 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006183 return true;
6184 }
6185
6186 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006187 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006188 }
6189
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006190 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006191 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6192 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6193 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6194 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006195
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006196 static bool CanCallRuntime(TypeCheckKind check_kind) {
6197 // Mips currently does runtime calls for any other checks.
6198 return check_kind != TypeCheckKind::kExactCheck;
6199 }
6200
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006202 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006203 }
6204
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006205 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006206
6207 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006208 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6209 static constexpr size_t kFieldTypeCheckKindSize =
6210 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6211 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6212 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6213 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6214 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006215
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006216 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6217};
6218
Vladimir Markofcb503c2016-05-18 12:48:17 +01006219class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006220 public:
David Brazdilf5552582015-12-27 13:36:12 +00006221 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006222 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006223 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6224 SetPackedFlag<kFlagUpperCanBeNull>(true);
6225 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006226 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006227 SetRawInputAt(0, input);
6228 }
6229
David Brazdilf5552582015-12-27 13:36:12 +00006230 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006231 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006233 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006234
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006235 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006236 DCHECK(GetUpperCanBeNull() || !can_be_null);
6237 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006238 }
6239
Vladimir Markoa1de9182016-02-25 11:37:38 +00006240 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006241
Calin Juravleb1498f62015-02-16 13:13:29 +00006242 DECLARE_INSTRUCTION(BoundType);
6243
6244 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006245 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6246 // is false then CanBeNull() cannot be true).
6247 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6248 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6249 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6250 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6251
Calin Juravleb1498f62015-02-16 13:13:29 +00006252 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006253 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6254 // It is used to bound the type in cases like:
6255 // if (x instanceof ClassX) {
6256 // // uper_bound_ will be ClassX
6257 // }
David Brazdilf5552582015-12-27 13:36:12 +00006258 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006259
6260 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6261};
6262
Vladimir Markofcb503c2016-05-18 12:48:17 +01006263class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006264 public:
6265 HCheckCast(HInstruction* object,
6266 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006268 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006269 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6270 SetPackedField<TypeCheckKindField>(check_kind);
6271 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006272 SetRawInputAt(0, object);
6273 SetRawInputAt(1, constant);
6274 }
6275
6276 bool CanBeMoved() const OVERRIDE { return true; }
6277
Vladimir Marko372f10e2016-05-17 16:30:10 +01006278 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006279 return true;
6280 }
6281
6282 bool NeedsEnvironment() const OVERRIDE {
6283 // Instruction may throw a CheckCastError.
6284 return true;
6285 }
6286
6287 bool CanThrow() const OVERRIDE { return true; }
6288
Vladimir Markoa1de9182016-02-25 11:37:38 +00006289 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6290 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6291 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6292 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006293
6294 DECLARE_INSTRUCTION(CheckCast);
6295
6296 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006297 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6298 static constexpr size_t kFieldTypeCheckKindSize =
6299 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6300 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6301 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6302 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6303 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006304
6305 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006306};
6307
Vladimir Markofcb503c2016-05-18 12:48:17 +01006308class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006310 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006311 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006312 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6313 SetPackedField<BarrierKindField>(barrier_kind);
6314 }
Calin Juravle27df7582015-04-17 19:12:31 +01006315
Vladimir Markoa1de9182016-02-25 11:37:38 +00006316 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006317
6318 DECLARE_INSTRUCTION(MemoryBarrier);
6319
6320 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006321 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6322 static constexpr size_t kFieldBarrierKindSize =
6323 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6324 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6325 kFieldBarrierKind + kFieldBarrierKindSize;
6326 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6327 "Too many packed fields.");
6328 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006329
6330 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6331};
6332
Vladimir Markofcb503c2016-05-18 12:48:17 +01006333class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006334 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006335 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006336 kEnter,
6337 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006338 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006339 };
6340
6341 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006342 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006343 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6344 dex_pc) {
6345 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006346 SetRawInputAt(0, object);
6347 }
6348
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006349 // Instruction may go into runtime, so we need an environment.
6350 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006351
6352 bool CanThrow() const OVERRIDE {
6353 // Verifier guarantees that monitor-exit cannot throw.
6354 // This is important because it allows the HGraphBuilder to remove
6355 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6356 return IsEnter();
6357 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006358
Vladimir Markoa1de9182016-02-25 11:37:38 +00006359 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6360 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006361
6362 DECLARE_INSTRUCTION(MonitorOperation);
6363
Calin Juravle52c48962014-12-16 17:02:57 +00006364 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006365 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6366 static constexpr size_t kFieldOperationKindSize =
6367 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6368 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6369 kFieldOperationKind + kFieldOperationKindSize;
6370 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6371 "Too many packed fields.");
6372 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006373
6374 private:
6375 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6376};
6377
Vladimir Markofcb503c2016-05-18 12:48:17 +01006378class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006379 public:
6380 HSelect(HInstruction* condition,
6381 HInstruction* true_value,
6382 HInstruction* false_value,
6383 uint32_t dex_pc)
6384 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6385 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6386
6387 // First input must be `true_value` or `false_value` to allow codegens to
6388 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6389 // that architectures which implement HSelect as a conditional move also
6390 // will not need to invert the condition.
6391 SetRawInputAt(0, false_value);
6392 SetRawInputAt(1, true_value);
6393 SetRawInputAt(2, condition);
6394 }
6395
6396 HInstruction* GetFalseValue() const { return InputAt(0); }
6397 HInstruction* GetTrueValue() const { return InputAt(1); }
6398 HInstruction* GetCondition() const { return InputAt(2); }
6399
6400 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006401 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6402 return true;
6403 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006404
6405 bool CanBeNull() const OVERRIDE {
6406 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6407 }
6408
6409 DECLARE_INSTRUCTION(Select);
6410
6411 private:
6412 DISALLOW_COPY_AND_ASSIGN(HSelect);
6413};
6414
Vladimir Markof9f64412015-09-02 14:05:49 +01006415class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006416 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006417 MoveOperands(Location source,
6418 Location destination,
6419 Primitive::Type type,
6420 HInstruction* instruction)
6421 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006422
6423 Location GetSource() const { return source_; }
6424 Location GetDestination() const { return destination_; }
6425
6426 void SetSource(Location value) { source_ = value; }
6427 void SetDestination(Location value) { destination_ = value; }
6428
6429 // The parallel move resolver marks moves as "in-progress" by clearing the
6430 // destination (but not the source).
6431 Location MarkPending() {
6432 DCHECK(!IsPending());
6433 Location dest = destination_;
6434 destination_ = Location::NoLocation();
6435 return dest;
6436 }
6437
6438 void ClearPending(Location dest) {
6439 DCHECK(IsPending());
6440 destination_ = dest;
6441 }
6442
6443 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006444 DCHECK(source_.IsValid() || destination_.IsInvalid());
6445 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006446 }
6447
6448 // True if this blocks a move from the given location.
6449 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006450 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006451 }
6452
6453 // A move is redundant if it's been eliminated, if its source and
6454 // destination are the same, or if its destination is unneeded.
6455 bool IsRedundant() const {
6456 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6457 }
6458
6459 // We clear both operands to indicate move that's been eliminated.
6460 void Eliminate() {
6461 source_ = destination_ = Location::NoLocation();
6462 }
6463
6464 bool IsEliminated() const {
6465 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6466 return source_.IsInvalid();
6467 }
6468
Alexey Frunze4dda3372015-06-01 18:31:49 -07006469 Primitive::Type GetType() const { return type_; }
6470
Nicolas Geoffray90218252015-04-15 11:56:51 +01006471 bool Is64BitMove() const {
6472 return Primitive::Is64BitType(type_);
6473 }
6474
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006475 HInstruction* GetInstruction() const { return instruction_; }
6476
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006477 private:
6478 Location source_;
6479 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006480 // The type this move is for.
6481 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006482 // The instruction this move is assocatied with. Null when this move is
6483 // for moving an input in the expected locations of user (including a phi user).
6484 // This is only used in debug mode, to ensure we do not connect interval siblings
6485 // in the same parallel move.
6486 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006487};
6488
Roland Levillainc9285912015-12-18 10:38:42 +00006489std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6490
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006491static constexpr size_t kDefaultNumberOfMoves = 4;
6492
Vladimir Markofcb503c2016-05-18 12:48:17 +01006493class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006494 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006495 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006496 : HTemplateInstruction(SideEffects::None(), dex_pc),
6497 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6498 moves_.reserve(kDefaultNumberOfMoves);
6499 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006500
Nicolas Geoffray90218252015-04-15 11:56:51 +01006501 void AddMove(Location source,
6502 Location destination,
6503 Primitive::Type type,
6504 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006505 DCHECK(source.IsValid());
6506 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006507 if (kIsDebugBuild) {
6508 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006509 for (const MoveOperands& move : moves_) {
6510 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006511 // Special case the situation where the move is for the spill slot
6512 // of the instruction.
6513 if ((GetPrevious() == instruction)
6514 || ((GetPrevious() == nullptr)
6515 && instruction->IsPhi()
6516 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006517 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006518 << "Doing parallel moves for the same instruction.";
6519 } else {
6520 DCHECK(false) << "Doing parallel moves for the same instruction.";
6521 }
6522 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006523 }
6524 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006525 for (const MoveOperands& move : moves_) {
6526 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006527 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006528 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006529 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006530 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006531 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006532 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006533 }
6534
Vladimir Marko225b6462015-09-28 12:17:40 +01006535 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006536 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006537 }
6538
Vladimir Marko225b6462015-09-28 12:17:40 +01006539 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006540
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006541 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006542
6543 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006544 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006545
6546 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6547};
6548
Mark Mendell0616ae02015-04-17 12:49:27 -04006549} // namespace art
6550
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006551#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6552#include "nodes_shared.h"
6553#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006554#ifdef ART_ENABLE_CODEGEN_arm
6555#include "nodes_arm.h"
6556#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006557#ifdef ART_ENABLE_CODEGEN_arm64
6558#include "nodes_arm64.h"
6559#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006560#ifdef ART_ENABLE_CODEGEN_mips
6561#include "nodes_mips.h"
6562#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006563#ifdef ART_ENABLE_CODEGEN_x86
6564#include "nodes_x86.h"
6565#endif
6566
6567namespace art {
6568
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006569class HGraphVisitor : public ValueObject {
6570 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006571 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6572 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006573
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006574 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006575 virtual void VisitBasicBlock(HBasicBlock* block);
6576
Roland Levillain633021e2014-10-01 14:12:25 +01006577 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006578 void VisitInsertionOrder();
6579
Roland Levillain633021e2014-10-01 14:12:25 +01006580 // Visit the graph following dominator tree reverse post-order.
6581 void VisitReversePostOrder();
6582
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006583 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006584
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006585 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006586#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006587 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6588
6589 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6590
6591#undef DECLARE_VISIT_INSTRUCTION
6592
6593 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006594 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006595
6596 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6597};
6598
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006599class HGraphDelegateVisitor : public HGraphVisitor {
6600 public:
6601 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6602 virtual ~HGraphDelegateVisitor() {}
6603
6604 // Visit functions that delegate to to super class.
6605#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006606 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006607
6608 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6609
6610#undef DECLARE_VISIT_INSTRUCTION
6611
6612 private:
6613 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6614};
6615
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006616class HInsertionOrderIterator : public ValueObject {
6617 public:
6618 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6619
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006620 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006621 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006622 void Advance() { ++index_; }
6623
6624 private:
6625 const HGraph& graph_;
6626 size_t index_;
6627
6628 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6629};
6630
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006631class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006632 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006633 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6634 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006635 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006636 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006637
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006638 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6639 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006640 void Advance() { ++index_; }
6641
6642 private:
6643 const HGraph& graph_;
6644 size_t index_;
6645
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006646 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006647};
6648
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006649class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006650 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006651 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006652 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006653 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006654 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006655 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006656
6657 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006658 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006659 void Advance() { --index_; }
6660
6661 private:
6662 const HGraph& graph_;
6663 size_t index_;
6664
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006665 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006666};
6667
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006668class HLinearPostOrderIterator : public ValueObject {
6669 public:
6670 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006671 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006672
6673 bool Done() const { return index_ == 0; }
6674
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006675 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006676
6677 void Advance() {
6678 --index_;
6679 DCHECK_GE(index_, 0U);
6680 }
6681
6682 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006683 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006684 size_t index_;
6685
6686 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6687};
6688
6689class HLinearOrderIterator : public ValueObject {
6690 public:
6691 explicit HLinearOrderIterator(const HGraph& graph)
6692 : order_(graph.GetLinearOrder()), index_(0) {}
6693
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006694 bool Done() const { return index_ == order_.size(); }
6695 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006696 void Advance() { ++index_; }
6697
6698 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006699 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006700 size_t index_;
6701
6702 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6703};
6704
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006705// Iterator over the blocks that art part of the loop. Includes blocks part
6706// of an inner loop. The order in which the blocks are iterated is on their
6707// block id.
6708class HBlocksInLoopIterator : public ValueObject {
6709 public:
6710 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6711 : blocks_in_loop_(info.GetBlocks()),
6712 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6713 index_(0) {
6714 if (!blocks_in_loop_.IsBitSet(index_)) {
6715 Advance();
6716 }
6717 }
6718
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006719 bool Done() const { return index_ == blocks_.size(); }
6720 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006721 void Advance() {
6722 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006723 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006724 if (blocks_in_loop_.IsBitSet(index_)) {
6725 break;
6726 }
6727 }
6728 }
6729
6730 private:
6731 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006732 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006733 size_t index_;
6734
6735 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6736};
6737
Mingyao Yang3584bce2015-05-19 16:01:59 -07006738// Iterator over the blocks that art part of the loop. Includes blocks part
6739// of an inner loop. The order in which the blocks are iterated is reverse
6740// post order.
6741class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6742 public:
6743 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6744 : blocks_in_loop_(info.GetBlocks()),
6745 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6746 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006747 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006748 Advance();
6749 }
6750 }
6751
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006752 bool Done() const { return index_ == blocks_.size(); }
6753 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006754 void Advance() {
6755 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006756 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6757 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006758 break;
6759 }
6760 }
6761 }
6762
6763 private:
6764 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006765 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006766 size_t index_;
6767
6768 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6769};
6770
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006771inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006772 if (constant->IsIntConstant()) {
6773 return constant->AsIntConstant()->GetValue();
6774 } else if (constant->IsLongConstant()) {
6775 return constant->AsLongConstant()->GetValue();
6776 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006777 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006778 return 0;
6779 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006780}
6781
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006782#define INSTRUCTION_TYPE_CHECK(type, super) \
6783 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6784 inline const H##type* HInstruction::As##type() const { \
6785 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6786 } \
6787 inline H##type* HInstruction::As##type() { \
6788 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6789 }
6790
6791 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6792#undef INSTRUCTION_TYPE_CHECK
6793
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006794// Create space in `blocks` for adding `number_of_new_blocks` entries
6795// starting at location `at`. Blocks after `at` are moved accordingly.
6796inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6797 size_t number_of_new_blocks,
6798 size_t after) {
6799 DCHECK_LT(after, blocks->size());
6800 size_t old_size = blocks->size();
6801 size_t new_size = old_size + number_of_new_blocks;
6802 blocks->resize(new_size);
6803 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6804}
6805
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006806} // namespace art
6807
6808#endif // ART_COMPILER_OPTIMIZING_NODES_H_