blob: 6b2c33e66864245e1ea44306de93e1679baac7df [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 Udovichenko4a0dad62016-01-26 12:28:31 +03001292 M(MultiplyAccumulate, Instruction)
1293#endif
1294
Vladimir Markob4536b72015-11-24 13:45:23 +00001295#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001296#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001297#else
1298#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1299 M(ArmDexCacheArraysBase, Instruction)
1300#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001302#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001303#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001304#else
1305#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001306 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001307 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001308#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001309
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1311
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001312#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1313
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001314#ifndef ART_ENABLE_CODEGEN_x86
1315#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1316#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001317#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1318 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001319 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001320 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001321 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001322#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001323
1324#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1325
1326#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1327 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001328 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001329 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1330 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001331 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001332 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001333 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1334 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1335
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001336#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1337 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001338 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001339 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001340 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001341 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001342
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001343#define FOR_EACH_INSTRUCTION(M) \
1344 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1345 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1346
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001347#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001348FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1349#undef FORWARD_DECLARATION
1350
Vladimir Marko372f10e2016-05-17 16:30:10 +01001351#define DECLARE_INSTRUCTION(type) \
1352 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1353 const char* DebugName() const OVERRIDE { return #type; } \
1354 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1355 return other->Is##type(); \
1356 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001357 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001358
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001359#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1360 bool Is##type() const { return As##type() != nullptr; } \
1361 const H##type* As##type() const { return this; } \
1362 H##type* As##type() { return this; }
1363
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001364template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001365class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366 public:
David Brazdiled596192015-01-23 10:39:45 +00001367 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001368 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001369 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001370
Vladimir Marko46817b82016-03-29 12:21:58 +01001371 // Hook for the IntrusiveForwardList<>.
1372 // TODO: Hide this better.
1373 IntrusiveForwardListHook hook;
1374
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375 private:
David Brazdiled596192015-01-23 10:39:45 +00001376 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001377 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001378
1379 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001380 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001381
Vladimir Marko46817b82016-03-29 12:21:58 +01001382 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383
1384 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1385};
1386
David Brazdiled596192015-01-23 10:39:45 +00001387template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001388using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001389
David Brazdil1abb4192015-02-17 18:33:36 +00001390// This class is used by HEnvironment and HInstruction classes to record the
1391// instructions they use and pointers to the corresponding HUseListNodes kept
1392// by the used instructions.
1393template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001394class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001395 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001396 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1397 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001398
Vladimir Marko46817b82016-03-29 12:21:58 +01001399 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1400 : HUserRecord(old_record.instruction_, before_use_node) {}
1401 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1402 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001403 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001404 }
1405
1406 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001407 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1408 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001409
1410 private:
1411 // Instruction used by the user.
1412 HInstruction* instruction_;
1413
Vladimir Marko46817b82016-03-29 12:21:58 +01001414 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1415 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001416};
1417
Aart Bik854a02b2015-07-14 16:07:00 -07001418/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001419 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001420 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001421 * For write/read dependences on fields/arrays, the dependence analysis uses
1422 * type disambiguation (e.g. a float field write cannot modify the value of an
1423 * integer field read) and the access type (e.g. a reference array write cannot
1424 * modify the value of a reference field read [although it may modify the
1425 * reference fetch prior to reading the field, which is represented by its own
1426 * write/read dependence]). The analysis makes conservative points-to
1427 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1428 * the same, and any reference read depends on any reference read without
1429 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001430 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001431 * The internal representation uses 38-bit and is described in the table below.
1432 * The first line indicates the side effect, and for field/array accesses the
1433 * second line indicates the type of the access (in the order of the
1434 * Primitive::Type enum).
1435 * The two numbered lines below indicate the bit position in the bitfield (read
1436 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001437 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001438 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1439 * +-------------+---------+---------+--------------+---------+---------+
1440 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1441 * | 3 |333333322|222222221| 1 |111111110|000000000|
1442 * | 7 |654321098|765432109| 8 |765432109|876543210|
1443 *
1444 * Note that, to ease the implementation, 'changes' bits are least significant
1445 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001446 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001447class SideEffects : public ValueObject {
1448 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001449 SideEffects() : flags_(0) {}
1450
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001451 static SideEffects None() {
1452 return SideEffects(0);
1453 }
1454
1455 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001456 return SideEffects(kAllChangeBits | kAllDependOnBits);
1457 }
1458
1459 static SideEffects AllChanges() {
1460 return SideEffects(kAllChangeBits);
1461 }
1462
1463 static SideEffects AllDependencies() {
1464 return SideEffects(kAllDependOnBits);
1465 }
1466
1467 static SideEffects AllExceptGCDependency() {
1468 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1469 }
1470
1471 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001472 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001473 }
1474
Aart Bik34c3ba92015-07-20 14:08:59 -07001475 static SideEffects AllWrites() {
1476 return SideEffects(kAllWrites);
1477 }
1478
1479 static SideEffects AllReads() {
1480 return SideEffects(kAllReads);
1481 }
1482
1483 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1484 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001485 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001486 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001487 }
1488
Aart Bik854a02b2015-07-14 16:07:00 -07001489 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001490 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491 }
1492
Aart Bik34c3ba92015-07-20 14:08:59 -07001493 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1494 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001495 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001496 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001497 }
1498
1499 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001500 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001501 }
1502
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001503 static SideEffects CanTriggerGC() {
1504 return SideEffects(1ULL << kCanTriggerGCBit);
1505 }
1506
1507 static SideEffects DependsOnGC() {
1508 return SideEffects(1ULL << kDependsOnGCBit);
1509 }
1510
Aart Bik854a02b2015-07-14 16:07:00 -07001511 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001512 SideEffects Union(SideEffects other) const {
1513 return SideEffects(flags_ | other.flags_);
1514 }
1515
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001516 SideEffects Exclusion(SideEffects other) const {
1517 return SideEffects(flags_ & ~other.flags_);
1518 }
1519
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001520 void Add(SideEffects other) {
1521 flags_ |= other.flags_;
1522 }
1523
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001524 bool Includes(SideEffects other) const {
1525 return (other.flags_ & flags_) == other.flags_;
1526 }
1527
1528 bool HasSideEffects() const {
1529 return (flags_ & kAllChangeBits);
1530 }
1531
1532 bool HasDependencies() const {
1533 return (flags_ & kAllDependOnBits);
1534 }
1535
1536 // Returns true if there are no side effects or dependencies.
1537 bool DoesNothing() const {
1538 return flags_ == 0;
1539 }
1540
Aart Bik854a02b2015-07-14 16:07:00 -07001541 // Returns true if something is written.
1542 bool DoesAnyWrite() const {
1543 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001544 }
1545
Aart Bik854a02b2015-07-14 16:07:00 -07001546 // Returns true if something is read.
1547 bool DoesAnyRead() const {
1548 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001549 }
1550
Aart Bik854a02b2015-07-14 16:07:00 -07001551 // Returns true if potentially everything is written and read
1552 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 bool DoesAllReadWrite() const {
1554 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1555 }
1556
Aart Bik854a02b2015-07-14 16:07:00 -07001557 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001558 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001559 }
1560
Roland Levillain0d5a2812015-11-13 10:07:31 +00001561 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001562 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1564 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001565 }
1566
1567 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001568 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001569 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001570 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 for (int s = kLastBit; s >= 0; s--) {
1572 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1573 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1574 // This is a bit for the GC side effect.
1575 if (current_bit_is_set) {
1576 flags += "GC";
1577 }
Aart Bik854a02b2015-07-14 16:07:00 -07001578 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 } else {
1580 // This is a bit for the array/field analysis.
1581 // The underscore character stands for the 'can trigger GC' bit.
1582 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1583 if (current_bit_is_set) {
1584 flags += kDebug[s];
1585 }
1586 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1587 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1588 flags += "|";
1589 }
1590 }
Aart Bik854a02b2015-07-14 16:07:00 -07001591 }
1592 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001593 }
1594
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001596
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001597 private:
1598 static constexpr int kFieldArrayAnalysisBits = 9;
1599
1600 static constexpr int kFieldWriteOffset = 0;
1601 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1602 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1603 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1604
1605 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1606
1607 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1608 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1609 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1610 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1611
1612 static constexpr int kLastBit = kDependsOnGCBit;
1613 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1614
1615 // Aliases.
1616
1617 static_assert(kChangeBits == kDependOnBits,
1618 "the 'change' bits should match the 'depend on' bits.");
1619
1620 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1621 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1622 static constexpr uint64_t kAllWrites =
1623 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1624 static constexpr uint64_t kAllReads =
1625 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001626
Aart Bik854a02b2015-07-14 16:07:00 -07001627 // Translates type to bit flag.
1628 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1629 CHECK_NE(type, Primitive::kPrimVoid);
1630 const uint64_t one = 1;
1631 const int shift = type; // 0-based consecutive enum
1632 DCHECK_LE(kFieldWriteOffset, shift);
1633 DCHECK_LT(shift, kArrayWriteOffset);
1634 return one << (type + offset);
1635 }
1636
1637 // Private constructor on direct flags value.
1638 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1639
1640 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641};
1642
David Brazdiled596192015-01-23 10:39:45 +00001643// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001644class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001645 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001646 HEnvironment(ArenaAllocator* arena,
1647 size_t number_of_vregs,
1648 const DexFile& dex_file,
1649 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001650 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001651 InvokeType invoke_type,
1652 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001653 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1654 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001655 parent_(nullptr),
1656 dex_file_(dex_file),
1657 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001658 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001659 invoke_type_(invoke_type),
1660 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001661 }
1662
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001663 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001664 : HEnvironment(arena,
1665 to_copy.Size(),
1666 to_copy.GetDexFile(),
1667 to_copy.GetMethodIdx(),
1668 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001669 to_copy.GetInvokeType(),
1670 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001671
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001672 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001673 if (parent_ != nullptr) {
1674 parent_->SetAndCopyParentChain(allocator, parent);
1675 } else {
1676 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1677 parent_->CopyFrom(parent);
1678 if (parent->GetParent() != nullptr) {
1679 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1680 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001681 }
David Brazdiled596192015-01-23 10:39:45 +00001682 }
1683
Vladimir Marko71bf8092015-09-15 15:33:14 +01001684 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001685 void CopyFrom(HEnvironment* environment);
1686
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001687 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1688 // input to the loop phi instead. This is for inserting instructions that
1689 // require an environment (like HDeoptimization) in the loop pre-header.
1690 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001691
1692 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001693 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001694 }
1695
1696 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001697 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001698 }
1699
David Brazdil1abb4192015-02-17 18:33:36 +00001700 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001701
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001702 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001703
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001704 HEnvironment* GetParent() const { return parent_; }
1705
1706 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001707 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001708 }
1709
1710 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001711 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001712 }
1713
1714 uint32_t GetDexPc() const {
1715 return dex_pc_;
1716 }
1717
1718 uint32_t GetMethodIdx() const {
1719 return method_idx_;
1720 }
1721
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001722 InvokeType GetInvokeType() const {
1723 return invoke_type_;
1724 }
1725
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001726 const DexFile& GetDexFile() const {
1727 return dex_file_;
1728 }
1729
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001730 HInstruction* GetHolder() const {
1731 return holder_;
1732 }
1733
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001734
1735 bool IsFromInlinedInvoke() const {
1736 return GetParent() != nullptr;
1737 }
1738
David Brazdiled596192015-01-23 10:39:45 +00001739 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001740 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1741 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001742 HEnvironment* parent_;
1743 const DexFile& dex_file_;
1744 const uint32_t method_idx_;
1745 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001746 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001747
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001748 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001749 HInstruction* const holder_;
1750
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001751 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001752
1753 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1754};
1755
Vladimir Markof9f64412015-09-02 14:05:49 +01001756class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001757 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001758 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001759 : previous_(nullptr),
1760 next_(nullptr),
1761 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001762 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001763 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001764 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001765 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001766 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001767 locations_(nullptr),
1768 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001769 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001770 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001771 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1772 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1773 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001774
Dave Allison20dfc792014-06-16 20:44:29 -07001775 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001776
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001777#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001778 enum InstructionKind {
1779 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1780 };
1781#undef DECLARE_KIND
1782
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 HInstruction* GetNext() const { return next_; }
1784 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001785
Calin Juravle77520bc2015-01-12 18:45:46 +00001786 HInstruction* GetNextDisregardingMoves() const;
1787 HInstruction* GetPreviousDisregardingMoves() const;
1788
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001789 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001790 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001791 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001792 bool IsInBlock() const { return block_ != nullptr; }
1793 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001794 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1795 bool IsIrreducibleLoopHeaderPhi() const {
1796 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1797 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001798
Vladimir Marko372f10e2016-05-17 16:30:10 +01001799 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1800
1801 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1802 // One virtual method is enough, just const_cast<> and then re-add the const.
1803 return ArrayRef<const HUserRecord<HInstruction*>>(
1804 const_cast<HInstruction*>(this)->GetInputRecords());
1805 }
1806
1807 auto GetInputs() {
1808 return MakeTransformArrayRef(
1809 GetInputRecords(),
1810 [](HUserRecord<HInstruction*>& record) -> HInstruction* {
1811 return record.GetInstruction();
1812 });
1813 }
1814
1815 auto GetInputs() const {
1816 return MakeTransformArrayRef(
1817 GetInputRecords(),
1818 [](const HUserRecord<HInstruction*>& record) -> const HInstruction* {
1819 return record.GetInstruction();
1820 });
1821 }
1822
1823 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001824 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825
Vladimir Marko372f10e2016-05-17 16:30:10 +01001826 void SetRawInputAt(size_t index, HInstruction* input) {
1827 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1828 }
1829
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001830 virtual void Accept(HGraphVisitor* visitor) = 0;
1831 virtual const char* DebugName() const = 0;
1832
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1834
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001835 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001836
1837 uint32_t GetDexPc() const { return dex_pc_; }
1838
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001839 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001840
Roland Levillaine161a2a2014-10-03 12:45:18 +01001841 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001842 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001843
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001844 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001845 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001846
Calin Juravle10e244f2015-01-26 18:54:32 +00001847 // Does not apply for all instructions, but having this at top level greatly
1848 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001849 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001850 virtual bool CanBeNull() const {
1851 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1852 return true;
1853 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001855 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001856 return false;
1857 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001858
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001859 virtual bool IsActualObject() const {
1860 return GetType() == Primitive::kPrimNot;
1861 }
1862
Calin Juravle2e768302015-07-28 14:41:11 +00001863 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001864
Calin Juravle61d544b2015-02-23 16:46:57 +00001865 ReferenceTypeInfo GetReferenceTypeInfo() const {
1866 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001867 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001868 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001869 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001870
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001872 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001873 // Note: fixup_end remains valid across push_front().
1874 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1875 HUseListNode<HInstruction*>* new_node =
1876 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1877 uses_.push_front(*new_node);
1878 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001879 }
1880
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001881 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001882 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001883 // Note: env_fixup_end remains valid across push_front().
1884 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1885 HUseListNode<HEnvironment*>* new_node =
1886 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1887 env_uses_.push_front(*new_node);
1888 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889 }
1890
David Brazdil1abb4192015-02-17 18:33:36 +00001891 void RemoveAsUserOfInput(size_t input) {
1892 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001893 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1894 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1895 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001896 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001897
Vladimir Marko372f10e2016-05-17 16:30:10 +01001898 void RemoveAsUserOfAllInputs() {
1899 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1900 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1901 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1902 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1903 }
1904 }
1905
David Brazdil1abb4192015-02-17 18:33:36 +00001906 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1907 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908
Vladimir Marko46817b82016-03-29 12:21:58 +01001909 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1910 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1911 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001912 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001913 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001914 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001915
Roland Levillain6c82d402014-10-13 16:10:27 +01001916 // Does this instruction strictly dominate `other_instruction`?
1917 // Returns false if this instruction and `other_instruction` are the same.
1918 // Aborts if this instruction and `other_instruction` are both phis.
1919 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001920
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001921 int GetId() const { return id_; }
1922 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001924 int GetSsaIndex() const { return ssa_index_; }
1925 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1926 bool HasSsaIndex() const { return ssa_index_ != -1; }
1927
1928 bool HasEnvironment() const { return environment_ != nullptr; }
1929 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001930 // Set the `environment_` field. Raw because this method does not
1931 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001932 void SetRawEnvironment(HEnvironment* environment) {
1933 DCHECK(environment_ == nullptr);
1934 DCHECK_EQ(environment->GetHolder(), this);
1935 environment_ = environment;
1936 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001937
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001938 void InsertRawEnvironment(HEnvironment* environment) {
1939 DCHECK(environment_ != nullptr);
1940 DCHECK_EQ(environment->GetHolder(), this);
1941 DCHECK(environment->GetParent() == nullptr);
1942 environment->parent_ = environment_;
1943 environment_ = environment;
1944 }
1945
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001946 void RemoveEnvironment();
1947
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001948 // Set the environment of this instruction, copying it from `environment`. While
1949 // copying, the uses lists are being updated.
1950 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001951 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001952 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001953 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001954 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001955 if (environment->GetParent() != nullptr) {
1956 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1957 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001958 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001960 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1961 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001962 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001963 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001964 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001965 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001966 if (environment->GetParent() != nullptr) {
1967 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1968 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001969 }
1970
Nicolas Geoffray39468442014-09-02 15:17:15 +01001971 // Returns the number of entries in the environment. Typically, that is the
1972 // number of dex registers in a method. It could be more in case of inlining.
1973 size_t EnvironmentSize() const;
1974
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001975 LocationSummary* GetLocations() const { return locations_; }
1976 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001977
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001978 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001979 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001980
Alexandre Rames188d4312015-04-09 18:30:21 +01001981 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1982 // uses of this instruction by `other` are *not* updated.
1983 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1984 ReplaceWith(other);
1985 other->ReplaceInput(this, use_index);
1986 }
1987
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001988 // Move `this` instruction before `cursor`.
1989 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001990
Vladimir Markofb337ea2015-11-25 15:25:10 +00001991 // Move `this` before its first user and out of any loops. If there is no
1992 // out-of-loop user that dominates all other users, move the instruction
1993 // to the end of the out-of-loop common dominator of the user's blocks.
1994 //
1995 // This can be used only on non-throwing instructions with no side effects that
1996 // have at least one use but no environment uses.
1997 void MoveBeforeFirstUserAndOutOfLoops();
1998
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001999#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 bool Is##type() const; \
2001 const H##type* As##type() const; \
2002 H##type* As##type();
2003
2004 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2005#undef INSTRUCTION_TYPE_CHECK
2006
2007#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002008 bool Is##type() const { return (As##type() != nullptr); } \
2009 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002010 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002012#undef INSTRUCTION_TYPE_CHECK
2013
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002014 // Returns whether the instruction can be moved within the graph.
2015 virtual bool CanBeMoved() const { return false; }
2016
2017 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002018 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002019 return false;
2020 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002021
2022 // Returns whether any data encoded in the two instructions is equal.
2023 // This method does not look at the inputs. Both instructions must be
2024 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002025 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002026 return false;
2027 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002028
2029 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002030 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002031 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002032 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002033
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002034 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2035 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2036 // the virtual function because the __attribute__((__pure__)) doesn't really
2037 // apply the strong requirement for virtual functions, preventing optimizations.
2038 InstructionKind GetKind() const PURE;
2039 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002040
2041 virtual size_t ComputeHashCode() const {
2042 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002043 for (const HInstruction* input : GetInputs()) {
2044 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002045 }
2046 return result;
2047 }
2048
2049 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002050 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002051 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002052
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002053 size_t GetLifetimePosition() const { return lifetime_position_; }
2054 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2055 LiveInterval* GetLiveInterval() const { return live_interval_; }
2056 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2057 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2058
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002059 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2060
2061 // Returns whether the code generation of the instruction will require to have access
2062 // to the current method. Such instructions are:
2063 // (1): Instructions that require an environment, as calling the runtime requires
2064 // to walk the stack and have the current method stored at a specific stack address.
2065 // (2): Object literals like classes and strings, that are loaded from the dex cache
2066 // fields of the current method.
2067 bool NeedsCurrentMethod() const {
2068 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2069 }
2070
Vladimir Markodc151b22015-10-15 18:02:30 +01002071 // Returns whether the code generation of the instruction will require to have access
2072 // to the dex cache of the current method's declaring class via the current method.
2073 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002074
Mark Mendellc4701932015-04-10 13:18:51 -04002075 // Does this instruction have any use in an environment before
2076 // control flow hits 'other'?
2077 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2078
2079 // Remove all references to environment uses of this instruction.
2080 // The caller must ensure that this is safe to do.
2081 void RemoveEnvironmentUsers();
2082
Vladimir Markoa1de9182016-02-25 11:37:38 +00002083 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2084 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002085
David Brazdil1abb4192015-02-17 18:33:36 +00002086 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002087 // If set, the machine code for this instruction is assumed to be generated by
2088 // its users. Used by liveness analysis to compute use positions accordingly.
2089 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2090 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2091 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2092 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2093
Vladimir Marko372f10e2016-05-17 16:30:10 +01002094 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2095 return GetInputRecords()[i];
2096 }
2097
2098 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2099 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2100 input_records[index] = input;
2101 }
David Brazdil1abb4192015-02-17 18:33:36 +00002102
Vladimir Markoa1de9182016-02-25 11:37:38 +00002103 uint32_t GetPackedFields() const {
2104 return packed_fields_;
2105 }
2106
2107 template <size_t flag>
2108 bool GetPackedFlag() const {
2109 return (packed_fields_ & (1u << flag)) != 0u;
2110 }
2111
2112 template <size_t flag>
2113 void SetPackedFlag(bool value = true) {
2114 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2115 }
2116
2117 template <typename BitFieldType>
2118 typename BitFieldType::value_type GetPackedField() const {
2119 return BitFieldType::Decode(packed_fields_);
2120 }
2121
2122 template <typename BitFieldType>
2123 void SetPackedField(typename BitFieldType::value_type value) {
2124 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2125 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2126 }
2127
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002129 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2130 auto before_use_node = uses_.before_begin();
2131 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2132 HInstruction* user = use_node->GetUser();
2133 size_t input_index = use_node->GetIndex();
2134 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2135 before_use_node = use_node;
2136 }
2137 }
2138
2139 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2140 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2141 if (next != uses_.end()) {
2142 HInstruction* next_user = next->GetUser();
2143 size_t next_index = next->GetIndex();
2144 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2145 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2146 }
2147 }
2148
2149 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2150 auto before_env_use_node = env_uses_.before_begin();
2151 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2152 HEnvironment* user = env_use_node->GetUser();
2153 size_t input_index = env_use_node->GetIndex();
2154 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2155 before_env_use_node = env_use_node;
2156 }
2157 }
2158
2159 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2160 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2161 if (next != env_uses_.end()) {
2162 HEnvironment* next_user = next->GetUser();
2163 size_t next_index = next->GetIndex();
2164 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2165 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2166 }
2167 }
David Brazdil1abb4192015-02-17 18:33:36 +00002168
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002169 HInstruction* previous_;
2170 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002171 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002172 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174 // An instruction gets an id when it is added to the graph.
2175 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002176 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177 int id_;
2178
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002179 // When doing liveness analysis, instructions that have uses get an SSA index.
2180 int ssa_index_;
2181
Vladimir Markoa1de9182016-02-25 11:37:38 +00002182 // Packed fields.
2183 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002184
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002186 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187
2188 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002189 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002190
Nicolas Geoffray39468442014-09-02 15:17:15 +01002191 // The environment associated with this instruction. Not null if the instruction
2192 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002194
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002195 // Set by the code generator.
2196 LocationSummary* locations_;
2197
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002198 // Set by the liveness analysis.
2199 LiveInterval* live_interval_;
2200
2201 // Set by the liveness analysis, this is the position in a linear
2202 // order of blocks where this instruction's live interval start.
2203 size_t lifetime_position_;
2204
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002205 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002206
Vladimir Markoa1de9182016-02-25 11:37:38 +00002207 // The reference handle part of the reference type info.
2208 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002209 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002210 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002211
David Brazdil1abb4192015-02-17 18:33:36 +00002212 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002214 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002215 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002216 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217
2218 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2219};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002220std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221
2222class HInstructionIterator : public ValueObject {
2223 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002224 explicit HInstructionIterator(const HInstructionList& instructions)
2225 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002226 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227 }
2228
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002229 bool Done() const { return instruction_ == nullptr; }
2230 HInstruction* Current() const { return instruction_; }
2231 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002233 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002234 }
2235
2236 private:
2237 HInstruction* instruction_;
2238 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002239
2240 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241};
2242
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002243class HBackwardInstructionIterator : public ValueObject {
2244 public:
2245 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2246 : instruction_(instructions.last_instruction_) {
2247 next_ = Done() ? nullptr : instruction_->GetPrevious();
2248 }
2249
2250 bool Done() const { return instruction_ == nullptr; }
2251 HInstruction* Current() const { return instruction_; }
2252 void Advance() {
2253 instruction_ = next_;
2254 next_ = Done() ? nullptr : instruction_->GetPrevious();
2255 }
2256
2257 private:
2258 HInstruction* instruction_;
2259 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002260
2261 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002262};
2263
Vladimir Markof9f64412015-09-02 14:05:49 +01002264template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002265class HTemplateInstruction: public HInstruction {
2266 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002267 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002268 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002269 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002270
Vladimir Marko372f10e2016-05-17 16:30:10 +01002271 using HInstruction::GetInputRecords; // Keep the const version visible.
2272 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2273 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002274 }
2275
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002277 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002278
2279 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280};
2281
Vladimir Markof9f64412015-09-02 14:05:49 +01002282// HTemplateInstruction specialization for N=0.
2283template<>
2284class HTemplateInstruction<0>: public HInstruction {
2285 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002286 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002287 : HInstruction(side_effects, dex_pc) {}
2288
Vladimir Markof9f64412015-09-02 14:05:49 +01002289 virtual ~HTemplateInstruction() {}
2290
Vladimir Marko372f10e2016-05-17 16:30:10 +01002291 using HInstruction::GetInputRecords; // Keep the const version visible.
2292 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2293 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002294 }
2295
2296 private:
2297 friend class SsaBuilder;
2298};
2299
Dave Allison20dfc792014-06-16 20:44:29 -07002300template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002301class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002302 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002303 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002304 : HTemplateInstruction<N>(side_effects, dex_pc) {
2305 this->template SetPackedField<TypeField>(type);
2306 }
Dave Allison20dfc792014-06-16 20:44:29 -07002307 virtual ~HExpression() {}
2308
Vladimir Markoa1de9182016-02-25 11:37:38 +00002309 Primitive::Type GetType() const OVERRIDE {
2310 return TypeField::Decode(this->GetPackedFields());
2311 }
Dave Allison20dfc792014-06-16 20:44:29 -07002312
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002313 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002314 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2315 static constexpr size_t kFieldTypeSize =
2316 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2317 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2318 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2319 "Too many packed fields.");
2320 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002321};
2322
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002323// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2324// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002325class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002326 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002327 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2328 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002329
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002330 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002331
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002332 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002333
2334 private:
2335 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2336};
2337
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002338// Represents dex's RETURN opcodes. A HReturn is a control flow
2339// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002340class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002341 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002342 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2343 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002344 SetRawInputAt(0, value);
2345 }
2346
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002347 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002348
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002349 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002350
2351 private:
2352 DISALLOW_COPY_AND_ASSIGN(HReturn);
2353};
2354
Vladimir Markofcb503c2016-05-18 12:48:17 +01002355class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002356 public:
2357 HPhi(ArenaAllocator* arena,
2358 uint32_t reg_number,
2359 size_t number_of_inputs,
2360 Primitive::Type type,
2361 uint32_t dex_pc = kNoDexPc)
2362 : HInstruction(SideEffects::None(), dex_pc),
2363 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2364 reg_number_(reg_number) {
2365 SetPackedField<TypeField>(ToPhiType(type));
2366 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2367 // Phis are constructed live and marked dead if conflicting or unused.
2368 // Individual steps of SsaBuilder should assume that if a phi has been
2369 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2370 SetPackedFlag<kFlagIsLive>(true);
2371 SetPackedFlag<kFlagCanBeNull>(true);
2372 }
2373
2374 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2375 static Primitive::Type ToPhiType(Primitive::Type type) {
2376 return Primitive::PrimitiveKind(type);
2377 }
2378
2379 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2380
Vladimir Marko372f10e2016-05-17 16:30:10 +01002381 using HInstruction::GetInputRecords; // Keep the const version visible.
2382 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2383 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2384 }
David Brazdildee58d62016-04-07 09:54:26 +00002385
2386 void AddInput(HInstruction* input);
2387 void RemoveInputAt(size_t index);
2388
2389 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2390 void SetType(Primitive::Type new_type) {
2391 // Make sure that only valid type changes occur. The following are allowed:
2392 // (1) int -> float/ref (primitive type propagation),
2393 // (2) long -> double (primitive type propagation).
2394 DCHECK(GetType() == new_type ||
2395 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2396 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2397 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2398 SetPackedField<TypeField>(new_type);
2399 }
2400
2401 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2402 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2403
2404 uint32_t GetRegNumber() const { return reg_number_; }
2405
2406 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2407 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2408 bool IsDead() const { return !IsLive(); }
2409 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2410
2411 bool IsVRegEquivalentOf(HInstruction* other) const {
2412 return other != nullptr
2413 && other->IsPhi()
2414 && other->AsPhi()->GetBlock() == GetBlock()
2415 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2416 }
2417
2418 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2419 // An equivalent phi is a phi having the same dex register and type.
2420 // It assumes that phis with the same dex register are adjacent.
2421 HPhi* GetNextEquivalentPhiWithSameType() {
2422 HInstruction* next = GetNext();
2423 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2424 if (next->GetType() == GetType()) {
2425 return next->AsPhi();
2426 }
2427 next = next->GetNext();
2428 }
2429 return nullptr;
2430 }
2431
2432 DECLARE_INSTRUCTION(Phi);
2433
David Brazdildee58d62016-04-07 09:54:26 +00002434 private:
2435 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2436 static constexpr size_t kFieldTypeSize =
2437 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2438 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2439 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2440 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2441 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2442 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2443
Vladimir Marko372f10e2016-05-17 16:30:10 +01002444 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002445 const uint32_t reg_number_;
2446
2447 DISALLOW_COPY_AND_ASSIGN(HPhi);
2448};
2449
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002450// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002451// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002452// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002453class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002455 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002456
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002457 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002458
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002459 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002460
2461 private:
2462 DISALLOW_COPY_AND_ASSIGN(HExit);
2463};
2464
2465// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002466class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002467 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002469
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002470 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002472 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002473 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002474 }
2475
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002476 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002477
2478 private:
2479 DISALLOW_COPY_AND_ASSIGN(HGoto);
2480};
2481
Roland Levillain9867bc72015-08-05 10:21:34 +01002482class HConstant : public HExpression<0> {
2483 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002484 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2485 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002486
2487 bool CanBeMoved() const OVERRIDE { return true; }
2488
Roland Levillain1a653882016-03-18 18:05:57 +00002489 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002490 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002491 // Is this constant 0 in the arithmetic sense?
2492 virtual bool IsArithmeticZero() const { return false; }
2493 // Is this constant a 0-bit pattern?
2494 virtual bool IsZeroBitPattern() const { return false; }
2495 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002496 virtual bool IsOne() const { return false; }
2497
David Brazdil77a48ae2015-09-15 12:34:04 +00002498 virtual uint64_t GetValueAsUint64() const = 0;
2499
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002500 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002501
2502 private:
2503 DISALLOW_COPY_AND_ASSIGN(HConstant);
2504};
2505
Vladimir Markofcb503c2016-05-18 12:48:17 +01002506class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002507 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002508 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002509 return true;
2510 }
2511
David Brazdil77a48ae2015-09-15 12:34:04 +00002512 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2513
Roland Levillain9867bc72015-08-05 10:21:34 +01002514 size_t ComputeHashCode() const OVERRIDE { return 0; }
2515
Roland Levillain1a653882016-03-18 18:05:57 +00002516 // The null constant representation is a 0-bit pattern.
2517 virtual bool IsZeroBitPattern() const { return true; }
2518
Roland Levillain9867bc72015-08-05 10:21:34 +01002519 DECLARE_INSTRUCTION(NullConstant);
2520
2521 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002522 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002523
2524 friend class HGraph;
2525 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2526};
2527
2528// Constants of the type int. Those can be from Dex instructions, or
2529// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002530class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002531 public:
2532 int32_t GetValue() const { return value_; }
2533
David Brazdil9f389d42015-10-01 14:32:56 +01002534 uint64_t GetValueAsUint64() const OVERRIDE {
2535 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2536 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002537
Vladimir Marko372f10e2016-05-17 16:30:10 +01002538 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002539 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002540 return other->AsIntConstant()->value_ == value_;
2541 }
2542
2543 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2544
2545 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002546 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2547 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002548 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2549
Roland Levillain1a653882016-03-18 18:05:57 +00002550 // Integer constants are used to encode Boolean values as well,
2551 // where 1 means true and 0 means false.
2552 bool IsTrue() const { return GetValue() == 1; }
2553 bool IsFalse() const { return GetValue() == 0; }
2554
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 DECLARE_INSTRUCTION(IntConstant);
2556
2557 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002558 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2559 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2560 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2561 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002562
2563 const int32_t value_;
2564
2565 friend class HGraph;
2566 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2567 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2568 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2569};
2570
Vladimir Markofcb503c2016-05-18 12:48:17 +01002571class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002572 public:
2573 int64_t GetValue() const { return value_; }
2574
David Brazdil77a48ae2015-09-15 12:34:04 +00002575 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2576
Vladimir Marko372f10e2016-05-17 16:30:10 +01002577 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002578 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002579 return other->AsLongConstant()->value_ == value_;
2580 }
2581
2582 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2583
2584 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002585 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2586 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002587 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2588
2589 DECLARE_INSTRUCTION(LongConstant);
2590
2591 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002592 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2593 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002594
2595 const int64_t value_;
2596
2597 friend class HGraph;
2598 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2599};
Dave Allison20dfc792014-06-16 20:44:29 -07002600
Vladimir Markofcb503c2016-05-18 12:48:17 +01002601class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002602 public:
2603 float GetValue() const { return value_; }
2604
2605 uint64_t GetValueAsUint64() const OVERRIDE {
2606 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2607 }
2608
Vladimir Marko372f10e2016-05-17 16:30:10 +01002609 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002610 DCHECK(other->IsFloatConstant()) << other->DebugName();
2611 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2612 }
2613
2614 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2615
2616 bool IsMinusOne() const OVERRIDE {
2617 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2618 }
Roland Levillain1a653882016-03-18 18:05:57 +00002619 bool IsArithmeticZero() const OVERRIDE {
2620 return std::fpclassify(value_) == FP_ZERO;
2621 }
2622 bool IsArithmeticPositiveZero() const {
2623 return IsArithmeticZero() && !std::signbit(value_);
2624 }
2625 bool IsArithmeticNegativeZero() const {
2626 return IsArithmeticZero() && std::signbit(value_);
2627 }
2628 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002629 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002630 }
2631 bool IsOne() const OVERRIDE {
2632 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2633 }
2634 bool IsNaN() const {
2635 return std::isnan(value_);
2636 }
2637
2638 DECLARE_INSTRUCTION(FloatConstant);
2639
2640 private:
2641 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2642 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2643 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2644 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2645
2646 const float value_;
2647
2648 // Only the SsaBuilder and HGraph can create floating-point constants.
2649 friend class SsaBuilder;
2650 friend class HGraph;
2651 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2652};
2653
Vladimir Markofcb503c2016-05-18 12:48:17 +01002654class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002655 public:
2656 double GetValue() const { return value_; }
2657
2658 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2659
Vladimir Marko372f10e2016-05-17 16:30:10 +01002660 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002661 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2662 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2663 }
2664
2665 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2666
2667 bool IsMinusOne() const OVERRIDE {
2668 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2669 }
Roland Levillain1a653882016-03-18 18:05:57 +00002670 bool IsArithmeticZero() const OVERRIDE {
2671 return std::fpclassify(value_) == FP_ZERO;
2672 }
2673 bool IsArithmeticPositiveZero() const {
2674 return IsArithmeticZero() && !std::signbit(value_);
2675 }
2676 bool IsArithmeticNegativeZero() const {
2677 return IsArithmeticZero() && std::signbit(value_);
2678 }
2679 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002680 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002681 }
2682 bool IsOne() const OVERRIDE {
2683 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2684 }
2685 bool IsNaN() const {
2686 return std::isnan(value_);
2687 }
2688
2689 DECLARE_INSTRUCTION(DoubleConstant);
2690
2691 private:
2692 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2693 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2694 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2695 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2696
2697 const double value_;
2698
2699 // Only the SsaBuilder and HGraph can create floating-point constants.
2700 friend class SsaBuilder;
2701 friend class HGraph;
2702 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2703};
2704
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002705// Conditional branch. A block ending with an HIf instruction must have
2706// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002707class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002708 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002709 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2710 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002711 SetRawInputAt(0, input);
2712 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002713
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002714 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002715
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002716 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002717 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002718 }
2719
2720 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002721 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002722 }
2723
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002724 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002725
2726 private:
2727 DISALLOW_COPY_AND_ASSIGN(HIf);
2728};
2729
David Brazdilfc6a86a2015-06-26 10:33:45 +00002730
2731// Abstract instruction which marks the beginning and/or end of a try block and
2732// links it to the respective exception handlers. Behaves the same as a Goto in
2733// non-exceptional control flow.
2734// Normal-flow successor is stored at index zero, exception handlers under
2735// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002736class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002737 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002738 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002739 kEntry,
2740 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002741 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002742 };
2743
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002744 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002745 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2746 SetPackedField<BoundaryKindField>(kind);
2747 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002748
2749 bool IsControlFlow() const OVERRIDE { return true; }
2750
2751 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002752 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002753
David Brazdild26a4112015-11-10 11:07:31 +00002754 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2755 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2756 }
2757
David Brazdilfc6a86a2015-06-26 10:33:45 +00002758 // Returns whether `handler` is among its exception handlers (non-zero index
2759 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002760 bool HasExceptionHandler(const HBasicBlock& handler) const {
2761 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002762 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002763 }
2764
2765 // If not present already, adds `handler` to its block's list of exception
2766 // handlers.
2767 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002768 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002769 GetBlock()->AddSuccessor(handler);
2770 }
2771 }
2772
Vladimir Markoa1de9182016-02-25 11:37:38 +00002773 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2774 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002775
David Brazdilffee3d32015-07-06 11:48:53 +01002776 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2777
David Brazdilfc6a86a2015-06-26 10:33:45 +00002778 DECLARE_INSTRUCTION(TryBoundary);
2779
2780 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002781 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2782 static constexpr size_t kFieldBoundaryKindSize =
2783 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2784 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2785 kFieldBoundaryKind + kFieldBoundaryKindSize;
2786 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2787 "Too many packed fields.");
2788 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002789
2790 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2791};
2792
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002793// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002794class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002795 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002796 // We set CanTriggerGC to prevent any intermediate address to be live
2797 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002798 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002799 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002800 SetRawInputAt(0, cond);
2801 }
2802
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002803 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002804 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002805 return true;
2806 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002807 bool NeedsEnvironment() const OVERRIDE { return true; }
2808 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002809
2810 DECLARE_INSTRUCTION(Deoptimize);
2811
2812 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002813 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2814};
2815
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002816// Represents the ArtMethod that was passed as a first argument to
2817// the method. It is used by instructions that depend on it, like
2818// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002819class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2822 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002823
2824 DECLARE_INSTRUCTION(CurrentMethod);
2825
2826 private:
2827 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2828};
2829
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002830// Fetches an ArtMethod from the virtual table or the interface method table
2831// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002832class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002833 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002834 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002835 kVTable,
2836 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002837 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002838 };
2839 HClassTableGet(HInstruction* cls,
2840 Primitive::Type type,
2841 TableKind kind,
2842 size_t index,
2843 uint32_t dex_pc)
2844 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002845 index_(index) {
2846 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002847 SetRawInputAt(0, cls);
2848 }
2849
2850 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002851 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002852 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002853 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002854 }
2855
Vladimir Markoa1de9182016-02-25 11:37:38 +00002856 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002857 size_t GetIndex() const { return index_; }
2858
2859 DECLARE_INSTRUCTION(ClassTableGet);
2860
2861 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002862 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2863 static constexpr size_t kFieldTableKindSize =
2864 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2865 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2866 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2867 "Too many packed fields.");
2868 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2869
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002870 // The index of the ArtMethod in the table.
2871 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002872
2873 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2874};
2875
Mark Mendellfe57faa2015-09-18 09:26:15 -04002876// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2877// have one successor for each entry in the switch table, and the final successor
2878// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002879class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002880 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002881 HPackedSwitch(int32_t start_value,
2882 uint32_t num_entries,
2883 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002884 uint32_t dex_pc = kNoDexPc)
2885 : HTemplateInstruction(SideEffects::None(), dex_pc),
2886 start_value_(start_value),
2887 num_entries_(num_entries) {
2888 SetRawInputAt(0, input);
2889 }
2890
2891 bool IsControlFlow() const OVERRIDE { return true; }
2892
2893 int32_t GetStartValue() const { return start_value_; }
2894
Vladimir Marko211c2112015-09-24 16:52:33 +01002895 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002896
2897 HBasicBlock* GetDefaultBlock() const {
2898 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002899 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002900 }
2901 DECLARE_INSTRUCTION(PackedSwitch);
2902
2903 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002904 const int32_t start_value_;
2905 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002906
2907 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2908};
2909
Roland Levillain88cb1752014-10-20 16:36:47 +01002910class HUnaryOperation : public HExpression<1> {
2911 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002912 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2913 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002914 SetRawInputAt(0, input);
2915 }
2916
2917 HInstruction* GetInput() const { return InputAt(0); }
2918 Primitive::Type GetResultType() const { return GetType(); }
2919
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002920 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002921 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002922 return true;
2923 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002924
Roland Levillain31dd3d62016-02-16 12:21:02 +00002925 // Try to statically evaluate `this` and return a HConstant
2926 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002927 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002928 HConstant* TryStaticEvaluation() const;
2929
2930 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002931 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2932 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002933 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2934 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002935
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002936 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002937
2938 private:
2939 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2940};
2941
Dave Allison20dfc792014-06-16 20:44:29 -07002942class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002943 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002944 HBinaryOperation(Primitive::Type result_type,
2945 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002946 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002947 SideEffects side_effects = SideEffects::None(),
2948 uint32_t dex_pc = kNoDexPc)
2949 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950 SetRawInputAt(0, left);
2951 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002952 }
2953
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002954 HInstruction* GetLeft() const { return InputAt(0); }
2955 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002956 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002958 virtual bool IsCommutative() const { return false; }
2959
2960 // Put constant on the right.
2961 // Returns whether order is changed.
2962 bool OrderInputsWithConstantOnTheRight() {
2963 HInstruction* left = InputAt(0);
2964 HInstruction* right = InputAt(1);
2965 if (left->IsConstant() && !right->IsConstant()) {
2966 ReplaceInput(right, 0);
2967 ReplaceInput(left, 1);
2968 return true;
2969 }
2970 return false;
2971 }
2972
2973 // Order inputs by instruction id, but favor constant on the right side.
2974 // This helps GVN for commutative ops.
2975 void OrderInputs() {
2976 DCHECK(IsCommutative());
2977 HInstruction* left = InputAt(0);
2978 HInstruction* right = InputAt(1);
2979 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2980 return;
2981 }
2982 if (OrderInputsWithConstantOnTheRight()) {
2983 return;
2984 }
2985 // Order according to instruction id.
2986 if (left->GetId() > right->GetId()) {
2987 ReplaceInput(right, 0);
2988 ReplaceInput(left, 1);
2989 }
2990 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002991
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002992 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002993 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002994 return true;
2995 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002996
Roland Levillain31dd3d62016-02-16 12:21:02 +00002997 // Try to statically evaluate `this` and return a HConstant
2998 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002999 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003000 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003001
3002 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003003 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3004 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003005 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3006 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003007 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003008 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3009 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003010 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3011 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003012 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3013 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003014 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003015 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3016 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003017
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003018 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003019 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003020 HConstant* GetConstantRight() const;
3021
3022 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003023 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003024 HInstruction* GetLeastConstantLeft() const;
3025
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003026 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003027
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003028 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003029 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3030};
3031
Mark Mendellc4701932015-04-10 13:18:51 -04003032// The comparison bias applies for floating point operations and indicates how NaN
3033// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003034enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003035 kNoBias, // bias is not applicable (i.e. for long operation)
3036 kGtBias, // return 1 for NaN comparisons
3037 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003038 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003039};
3040
Roland Levillain31dd3d62016-02-16 12:21:02 +00003041std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3042
Dave Allison20dfc792014-06-16 20:44:29 -07003043class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003044 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003045 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003046 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3047 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3048 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003049
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003050 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003051 // `instruction`, and disregard moves in between.
3052 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003053
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003054 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003055
3056 virtual IfCondition GetCondition() const = 0;
3057
Mark Mendellc4701932015-04-10 13:18:51 -04003058 virtual IfCondition GetOppositeCondition() const = 0;
3059
Vladimir Markoa1de9182016-02-25 11:37:38 +00003060 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003061 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3062
Vladimir Markoa1de9182016-02-25 11:37:38 +00003063 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3064 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003065
Vladimir Marko372f10e2016-05-17 16:30:10 +01003066 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003067 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003068 }
3069
Roland Levillain4fa13f62015-07-06 18:11:54 +01003070 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003071 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003072 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003073 if (if_cond == kCondNE) {
3074 return true;
3075 } else if (if_cond == kCondEQ) {
3076 return false;
3077 }
3078 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003079 }
3080
3081 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003082 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003083 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003084 if (if_cond == kCondEQ) {
3085 return true;
3086 } else if (if_cond == kCondNE) {
3087 return false;
3088 }
3089 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003090 }
3091
Roland Levillain31dd3d62016-02-16 12:21:02 +00003092 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003093 // Needed if we merge a HCompare into a HCondition.
3094 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3095 static constexpr size_t kFieldComparisonBiasSize =
3096 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3097 static constexpr size_t kNumberOfConditionPackedBits =
3098 kFieldComparisonBias + kFieldComparisonBiasSize;
3099 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3100 using ComparisonBiasField =
3101 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3102
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103 template <typename T>
3104 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3105
3106 template <typename T>
3107 int32_t CompareFP(T x, T y) const {
3108 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3109 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3110 // Handle the bias.
3111 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3112 }
3113
3114 // Return an integer constant containing the result of a condition evaluated at compile time.
3115 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3116 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3117 }
3118
Dave Allison20dfc792014-06-16 20:44:29 -07003119 private:
3120 DISALLOW_COPY_AND_ASSIGN(HCondition);
3121};
3122
3123// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003124class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003125 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003126 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3127 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003128
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003129 bool IsCommutative() const OVERRIDE { return true; }
3130
Vladimir Marko9e23df52015-11-10 17:14:35 +00003131 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3132 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003133 return MakeConstantCondition(true, GetDexPc());
3134 }
3135 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3136 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3137 }
3138 // In the following Evaluate methods, a HCompare instruction has
3139 // been merged into this HEqual instruction; evaluate it as
3140 // `Compare(x, y) == 0`.
3141 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3142 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3143 GetDexPc());
3144 }
3145 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3146 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3147 }
3148 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3149 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003150 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003151
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003152 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003153
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003154 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003155 return kCondEQ;
3156 }
3157
Mark Mendellc4701932015-04-10 13:18:51 -04003158 IfCondition GetOppositeCondition() const OVERRIDE {
3159 return kCondNE;
3160 }
3161
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003162 private:
Aart Bike9f37602015-10-09 11:15:55 -07003163 template <typename T> bool Compute(T x, T y) const { return x == y; }
3164
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003165 DISALLOW_COPY_AND_ASSIGN(HEqual);
3166};
3167
Vladimir Markofcb503c2016-05-18 12:48:17 +01003168class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003169 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003170 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3171 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003172
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003173 bool IsCommutative() const OVERRIDE { return true; }
3174
Vladimir Marko9e23df52015-11-10 17:14:35 +00003175 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3176 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003177 return MakeConstantCondition(false, GetDexPc());
3178 }
3179 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3180 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3181 }
3182 // In the following Evaluate methods, a HCompare instruction has
3183 // been merged into this HNotEqual instruction; evaluate it as
3184 // `Compare(x, y) != 0`.
3185 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3186 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3187 }
3188 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3189 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3190 }
3191 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3192 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003193 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003194
Dave Allison20dfc792014-06-16 20:44:29 -07003195 DECLARE_INSTRUCTION(NotEqual);
3196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003197 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003198 return kCondNE;
3199 }
3200
Mark Mendellc4701932015-04-10 13:18:51 -04003201 IfCondition GetOppositeCondition() const OVERRIDE {
3202 return kCondEQ;
3203 }
3204
Dave Allison20dfc792014-06-16 20:44:29 -07003205 private:
Aart Bike9f37602015-10-09 11:15:55 -07003206 template <typename T> bool Compute(T x, T y) const { return x != y; }
3207
Dave Allison20dfc792014-06-16 20:44:29 -07003208 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3209};
3210
Vladimir Markofcb503c2016-05-18 12:48:17 +01003211class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003212 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003213 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3214 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003215
Roland Levillain9867bc72015-08-05 10:21:34 +01003216 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003217 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003218 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003219 // In the following Evaluate methods, a HCompare instruction has
3220 // been merged into this HLessThan instruction; evaluate it as
3221 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003222 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003223 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3224 }
3225 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3226 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3227 }
3228 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3229 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003230 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003231
Dave Allison20dfc792014-06-16 20:44:29 -07003232 DECLARE_INSTRUCTION(LessThan);
3233
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003234 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003235 return kCondLT;
3236 }
3237
Mark Mendellc4701932015-04-10 13:18:51 -04003238 IfCondition GetOppositeCondition() const OVERRIDE {
3239 return kCondGE;
3240 }
3241
Dave Allison20dfc792014-06-16 20:44:29 -07003242 private:
Aart Bike9f37602015-10-09 11:15:55 -07003243 template <typename T> bool Compute(T x, T y) const { return x < y; }
3244
Dave Allison20dfc792014-06-16 20:44:29 -07003245 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3246};
3247
Vladimir Markofcb503c2016-05-18 12:48:17 +01003248class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003249 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003250 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3251 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003252
Roland Levillain9867bc72015-08-05 10:21:34 +01003253 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003254 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003255 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003256 // In the following Evaluate methods, a HCompare instruction has
3257 // been merged into this HLessThanOrEqual instruction; evaluate it as
3258 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003259 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003260 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3261 }
3262 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3263 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3264 }
3265 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3266 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003267 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003268
Dave Allison20dfc792014-06-16 20:44:29 -07003269 DECLARE_INSTRUCTION(LessThanOrEqual);
3270
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003271 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003272 return kCondLE;
3273 }
3274
Mark Mendellc4701932015-04-10 13:18:51 -04003275 IfCondition GetOppositeCondition() const OVERRIDE {
3276 return kCondGT;
3277 }
3278
Dave Allison20dfc792014-06-16 20:44:29 -07003279 private:
Aart Bike9f37602015-10-09 11:15:55 -07003280 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3281
Dave Allison20dfc792014-06-16 20:44:29 -07003282 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3283};
3284
Vladimir Markofcb503c2016-05-18 12:48:17 +01003285class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003286 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003287 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3288 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003289
Roland Levillain9867bc72015-08-05 10:21:34 +01003290 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003291 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003292 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003293 // In the following Evaluate methods, a HCompare instruction has
3294 // been merged into this HGreaterThan instruction; evaluate it as
3295 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003296 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003297 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3298 }
3299 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3300 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3301 }
3302 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3303 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003304 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003305
Dave Allison20dfc792014-06-16 20:44:29 -07003306 DECLARE_INSTRUCTION(GreaterThan);
3307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003308 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003309 return kCondGT;
3310 }
3311
Mark Mendellc4701932015-04-10 13:18:51 -04003312 IfCondition GetOppositeCondition() const OVERRIDE {
3313 return kCondLE;
3314 }
3315
Dave Allison20dfc792014-06-16 20:44:29 -07003316 private:
Aart Bike9f37602015-10-09 11:15:55 -07003317 template <typename T> bool Compute(T x, T y) const { return x > y; }
3318
Dave Allison20dfc792014-06-16 20:44:29 -07003319 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3320};
3321
Vladimir Markofcb503c2016-05-18 12:48:17 +01003322class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003323 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003324 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3325 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003326
Roland Levillain9867bc72015-08-05 10:21:34 +01003327 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003328 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003329 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003330 // In the following Evaluate methods, a HCompare instruction has
3331 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3332 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003333 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003334 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3335 }
3336 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3337 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3338 }
3339 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3340 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003341 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003342
Dave Allison20dfc792014-06-16 20:44:29 -07003343 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3344
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003345 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003346 return kCondGE;
3347 }
3348
Mark Mendellc4701932015-04-10 13:18:51 -04003349 IfCondition GetOppositeCondition() const OVERRIDE {
3350 return kCondLT;
3351 }
3352
Dave Allison20dfc792014-06-16 20:44:29 -07003353 private:
Aart Bike9f37602015-10-09 11:15:55 -07003354 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3355
Dave Allison20dfc792014-06-16 20:44:29 -07003356 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3357};
3358
Vladimir Markofcb503c2016-05-18 12:48:17 +01003359class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003360 public:
3361 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3362 : HCondition(first, second, dex_pc) {}
3363
3364 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003365 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003366 }
3367 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3369 }
3370 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3371 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3372 LOG(FATAL) << DebugName() << " is not defined for float values";
3373 UNREACHABLE();
3374 }
3375 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3376 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3377 LOG(FATAL) << DebugName() << " is not defined for double values";
3378 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003379 }
3380
3381 DECLARE_INSTRUCTION(Below);
3382
3383 IfCondition GetCondition() const OVERRIDE {
3384 return kCondB;
3385 }
3386
3387 IfCondition GetOppositeCondition() const OVERRIDE {
3388 return kCondAE;
3389 }
3390
3391 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003392 template <typename T> bool Compute(T x, T y) const {
3393 return MakeUnsigned(x) < MakeUnsigned(y);
3394 }
Aart Bike9f37602015-10-09 11:15:55 -07003395
3396 DISALLOW_COPY_AND_ASSIGN(HBelow);
3397};
3398
Vladimir Markofcb503c2016-05-18 12:48:17 +01003399class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003400 public:
3401 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3402 : HCondition(first, second, dex_pc) {}
3403
3404 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003405 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003406 }
3407 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003408 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3409 }
3410 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3411 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3412 LOG(FATAL) << DebugName() << " is not defined for float values";
3413 UNREACHABLE();
3414 }
3415 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3416 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3417 LOG(FATAL) << DebugName() << " is not defined for double values";
3418 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003419 }
3420
3421 DECLARE_INSTRUCTION(BelowOrEqual);
3422
3423 IfCondition GetCondition() const OVERRIDE {
3424 return kCondBE;
3425 }
3426
3427 IfCondition GetOppositeCondition() const OVERRIDE {
3428 return kCondA;
3429 }
3430
3431 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 template <typename T> bool Compute(T x, T y) const {
3433 return MakeUnsigned(x) <= MakeUnsigned(y);
3434 }
Aart Bike9f37602015-10-09 11:15:55 -07003435
3436 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3437};
3438
Vladimir Markofcb503c2016-05-18 12:48:17 +01003439class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003440 public:
3441 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3442 : HCondition(first, second, dex_pc) {}
3443
3444 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003445 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003446 }
3447 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003448 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3449 }
3450 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3451 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3452 LOG(FATAL) << DebugName() << " is not defined for float values";
3453 UNREACHABLE();
3454 }
3455 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3456 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3457 LOG(FATAL) << DebugName() << " is not defined for double values";
3458 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003459 }
3460
3461 DECLARE_INSTRUCTION(Above);
3462
3463 IfCondition GetCondition() const OVERRIDE {
3464 return kCondA;
3465 }
3466
3467 IfCondition GetOppositeCondition() const OVERRIDE {
3468 return kCondBE;
3469 }
3470
3471 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 template <typename T> bool Compute(T x, T y) const {
3473 return MakeUnsigned(x) > MakeUnsigned(y);
3474 }
Aart Bike9f37602015-10-09 11:15:55 -07003475
3476 DISALLOW_COPY_AND_ASSIGN(HAbove);
3477};
3478
Vladimir Markofcb503c2016-05-18 12:48:17 +01003479class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003480 public:
3481 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3482 : HCondition(first, second, dex_pc) {}
3483
3484 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003485 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003486 }
3487 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003488 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3489 }
3490 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3491 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3492 LOG(FATAL) << DebugName() << " is not defined for float values";
3493 UNREACHABLE();
3494 }
3495 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3496 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3497 LOG(FATAL) << DebugName() << " is not defined for double values";
3498 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003499 }
3500
3501 DECLARE_INSTRUCTION(AboveOrEqual);
3502
3503 IfCondition GetCondition() const OVERRIDE {
3504 return kCondAE;
3505 }
3506
3507 IfCondition GetOppositeCondition() const OVERRIDE {
3508 return kCondB;
3509 }
3510
3511 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003512 template <typename T> bool Compute(T x, T y) const {
3513 return MakeUnsigned(x) >= MakeUnsigned(y);
3514 }
Aart Bike9f37602015-10-09 11:15:55 -07003515
3516 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3517};
Dave Allison20dfc792014-06-16 20:44:29 -07003518
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003519// Instruction to check how two inputs compare to each other.
3520// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003521class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003522 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003523 // Note that `comparison_type` is the type of comparison performed
3524 // between the comparison's inputs, not the type of the instantiated
3525 // HCompare instruction (which is always Primitive::kPrimInt).
3526 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003527 HInstruction* first,
3528 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003529 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003530 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003531 : HBinaryOperation(Primitive::kPrimInt,
3532 first,
3533 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003534 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003535 dex_pc) {
3536 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003537 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3538 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003539 }
3540
Roland Levillain9867bc72015-08-05 10:21:34 +01003541 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003542 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3543
3544 template <typename T>
3545 int32_t ComputeFP(T x, T y) const {
3546 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3547 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3548 // Handle the bias.
3549 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3550 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003551
Roland Levillain9867bc72015-08-05 10:21:34 +01003552 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003553 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3554 // reach this code path when processing a freshly built HIR
3555 // graph. However HCompare integer instructions can be synthesized
3556 // by the instruction simplifier to implement IntegerCompare and
3557 // IntegerSignum intrinsics, so we have to handle this case.
3558 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003559 }
3560 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003561 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3562 }
3563 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3564 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3565 }
3566 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3567 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003568 }
3569
Vladimir Marko372f10e2016-05-17 16:30:10 +01003570 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003571 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003572 }
3573
Vladimir Markoa1de9182016-02-25 11:37:38 +00003574 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003575
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003577 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003578 bool IsGtBias() const {
3579 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003580 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003581 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003582
Roland Levillain1693a1f2016-03-15 14:57:31 +00003583 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3584 // Comparisons do not require a runtime call in any back end.
3585 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003586 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003587
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003588 DECLARE_INSTRUCTION(Compare);
3589
Roland Levillain31dd3d62016-02-16 12:21:02 +00003590 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003591 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3592 static constexpr size_t kFieldComparisonBiasSize =
3593 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3594 static constexpr size_t kNumberOfComparePackedBits =
3595 kFieldComparisonBias + kFieldComparisonBiasSize;
3596 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3597 using ComparisonBiasField =
3598 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3599
Roland Levillain31dd3d62016-02-16 12:21:02 +00003600 // Return an integer constant containing the result of a comparison evaluated at compile time.
3601 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3602 DCHECK(value == -1 || value == 0 || value == 1) << value;
3603 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3604 }
3605
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003606 private:
3607 DISALLOW_COPY_AND_ASSIGN(HCompare);
3608};
3609
Vladimir Markofcb503c2016-05-18 12:48:17 +01003610class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003611 public:
3612 HNewInstance(HInstruction* cls,
3613 HCurrentMethod* current_method,
3614 uint32_t dex_pc,
3615 uint16_t type_index,
3616 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003617 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003618 bool finalizable,
3619 QuickEntrypointEnum entrypoint)
3620 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3621 type_index_(type_index),
3622 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003623 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003624 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003625 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003626 SetRawInputAt(0, cls);
3627 SetRawInputAt(1, current_method);
3628 }
3629
3630 uint16_t GetTypeIndex() const { return type_index_; }
3631 const DexFile& GetDexFile() const { return dex_file_; }
3632
3633 // Calls runtime so needs an environment.
3634 bool NeedsEnvironment() const OVERRIDE { return true; }
3635
Mingyao Yang062157f2016-03-02 10:15:36 -08003636 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3637 bool CanThrow() const OVERRIDE { return true; }
3638
3639 // Needs to call into runtime to make sure it's instantiable/accessible.
3640 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003641
Vladimir Markoa1de9182016-02-25 11:37:38 +00003642 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003643
3644 bool CanBeNull() const OVERRIDE { return false; }
3645
3646 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3647
3648 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3649 entrypoint_ = entrypoint;
3650 }
3651
3652 bool IsStringAlloc() const;
3653
3654 DECLARE_INSTRUCTION(NewInstance);
3655
3656 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003657 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3658 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003659 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3660 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3661 "Too many packed fields.");
3662
David Brazdil6de19382016-01-08 17:37:10 +00003663 const uint16_t type_index_;
3664 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003665 QuickEntrypointEnum entrypoint_;
3666
3667 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3668};
3669
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003670enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003671#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3672 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003673#include "intrinsics_list.h"
3674 kNone,
3675 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3676#undef INTRINSICS_LIST
3677#undef OPTIMIZING_INTRINSICS
3678};
3679std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3680
Agi Csaki05f20562015-08-19 14:58:14 -07003681enum IntrinsicNeedsEnvironmentOrCache {
3682 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3683 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003684};
3685
Aart Bik5d75afe2015-12-14 11:57:01 -08003686enum IntrinsicSideEffects {
3687 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3688 kReadSideEffects, // Intrinsic may read heap memory.
3689 kWriteSideEffects, // Intrinsic may write heap memory.
3690 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3691};
3692
3693enum IntrinsicExceptions {
3694 kNoThrow, // Intrinsic does not throw any exceptions.
3695 kCanThrow // Intrinsic may throw exceptions.
3696};
3697
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003698class HInvoke : public HInstruction {
3699 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003700 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003701
Vladimir Marko372f10e2016-05-17 16:30:10 +01003702 using HInstruction::GetInputRecords; // Keep the const version visible.
3703 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3704 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3705 }
3706
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003707 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003708 SetRawInputAt(index, argument);
3709 }
3710
Roland Levillain3e3d7332015-04-28 11:00:54 +01003711 // Return the number of arguments. This number can be lower than
3712 // the number of inputs returned by InputCount(), as some invoke
3713 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3714 // inputs at the end of their list of inputs.
3715 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3716
Vladimir Markoa1de9182016-02-25 11:37:38 +00003717 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003718
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003719 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003720 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003721
Vladimir Markoa1de9182016-02-25 11:37:38 +00003722 InvokeType GetOriginalInvokeType() const {
3723 return GetPackedField<OriginalInvokeTypeField>();
3724 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003725
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003726 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003727 return intrinsic_;
3728 }
3729
Aart Bik5d75afe2015-12-14 11:57:01 -08003730 void SetIntrinsic(Intrinsics intrinsic,
3731 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3732 IntrinsicSideEffects side_effects,
3733 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003734
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003735 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003736 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003737 }
3738
Vladimir Markoa1de9182016-02-25 11:37:38 +00003739 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003740
3741 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3742
Vladimir Marko372f10e2016-05-17 16:30:10 +01003743 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003744 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3745 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003746
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003747 uint32_t* GetIntrinsicOptimizations() {
3748 return &intrinsic_optimizations_;
3749 }
3750
3751 const uint32_t* GetIntrinsicOptimizations() const {
3752 return &intrinsic_optimizations_;
3753 }
3754
3755 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3756
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003757 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003758
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003759 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003760 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3761 static constexpr size_t kFieldOriginalInvokeTypeSize =
3762 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3763 static constexpr size_t kFieldReturnType =
3764 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3765 static constexpr size_t kFieldReturnTypeSize =
3766 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3767 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3768 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3769 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3770 using OriginalInvokeTypeField =
3771 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3772 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3773
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003774 HInvoke(ArenaAllocator* arena,
3775 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003776 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003777 Primitive::Type return_type,
3778 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003779 uint32_t dex_method_index,
3780 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003781 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003782 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003783 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003784 inputs_(number_of_arguments + number_of_other_inputs,
3785 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003786 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003787 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003788 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003789 SetPackedField<ReturnTypeField>(return_type);
3790 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3791 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003792 }
3793
Vladimir Markoa1de9182016-02-25 11:37:38 +00003794 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003795
Roland Levillain3e3d7332015-04-28 11:00:54 +01003796 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003797 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003798 const uint32_t dex_method_index_;
3799 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003800
3801 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3802 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003803
3804 private:
3805 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3806};
3807
Vladimir Markofcb503c2016-05-18 12:48:17 +01003808class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003809 public:
3810 HInvokeUnresolved(ArenaAllocator* arena,
3811 uint32_t number_of_arguments,
3812 Primitive::Type return_type,
3813 uint32_t dex_pc,
3814 uint32_t dex_method_index,
3815 InvokeType invoke_type)
3816 : HInvoke(arena,
3817 number_of_arguments,
3818 0u /* number_of_other_inputs */,
3819 return_type,
3820 dex_pc,
3821 dex_method_index,
3822 invoke_type) {
3823 }
3824
3825 DECLARE_INSTRUCTION(InvokeUnresolved);
3826
3827 private:
3828 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3829};
3830
Vladimir Markofcb503c2016-05-18 12:48:17 +01003831class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003832 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003833 // Requirements of this method call regarding the class
3834 // initialization (clinit) check of its declaring class.
3835 enum class ClinitCheckRequirement {
3836 kNone, // Class already initialized.
3837 kExplicit, // Static call having explicit clinit check as last input.
3838 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003839 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003840 };
3841
Vladimir Marko58155012015-08-19 12:49:41 +00003842 // Determines how to load the target ArtMethod*.
3843 enum class MethodLoadKind {
3844 // Use a String init ArtMethod* loaded from Thread entrypoints.
3845 kStringInit,
3846
3847 // Use the method's own ArtMethod* loaded by the register allocator.
3848 kRecursive,
3849
3850 // Use ArtMethod* at a known address, embed the direct address in the code.
3851 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3852 kDirectAddress,
3853
3854 // Use ArtMethod* at an address that will be known at link time, embed the direct
3855 // address in the code. If the image is relocatable, emit .patch_oat entry.
3856 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3857 // the image relocatable or not.
3858 kDirectAddressWithFixup,
3859
Vladimir Markoa1de9182016-02-25 11:37:38 +00003860 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003861 // Used when we need to use the dex cache, for example for invoke-static that
3862 // may cause class initialization (the entry may point to a resolution method),
3863 // and we know that we can access the dex cache arrays using a PC-relative load.
3864 kDexCachePcRelative,
3865
3866 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3867 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3868 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3869 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3870 kDexCacheViaMethod,
3871 };
3872
3873 // Determines the location of the code pointer.
3874 enum class CodePtrLocation {
3875 // Recursive call, use local PC-relative call instruction.
3876 kCallSelf,
3877
3878 // Use PC-relative call instruction patched at link time.
3879 // Used for calls within an oat file, boot->boot or app->app.
3880 kCallPCRelative,
3881
3882 // Call to a known target address, embed the direct address in code.
3883 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3884 kCallDirect,
3885
3886 // Call to a target address that will be known at link time, embed the direct
3887 // address in code. If the image is relocatable, emit .patch_oat entry.
3888 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3889 // the image relocatable or not.
3890 kCallDirectWithFixup,
3891
3892 // Use code pointer from the ArtMethod*.
3893 // Used when we don't know the target code. This is also the last-resort-kind used when
3894 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3895 kCallArtMethod,
3896 };
3897
3898 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003899 MethodLoadKind method_load_kind;
3900 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003901 // The method load data holds
3902 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3903 // Note that there are multiple string init methods, each having its own offset.
3904 // - the method address for kDirectAddress
3905 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003906 uint64_t method_load_data;
3907 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003908 };
3909
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003910 HInvokeStaticOrDirect(ArenaAllocator* arena,
3911 uint32_t number_of_arguments,
3912 Primitive::Type return_type,
3913 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003914 uint32_t method_index,
3915 MethodReference target_method,
3916 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003917 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003918 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003919 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003920 : HInvoke(arena,
3921 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003922 // There is potentially one extra argument for the HCurrentMethod node, and
3923 // potentially one other if the clinit check is explicit, and potentially
3924 // one other if the method is a string factory.
3925 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003926 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003927 return_type,
3928 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003929 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003930 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003931 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003932 dispatch_info_(dispatch_info) {
3933 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3934 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3935 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003936
Vladimir Markodc151b22015-10-15 18:02:30 +01003937 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003938 bool had_current_method_input = HasCurrentMethodInput();
3939 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3940
3941 // Using the current method is the default and once we find a better
3942 // method load kind, we should not go back to using the current method.
3943 DCHECK(had_current_method_input || !needs_current_method_input);
3944
3945 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003946 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3947 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003948 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003949 dispatch_info_ = dispatch_info;
3950 }
3951
Vladimir Markoc53c0792015-11-19 15:48:33 +00003952 void AddSpecialInput(HInstruction* input) {
3953 // We allow only one special input.
3954 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3955 DCHECK(InputCount() == GetSpecialInputIndex() ||
3956 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3957 InsertInputAt(GetSpecialInputIndex(), input);
3958 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003959
Vladimir Marko372f10e2016-05-17 16:30:10 +01003960 using HInstruction::GetInputRecords; // Keep the const version visible.
3961 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3962 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3963 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3964 DCHECK(!input_records.empty());
3965 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3966 HInstruction* last_input = input_records.back().GetInstruction();
3967 // Note: `last_input` may be null during arguments setup.
3968 if (last_input != nullptr) {
3969 // `last_input` is the last input of a static invoke marked as having
3970 // an explicit clinit check. It must either be:
3971 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3972 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3973 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3974 }
3975 }
3976 return input_records;
3977 }
3978
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003979 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003980 // We access the method via the dex cache so we can't do an implicit null check.
3981 // TODO: for intrinsics we can generate implicit null checks.
3982 return false;
3983 }
3984
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003985 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003986 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003987 }
3988
Vladimir Markoc53c0792015-11-19 15:48:33 +00003989 // Get the index of the special input, if any.
3990 //
David Brazdil6de19382016-01-08 17:37:10 +00003991 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3992 // method pointer; otherwise there may be one platform-specific special input,
3993 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003994 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003995 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003996
Vladimir Markoa1de9182016-02-25 11:37:38 +00003997 InvokeType GetOptimizedInvokeType() const {
3998 return GetPackedField<OptimizedInvokeTypeField>();
3999 }
4000
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004001 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004002 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004003 }
4004
Vladimir Marko58155012015-08-19 12:49:41 +00004005 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4006 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4007 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004008 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004009 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004010 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004011 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004012 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4013 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004014 bool HasCurrentMethodInput() const {
4015 // This function can be called only after the invoke has been fully initialized by the builder.
4016 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004017 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004018 return true;
4019 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004020 DCHECK(InputCount() == GetSpecialInputIndex() ||
4021 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004022 return false;
4023 }
4024 }
Vladimir Marko58155012015-08-19 12:49:41 +00004025 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4026 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004027 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004028
4029 int32_t GetStringInitOffset() const {
4030 DCHECK(IsStringInit());
4031 return dispatch_info_.method_load_data;
4032 }
4033
4034 uint64_t GetMethodAddress() const {
4035 DCHECK(HasMethodAddress());
4036 return dispatch_info_.method_load_data;
4037 }
4038
4039 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004040 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004041 return dispatch_info_.method_load_data;
4042 }
4043
4044 uint64_t GetDirectCodePtr() const {
4045 DCHECK(HasDirectCodePtr());
4046 return dispatch_info_.direct_code_ptr;
4047 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004048
Vladimir Markoa1de9182016-02-25 11:37:38 +00004049 ClinitCheckRequirement GetClinitCheckRequirement() const {
4050 return GetPackedField<ClinitCheckRequirementField>();
4051 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004052
Roland Levillain4c0eb422015-04-24 16:43:49 +01004053 // Is this instruction a call to a static method?
4054 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004055 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004056 }
4057
Vladimir Markofbb184a2015-11-13 14:47:00 +00004058 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4059 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4060 // instruction; only relevant for static calls with explicit clinit check.
4061 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004062 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004063 size_t last_input_index = inputs_.size() - 1u;
4064 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004065 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004066 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004067 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004068 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004069 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004070 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004071 }
4072
4073 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004074 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004075 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004076 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004077 }
4078
4079 // Is this a call to a static method whose declaring class has an
4080 // implicit intialization check requirement?
4081 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004082 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004083 }
4084
Vladimir Markob554b5a2015-11-06 12:57:55 +00004085 // Does this method load kind need the current method as an input?
4086 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4087 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4088 }
4089
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004090 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004091
Roland Levillain4c0eb422015-04-24 16:43:49 +01004092 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004093 void InsertInputAt(size_t index, HInstruction* input);
4094 void RemoveInputAt(size_t index);
4095
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004096 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004097 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4098 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4099 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4100 static constexpr size_t kFieldClinitCheckRequirement =
4101 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4102 static constexpr size_t kFieldClinitCheckRequirementSize =
4103 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4104 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4105 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4106 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4107 "Too many packed fields.");
4108 using OptimizedInvokeTypeField =
4109 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4110 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4111 kFieldClinitCheckRequirement,
4112 kFieldClinitCheckRequirementSize>;
4113
Vladimir Marko58155012015-08-19 12:49:41 +00004114 // The target method may refer to different dex file or method index than the original
4115 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4116 // in derived class to increase visibility.
4117 MethodReference target_method_;
4118 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004119
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004120 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004121};
Vladimir Markof64242a2015-12-01 14:58:23 +00004122std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004123std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004124
Vladimir Markofcb503c2016-05-18 12:48:17 +01004125class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004126 public:
4127 HInvokeVirtual(ArenaAllocator* arena,
4128 uint32_t number_of_arguments,
4129 Primitive::Type return_type,
4130 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004131 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004132 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004133 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004134 vtable_index_(vtable_index) {}
4135
Calin Juravle641547a2015-04-21 22:08:51 +01004136 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004137 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004138 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004139 }
4140
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004141 uint32_t GetVTableIndex() const { return vtable_index_; }
4142
4143 DECLARE_INSTRUCTION(InvokeVirtual);
4144
4145 private:
4146 const uint32_t vtable_index_;
4147
4148 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4149};
4150
Vladimir Markofcb503c2016-05-18 12:48:17 +01004151class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004152 public:
4153 HInvokeInterface(ArenaAllocator* arena,
4154 uint32_t number_of_arguments,
4155 Primitive::Type return_type,
4156 uint32_t dex_pc,
4157 uint32_t dex_method_index,
4158 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004159 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004160 imt_index_(imt_index) {}
4161
Calin Juravle641547a2015-04-21 22:08:51 +01004162 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004163 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004164 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004165 }
4166
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004167 uint32_t GetImtIndex() const { return imt_index_; }
4168 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4169
4170 DECLARE_INSTRUCTION(InvokeInterface);
4171
4172 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004173 const uint32_t imt_index_;
4174
4175 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4176};
4177
Vladimir Markofcb503c2016-05-18 12:48:17 +01004178class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004179 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004180 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004181 : HUnaryOperation(result_type, input, dex_pc) {
4182 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4183 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004184
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 template <typename T> T Compute(T x) const { return -x; }
4186
4187 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004188 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004189 }
4190 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004191 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004192 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004193 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4194 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4195 }
4196 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4197 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4198 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004199
Roland Levillain88cb1752014-10-20 16:36:47 +01004200 DECLARE_INSTRUCTION(Neg);
4201
4202 private:
4203 DISALLOW_COPY_AND_ASSIGN(HNeg);
4204};
4205
Vladimir Markofcb503c2016-05-18 12:48:17 +01004206class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004207 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004208 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004209 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004210 uint32_t dex_pc,
4211 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004212 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004213 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004215 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004216 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004217 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004219 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004220 }
4221
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004222 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004223 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004224
4225 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004226 bool NeedsEnvironment() const OVERRIDE { return true; }
4227
Mingyao Yang0c365e62015-03-31 15:09:29 -07004228 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4229 bool CanThrow() const OVERRIDE { return true; }
4230
Calin Juravle10e244f2015-01-26 18:54:32 +00004231 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004232
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004233 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4234
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004235 DECLARE_INSTRUCTION(NewArray);
4236
4237 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004238 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004239 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004240 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004241
4242 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4243};
4244
Vladimir Markofcb503c2016-05-18 12:48:17 +01004245class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004246 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004247 HAdd(Primitive::Type result_type,
4248 HInstruction* left,
4249 HInstruction* right,
4250 uint32_t dex_pc = kNoDexPc)
4251 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004253 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004254
Roland Levillain9867bc72015-08-05 10:21:34 +01004255 template <typename T> T Compute(T x, T y) const { return x + y; }
4256
4257 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 return GetBlock()->GetGraph()->GetIntConstant(
4259 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004260 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004261 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004262 return GetBlock()->GetGraph()->GetLongConstant(
4263 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004264 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004265 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4266 return GetBlock()->GetGraph()->GetFloatConstant(
4267 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4268 }
4269 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4270 return GetBlock()->GetGraph()->GetDoubleConstant(
4271 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4272 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004273
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004274 DECLARE_INSTRUCTION(Add);
4275
4276 private:
4277 DISALLOW_COPY_AND_ASSIGN(HAdd);
4278};
4279
Vladimir Markofcb503c2016-05-18 12:48:17 +01004280class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004281 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004282 HSub(Primitive::Type result_type,
4283 HInstruction* left,
4284 HInstruction* right,
4285 uint32_t dex_pc = kNoDexPc)
4286 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004287
Roland Levillain9867bc72015-08-05 10:21:34 +01004288 template <typename T> T Compute(T x, T y) const { return x - y; }
4289
4290 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 return GetBlock()->GetGraph()->GetIntConstant(
4292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004293 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004294 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004295 return GetBlock()->GetGraph()->GetLongConstant(
4296 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004297 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004298 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4299 return GetBlock()->GetGraph()->GetFloatConstant(
4300 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4301 }
4302 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4303 return GetBlock()->GetGraph()->GetDoubleConstant(
4304 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4305 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004306
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004307 DECLARE_INSTRUCTION(Sub);
4308
4309 private:
4310 DISALLOW_COPY_AND_ASSIGN(HSub);
4311};
4312
Vladimir Markofcb503c2016-05-18 12:48:17 +01004313class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 HMul(Primitive::Type result_type,
4316 HInstruction* left,
4317 HInstruction* right,
4318 uint32_t dex_pc = kNoDexPc)
4319 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004321 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004322
Roland Levillain9867bc72015-08-05 10:21:34 +01004323 template <typename T> T Compute(T x, T y) const { return x * y; }
4324
4325 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004326 return GetBlock()->GetGraph()->GetIntConstant(
4327 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004328 }
4329 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004330 return GetBlock()->GetGraph()->GetLongConstant(
4331 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004332 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004333 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4334 return GetBlock()->GetGraph()->GetFloatConstant(
4335 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4336 }
4337 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4338 return GetBlock()->GetGraph()->GetDoubleConstant(
4339 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4340 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004341
4342 DECLARE_INSTRUCTION(Mul);
4343
4344 private:
4345 DISALLOW_COPY_AND_ASSIGN(HMul);
4346};
4347
Vladimir Markofcb503c2016-05-18 12:48:17 +01004348class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004349 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004350 HDiv(Primitive::Type result_type,
4351 HInstruction* left,
4352 HInstruction* right,
4353 uint32_t dex_pc)
4354 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004355
Roland Levillain9867bc72015-08-05 10:21:34 +01004356 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004357 T ComputeIntegral(T x, T y) const {
4358 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 // Our graph structure ensures we never have 0 for `y` during
4360 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004361 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004362 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004363 return (y == -1) ? -x : x / y;
4364 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004365
Roland Levillain31dd3d62016-02-16 12:21:02 +00004366 template <typename T>
4367 T ComputeFP(T x, T y) const {
4368 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4369 return x / y;
4370 }
4371
Roland Levillain9867bc72015-08-05 10:21:34 +01004372 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004374 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004375 }
4376 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004377 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004378 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4379 }
4380 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4381 return GetBlock()->GetGraph()->GetFloatConstant(
4382 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4383 }
4384 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4385 return GetBlock()->GetGraph()->GetDoubleConstant(
4386 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004387 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004388
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004389 static SideEffects SideEffectsForArchRuntimeCalls() {
4390 // The generated code can use a runtime call.
4391 return SideEffects::CanTriggerGC();
4392 }
4393
Calin Juravle7c4954d2014-10-28 16:57:40 +00004394 DECLARE_INSTRUCTION(Div);
4395
4396 private:
4397 DISALLOW_COPY_AND_ASSIGN(HDiv);
4398};
4399
Vladimir Markofcb503c2016-05-18 12:48:17 +01004400class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004401 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004402 HRem(Primitive::Type result_type,
4403 HInstruction* left,
4404 HInstruction* right,
4405 uint32_t dex_pc)
4406 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004407
Roland Levillain9867bc72015-08-05 10:21:34 +01004408 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004409 T ComputeIntegral(T x, T y) const {
4410 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004411 // Our graph structure ensures we never have 0 for `y` during
4412 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004413 DCHECK_NE(y, 0);
4414 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4415 return (y == -1) ? 0 : x % y;
4416 }
4417
Roland Levillain31dd3d62016-02-16 12:21:02 +00004418 template <typename T>
4419 T ComputeFP(T x, T y) const {
4420 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4421 return std::fmod(x, y);
4422 }
4423
Roland Levillain9867bc72015-08-05 10:21:34 +01004424 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004426 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004427 }
4428 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004429 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004430 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004431 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004432 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4433 return GetBlock()->GetGraph()->GetFloatConstant(
4434 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4435 }
4436 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4437 return GetBlock()->GetGraph()->GetDoubleConstant(
4438 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4439 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004440
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004441 static SideEffects SideEffectsForArchRuntimeCalls() {
4442 return SideEffects::CanTriggerGC();
4443 }
4444
Calin Juravlebacfec32014-11-14 15:54:36 +00004445 DECLARE_INSTRUCTION(Rem);
4446
4447 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004448 DISALLOW_COPY_AND_ASSIGN(HRem);
4449};
4450
Vladimir Markofcb503c2016-05-18 12:48:17 +01004451class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004452 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004453 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4454 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004455 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004456 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004457 SetRawInputAt(0, value);
4458 }
4459
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004460 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4461
Calin Juravled0d48522014-11-04 16:40:20 +00004462 bool CanBeMoved() const OVERRIDE { return true; }
4463
Vladimir Marko372f10e2016-05-17 16:30:10 +01004464 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004465 return true;
4466 }
4467
4468 bool NeedsEnvironment() const OVERRIDE { return true; }
4469 bool CanThrow() const OVERRIDE { return true; }
4470
Calin Juravled0d48522014-11-04 16:40:20 +00004471 DECLARE_INSTRUCTION(DivZeroCheck);
4472
4473 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004474 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4475};
4476
Vladimir Markofcb503c2016-05-18 12:48:17 +01004477class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004478 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004479 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004480 HInstruction* value,
4481 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004483 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4484 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4485 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004486 }
4487
Roland Levillain5b5b9312016-03-22 14:57:31 +00004488 template <typename T>
4489 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4490 return value << (distance & max_shift_distance);
4491 }
4492
4493 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004494 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004495 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004496 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004497 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004498 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004499 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004501 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4502 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4503 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4504 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004505 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004506 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4507 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004508 LOG(FATAL) << DebugName() << " is not defined for float values";
4509 UNREACHABLE();
4510 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4512 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004513 LOG(FATAL) << DebugName() << " is not defined for double values";
4514 UNREACHABLE();
4515 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004516
4517 DECLARE_INSTRUCTION(Shl);
4518
4519 private:
4520 DISALLOW_COPY_AND_ASSIGN(HShl);
4521};
4522
Vladimir Markofcb503c2016-05-18 12:48:17 +01004523class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004524 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004525 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 HInstruction* value,
4527 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004528 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004529 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4530 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4531 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 }
4533
Roland Levillain5b5b9312016-03-22 14:57:31 +00004534 template <typename T>
4535 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4536 return value >> (distance & max_shift_distance);
4537 }
4538
4539 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004541 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004545 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004546 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4548 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4549 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4550 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004551 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004552 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4553 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004554 LOG(FATAL) << DebugName() << " is not defined for float values";
4555 UNREACHABLE();
4556 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4558 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004559 LOG(FATAL) << DebugName() << " is not defined for double values";
4560 UNREACHABLE();
4561 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004562
4563 DECLARE_INSTRUCTION(Shr);
4564
4565 private:
4566 DISALLOW_COPY_AND_ASSIGN(HShr);
4567};
4568
Vladimir Markofcb503c2016-05-18 12:48:17 +01004569class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004570 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004571 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HInstruction* value,
4573 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004574 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004575 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4576 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4577 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004578 }
4579
Roland Levillain5b5b9312016-03-22 14:57:31 +00004580 template <typename T>
4581 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4582 typedef typename std::make_unsigned<T>::type V;
4583 V ux = static_cast<V>(value);
4584 return static_cast<T>(ux >> (distance & max_shift_distance));
4585 }
4586
4587 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004591 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004592 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004593 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004594 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4596 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4597 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4598 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004599 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004600 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4601 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004602 LOG(FATAL) << DebugName() << " is not defined for float values";
4603 UNREACHABLE();
4604 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4606 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004607 LOG(FATAL) << DebugName() << " is not defined for double values";
4608 UNREACHABLE();
4609 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004610
4611 DECLARE_INSTRUCTION(UShr);
4612
4613 private:
4614 DISALLOW_COPY_AND_ASSIGN(HUShr);
4615};
4616
Vladimir Markofcb503c2016-05-18 12:48:17 +01004617class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004619 HAnd(Primitive::Type result_type,
4620 HInstruction* left,
4621 HInstruction* right,
4622 uint32_t dex_pc = kNoDexPc)
4623 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004625 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004626
Roland Levillaine53bd812016-02-24 14:54:18 +00004627 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004628
4629 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 return GetBlock()->GetGraph()->GetIntConstant(
4631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004632 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004633 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004634 return GetBlock()->GetGraph()->GetLongConstant(
4635 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004636 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004637 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4638 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4639 LOG(FATAL) << DebugName() << " is not defined for float values";
4640 UNREACHABLE();
4641 }
4642 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4643 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4644 LOG(FATAL) << DebugName() << " is not defined for double values";
4645 UNREACHABLE();
4646 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004647
4648 DECLARE_INSTRUCTION(And);
4649
4650 private:
4651 DISALLOW_COPY_AND_ASSIGN(HAnd);
4652};
4653
Vladimir Markofcb503c2016-05-18 12:48:17 +01004654class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004655 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004656 HOr(Primitive::Type result_type,
4657 HInstruction* left,
4658 HInstruction* right,
4659 uint32_t dex_pc = kNoDexPc)
4660 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004661
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004662 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004663
Roland Levillaine53bd812016-02-24 14:54:18 +00004664 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004665
4666 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 return GetBlock()->GetGraph()->GetIntConstant(
4668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 return GetBlock()->GetGraph()->GetLongConstant(
4672 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004674 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4675 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4676 LOG(FATAL) << DebugName() << " is not defined for float values";
4677 UNREACHABLE();
4678 }
4679 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4680 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4681 LOG(FATAL) << DebugName() << " is not defined for double values";
4682 UNREACHABLE();
4683 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004684
4685 DECLARE_INSTRUCTION(Or);
4686
4687 private:
4688 DISALLOW_COPY_AND_ASSIGN(HOr);
4689};
4690
Vladimir Markofcb503c2016-05-18 12:48:17 +01004691class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004693 HXor(Primitive::Type result_type,
4694 HInstruction* left,
4695 HInstruction* right,
4696 uint32_t dex_pc = kNoDexPc)
4697 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004698
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004699 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004700
Roland Levillaine53bd812016-02-24 14:54:18 +00004701 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004702
4703 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 return GetBlock()->GetGraph()->GetIntConstant(
4705 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004706 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004707 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 return GetBlock()->GetGraph()->GetLongConstant(
4709 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004710 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004711 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4712 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4713 LOG(FATAL) << DebugName() << " is not defined for float values";
4714 UNREACHABLE();
4715 }
4716 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4717 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4718 LOG(FATAL) << DebugName() << " is not defined for double values";
4719 UNREACHABLE();
4720 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004721
4722 DECLARE_INSTRUCTION(Xor);
4723
4724 private:
4725 DISALLOW_COPY_AND_ASSIGN(HXor);
4726};
4727
Vladimir Markofcb503c2016-05-18 12:48:17 +01004728class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004729 public:
4730 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004731 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4733 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004734 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004735
Roland Levillain5b5b9312016-03-22 14:57:31 +00004736 template <typename T>
4737 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4738 typedef typename std::make_unsigned<T>::type V;
4739 V ux = static_cast<V>(value);
4740 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004741 return static_cast<T>(ux);
4742 } else {
4743 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 return static_cast<T>(ux >> (distance & max_shift_value)) |
4745 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004746 }
4747 }
4748
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004750 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004751 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004752 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004754 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004755 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004756 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4758 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4759 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4760 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004761 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4763 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004764 LOG(FATAL) << DebugName() << " is not defined for float values";
4765 UNREACHABLE();
4766 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4768 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004769 LOG(FATAL) << DebugName() << " is not defined for double values";
4770 UNREACHABLE();
4771 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004772
4773 DECLARE_INSTRUCTION(Ror);
4774
4775 private:
4776 DISALLOW_COPY_AND_ASSIGN(HRor);
4777};
4778
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004779// The value of a parameter in this method. Its location depends on
4780// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004781class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004782 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004783 HParameterValue(const DexFile& dex_file,
4784 uint16_t type_index,
4785 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004786 Primitive::Type parameter_type,
4787 bool is_this = false)
4788 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004789 dex_file_(dex_file),
4790 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004791 index_(index) {
4792 SetPackedFlag<kFlagIsThis>(is_this);
4793 SetPackedFlag<kFlagCanBeNull>(!is_this);
4794 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004795
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004796 const DexFile& GetDexFile() const { return dex_file_; }
4797 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004798 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004799 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004800
Vladimir Markoa1de9182016-02-25 11:37:38 +00004801 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4802 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004803
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004804 DECLARE_INSTRUCTION(ParameterValue);
4805
4806 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004807 // Whether or not the parameter value corresponds to 'this' argument.
4808 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4809 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4810 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4811 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4812 "Too many packed fields.");
4813
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004814 const DexFile& dex_file_;
4815 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004816 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004817 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004818 const uint8_t index_;
4819
4820 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4821};
4822
Vladimir Markofcb503c2016-05-18 12:48:17 +01004823class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004824 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4826 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004827
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004828 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004829 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004830 return true;
4831 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004832
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 template <typename T> T Compute(T x) const { return ~x; }
4834
4835 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004836 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 }
4838 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004840 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004841 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4842 LOG(FATAL) << DebugName() << " is not defined for float values";
4843 UNREACHABLE();
4844 }
4845 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4846 LOG(FATAL) << DebugName() << " is not defined for double values";
4847 UNREACHABLE();
4848 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004849
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004850 DECLARE_INSTRUCTION(Not);
4851
4852 private:
4853 DISALLOW_COPY_AND_ASSIGN(HNot);
4854};
4855
Vladimir Markofcb503c2016-05-18 12:48:17 +01004856class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004857 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004858 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4859 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004860
4861 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004862 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004863 return true;
4864 }
4865
Roland Levillain9867bc72015-08-05 10:21:34 +01004866 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004867 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004868 return !x;
4869 }
4870
Roland Levillain9867bc72015-08-05 10:21:34 +01004871 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004872 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004873 }
4874 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4875 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004876 UNREACHABLE();
4877 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004878 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4879 LOG(FATAL) << DebugName() << " is not defined for float values";
4880 UNREACHABLE();
4881 }
4882 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4883 LOG(FATAL) << DebugName() << " is not defined for double values";
4884 UNREACHABLE();
4885 }
David Brazdil66d126e2015-04-03 16:02:44 +01004886
4887 DECLARE_INSTRUCTION(BooleanNot);
4888
4889 private:
4890 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4891};
4892
Vladimir Markofcb503c2016-05-18 12:48:17 +01004893class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004894 public:
4895 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004896 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004897 : HExpression(result_type,
4898 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4899 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004900 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004901 // Invariant: We should never generate a conversion to a Boolean value.
4902 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004903 }
4904
4905 HInstruction* GetInput() const { return InputAt(0); }
4906 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4907 Primitive::Type GetResultType() const { return GetType(); }
4908
4909 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004910 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4911 return true;
4912 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004913
Mark Mendelle82549b2015-05-06 10:55:34 -04004914 // Try to statically evaluate the conversion and return a HConstant
4915 // containing the result. If the input cannot be converted, return nullptr.
4916 HConstant* TryStaticEvaluation() const;
4917
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004918 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4919 Primitive::Type result_type) {
4920 // Some architectures may not require the 'GC' side effects, but at this point
4921 // in the compilation process we do not know what architecture we will
4922 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004923 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4924 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004925 return SideEffects::CanTriggerGC();
4926 }
4927 return SideEffects::None();
4928 }
4929
Roland Levillaindff1f282014-11-05 14:15:05 +00004930 DECLARE_INSTRUCTION(TypeConversion);
4931
4932 private:
4933 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4934};
4935
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004936static constexpr uint32_t kNoRegNumber = -1;
4937
Vladimir Markofcb503c2016-05-18 12:48:17 +01004938class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004939 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004940 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4941 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004942 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004943 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004944 SetRawInputAt(0, value);
4945 }
4946
Calin Juravle10e244f2015-01-26 18:54:32 +00004947 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004948 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004949 return true;
4950 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004951
Calin Juravle10e244f2015-01-26 18:54:32 +00004952 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953
Calin Juravle10e244f2015-01-26 18:54:32 +00004954 bool CanThrow() const OVERRIDE { return true; }
4955
4956 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004957
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004958
4959 DECLARE_INSTRUCTION(NullCheck);
4960
4961 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4963};
4964
4965class FieldInfo : public ValueObject {
4966 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004967 FieldInfo(MemberOffset field_offset,
4968 Primitive::Type field_type,
4969 bool is_volatile,
4970 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004971 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004972 const DexFile& dex_file,
4973 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004974 : field_offset_(field_offset),
4975 field_type_(field_type),
4976 is_volatile_(is_volatile),
4977 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004978 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004979 dex_file_(dex_file),
4980 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981
4982 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004983 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004984 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004985 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004986 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004987 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004988 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989
4990 private:
4991 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004992 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004993 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004994 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004995 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004996 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004997 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998};
4999
Vladimir Markofcb503c2016-05-18 12:48:17 +01005000class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001 public:
5002 HInstanceFieldGet(HInstruction* value,
5003 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005004 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005005 bool is_volatile,
5006 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005007 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005008 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005009 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005010 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005011 : HExpression(field_type,
5012 SideEffects::FieldReadOfType(field_type, is_volatile),
5013 dex_pc),
5014 field_info_(field_offset,
5015 field_type,
5016 is_volatile,
5017 field_idx,
5018 declaring_class_def_index,
5019 dex_file,
5020 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005021 SetRawInputAt(0, value);
5022 }
5023
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005024 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005025
Vladimir Marko372f10e2016-05-17 16:30:10 +01005026 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5027 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005028 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005029 }
5030
Calin Juravle641547a2015-04-21 22:08:51 +01005031 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005032 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005033 }
5034
5035 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005036 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5037 }
5038
Calin Juravle52c48962014-12-16 17:02:57 +00005039 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005040 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005041 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005042 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005043
5044 DECLARE_INSTRUCTION(InstanceFieldGet);
5045
5046 private:
5047 const FieldInfo field_info_;
5048
5049 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5050};
5051
Vladimir Markofcb503c2016-05-18 12:48:17 +01005052class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053 public:
5054 HInstanceFieldSet(HInstruction* object,
5055 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005056 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005057 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005058 bool is_volatile,
5059 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005060 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005061 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005062 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005063 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005064 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5065 dex_pc),
5066 field_info_(field_offset,
5067 field_type,
5068 is_volatile,
5069 field_idx,
5070 declaring_class_def_index,
5071 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005072 dex_cache) {
5073 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005074 SetRawInputAt(0, object);
5075 SetRawInputAt(1, value);
5076 }
5077
Calin Juravle641547a2015-04-21 22:08:51 +01005078 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005079 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005080 }
5081
Calin Juravle52c48962014-12-16 17:02:57 +00005082 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005084 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005085 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005086 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005087 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5088 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005089
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005090 DECLARE_INSTRUCTION(InstanceFieldSet);
5091
5092 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005093 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5094 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5095 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5096 "Too many packed fields.");
5097
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005098 const FieldInfo field_info_;
5099
5100 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5101};
5102
Vladimir Markofcb503c2016-05-18 12:48:17 +01005103class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005105 HArrayGet(HInstruction* array,
5106 HInstruction* index,
5107 Primitive::Type type,
5108 uint32_t dex_pc,
5109 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005110 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005111 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 SetRawInputAt(0, array);
5113 SetRawInputAt(1, index);
5114 }
5115
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005116 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005117 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005118 return true;
5119 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005120 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005121 // TODO: We can be smarter here.
5122 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5123 // which generates the implicit null check. There are cases when these can be removed
5124 // to produce better code. If we ever add optimizations to do so we should allow an
5125 // implicit check here (as long as the address falls in the first page).
5126 return false;
5127 }
5128
David Brazdil4833f5a2015-12-16 10:37:39 +00005129 bool IsEquivalentOf(HArrayGet* other) const {
5130 bool result = (GetDexPc() == other->GetDexPc());
5131 if (kIsDebugBuild && result) {
5132 DCHECK_EQ(GetBlock(), other->GetBlock());
5133 DCHECK_EQ(GetArray(), other->GetArray());
5134 DCHECK_EQ(GetIndex(), other->GetIndex());
5135 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005136 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005137 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005138 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5139 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005140 }
5141 }
5142 return result;
5143 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005144
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005145 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5146
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005147 HInstruction* GetArray() const { return InputAt(0); }
5148 HInstruction* GetIndex() const { return InputAt(1); }
5149
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005150 DECLARE_INSTRUCTION(ArrayGet);
5151
5152 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005153 // We treat a String as an array, creating the HArrayGet from String.charAt()
5154 // intrinsic in the instruction simplifier. We can always determine whether
5155 // a particular HArrayGet is actually a String.charAt() by looking at the type
5156 // of the input but that requires holding the mutator lock, so we prefer to use
5157 // a flag, so that code generators don't need to do the locking.
5158 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5159 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5160 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5161 "Too many packed fields.");
5162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5164};
5165
Vladimir Markofcb503c2016-05-18 12:48:17 +01005166class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167 public:
5168 HArraySet(HInstruction* array,
5169 HInstruction* index,
5170 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005171 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005172 uint32_t dex_pc)
5173 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005174 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5175 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5176 SetPackedFlag<kFlagValueCanBeNull>(true);
5177 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178 SetRawInputAt(0, array);
5179 SetRawInputAt(1, index);
5180 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005181 // Make a best guess now, may be refined during SSA building.
5182 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183 }
5184
Calin Juravle77520bc2015-01-12 18:45:46 +00005185 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005186 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005187 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005188 }
5189
Mingyao Yang81014cb2015-06-02 03:16:27 -07005190 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005191 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005192
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005193 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005194 // TODO: Same as for ArrayGet.
5195 return false;
5196 }
5197
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005198 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005199 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005200 }
5201
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005202 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005204 }
5205
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005206 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005207 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005208 }
5209
Vladimir Markoa1de9182016-02-25 11:37:38 +00005210 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5211 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5212 bool StaticTypeOfArrayIsObjectArray() const {
5213 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5214 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005215
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005216 HInstruction* GetArray() const { return InputAt(0); }
5217 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005218 HInstruction* GetValue() const { return InputAt(2); }
5219
5220 Primitive::Type GetComponentType() const {
5221 // The Dex format does not type floating point index operations. Since the
5222 // `expected_component_type_` is set during building and can therefore not
5223 // be correct, we also check what is the value type. If it is a floating
5224 // point type, we must use that type.
5225 Primitive::Type value_type = GetValue()->GetType();
5226 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5227 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005228 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005229 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005230
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005231 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005232 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005233 }
5234
Aart Bik18b36ab2016-04-13 16:41:35 -07005235 void ComputeSideEffects() {
5236 Primitive::Type type = GetComponentType();
5237 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5238 SideEffectsForArchRuntimeCalls(type)));
5239 }
5240
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005241 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5242 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5243 }
5244
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 DECLARE_INSTRUCTION(ArraySet);
5246
5247 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005248 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5249 static constexpr size_t kFieldExpectedComponentTypeSize =
5250 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5251 static constexpr size_t kFlagNeedsTypeCheck =
5252 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5253 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005254 // Cached information for the reference_type_info_ so that codegen
5255 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005256 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5257 static constexpr size_t kNumberOfArraySetPackedBits =
5258 kFlagStaticTypeOfArrayIsObjectArray + 1;
5259 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5260 using ExpectedComponentTypeField =
5261 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262
5263 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5264};
5265
Vladimir Markofcb503c2016-05-18 12:48:17 +01005266class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005268 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005269 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005270 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005271 // Note that arrays do not change length, so the instruction does not
5272 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 SetRawInputAt(0, array);
5274 }
5275
Calin Juravle77520bc2015-01-12 18:45:46 +00005276 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005277 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005278 return true;
5279 }
Calin Juravle641547a2015-04-21 22:08:51 +01005280 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5281 return obj == InputAt(0);
5282 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005283
Vladimir Markodce016e2016-04-28 13:10:02 +01005284 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5285
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 DECLARE_INSTRUCTION(ArrayLength);
5287
5288 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005289 // We treat a String as an array, creating the HArrayLength from String.length()
5290 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5291 // determine whether a particular HArrayLength is actually a String.length() by
5292 // looking at the type of the input but that requires holding the mutator lock, so
5293 // we prefer to use a flag, so that code generators don't need to do the locking.
5294 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5295 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5296 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5297 "Too many packed fields.");
5298
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5300};
5301
Vladimir Markofcb503c2016-05-18 12:48:17 +01005302class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005304 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5305 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005306 HBoundsCheck(HInstruction* index,
5307 HInstruction* length,
5308 uint32_t dex_pc,
5309 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5310 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5311 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005312 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313 SetRawInputAt(0, index);
5314 SetRawInputAt(1, length);
5315 }
5316
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005317 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005318 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005319 return true;
5320 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005322 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005324 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005325
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005326 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5327 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5328
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005329 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330
5331 DECLARE_INSTRUCTION(BoundsCheck);
5332
5333 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005334 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5335 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5336 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5337 // so we need to create an HEnvironment which will be translated to an InlineInfo
5338 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5339 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5340 // from the invoke as we don't want to look again at the dex bytecode.
5341 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5342
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5344};
5345
Vladimir Markofcb503c2016-05-18 12:48:17 +01005346class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005347 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005348 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005349 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005350
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005351 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005352 return true;
5353 }
5354
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005355 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5356 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005357
5358 DECLARE_INSTRUCTION(SuspendCheck);
5359
5360 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005361 // Only used for code generation, in order to share the same slow path between back edges
5362 // of a same loop.
5363 SlowPathCode* slow_path_;
5364
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005365 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5366};
5367
David Srbecky0cf44932015-12-09 14:09:59 +00005368// Pseudo-instruction which provides the native debugger with mapping information.
5369// It ensures that we can generate line number and local variables at this point.
5370class HNativeDebugInfo : public HTemplateInstruction<0> {
5371 public:
5372 explicit HNativeDebugInfo(uint32_t dex_pc)
5373 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5374
5375 bool NeedsEnvironment() const OVERRIDE {
5376 return true;
5377 }
5378
5379 DECLARE_INSTRUCTION(NativeDebugInfo);
5380
5381 private:
5382 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5383};
5384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385/**
5386 * Instruction to load a Class object.
5387 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005388class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005389 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005390 // Determines how to load the Class.
5391 enum class LoadKind {
5392 // Use the Class* from the method's own ArtMethod*.
5393 kReferrersClass,
5394
5395 // Use boot image Class* address that will be known at link time.
5396 // Used for boot image classes referenced by boot image code in non-PIC mode.
5397 kBootImageLinkTimeAddress,
5398
5399 // Use PC-relative boot image Class* address that will be known at link time.
5400 // Used for boot image classes referenced by boot image code in PIC mode.
5401 kBootImageLinkTimePcRelative,
5402
5403 // Use a known boot image Class* address, embedded in the code by the codegen.
5404 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5405 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5406 // GetIncludePatchInformation().
5407 kBootImageAddress,
5408
5409 // Load from the resolved types array at an absolute address.
5410 // Used for classes outside the boot image referenced by JIT-compiled code.
5411 kDexCacheAddress,
5412
5413 // Load from resolved types array in the dex cache using a PC-relative load.
5414 // Used for classes outside boot image when we know that we can access
5415 // the dex cache arrays using a PC-relative load.
5416 kDexCachePcRelative,
5417
5418 // Load from resolved types array accessed through the class loaded from
5419 // the compiled method's own ArtMethod*. This is the default access type when
5420 // all other types are unavailable.
5421 kDexCacheViaMethod,
5422
5423 kLast = kDexCacheViaMethod
5424 };
5425
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005426 HLoadClass(HCurrentMethod* current_method,
5427 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005428 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005429 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005430 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005431 bool needs_access_check,
5432 bool is_in_dex_cache)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005433 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5434 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005435 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005436 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005437 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005438 // Referrers class should not need access check. We never inline unverified
5439 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005440 DCHECK(!is_referrers_class || !needs_access_check);
5441
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005442 SetPackedField<LoadKindField>(
5443 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005444 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5445 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5446 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005447 }
5448
5449 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5450 DCHECK(HasAddress(load_kind));
5451 load_data_.address = address;
5452 SetLoadKindInternal(load_kind);
5453 }
5454
5455 void SetLoadKindWithTypeReference(LoadKind load_kind,
5456 const DexFile& dex_file,
5457 uint32_t type_index) {
5458 DCHECK(HasTypeReference(load_kind));
5459 DCHECK(IsSameDexFile(dex_file_, dex_file));
5460 DCHECK_EQ(type_index_, type_index);
5461 SetLoadKindInternal(load_kind);
5462 }
5463
5464 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5465 const DexFile& dex_file,
5466 uint32_t element_index) {
5467 DCHECK(HasDexCacheReference(load_kind));
5468 DCHECK(IsSameDexFile(dex_file_, dex_file));
5469 load_data_.dex_cache_element_index = element_index;
5470 SetLoadKindInternal(load_kind);
5471 }
5472
5473 LoadKind GetLoadKind() const {
5474 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005475 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005476
5477 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005478
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005479 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005480
5481 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5482
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005483 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005484
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005485 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005486 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005487 }
5488
Calin Juravle0ba218d2015-05-19 18:46:01 +01005489 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005490 // The entrypoint the code generator is going to call does not do
5491 // clinit of the class.
5492 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005493 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005494 }
5495
5496 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005497 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005498 (!IsReferrersClass() && !IsInDexCache()) ||
5499 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005500 }
5501
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005502
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005503 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005504 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005505 }
5506
Calin Juravleacf735c2015-02-12 15:25:22 +00005507 ReferenceTypeInfo GetLoadedClassRTI() {
5508 return loaded_class_rti_;
5509 }
5510
5511 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5512 // Make sure we only set exact types (the loaded class should never be merged).
5513 DCHECK(rti.IsExact());
5514 loaded_class_rti_ = rti;
5515 }
5516
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005517 uint32_t GetTypeIndex() const { return type_index_; }
5518 const DexFile& GetDexFile() const { return dex_file_; }
5519
5520 uint32_t GetDexCacheElementOffset() const;
5521
5522 uint64_t GetAddress() const {
5523 DCHECK(HasAddress(GetLoadKind()));
5524 return load_data_.address;
5525 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005526
Vladimir Markoa1de9182016-02-25 11:37:38 +00005527 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005528
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005529 static SideEffects SideEffectsForArchRuntimeCalls() {
5530 return SideEffects::CanTriggerGC();
5531 }
5532
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005533 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005534 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5535 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5536 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005537
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005538 void MarkInDexCache() {
5539 SetPackedFlag<kFlagIsInDexCache>(true);
5540 DCHECK(!NeedsEnvironment());
5541 RemoveEnvironment();
5542 SetSideEffects(SideEffects::None());
5543 }
5544
5545 void AddSpecialInput(HInstruction* special_input);
5546
5547 using HInstruction::GetInputRecords; // Keep the const version visible.
5548 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5549 return ArrayRef<HUserRecord<HInstruction*>>(
5550 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5551 }
5552
5553 Primitive::Type GetType() const OVERRIDE {
5554 return Primitive::kPrimNot;
5555 }
5556
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005557 DECLARE_INSTRUCTION(LoadClass);
5558
5559 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005560 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005561 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005562 // Whether this instruction must generate the initialization check.
5563 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005564 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005565 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5566 static constexpr size_t kFieldLoadKindSize =
5567 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5568 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005569 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005570 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5571
5572 static bool HasTypeReference(LoadKind load_kind) {
5573 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5574 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5575 load_kind == LoadKind::kDexCacheViaMethod ||
5576 load_kind == LoadKind::kReferrersClass;
5577 }
5578
5579 static bool HasAddress(LoadKind load_kind) {
5580 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5581 }
5582
5583 static bool HasDexCacheReference(LoadKind load_kind) {
5584 return load_kind == LoadKind::kDexCachePcRelative;
5585 }
5586
5587 void SetLoadKindInternal(LoadKind load_kind);
5588
5589 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5590 // For other load kinds it's empty or possibly some architecture-specific instruction
5591 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5592 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005593
5594 const uint16_t type_index_;
5595 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005596
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005597 union {
5598 uint32_t dex_cache_element_index; // Only for dex cache reference.
5599 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5600 } load_data_;
5601
Calin Juravleacf735c2015-02-12 15:25:22 +00005602 ReferenceTypeInfo loaded_class_rti_;
5603
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005604 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5605};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005606std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5607
5608// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5609inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5610 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5611 return load_data_.dex_cache_element_index;
5612}
5613
5614// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5615inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
5616 // The special input is used for PC-relative loads on some architectures.
5617 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5618 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5619 DCHECK(special_input_.GetInstruction() == nullptr);
5620 special_input_ = HUserRecord<HInstruction*>(special_input);
5621 special_input->AddUseAt(this, 0);
5622}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005623
Vladimir Marko372f10e2016-05-17 16:30:10 +01005624class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005625 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005626 // Determines how to load the String.
5627 enum class LoadKind {
5628 // Use boot image String* address that will be known at link time.
5629 // Used for boot image strings referenced by boot image code in non-PIC mode.
5630 kBootImageLinkTimeAddress,
5631
5632 // Use PC-relative boot image String* address that will be known at link time.
5633 // Used for boot image strings referenced by boot image code in PIC mode.
5634 kBootImageLinkTimePcRelative,
5635
5636 // Use a known boot image String* address, embedded in the code by the codegen.
5637 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5638 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5639 // GetIncludePatchInformation().
5640 kBootImageAddress,
5641
5642 // Load from the resolved strings array at an absolute address.
5643 // Used for strings outside the boot image referenced by JIT-compiled code.
5644 kDexCacheAddress,
5645
5646 // Load from resolved strings array in the dex cache using a PC-relative load.
5647 // Used for strings outside boot image when we know that we can access
5648 // the dex cache arrays using a PC-relative load.
5649 kDexCachePcRelative,
5650
5651 // Load from resolved strings array accessed through the class loaded from
5652 // the compiled method's own ArtMethod*. This is the default access type when
5653 // all other types are unavailable.
5654 kDexCacheViaMethod,
5655
5656 kLast = kDexCacheViaMethod
5657 };
5658
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005659 HLoadString(HCurrentMethod* current_method,
5660 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005661 const DexFile& dex_file,
5662 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005663 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5664 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005665 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005666 SetPackedFlag<kFlagIsInDexCache>(false);
5667 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5668 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005669 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005670
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005671 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5672 DCHECK(HasAddress(load_kind));
5673 load_data_.address = address;
5674 SetLoadKindInternal(load_kind);
5675 }
5676
5677 void SetLoadKindWithStringReference(LoadKind load_kind,
5678 const DexFile& dex_file,
5679 uint32_t string_index) {
5680 DCHECK(HasStringReference(load_kind));
5681 load_data_.ref.dex_file = &dex_file;
5682 string_index_ = string_index;
5683 SetLoadKindInternal(load_kind);
5684 }
5685
5686 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5687 const DexFile& dex_file,
5688 uint32_t element_index) {
5689 DCHECK(HasDexCacheReference(load_kind));
5690 load_data_.ref.dex_file = &dex_file;
5691 load_data_.ref.dex_cache_element_index = element_index;
5692 SetLoadKindInternal(load_kind);
5693 }
5694
5695 LoadKind GetLoadKind() const {
5696 return GetPackedField<LoadKindField>();
5697 }
5698
5699 const DexFile& GetDexFile() const;
5700
5701 uint32_t GetStringIndex() const {
5702 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5703 return string_index_;
5704 }
5705
5706 uint32_t GetDexCacheElementOffset() const;
5707
5708 uint64_t GetAddress() const {
5709 DCHECK(HasAddress(GetLoadKind()));
5710 return load_data_.address;
5711 }
5712
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005713 bool CanBeMoved() const OVERRIDE { return true; }
5714
Vladimir Marko372f10e2016-05-17 16:30:10 +01005715 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005716
5717 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5718
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005719 // Will call the runtime if we need to load the string through
5720 // the dex cache and the string is not guaranteed to be there yet.
5721 bool NeedsEnvironment() const OVERRIDE {
5722 LoadKind load_kind = GetLoadKind();
5723 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5724 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5725 load_kind == LoadKind::kBootImageAddress) {
5726 return false;
5727 }
5728 return !IsInDexCache();
5729 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005730
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005731 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5732 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5733 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005734
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005735 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005736 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005737
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005738 static SideEffects SideEffectsForArchRuntimeCalls() {
5739 return SideEffects::CanTriggerGC();
5740 }
5741
Vladimir Markoa1de9182016-02-25 11:37:38 +00005742 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5743
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005744 void MarkInDexCache() {
5745 SetPackedFlag<kFlagIsInDexCache>(true);
5746 DCHECK(!NeedsEnvironment());
5747 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005748 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749 }
5750
Vladimir Marko372f10e2016-05-17 16:30:10 +01005751 void AddSpecialInput(HInstruction* special_input);
5752
5753 using HInstruction::GetInputRecords; // Keep the const version visible.
5754 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5755 return ArrayRef<HUserRecord<HInstruction*>>(
5756 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005757 }
5758
Vladimir Marko372f10e2016-05-17 16:30:10 +01005759 Primitive::Type GetType() const OVERRIDE {
5760 return Primitive::kPrimNot;
5761 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005762
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005763 DECLARE_INSTRUCTION(LoadString);
5764
5765 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005766 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005767 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5768 static constexpr size_t kFieldLoadKindSize =
5769 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5770 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005771 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005772 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005773
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774 static bool HasStringReference(LoadKind load_kind) {
5775 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5776 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5777 load_kind == LoadKind::kDexCacheViaMethod;
5778 }
5779
5780 static bool HasAddress(LoadKind load_kind) {
5781 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5782 }
5783
5784 static bool HasDexCacheReference(LoadKind load_kind) {
5785 return load_kind == LoadKind::kDexCachePcRelative;
5786 }
5787
5788 void SetLoadKindInternal(LoadKind load_kind);
5789
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005790 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5791 // For other load kinds it's empty or possibly some architecture-specific instruction
5792 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005793 HUserRecord<HInstruction*> special_input_;
5794
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795 // String index serves also as the hash code and it's also needed for slow-paths,
5796 // so it must not be overwritten with other load data.
5797 uint32_t string_index_;
5798
5799 union {
5800 struct {
5801 const DexFile* dex_file; // For string reference and dex cache reference.
5802 uint32_t dex_cache_element_index; // Only for dex cache reference.
5803 } ref;
5804 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5805 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005806
5807 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5808};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005809std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5810
5811// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5812inline const DexFile& HLoadString::GetDexFile() const {
5813 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5814 << GetLoadKind();
5815 return *load_data_.ref.dex_file;
5816}
5817
5818// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5819inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5820 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5821 return load_data_.ref.dex_cache_element_index;
5822}
5823
5824// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5825inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5826 // The special input is used for PC-relative loads on some architectures.
5827 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5828 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005829 // HLoadString::GetInputRecords() returns an empty array at this point,
5830 // so use the GetInputRecords() from the base class to set the input record.
5831 DCHECK(special_input_.GetInstruction() == nullptr);
5832 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005833 special_input->AddUseAt(this, 0);
5834}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005835
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005836/**
5837 * Performs an initialization check on its Class object input.
5838 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005839class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005840 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005841 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005842 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005843 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005844 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5845 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005846 SetRawInputAt(0, constant);
5847 }
5848
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005849 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005850 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005851 return true;
5852 }
5853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005854 bool NeedsEnvironment() const OVERRIDE {
5855 // May call runtime to initialize the class.
5856 return true;
5857 }
5858
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005859 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005860
5861 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5862
5863 DECLARE_INSTRUCTION(ClinitCheck);
5864
5865 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005866 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5867};
5868
Vladimir Markofcb503c2016-05-18 12:48:17 +01005869class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005870 public:
5871 HStaticFieldGet(HInstruction* cls,
5872 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005873 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005874 bool is_volatile,
5875 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005876 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005877 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005878 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005879 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005880 : HExpression(field_type,
5881 SideEffects::FieldReadOfType(field_type, is_volatile),
5882 dex_pc),
5883 field_info_(field_offset,
5884 field_type,
5885 is_volatile,
5886 field_idx,
5887 declaring_class_def_index,
5888 dex_file,
5889 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005890 SetRawInputAt(0, cls);
5891 }
5892
Calin Juravle52c48962014-12-16 17:02:57 +00005893
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005894 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005895
Vladimir Marko372f10e2016-05-17 16:30:10 +01005896 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5897 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005898 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005899 }
5900
5901 size_t ComputeHashCode() const OVERRIDE {
5902 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5903 }
5904
Calin Juravle52c48962014-12-16 17:02:57 +00005905 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005906 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5907 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005908 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005909
5910 DECLARE_INSTRUCTION(StaticFieldGet);
5911
5912 private:
5913 const FieldInfo field_info_;
5914
5915 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5916};
5917
Vladimir Markofcb503c2016-05-18 12:48:17 +01005918class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005919 public:
5920 HStaticFieldSet(HInstruction* cls,
5921 HInstruction* value,
5922 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005923 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005924 bool is_volatile,
5925 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005926 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005927 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005928 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005929 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005930 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5931 dex_pc),
5932 field_info_(field_offset,
5933 field_type,
5934 is_volatile,
5935 field_idx,
5936 declaring_class_def_index,
5937 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005938 dex_cache) {
5939 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005940 SetRawInputAt(0, cls);
5941 SetRawInputAt(1, value);
5942 }
5943
Calin Juravle52c48962014-12-16 17:02:57 +00005944 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5946 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005947 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005948
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005949 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005950 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5951 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005952
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005953 DECLARE_INSTRUCTION(StaticFieldSet);
5954
5955 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005956 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5957 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5958 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5959 "Too many packed fields.");
5960
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961 const FieldInfo field_info_;
5962
5963 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5964};
5965
Vladimir Markofcb503c2016-05-18 12:48:17 +01005966class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005967 public:
5968 HUnresolvedInstanceFieldGet(HInstruction* obj,
5969 Primitive::Type field_type,
5970 uint32_t field_index,
5971 uint32_t dex_pc)
5972 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5973 field_index_(field_index) {
5974 SetRawInputAt(0, obj);
5975 }
5976
5977 bool NeedsEnvironment() const OVERRIDE { return true; }
5978 bool CanThrow() const OVERRIDE { return true; }
5979
5980 Primitive::Type GetFieldType() const { return GetType(); }
5981 uint32_t GetFieldIndex() const { return field_index_; }
5982
5983 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5984
5985 private:
5986 const uint32_t field_index_;
5987
5988 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5989};
5990
Vladimir Markofcb503c2016-05-18 12:48:17 +01005991class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005992 public:
5993 HUnresolvedInstanceFieldSet(HInstruction* obj,
5994 HInstruction* value,
5995 Primitive::Type field_type,
5996 uint32_t field_index,
5997 uint32_t dex_pc)
5998 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005999 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006000 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006001 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006002 SetRawInputAt(0, obj);
6003 SetRawInputAt(1, value);
6004 }
6005
6006 bool NeedsEnvironment() const OVERRIDE { return true; }
6007 bool CanThrow() const OVERRIDE { return true; }
6008
Vladimir Markoa1de9182016-02-25 11:37:38 +00006009 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006010 uint32_t GetFieldIndex() const { return field_index_; }
6011
6012 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6013
6014 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006015 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6016 static constexpr size_t kFieldFieldTypeSize =
6017 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6018 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6019 kFieldFieldType + kFieldFieldTypeSize;
6020 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6021 "Too many packed fields.");
6022 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6023
Calin Juravlee460d1d2015-09-29 04:52:17 +01006024 const uint32_t field_index_;
6025
6026 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6027};
6028
Vladimir Markofcb503c2016-05-18 12:48:17 +01006029class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006030 public:
6031 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6032 uint32_t field_index,
6033 uint32_t dex_pc)
6034 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6035 field_index_(field_index) {
6036 }
6037
6038 bool NeedsEnvironment() const OVERRIDE { return true; }
6039 bool CanThrow() const OVERRIDE { return true; }
6040
6041 Primitive::Type GetFieldType() const { return GetType(); }
6042 uint32_t GetFieldIndex() const { return field_index_; }
6043
6044 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6045
6046 private:
6047 const uint32_t field_index_;
6048
6049 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6050};
6051
Vladimir Markofcb503c2016-05-18 12:48:17 +01006052class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006053 public:
6054 HUnresolvedStaticFieldSet(HInstruction* value,
6055 Primitive::Type field_type,
6056 uint32_t field_index,
6057 uint32_t dex_pc)
6058 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006059 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006060 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006061 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006062 SetRawInputAt(0, value);
6063 }
6064
6065 bool NeedsEnvironment() const OVERRIDE { return true; }
6066 bool CanThrow() const OVERRIDE { return true; }
6067
Vladimir Markoa1de9182016-02-25 11:37:38 +00006068 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006069 uint32_t GetFieldIndex() const { return field_index_; }
6070
6071 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6072
6073 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6075 static constexpr size_t kFieldFieldTypeSize =
6076 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6077 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6078 kFieldFieldType + kFieldFieldTypeSize;
6079 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6080 "Too many packed fields.");
6081 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6082
Calin Juravlee460d1d2015-09-29 04:52:17 +01006083 const uint32_t field_index_;
6084
6085 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6086};
6087
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006088// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006089class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006090 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006091 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6092 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006093
David Brazdilbbd733e2015-08-18 17:48:17 +01006094 bool CanBeNull() const OVERRIDE { return false; }
6095
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006096 DECLARE_INSTRUCTION(LoadException);
6097
6098 private:
6099 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6100};
6101
David Brazdilcb1c0552015-08-04 16:22:25 +01006102// Implicit part of move-exception which clears thread-local exception storage.
6103// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006104class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006106 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6107 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006108
6109 DECLARE_INSTRUCTION(ClearException);
6110
6111 private:
6112 DISALLOW_COPY_AND_ASSIGN(HClearException);
6113};
6114
Vladimir Markofcb503c2016-05-18 12:48:17 +01006115class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006116 public:
6117 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006118 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006119 SetRawInputAt(0, exception);
6120 }
6121
6122 bool IsControlFlow() const OVERRIDE { return true; }
6123
6124 bool NeedsEnvironment() const OVERRIDE { return true; }
6125
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006126 bool CanThrow() const OVERRIDE { return true; }
6127
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006128
6129 DECLARE_INSTRUCTION(Throw);
6130
6131 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006132 DISALLOW_COPY_AND_ASSIGN(HThrow);
6133};
6134
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006135/**
6136 * Implementation strategies for the code generator of a HInstanceOf
6137 * or `HCheckCast`.
6138 */
6139enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006140 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 kExactCheck, // Can do a single class compare.
6142 kClassHierarchyCheck, // Can just walk the super class chain.
6143 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6144 kInterfaceCheck, // No optimization yet when checking against an interface.
6145 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006146 kArrayCheck, // No optimization yet when checking against a generic array.
6147 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148};
6149
Roland Levillain86503782016-02-11 19:07:30 +00006150std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6151
Vladimir Markofcb503c2016-05-18 12:48:17 +01006152class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006153 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006154 HInstanceOf(HInstruction* object,
6155 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006157 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006158 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006159 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006160 dex_pc) {
6161 SetPackedField<TypeCheckKindField>(check_kind);
6162 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006163 SetRawInputAt(0, object);
6164 SetRawInputAt(1, constant);
6165 }
6166
6167 bool CanBeMoved() const OVERRIDE { return true; }
6168
Vladimir Marko372f10e2016-05-17 16:30:10 +01006169 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006170 return true;
6171 }
6172
6173 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006174 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006175 }
6176
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006177 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006178 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6179 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6180 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6181 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006182
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006183 static bool CanCallRuntime(TypeCheckKind check_kind) {
6184 // Mips currently does runtime calls for any other checks.
6185 return check_kind != TypeCheckKind::kExactCheck;
6186 }
6187
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006188 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006189 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006190 }
6191
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006192 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006193
6194 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006195 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6196 static constexpr size_t kFieldTypeCheckKindSize =
6197 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6198 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6199 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6200 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6201 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006202
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006203 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6204};
6205
Vladimir Markofcb503c2016-05-18 12:48:17 +01006206class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006207 public:
David Brazdilf5552582015-12-27 13:36:12 +00006208 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006209 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006210 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6211 SetPackedFlag<kFlagUpperCanBeNull>(true);
6212 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006213 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006214 SetRawInputAt(0, input);
6215 }
6216
David Brazdilf5552582015-12-27 13:36:12 +00006217 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006218 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006219 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006220 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006221
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006222 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006223 DCHECK(GetUpperCanBeNull() || !can_be_null);
6224 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006225 }
6226
Vladimir Markoa1de9182016-02-25 11:37:38 +00006227 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006228
Calin Juravleb1498f62015-02-16 13:13:29 +00006229 DECLARE_INSTRUCTION(BoundType);
6230
6231 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6233 // is false then CanBeNull() cannot be true).
6234 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6235 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6236 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6237 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6238
Calin Juravleb1498f62015-02-16 13:13:29 +00006239 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006240 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6241 // It is used to bound the type in cases like:
6242 // if (x instanceof ClassX) {
6243 // // uper_bound_ will be ClassX
6244 // }
David Brazdilf5552582015-12-27 13:36:12 +00006245 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006246
6247 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6248};
6249
Vladimir Markofcb503c2016-05-18 12:48:17 +01006250class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006251 public:
6252 HCheckCast(HInstruction* object,
6253 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006254 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006255 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006256 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6257 SetPackedField<TypeCheckKindField>(check_kind);
6258 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006259 SetRawInputAt(0, object);
6260 SetRawInputAt(1, constant);
6261 }
6262
6263 bool CanBeMoved() const OVERRIDE { return true; }
6264
Vladimir Marko372f10e2016-05-17 16:30:10 +01006265 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006266 return true;
6267 }
6268
6269 bool NeedsEnvironment() const OVERRIDE {
6270 // Instruction may throw a CheckCastError.
6271 return true;
6272 }
6273
6274 bool CanThrow() const OVERRIDE { return true; }
6275
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6277 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6278 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6279 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006280
6281 DECLARE_INSTRUCTION(CheckCast);
6282
6283 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006284 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6285 static constexpr size_t kFieldTypeCheckKindSize =
6286 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6287 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6288 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6289 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6290 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006291
6292 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006293};
6294
Vladimir Markofcb503c2016-05-18 12:48:17 +01006295class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006296 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006297 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006298 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006299 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6300 SetPackedField<BarrierKindField>(barrier_kind);
6301 }
Calin Juravle27df7582015-04-17 19:12:31 +01006302
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006304
6305 DECLARE_INSTRUCTION(MemoryBarrier);
6306
6307 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006308 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6309 static constexpr size_t kFieldBarrierKindSize =
6310 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6311 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6312 kFieldBarrierKind + kFieldBarrierKindSize;
6313 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6314 "Too many packed fields.");
6315 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006316
6317 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6318};
6319
Vladimir Markofcb503c2016-05-18 12:48:17 +01006320class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006321 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006322 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006323 kEnter,
6324 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006325 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006326 };
6327
6328 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006329 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006330 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6331 dex_pc) {
6332 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006333 SetRawInputAt(0, object);
6334 }
6335
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006336 // Instruction may go into runtime, so we need an environment.
6337 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006338
6339 bool CanThrow() const OVERRIDE {
6340 // Verifier guarantees that monitor-exit cannot throw.
6341 // This is important because it allows the HGraphBuilder to remove
6342 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6343 return IsEnter();
6344 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006345
Vladimir Markoa1de9182016-02-25 11:37:38 +00006346 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6347 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006348
6349 DECLARE_INSTRUCTION(MonitorOperation);
6350
Calin Juravle52c48962014-12-16 17:02:57 +00006351 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006352 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6353 static constexpr size_t kFieldOperationKindSize =
6354 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6355 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6356 kFieldOperationKind + kFieldOperationKindSize;
6357 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6358 "Too many packed fields.");
6359 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006360
6361 private:
6362 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6363};
6364
Vladimir Markofcb503c2016-05-18 12:48:17 +01006365class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006366 public:
6367 HSelect(HInstruction* condition,
6368 HInstruction* true_value,
6369 HInstruction* false_value,
6370 uint32_t dex_pc)
6371 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6372 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6373
6374 // First input must be `true_value` or `false_value` to allow codegens to
6375 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6376 // that architectures which implement HSelect as a conditional move also
6377 // will not need to invert the condition.
6378 SetRawInputAt(0, false_value);
6379 SetRawInputAt(1, true_value);
6380 SetRawInputAt(2, condition);
6381 }
6382
6383 HInstruction* GetFalseValue() const { return InputAt(0); }
6384 HInstruction* GetTrueValue() const { return InputAt(1); }
6385 HInstruction* GetCondition() const { return InputAt(2); }
6386
6387 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006388 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6389 return true;
6390 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006391
6392 bool CanBeNull() const OVERRIDE {
6393 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6394 }
6395
6396 DECLARE_INSTRUCTION(Select);
6397
6398 private:
6399 DISALLOW_COPY_AND_ASSIGN(HSelect);
6400};
6401
Vladimir Markof9f64412015-09-02 14:05:49 +01006402class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006403 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006404 MoveOperands(Location source,
6405 Location destination,
6406 Primitive::Type type,
6407 HInstruction* instruction)
6408 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006409
6410 Location GetSource() const { return source_; }
6411 Location GetDestination() const { return destination_; }
6412
6413 void SetSource(Location value) { source_ = value; }
6414 void SetDestination(Location value) { destination_ = value; }
6415
6416 // The parallel move resolver marks moves as "in-progress" by clearing the
6417 // destination (but not the source).
6418 Location MarkPending() {
6419 DCHECK(!IsPending());
6420 Location dest = destination_;
6421 destination_ = Location::NoLocation();
6422 return dest;
6423 }
6424
6425 void ClearPending(Location dest) {
6426 DCHECK(IsPending());
6427 destination_ = dest;
6428 }
6429
6430 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006431 DCHECK(source_.IsValid() || destination_.IsInvalid());
6432 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006433 }
6434
6435 // True if this blocks a move from the given location.
6436 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006437 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006438 }
6439
6440 // A move is redundant if it's been eliminated, if its source and
6441 // destination are the same, or if its destination is unneeded.
6442 bool IsRedundant() const {
6443 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6444 }
6445
6446 // We clear both operands to indicate move that's been eliminated.
6447 void Eliminate() {
6448 source_ = destination_ = Location::NoLocation();
6449 }
6450
6451 bool IsEliminated() const {
6452 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6453 return source_.IsInvalid();
6454 }
6455
Alexey Frunze4dda3372015-06-01 18:31:49 -07006456 Primitive::Type GetType() const { return type_; }
6457
Nicolas Geoffray90218252015-04-15 11:56:51 +01006458 bool Is64BitMove() const {
6459 return Primitive::Is64BitType(type_);
6460 }
6461
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006462 HInstruction* GetInstruction() const { return instruction_; }
6463
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006464 private:
6465 Location source_;
6466 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006467 // The type this move is for.
6468 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006469 // The instruction this move is assocatied with. Null when this move is
6470 // for moving an input in the expected locations of user (including a phi user).
6471 // This is only used in debug mode, to ensure we do not connect interval siblings
6472 // in the same parallel move.
6473 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006474};
6475
Roland Levillainc9285912015-12-18 10:38:42 +00006476std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6477
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006478static constexpr size_t kDefaultNumberOfMoves = 4;
6479
Vladimir Markofcb503c2016-05-18 12:48:17 +01006480class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006481 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006482 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006483 : HTemplateInstruction(SideEffects::None(), dex_pc),
6484 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6485 moves_.reserve(kDefaultNumberOfMoves);
6486 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006487
Nicolas Geoffray90218252015-04-15 11:56:51 +01006488 void AddMove(Location source,
6489 Location destination,
6490 Primitive::Type type,
6491 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006492 DCHECK(source.IsValid());
6493 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006494 if (kIsDebugBuild) {
6495 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006496 for (const MoveOperands& move : moves_) {
6497 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006498 // Special case the situation where the move is for the spill slot
6499 // of the instruction.
6500 if ((GetPrevious() == instruction)
6501 || ((GetPrevious() == nullptr)
6502 && instruction->IsPhi()
6503 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006504 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006505 << "Doing parallel moves for the same instruction.";
6506 } else {
6507 DCHECK(false) << "Doing parallel moves for the same instruction.";
6508 }
6509 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006510 }
6511 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006512 for (const MoveOperands& move : moves_) {
6513 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006514 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006515 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006516 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006517 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006518 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006519 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006520 }
6521
Vladimir Marko225b6462015-09-28 12:17:40 +01006522 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006523 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006524 }
6525
Vladimir Marko225b6462015-09-28 12:17:40 +01006526 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006527
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006528 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006529
6530 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006531 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006532
6533 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6534};
6535
Mark Mendell0616ae02015-04-17 12:49:27 -04006536} // namespace art
6537
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006538#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6539#include "nodes_shared.h"
6540#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006541#ifdef ART_ENABLE_CODEGEN_arm
6542#include "nodes_arm.h"
6543#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006544#ifdef ART_ENABLE_CODEGEN_arm64
6545#include "nodes_arm64.h"
6546#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006547#ifdef ART_ENABLE_CODEGEN_x86
6548#include "nodes_x86.h"
6549#endif
6550
6551namespace art {
6552
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006553class HGraphVisitor : public ValueObject {
6554 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006555 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6556 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006557
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006558 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006559 virtual void VisitBasicBlock(HBasicBlock* block);
6560
Roland Levillain633021e2014-10-01 14:12:25 +01006561 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006562 void VisitInsertionOrder();
6563
Roland Levillain633021e2014-10-01 14:12:25 +01006564 // Visit the graph following dominator tree reverse post-order.
6565 void VisitReversePostOrder();
6566
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006567 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006568
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006569 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006570#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006571 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6572
6573 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6574
6575#undef DECLARE_VISIT_INSTRUCTION
6576
6577 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006578 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006579
6580 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6581};
6582
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006583class HGraphDelegateVisitor : public HGraphVisitor {
6584 public:
6585 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6586 virtual ~HGraphDelegateVisitor() {}
6587
6588 // Visit functions that delegate to to super class.
6589#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006590 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006591
6592 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6593
6594#undef DECLARE_VISIT_INSTRUCTION
6595
6596 private:
6597 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6598};
6599
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006600class HInsertionOrderIterator : public ValueObject {
6601 public:
6602 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6603
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006604 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006605 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006606 void Advance() { ++index_; }
6607
6608 private:
6609 const HGraph& graph_;
6610 size_t index_;
6611
6612 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6613};
6614
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006615class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006616 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006617 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6618 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006619 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006620 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006621
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006622 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6623 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006624 void Advance() { ++index_; }
6625
6626 private:
6627 const HGraph& graph_;
6628 size_t index_;
6629
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006630 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006631};
6632
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006633class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006634 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006635 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006636 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006637 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006638 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006639 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006640
6641 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006642 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006643 void Advance() { --index_; }
6644
6645 private:
6646 const HGraph& graph_;
6647 size_t index_;
6648
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006649 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006650};
6651
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006652class HLinearPostOrderIterator : public ValueObject {
6653 public:
6654 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006655 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006656
6657 bool Done() const { return index_ == 0; }
6658
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006659 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006660
6661 void Advance() {
6662 --index_;
6663 DCHECK_GE(index_, 0U);
6664 }
6665
6666 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006667 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006668 size_t index_;
6669
6670 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6671};
6672
6673class HLinearOrderIterator : public ValueObject {
6674 public:
6675 explicit HLinearOrderIterator(const HGraph& graph)
6676 : order_(graph.GetLinearOrder()), index_(0) {}
6677
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006678 bool Done() const { return index_ == order_.size(); }
6679 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006680 void Advance() { ++index_; }
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(HLinearOrderIterator);
6687};
6688
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006689// Iterator over the blocks that art part of the loop. Includes blocks part
6690// of an inner loop. The order in which the blocks are iterated is on their
6691// block id.
6692class HBlocksInLoopIterator : public ValueObject {
6693 public:
6694 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6695 : blocks_in_loop_(info.GetBlocks()),
6696 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6697 index_(0) {
6698 if (!blocks_in_loop_.IsBitSet(index_)) {
6699 Advance();
6700 }
6701 }
6702
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006703 bool Done() const { return index_ == blocks_.size(); }
6704 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006705 void Advance() {
6706 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006707 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006708 if (blocks_in_loop_.IsBitSet(index_)) {
6709 break;
6710 }
6711 }
6712 }
6713
6714 private:
6715 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006716 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006717 size_t index_;
6718
6719 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6720};
6721
Mingyao Yang3584bce2015-05-19 16:01:59 -07006722// Iterator over the blocks that art part of the loop. Includes blocks part
6723// of an inner loop. The order in which the blocks are iterated is reverse
6724// post order.
6725class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6726 public:
6727 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6728 : blocks_in_loop_(info.GetBlocks()),
6729 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6730 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006731 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006732 Advance();
6733 }
6734 }
6735
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006736 bool Done() const { return index_ == blocks_.size(); }
6737 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006738 void Advance() {
6739 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006740 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6741 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006742 break;
6743 }
6744 }
6745 }
6746
6747 private:
6748 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006749 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006750 size_t index_;
6751
6752 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6753};
6754
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006755inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006756 if (constant->IsIntConstant()) {
6757 return constant->AsIntConstant()->GetValue();
6758 } else if (constant->IsLongConstant()) {
6759 return constant->AsLongConstant()->GetValue();
6760 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006761 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006762 return 0;
6763 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006764}
6765
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006766#define INSTRUCTION_TYPE_CHECK(type, super) \
6767 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6768 inline const H##type* HInstruction::As##type() const { \
6769 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6770 } \
6771 inline H##type* HInstruction::As##type() { \
6772 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6773 }
6774
6775 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6776#undef INSTRUCTION_TYPE_CHECK
6777
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006778// Create space in `blocks` for adding `number_of_new_blocks` entries
6779// starting at location `at`. Blocks after `at` are moved accordingly.
6780inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6781 size_t number_of_new_blocks,
6782 size_t after) {
6783 DCHECK_LT(after, blocks->size());
6784 size_t old_size = blocks->size();
6785 size_t new_size = old_size + number_of_new_blocks;
6786 blocks->resize(new_size);
6787 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6788}
6789
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006790} // namespace art
6791
6792#endif // ART_COMPILER_OPTIMIZING_NODES_H_