blob: 9425ef3267b60bdc1e8ab39aa70576b75920b3fe [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"
Andreas Gampe7c9c31c2016-03-08 16:00:41 -080029#include "dex_instruction-inl.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"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41namespace art {
42
David Brazdil1abb4192015-02-17 18:33:36 +000043class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Roland Levillain5b5b9312016-03-22 14:57:31 +000075// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
76static constexpr int32_t kMaxIntShiftDistance = 0x1f;
77// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
78static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000079
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070081static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010083static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
84
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060085static constexpr uint32_t kNoDexPc = -1;
86
Dave Allison20dfc792014-06-16 20:44:29 -070087enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070088 // All types.
89 kCondEQ, // ==
90 kCondNE, // !=
91 // Signed integers and floating-point numbers.
92 kCondLT, // <
93 kCondLE, // <=
94 kCondGT, // >
95 kCondGE, // >=
96 // Unsigned integers.
97 kCondB, // <
98 kCondBE, // <=
99 kCondA, // >
100 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700101};
102
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000103enum GraphAnalysisResult {
David Brazdilbadd8262016-02-02 16:28:56 +0000104 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000105 kAnalysisFailThrowCatchLoop,
106 kAnalysisFailAmbiguousArrayOp,
107 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000108};
109
Vladimir Markof9f64412015-09-02 14:05:49 +0100110class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100111 public:
112 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
113
114 void AddInstruction(HInstruction* instruction);
115 void RemoveInstruction(HInstruction* instruction);
116
David Brazdilc3d743f2015-04-22 13:40:50 +0100117 // Insert `instruction` before/after an existing instruction `cursor`.
118 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
119 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
120
Roland Levillain6b469232014-09-25 10:10:38 +0100121 // Return true if this list contains `instruction`.
122 bool Contains(HInstruction* instruction) const;
123
Roland Levillainccc07a92014-09-16 14:48:16 +0100124 // Return true if `instruction1` is found before `instruction2` in
125 // this instruction list and false otherwise. Abort if none
126 // of these instructions is found.
127 bool FoundBefore(const HInstruction* instruction1,
128 const HInstruction* instruction2) const;
129
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000130 bool IsEmpty() const { return first_instruction_ == nullptr; }
131 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
132
133 // Update the block of all instructions to be `block`.
134 void SetBlockOfInstructions(HBasicBlock* block) const;
135
136 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000137 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000138 void Add(const HInstructionList& instruction_list);
139
David Brazdil2d7352b2015-04-20 14:52:42 +0100140 // Return the number of instructions in the list. This is an expensive operation.
141 size_t CountSize() const;
142
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100143 private:
144 HInstruction* first_instruction_;
145 HInstruction* last_instruction_;
146
147 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000148 friend class HGraph;
149 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100151 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100152
153 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
154};
155
David Brazdil4833f5a2015-12-16 10:37:39 +0000156class ReferenceTypeInfo : ValueObject {
157 public:
158 typedef Handle<mirror::Class> TypeHandle;
159
Vladimir Markoa1de9182016-02-25 11:37:38 +0000160 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
161
162 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000163 return ReferenceTypeInfo(type_handle, is_exact);
164 }
165
166 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
167
Vladimir Markof39745e2016-01-26 12:16:55 +0000168 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000169 return handle.GetReference() != nullptr;
170 }
171
172 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
173 return IsValidHandle(type_handle_);
174 }
175
176 bool IsExact() const { return is_exact_; }
177
178 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
179 DCHECK(IsValid());
180 return GetTypeHandle()->IsObjectClass();
181 }
182
183 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
184 DCHECK(IsValid());
185 return GetTypeHandle()->IsStringClass();
186 }
187
188 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
189 DCHECK(IsValid());
190 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
191 }
192
193 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
194 DCHECK(IsValid());
195 return GetTypeHandle()->IsInterface();
196 }
197
198 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
199 DCHECK(IsValid());
200 return GetTypeHandle()->IsArrayClass();
201 }
202
203 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
204 DCHECK(IsValid());
205 return GetTypeHandle()->IsPrimitiveArray();
206 }
207
208 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
209 DCHECK(IsValid());
210 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
211 }
212
213 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
214 DCHECK(IsValid());
215 if (!IsExact()) return false;
216 if (!IsArrayClass()) return false;
217 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
218 }
219
220 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
221 DCHECK(IsValid());
222 if (!IsExact()) return false;
223 if (!IsArrayClass()) return false;
224 if (!rti.IsArrayClass()) return false;
225 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
226 rti.GetTypeHandle()->GetComponentType());
227 }
228
229 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
230
231 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
232 DCHECK(IsValid());
233 DCHECK(rti.IsValid());
234 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
235 }
236
237 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
238 DCHECK(IsValid());
239 DCHECK(rti.IsValid());
240 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
241 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
242 }
243
244 // Returns true if the type information provide the same amount of details.
245 // Note that it does not mean that the instructions have the same actual type
246 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000247 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 if (!IsValid() && !rti.IsValid()) {
249 // Invalid types are equal.
250 return true;
251 }
252 if (!IsValid() || !rti.IsValid()) {
253 // One is valid, the other not.
254 return false;
255 }
256 return IsExact() == rti.IsExact()
257 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
258 }
259
260 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000261 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
262 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
263 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000264
265 // The class of the object.
266 TypeHandle type_handle_;
267 // Whether or not the type is exact or a superclass of the actual type.
268 // Whether or not we have any information about this type.
269 bool is_exact_;
270};
271
272std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
273
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100275class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000276 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100277 HGraph(ArenaAllocator* arena,
278 const DexFile& dex_file,
279 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100280 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100282 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000284 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100285 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100287 blocks_(arena->Adapter(kArenaAllocBlockList)),
288 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
289 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700290 entry_block_(nullptr),
291 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100292 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100293 number_of_vregs_(0),
294 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000295 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400296 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000297 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000298 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000299 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100301 dex_file_(dex_file),
302 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100303 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100304 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100305 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000307 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
309 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
310 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000312 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000313 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
314 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_.reserve(kDefaultNumberOfBlocks);
316 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000317
David Brazdilbadd8262016-02-02 16:28:56 +0000318 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
319 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
320
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000321 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100322 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
323
David Brazdil69ba7b72015-06-23 18:27:30 +0100324 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000325 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100326
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000327 HBasicBlock* GetEntryBlock() const { return entry_block_; }
328 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100329 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
332 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000333
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100335
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100336 void ComputeDominanceInformation();
337 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000338 void ClearLoopInformation();
339 void FindBackEdges(ArenaBitVector* visited);
340 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100342 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000343
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 // Analyze all natural loops in this graph. Returns a code specifying that it
345 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000346 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000347 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348
David Brazdilffee3d32015-07-06 11:48:53 +0100349 // Iterate over blocks to compute try block membership. Needs reverse post
350 // order and loop information.
351 void ComputeTryBlockInformation();
352
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000353 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000354 // Returns the instruction to replace the invoke expression or null if the
355 // invoke is for a void method. Note that the caller is responsible for replacing
356 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000357 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000358
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000359 // Update the loop and try membership of `block`, which was spawned from `reference`.
360 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
361 // should be the new back edge.
362 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
363 HBasicBlock* reference,
364 bool replace_if_back_edge);
365
Mingyao Yang3584bce2015-05-19 16:01:59 -0700366 // Need to add a couple of blocks to test if the loop body is entered and
367 // put deoptimization instructions, etc.
368 void TransformLoopHeaderForBCE(HBasicBlock* header);
369
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000370 // Removes `block` from the graph. Assumes `block` has been disconnected from
371 // other blocks and has no instructions or phis.
372 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000373
David Brazdilfc6a86a2015-06-26 10:33:45 +0000374 // Splits the edge between `block` and `successor` while preserving the
375 // indices in the predecessor/successor lists. If there are multiple edges
376 // between the blocks, the lowest indices are used.
377 // Returns the new block which is empty and has the same dex pc as `successor`.
378 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
379
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100380 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
381 void SimplifyLoop(HBasicBlock* header);
382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 int32_t GetNextInstructionId() {
384 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000385 return current_instruction_id_++;
386 }
387
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388 int32_t GetCurrentInstructionId() const {
389 return current_instruction_id_;
390 }
391
392 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000393 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000394 current_instruction_id_ = id;
395 }
396
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100397 uint16_t GetMaximumNumberOfOutVRegs() const {
398 return maximum_number_of_out_vregs_;
399 }
400
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000401 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
402 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100403 }
404
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100405 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
406 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
407 }
408
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000409 void UpdateTemporariesVRegSlots(size_t slots) {
410 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 }
412
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000413 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000415 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100416 }
417
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100418 void SetNumberOfVRegs(uint16_t number_of_vregs) {
419 number_of_vregs_ = number_of_vregs;
420 }
421
422 uint16_t GetNumberOfVRegs() const {
423 return number_of_vregs_;
424 }
425
426 void SetNumberOfInVRegs(uint16_t value) {
427 number_of_in_vregs_ = value;
428 }
429
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100430 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100431 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100432 return number_of_vregs_ - number_of_in_vregs_;
433 }
434
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100435 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100436 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100437 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100438
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100439 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100440 return linear_order_;
441 }
442
Mark Mendell1152c922015-04-24 17:06:35 -0400443 bool HasBoundsChecks() const {
444 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800445 }
446
Mark Mendell1152c922015-04-24 17:06:35 -0400447 void SetHasBoundsChecks(bool value) {
448 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800449 }
450
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100451 bool ShouldGenerateConstructorBarrier() const {
452 return should_generate_constructor_barrier_;
453 }
454
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000455 bool IsDebuggable() const { return debuggable_; }
456
David Brazdil8d5b8b22015-03-24 10:51:52 +0000457 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 // already, it is created and inserted into the graph. This method is only for
459 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600460 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000461
462 // TODO: This is problematic for the consistency of reference type propagation
463 // because it can be created anytime after the pass and thus it will be left
464 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000466
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600467 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
468 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600470 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
471 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000472 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600473 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
474 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000475 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600476 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
477 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000478 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000479
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100480 HCurrentMethod* GetCurrentMethod();
481
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100482 const DexFile& GetDexFile() const {
483 return dex_file_;
484 }
485
486 uint32_t GetMethodIdx() const {
487 return method_idx_;
488 }
489
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100490 InvokeType GetInvokeType() const {
491 return invoke_type_;
492 }
493
Mark Mendellc4701932015-04-10 13:18:51 -0400494 InstructionSet GetInstructionSet() const {
495 return instruction_set_;
496 }
497
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000498 bool IsCompilingOsr() const { return osr_; }
499
David Brazdil77a48ae2015-09-15 12:34:04 +0000500 bool HasTryCatch() const { return has_try_catch_; }
501 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100502
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000503 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
504 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
505
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100506 ArtMethod* GetArtMethod() const { return art_method_; }
507 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
508
Mark Mendellf6529172015-11-17 11:16:56 -0500509 // Returns an instruction with the opposite boolean value from 'cond'.
510 // The instruction has been inserted into the graph, either as a constant, or
511 // before cursor.
512 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
513
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000514 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
515
David Brazdil2d7352b2015-04-20 14:52:42 +0100516 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000517 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100518 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000519
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000520 template <class InstructionType, typename ValueType>
521 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600522 ArenaSafeMap<ValueType, InstructionType*>* cache,
523 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000524 // Try to find an existing constant of the given value.
525 InstructionType* constant = nullptr;
526 auto cached_constant = cache->find(value);
527 if (cached_constant != cache->end()) {
528 constant = cached_constant->second;
529 }
530
531 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100532 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000533 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000535 cache->Overwrite(value, constant);
536 InsertConstant(constant);
537 }
538 return constant;
539 }
540
David Brazdil8d5b8b22015-03-24 10:51:52 +0000541 void InsertConstant(HConstant* instruction);
542
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000543 // Cache a float constant into the graph. This method should only be
544 // called by the SsaBuilder when creating "equivalent" instructions.
545 void CacheFloatConstant(HFloatConstant* constant);
546
547 // See CacheFloatConstant comment.
548 void CacheDoubleConstant(HDoubleConstant* constant);
549
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551
552 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100553 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100555 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100556 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100558 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100559 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100560
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000561 HBasicBlock* entry_block_;
562 HBasicBlock* exit_block_;
563
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100564 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100565 uint16_t maximum_number_of_out_vregs_;
566
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100567 // The number of virtual registers in this method. Contains the parameters.
568 uint16_t number_of_vregs_;
569
570 // The number of virtual registers used by parameters of this method.
571 uint16_t number_of_in_vregs_;
572
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000573 // Number of vreg size slots that the temporaries use (used in baseline compiler).
574 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100575
Mark Mendell1152c922015-04-24 17:06:35 -0400576 // Has bounds checks. We can totally skip BCE if it's false.
577 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800578
David Brazdil77a48ae2015-09-15 12:34:04 +0000579 // Flag whether there are any try/catch blocks in the graph. We will skip
580 // try/catch-related passes if false.
581 bool has_try_catch_;
582
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000583 // Flag whether there are any irreducible loops in the graph.
584 bool has_irreducible_loops_;
585
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000586 // Indicates whether the graph should be compiled in a way that
587 // ensures full debuggability. If false, we can apply more
588 // aggressive optimizations that may limit the level of debugging.
589 const bool debuggable_;
590
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000592 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000593
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100594 // The dex file from which the method is from.
595 const DexFile& dex_file_;
596
597 // The method index in the dex file.
598 const uint32_t method_idx_;
599
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100600 // If inlined, this encodes how the callee is being invoked.
601 const InvokeType invoke_type_;
602
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100603 // Whether the graph has been transformed to SSA form. Only used
604 // in debug mode to ensure we are not using properties only valid
605 // for non-SSA form (like the number of temporaries).
606 bool in_ssa_form_;
607
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100608 const bool should_generate_constructor_barrier_;
609
Mathieu Chartiere401d142015-04-22 13:56:20 -0700610 const InstructionSet instruction_set_;
611
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000612 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000613 HNullConstant* cached_null_constant_;
614 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000616 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000618
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100619 HCurrentMethod* cached_current_method_;
620
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100621 // The ArtMethod this graph is for. Note that for AOT, it may be null,
622 // for example for methods whose declaring class could not be resolved
623 // (such as when the superclass could not be found).
624 ArtMethod* art_method_;
625
David Brazdil4833f5a2015-12-16 10:37:39 +0000626 // Keep the RTI of inexact Object to avoid having to pass stack handle
627 // collection pointer to passes which may create NullConstant.
628 ReferenceTypeInfo inexact_object_rti_;
629
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000630 // Whether we are compiling this graph for on stack replacement: this will
631 // make all loops seen as irreducible and emit special stack maps to mark
632 // compiled code entries which the interpreter can directly jump to.
633 const bool osr_;
634
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000635 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100636 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000637 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000638 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639 DISALLOW_COPY_AND_ASSIGN(HGraph);
640};
641
Vladimir Markof9f64412015-09-02 14:05:49 +0100642class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 public:
644 HLoopInformation(HBasicBlock* header, HGraph* graph)
645 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100646 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000647 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100648 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100649 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000650 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 back_edges_.reserve(kDefaultNumberOfBackEdges);
652 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000654 bool IsIrreducible() const { return irreducible_; }
655
656 void Dump(std::ostream& os);
657
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 HBasicBlock* GetHeader() const {
659 return header_;
660 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000661
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000662 void SetHeader(HBasicBlock* block) {
663 header_ = block;
664 }
665
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100666 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
667 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
668 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
669
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000670 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000672 }
673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 }
677
David Brazdil46e2a392015-03-16 17:31:52 +0000678 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100680 }
681
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000682 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000684 }
685
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100686 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100689 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100690 }
691
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100692 // Returns the lifetime position of the back edge that has the
693 // greatest lifetime position.
694 size_t GetLifetimeEnd() const;
695
696 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100697 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100698 }
699
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000700 // Finds blocks that are part of this loop.
701 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100702
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 // Returns whether this loop information contains `block`.
704 // Note that this loop information *must* be populated before entering this function.
705 bool Contains(const HBasicBlock& block) const;
706
707 // Returns whether this loop information is an inner loop of `other`.
708 // Note that `other` *must* be populated before entering this function.
709 bool IsIn(const HLoopInformation& other) const;
710
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800711 // Returns true if instruction is not defined within this loop.
712 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700713
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100714 const ArenaBitVector& GetBlocks() const { return blocks_; }
715
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000716 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000717 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000718
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000719 void ClearAllBlocks() {
720 blocks_.ClearAllBits();
721 }
722
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100724 // Internal recursive implementation of `Populate`.
725 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100727
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000728 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100729 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000730 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100731 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100732 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000733
734 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
735};
736
David Brazdilec16f792015-08-19 15:04:01 +0100737// Stores try/catch information for basic blocks.
738// Note that HGraph is constructed so that catch blocks cannot simultaneously
739// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100740class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100741 public:
742 // Try block information constructor.
743 explicit TryCatchInformation(const HTryBoundary& try_entry)
744 : try_entry_(&try_entry),
745 catch_dex_file_(nullptr),
746 catch_type_index_(DexFile::kDexNoIndex16) {
747 DCHECK(try_entry_ != nullptr);
748 }
749
750 // Catch block information constructor.
751 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
752 : try_entry_(nullptr),
753 catch_dex_file_(&dex_file),
754 catch_type_index_(catch_type_index) {}
755
756 bool IsTryBlock() const { return try_entry_ != nullptr; }
757
758 const HTryBoundary& GetTryEntry() const {
759 DCHECK(IsTryBlock());
760 return *try_entry_;
761 }
762
763 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
764
765 bool IsCatchAllTypeIndex() const {
766 DCHECK(IsCatchBlock());
767 return catch_type_index_ == DexFile::kDexNoIndex16;
768 }
769
770 uint16_t GetCatchTypeIndex() const {
771 DCHECK(IsCatchBlock());
772 return catch_type_index_;
773 }
774
775 const DexFile& GetCatchDexFile() const {
776 DCHECK(IsCatchBlock());
777 return *catch_dex_file_;
778 }
779
780 private:
781 // One of possibly several TryBoundary instructions entering the block's try.
782 // Only set for try blocks.
783 const HTryBoundary* try_entry_;
784
785 // Exception type information. Only set for catch blocks.
786 const DexFile* catch_dex_file_;
787 const uint16_t catch_type_index_;
788};
789
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100791static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100792
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793// A block in a method. Contains the list of instructions represented
794// as a double linked list. Each block knows its predecessors and
795// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100796
Vladimir Markof9f64412015-09-02 14:05:49 +0100797class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000798 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600799 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000801 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
802 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000803 loop_information_(nullptr),
804 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000805 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100806 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100807 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100808 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000809 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000810 try_catch_information_(nullptr) {
811 predecessors_.reserve(kDefaultNumberOfPredecessors);
812 successors_.reserve(kDefaultNumberOfSuccessors);
813 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
814 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000815
Vladimir Marko60584552015-09-03 13:35:12 +0000816 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818 }
819
Vladimir Marko60584552015-09-03 13:35:12 +0000820 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822 }
823
David Brazdild26a4112015-11-10 11:07:31 +0000824 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
825 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
826
Vladimir Marko60584552015-09-03 13:35:12 +0000827 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
828 return ContainsElement(successors_, block, start_from);
829 }
830
831 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100832 return dominated_blocks_;
833 }
834
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100835 bool IsEntryBlock() const {
836 return graph_->GetEntryBlock() == this;
837 }
838
839 bool IsExitBlock() const {
840 return graph_->GetExitBlock() == this;
841 }
842
David Brazdil46e2a392015-03-16 17:31:52 +0000843 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000844 bool IsSingleTryBoundary() const;
845
846 // Returns true if this block emits nothing but a jump.
847 bool IsSingleJump() const {
848 HLoopInformation* loop_info = GetLoopInformation();
849 return (IsSingleGoto() || IsSingleTryBoundary())
850 // Back edges generate a suspend check.
851 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
852 }
David Brazdil46e2a392015-03-16 17:31:52 +0000853
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000854 void AddBackEdge(HBasicBlock* back_edge) {
855 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000857 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100858 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859 loop_information_->AddBackEdge(back_edge);
860 }
861
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000862 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000863 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100865 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000866 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600867 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000869 HBasicBlock* GetDominator() const { return dominator_; }
870 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000871 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
872
873 void RemoveDominatedBlock(HBasicBlock* block) {
874 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100875 }
Vladimir Marko60584552015-09-03 13:35:12 +0000876
877 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
878 ReplaceElement(dominated_blocks_, existing, new_block);
879 }
880
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100881 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882
883 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100884 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000885 }
886
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
888 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100889 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100890 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100891 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
892 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000894 HInstruction* GetFirstInstructionDisregardMoves() const;
895
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000897 successors_.push_back(block);
898 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899 }
900
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100901 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
902 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100903 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000904 new_block->predecessors_.push_back(this);
905 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000906 }
907
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
909 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000910 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000911 new_block->successors_.push_back(this);
912 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 }
914
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100915 // Insert `this` between `predecessor` and `successor. This method
916 // preserves the indicies, and will update the first edge found between
917 // `predecessor` and `successor`.
918 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
919 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100920 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000921 successor->predecessors_[predecessor_index] = this;
922 predecessor->successors_[successor_index] = this;
923 successors_.push_back(successor);
924 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100925 }
926
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100927 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 }
930
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000931 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000933 }
934
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100935 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100937 }
938
939 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000940 predecessors_.push_back(block);
941 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 }
943
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100944 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000945 DCHECK_EQ(predecessors_.size(), 2u);
946 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100947 }
948
David Brazdil769c9e52015-04-27 13:54:09 +0100949 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 DCHECK_EQ(successors_.size(), 2u);
951 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100952 }
953
David Brazdilfc6a86a2015-06-26 10:33:45 +0000954 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100956 }
957
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100960 }
961
David Brazdilfc6a86a2015-06-26 10:33:45 +0000962 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100964 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000965 }
966
967 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100969 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000970 }
971
972 // Returns whether the first occurrence of `predecessor` in the list of
973 // predecessors is at index `idx`.
974 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100975 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000976 return GetPredecessorIndexOf(predecessor) == idx;
977 }
978
David Brazdild7558da2015-09-22 13:04:14 +0100979 // Create a new block between this block and its predecessors. The new block
980 // is added to the graph, all predecessor edges are relinked to it and an edge
981 // is created to `this`. Returns the new empty block. Reverse post order or
982 // loop and try/catch information are not updated.
983 HBasicBlock* CreateImmediateDominator();
984
David Brazdilfc6a86a2015-06-26 10:33:45 +0000985 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100986 // created, latter block. Note that this method will add the block to the
987 // graph, create a Goto at the end of the former block and will create an edge
988 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100989 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 HBasicBlock* SplitBefore(HInstruction* cursor);
991
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000992 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000993 // created block. Note that this method just updates raw block information,
994 // like predecessors, successors, dominators, and instruction list. It does not
995 // update the graph, reverse post order, loop information, nor make sure the
996 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000997 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
998
999 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1000 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001001
David Brazdil9bc43612015-11-05 21:25:24 +00001002 // Split catch block into two blocks after the original move-exception bytecode
1003 // instruction, or at the beginning if not present. Returns the newly created,
1004 // latter block, or nullptr if such block could not be created (must be dead
1005 // in that case). Note that this method just updates raw block information,
1006 // like predecessors, successors, dominators, and instruction list. It does not
1007 // update the graph, reverse post order, loop information, nor make sure the
1008 // blocks are consistent (for example ending with a control flow instruction).
1009 HBasicBlock* SplitCatchBlockAfterMoveException();
1010
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001011 // Merge `other` at the end of `this`. Successors and dominated blocks of
1012 // `other` are changed to be successors and dominated blocks of `this`. Note
1013 // that this method does not update the graph, reverse post order, loop
1014 // information, nor make sure the blocks are consistent (for example ending
1015 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001016 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017
1018 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1019 // of `this` are moved to `other`.
1020 // Note that this method does not update the graph, reverse post order, loop
1021 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001022 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001023 void ReplaceWith(HBasicBlock* other);
1024
David Brazdil2d7352b2015-04-20 14:52:42 +01001025 // Merge `other` at the end of `this`. This method updates loops, reverse post
1026 // order, links to predecessors, successors, dominators and deletes the block
1027 // from the graph. The two blocks must be successive, i.e. `this` the only
1028 // predecessor of `other` and vice versa.
1029 void MergeWith(HBasicBlock* other);
1030
1031 // Disconnects `this` from all its predecessors, successors and dominator,
1032 // removes it from all loops it is included in and eventually from the graph.
1033 // The block must not dominate any other block. Predecessors and successors
1034 // are safely updated.
1035 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001036
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001037 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001038 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001039 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001040 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001041 // Replace instruction `initial` with `replacement` within this block.
1042 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1043 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001044 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001045 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001046 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001047 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1048 // instruction list. With 'ensure_safety' set to true, it verifies that the
1049 // instruction is not in use and removes it from the use lists of its inputs.
1050 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1051 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001052 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001053
1054 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001055 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001056 }
1057
Roland Levillain6b879dd2014-09-22 17:13:44 +01001058 bool IsLoopPreHeaderFirstPredecessor() const {
1059 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001060 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001061 }
1062
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001063 bool IsFirstPredecessorBackEdge() const {
1064 DCHECK(IsLoopHeader());
1065 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1066 }
1067
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001068 HLoopInformation* GetLoopInformation() const {
1069 return loop_information_;
1070 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001071
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001073 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001074 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 void SetInLoop(HLoopInformation* info) {
1076 if (IsLoopHeader()) {
1077 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001078 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001079 loop_information_ = info;
1080 } else if (loop_information_->Contains(*info->GetHeader())) {
1081 // Block is currently part of an outer loop. Make it part of this inner loop.
1082 // Note that a non loop header having a loop information means this loop information
1083 // has already been populated
1084 loop_information_ = info;
1085 } else {
1086 // Block is part of an inner loop. Do not update the loop information.
1087 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1088 // at this point, because this method is being called while populating `info`.
1089 }
1090 }
1091
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092 // Raw update of the loop information.
1093 void SetLoopInformation(HLoopInformation* info) {
1094 loop_information_ = info;
1095 }
1096
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001097 bool IsInLoop() const { return loop_information_ != nullptr; }
1098
David Brazdilec16f792015-08-19 15:04:01 +01001099 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1100
1101 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1102 try_catch_information_ = try_catch_information;
1103 }
1104
1105 bool IsTryBlock() const {
1106 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1107 }
1108
1109 bool IsCatchBlock() const {
1110 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1111 }
David Brazdilffee3d32015-07-06 11:48:53 +01001112
1113 // Returns the try entry that this block's successors should have. They will
1114 // be in the same try, unless the block ends in a try boundary. In that case,
1115 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001116 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001117
David Brazdild7558da2015-09-22 13:04:14 +01001118 bool HasThrowingInstructions() const;
1119
David Brazdila4b8c212015-05-07 09:59:30 +01001120 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001121 bool Dominates(HBasicBlock* block) const;
1122
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001123 size_t GetLifetimeStart() const { return lifetime_start_; }
1124 size_t GetLifetimeEnd() const { return lifetime_end_; }
1125
1126 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1127 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1128
David Brazdil8d5b8b22015-03-24 10:51:52 +00001129 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001130 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001131 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001132 bool HasSinglePhi() const;
1133
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001134 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001135 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001136 ArenaVector<HBasicBlock*> predecessors_;
1137 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001138 HInstructionList instructions_;
1139 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001140 HLoopInformation* loop_information_;
1141 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001142 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001143 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001144 // The dex program counter of the first instruction of this block.
1145 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001146 size_t lifetime_start_;
1147 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001148 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001149
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001150 friend class HGraph;
1151 friend class HInstruction;
1152
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001153 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1154};
1155
David Brazdilb2bd1c52015-03-25 11:17:37 +00001156// Iterates over the LoopInformation of all loops which contain 'block'
1157// from the innermost to the outermost.
1158class HLoopInformationOutwardIterator : public ValueObject {
1159 public:
1160 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1161 : current_(block.GetLoopInformation()) {}
1162
1163 bool Done() const { return current_ == nullptr; }
1164
1165 void Advance() {
1166 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001167 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001168 }
1169
1170 HLoopInformation* Current() const {
1171 DCHECK(!Done());
1172 return current_;
1173 }
1174
1175 private:
1176 HLoopInformation* current_;
1177
1178 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1179};
1180
Alexandre Ramesef20f712015-06-09 10:29:30 +01001181#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001182 M(Above, Condition) \
1183 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001184 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001185 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001186 M(ArrayGet, Instruction) \
1187 M(ArrayLength, Instruction) \
1188 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001189 M(Below, Condition) \
1190 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001191 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001192 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001193 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001194 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001195 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001196 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001197 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001198 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001199 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001200 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001202 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(Exit, Instruction) \
1206 M(FloatConstant, Constant) \
1207 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(GreaterThan, Condition) \
1209 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001211 M(InstanceFieldGet, Instruction) \
1212 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001213 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001215 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001216 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001217 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001218 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001219 M(LessThan, Condition) \
1220 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001221 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001222 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001223 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001224 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(Local, Instruction) \
1226 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001227 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001228 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001230 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(Neg, UnaryOperation) \
1232 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001233 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001234 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001236 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001238 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001239 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001241 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001242 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001243 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001244 M(Return, Instruction) \
1245 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001246 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001247 M(Shl, BinaryOperation) \
1248 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001249 M(StaticFieldGet, Instruction) \
1250 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 M(UnresolvedInstanceFieldGet, Instruction) \
1252 M(UnresolvedInstanceFieldSet, Instruction) \
1253 M(UnresolvedStaticFieldGet, Instruction) \
1254 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001255 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001256 M(StoreLocal, Instruction) \
1257 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001258 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001259 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001260 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001261 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001262 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001263 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001264
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001265/*
1266 * Instructions, shared across several (not all) architectures.
1267 */
1268#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1269#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1270#else
1271#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001272 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001273 M(MultiplyAccumulate, Instruction)
1274#endif
1275
Vladimir Markob4536b72015-11-24 13:45:23 +00001276#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001277#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001278#else
1279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1280 M(ArmDexCacheArraysBase, Instruction)
1281#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001282
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001283#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001284#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001285#else
1286#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001287 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001288 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001289#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001290
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1292
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001293#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1294
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001295#ifndef ART_ENABLE_CODEGEN_x86
1296#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1297#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001298#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1299 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001300 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001301 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001302 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001303#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001304
1305#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1306
1307#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1308 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001309 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001310 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1311 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001312 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001313 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001314 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1315 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1316
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001317#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1318 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001319 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001320 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001321 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001323
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001324#define FOR_EACH_INSTRUCTION(M) \
1325 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1326 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1327
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001328#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001329FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1330#undef FORWARD_DECLARATION
1331
Roland Levillainccc07a92014-09-16 14:48:16 +01001332#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001333 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001334 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001335 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001336 return other->Is##type(); \
1337 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001338 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001339
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001340#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1341 bool Is##type() const { return As##type() != nullptr; } \
1342 const H##type* As##type() const { return this; } \
1343 H##type* As##type() { return this; }
1344
David Brazdiled596192015-01-23 10:39:45 +00001345template <typename T> class HUseList;
1346
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001348class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 public:
David Brazdiled596192015-01-23 10:39:45 +00001350 HUseListNode* GetPrevious() const { return prev_; }
1351 HUseListNode* GetNext() const { return next_; }
1352 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001354 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001355
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001356 private:
David Brazdiled596192015-01-23 10:39:45 +00001357 HUseListNode(T user, size_t index)
1358 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1359
1360 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001361 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001362 HUseListNode<T>* prev_;
1363 HUseListNode<T>* next_;
1364
1365 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366
1367 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1368};
1369
David Brazdiled596192015-01-23 10:39:45 +00001370template <typename T>
1371class HUseList : public ValueObject {
1372 public:
1373 HUseList() : first_(nullptr) {}
1374
1375 void Clear() {
1376 first_ = nullptr;
1377 }
1378
1379 // Adds a new entry at the beginning of the use list and returns
1380 // the newly created node.
1381 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001382 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001383 if (IsEmpty()) {
1384 first_ = new_node;
1385 } else {
1386 first_->prev_ = new_node;
1387 new_node->next_ = first_;
1388 first_ = new_node;
1389 }
1390 return new_node;
1391 }
1392
1393 HUseListNode<T>* GetFirst() const {
1394 return first_;
1395 }
1396
1397 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001398 DCHECK(node != nullptr);
1399 DCHECK(Contains(node));
1400
David Brazdiled596192015-01-23 10:39:45 +00001401 if (node->prev_ != nullptr) {
1402 node->prev_->next_ = node->next_;
1403 }
1404 if (node->next_ != nullptr) {
1405 node->next_->prev_ = node->prev_;
1406 }
1407 if (node == first_) {
1408 first_ = node->next_;
1409 }
1410 }
1411
David Brazdil1abb4192015-02-17 18:33:36 +00001412 bool Contains(const HUseListNode<T>* node) const {
1413 if (node == nullptr) {
1414 return false;
1415 }
1416 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1417 if (current == node) {
1418 return true;
1419 }
1420 }
1421 return false;
1422 }
1423
David Brazdiled596192015-01-23 10:39:45 +00001424 bool IsEmpty() const {
1425 return first_ == nullptr;
1426 }
1427
1428 bool HasOnlyOneUse() const {
1429 return first_ != nullptr && first_->next_ == nullptr;
1430 }
1431
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001432 size_t SizeSlow() const {
1433 size_t count = 0;
1434 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1435 ++count;
1436 }
1437 return count;
1438 }
1439
David Brazdiled596192015-01-23 10:39:45 +00001440 private:
1441 HUseListNode<T>* first_;
1442};
1443
1444template<typename T>
1445class HUseIterator : public ValueObject {
1446 public:
1447 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1448
1449 bool Done() const { return current_ == nullptr; }
1450
1451 void Advance() {
1452 DCHECK(!Done());
1453 current_ = current_->GetNext();
1454 }
1455
1456 HUseListNode<T>* Current() const {
1457 DCHECK(!Done());
1458 return current_;
1459 }
1460
1461 private:
1462 HUseListNode<T>* current_;
1463
1464 friend class HValue;
1465};
1466
David Brazdil1abb4192015-02-17 18:33:36 +00001467// This class is used by HEnvironment and HInstruction classes to record the
1468// instructions they use and pointers to the corresponding HUseListNodes kept
1469// by the used instructions.
1470template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001471class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001472 public:
1473 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1474 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1475
1476 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1477 : instruction_(old_record.instruction_), use_node_(use_node) {
1478 DCHECK(instruction_ != nullptr);
1479 DCHECK(use_node_ != nullptr);
1480 DCHECK(old_record.use_node_ == nullptr);
1481 }
1482
1483 HInstruction* GetInstruction() const { return instruction_; }
1484 HUseListNode<T>* GetUseNode() const { return use_node_; }
1485
1486 private:
1487 // Instruction used by the user.
1488 HInstruction* instruction_;
1489
1490 // Corresponding entry in the use list kept by 'instruction_'.
1491 HUseListNode<T>* use_node_;
1492};
1493
Aart Bik854a02b2015-07-14 16:07:00 -07001494/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001495 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001496 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001497 * For write/read dependences on fields/arrays, the dependence analysis uses
1498 * type disambiguation (e.g. a float field write cannot modify the value of an
1499 * integer field read) and the access type (e.g. a reference array write cannot
1500 * modify the value of a reference field read [although it may modify the
1501 * reference fetch prior to reading the field, which is represented by its own
1502 * write/read dependence]). The analysis makes conservative points-to
1503 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1504 * the same, and any reference read depends on any reference read without
1505 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001506 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001507 * The internal representation uses 38-bit and is described in the table below.
1508 * The first line indicates the side effect, and for field/array accesses the
1509 * second line indicates the type of the access (in the order of the
1510 * Primitive::Type enum).
1511 * The two numbered lines below indicate the bit position in the bitfield (read
1512 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001513 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001514 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1515 * +-------------+---------+---------+--------------+---------+---------+
1516 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1517 * | 3 |333333322|222222221| 1 |111111110|000000000|
1518 * | 7 |654321098|765432109| 8 |765432109|876543210|
1519 *
1520 * Note that, to ease the implementation, 'changes' bits are least significant
1521 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001522 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523class SideEffects : public ValueObject {
1524 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001525 SideEffects() : flags_(0) {}
1526
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001527 static SideEffects None() {
1528 return SideEffects(0);
1529 }
1530
1531 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001532 return SideEffects(kAllChangeBits | kAllDependOnBits);
1533 }
1534
1535 static SideEffects AllChanges() {
1536 return SideEffects(kAllChangeBits);
1537 }
1538
1539 static SideEffects AllDependencies() {
1540 return SideEffects(kAllDependOnBits);
1541 }
1542
1543 static SideEffects AllExceptGCDependency() {
1544 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1545 }
1546
1547 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001548 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001549 }
1550
Aart Bik34c3ba92015-07-20 14:08:59 -07001551 static SideEffects AllWrites() {
1552 return SideEffects(kAllWrites);
1553 }
1554
1555 static SideEffects AllReads() {
1556 return SideEffects(kAllReads);
1557 }
1558
1559 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1560 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001561 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001562 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001563 }
1564
Aart Bik854a02b2015-07-14 16:07:00 -07001565 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1566 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001567 }
1568
Aart Bik34c3ba92015-07-20 14:08:59 -07001569 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1570 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001572 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001573 }
1574
1575 static SideEffects ArrayReadOfType(Primitive::Type type) {
1576 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1577 }
1578
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 static SideEffects CanTriggerGC() {
1580 return SideEffects(1ULL << kCanTriggerGCBit);
1581 }
1582
1583 static SideEffects DependsOnGC() {
1584 return SideEffects(1ULL << kDependsOnGCBit);
1585 }
1586
Aart Bik854a02b2015-07-14 16:07:00 -07001587 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001588 SideEffects Union(SideEffects other) const {
1589 return SideEffects(flags_ | other.flags_);
1590 }
1591
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 SideEffects Exclusion(SideEffects other) const {
1593 return SideEffects(flags_ & ~other.flags_);
1594 }
1595
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001596 void Add(SideEffects other) {
1597 flags_ |= other.flags_;
1598 }
1599
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001600 bool Includes(SideEffects other) const {
1601 return (other.flags_ & flags_) == other.flags_;
1602 }
1603
1604 bool HasSideEffects() const {
1605 return (flags_ & kAllChangeBits);
1606 }
1607
1608 bool HasDependencies() const {
1609 return (flags_ & kAllDependOnBits);
1610 }
1611
1612 // Returns true if there are no side effects or dependencies.
1613 bool DoesNothing() const {
1614 return flags_ == 0;
1615 }
1616
Aart Bik854a02b2015-07-14 16:07:00 -07001617 // Returns true if something is written.
1618 bool DoesAnyWrite() const {
1619 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001620 }
1621
Aart Bik854a02b2015-07-14 16:07:00 -07001622 // Returns true if something is read.
1623 bool DoesAnyRead() const {
1624 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001625 }
1626
Aart Bik854a02b2015-07-14 16:07:00 -07001627 // Returns true if potentially everything is written and read
1628 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 bool DoesAllReadWrite() const {
1630 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1631 }
1632
Aart Bik854a02b2015-07-14 16:07:00 -07001633 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001635 }
1636
Roland Levillain0d5a2812015-11-13 10:07:31 +00001637 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001638 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1640 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001641 }
1642
1643 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001644 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001645 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001646 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 for (int s = kLastBit; s >= 0; s--) {
1648 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1649 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1650 // This is a bit for the GC side effect.
1651 if (current_bit_is_set) {
1652 flags += "GC";
1653 }
Aart Bik854a02b2015-07-14 16:07:00 -07001654 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001655 } else {
1656 // This is a bit for the array/field analysis.
1657 // The underscore character stands for the 'can trigger GC' bit.
1658 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1659 if (current_bit_is_set) {
1660 flags += kDebug[s];
1661 }
1662 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1663 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1664 flags += "|";
1665 }
1666 }
Aart Bik854a02b2015-07-14 16:07:00 -07001667 }
1668 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001669 }
1670
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001671 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001672
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001673 private:
1674 static constexpr int kFieldArrayAnalysisBits = 9;
1675
1676 static constexpr int kFieldWriteOffset = 0;
1677 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1678 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1679 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1680
1681 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1682
1683 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1684 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1685 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1686 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1687
1688 static constexpr int kLastBit = kDependsOnGCBit;
1689 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1690
1691 // Aliases.
1692
1693 static_assert(kChangeBits == kDependOnBits,
1694 "the 'change' bits should match the 'depend on' bits.");
1695
1696 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1697 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1698 static constexpr uint64_t kAllWrites =
1699 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1700 static constexpr uint64_t kAllReads =
1701 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702
Aart Bik854a02b2015-07-14 16:07:00 -07001703 // Work around the fact that HIR aliases I/F and J/D.
1704 // TODO: remove this interceptor once HIR types are clean
1705 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1706 switch (type) {
1707 case Primitive::kPrimInt:
1708 case Primitive::kPrimFloat:
1709 return TypeFlag(Primitive::kPrimInt, offset) |
1710 TypeFlag(Primitive::kPrimFloat, offset);
1711 case Primitive::kPrimLong:
1712 case Primitive::kPrimDouble:
1713 return TypeFlag(Primitive::kPrimLong, offset) |
1714 TypeFlag(Primitive::kPrimDouble, offset);
1715 default:
1716 return TypeFlag(type, offset);
1717 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001718 }
1719
Aart Bik854a02b2015-07-14 16:07:00 -07001720 // Translates type to bit flag.
1721 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1722 CHECK_NE(type, Primitive::kPrimVoid);
1723 const uint64_t one = 1;
1724 const int shift = type; // 0-based consecutive enum
1725 DCHECK_LE(kFieldWriteOffset, shift);
1726 DCHECK_LT(shift, kArrayWriteOffset);
1727 return one << (type + offset);
1728 }
1729
1730 // Private constructor on direct flags value.
1731 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1732
1733 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001734};
1735
David Brazdiled596192015-01-23 10:39:45 +00001736// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001737class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001738 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 HEnvironment(ArenaAllocator* arena,
1740 size_t number_of_vregs,
1741 const DexFile& dex_file,
1742 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001743 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001744 InvokeType invoke_type,
1745 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001746 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1747 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001748 parent_(nullptr),
1749 dex_file_(dex_file),
1750 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001751 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001752 invoke_type_(invoke_type),
1753 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001754 }
1755
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001756 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001757 : HEnvironment(arena,
1758 to_copy.Size(),
1759 to_copy.GetDexFile(),
1760 to_copy.GetMethodIdx(),
1761 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001762 to_copy.GetInvokeType(),
1763 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001764
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001765 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001766 if (parent_ != nullptr) {
1767 parent_->SetAndCopyParentChain(allocator, parent);
1768 } else {
1769 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1770 parent_->CopyFrom(parent);
1771 if (parent->GetParent() != nullptr) {
1772 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1773 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001774 }
David Brazdiled596192015-01-23 10:39:45 +00001775 }
1776
Vladimir Marko71bf8092015-09-15 15:33:14 +01001777 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001778 void CopyFrom(HEnvironment* environment);
1779
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001780 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1781 // input to the loop phi instead. This is for inserting instructions that
1782 // require an environment (like HDeoptimization) in the loop pre-header.
1783 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001784
1785 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001786 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001787 }
1788
1789 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001790 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001791 }
1792
David Brazdil1abb4192015-02-17 18:33:36 +00001793 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001794
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001795 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001796
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001797 HEnvironment* GetParent() const { return parent_; }
1798
1799 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001800 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001801 }
1802
1803 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001804 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001805 }
1806
1807 uint32_t GetDexPc() const {
1808 return dex_pc_;
1809 }
1810
1811 uint32_t GetMethodIdx() const {
1812 return method_idx_;
1813 }
1814
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001815 InvokeType GetInvokeType() const {
1816 return invoke_type_;
1817 }
1818
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001819 const DexFile& GetDexFile() const {
1820 return dex_file_;
1821 }
1822
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001823 HInstruction* GetHolder() const {
1824 return holder_;
1825 }
1826
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001827
1828 bool IsFromInlinedInvoke() const {
1829 return GetParent() != nullptr;
1830 }
1831
David Brazdiled596192015-01-23 10:39:45 +00001832 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001833 // Record instructions' use entries of this environment for constant-time removal.
1834 // It should only be called by HInstruction when a new environment use is added.
1835 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1836 DCHECK(env_use->GetUser() == this);
1837 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001838 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001839 }
David Brazdiled596192015-01-23 10:39:45 +00001840
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001841 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1842 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001843 HEnvironment* parent_;
1844 const DexFile& dex_file_;
1845 const uint32_t method_idx_;
1846 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001847 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001848
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001849 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001850 HInstruction* const holder_;
1851
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001852 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001853
1854 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1855};
1856
Vladimir Markof9f64412015-09-02 14:05:49 +01001857class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001858 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001859 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860 : previous_(nullptr),
1861 next_(nullptr),
1862 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001863 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001865 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001866 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001867 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001868 locations_(nullptr),
1869 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001870 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001871 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001872 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1873 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1874 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001875
Dave Allison20dfc792014-06-16 20:44:29 -07001876 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001877
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001878#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001879 enum InstructionKind {
1880 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1881 };
1882#undef DECLARE_KIND
1883
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001884 HInstruction* GetNext() const { return next_; }
1885 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001886
Calin Juravle77520bc2015-01-12 18:45:46 +00001887 HInstruction* GetNextDisregardingMoves() const;
1888 HInstruction* GetPreviousDisregardingMoves() const;
1889
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001890 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001891 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001892 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001893 bool IsInBlock() const { return block_ != nullptr; }
1894 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001895 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1896 bool IsIrreducibleLoopHeaderPhi() const {
1897 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1898 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001899
Roland Levillain6b879dd2014-09-22 17:13:44 +01001900 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001901 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001902
1903 virtual void Accept(HGraphVisitor* visitor) = 0;
1904 virtual const char* DebugName() const = 0;
1905
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001907 void SetRawInputAt(size_t index, HInstruction* input) {
1908 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1909 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001910
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001912
1913 uint32_t GetDexPc() const { return dex_pc_; }
1914
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001915 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001916
Roland Levillaine161a2a2014-10-03 12:45:18 +01001917 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001918 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001919
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001920 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001921 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922
Calin Juravle10e244f2015-01-26 18:54:32 +00001923 // Does not apply for all instructions, but having this at top level greatly
1924 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001925 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001926 virtual bool CanBeNull() const {
1927 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1928 return true;
1929 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001931 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001932 return false;
1933 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001934
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001935 virtual bool IsActualObject() const {
1936 return GetType() == Primitive::kPrimNot;
1937 }
1938
Calin Juravle2e768302015-07-28 14:41:11 +00001939 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001940
Calin Juravle61d544b2015-02-23 16:46:57 +00001941 ReferenceTypeInfo GetReferenceTypeInfo() const {
1942 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001943 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1944 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001945 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001946
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001948 DCHECK(user != nullptr);
1949 HUseListNode<HInstruction*>* use =
1950 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1951 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001952 }
1953
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001954 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001955 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001956 HUseListNode<HEnvironment*>* env_use =
1957 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1958 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959 }
1960
David Brazdil1abb4192015-02-17 18:33:36 +00001961 void RemoveAsUserOfInput(size_t input) {
1962 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1963 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1964 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965
David Brazdil1abb4192015-02-17 18:33:36 +00001966 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1967 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001968
David Brazdiled596192015-01-23 10:39:45 +00001969 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1970 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001971 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001972 bool HasOnlyOneNonEnvironmentUse() const {
1973 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1974 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001975
Roland Levillain6c82d402014-10-13 16:10:27 +01001976 // Does this instruction strictly dominate `other_instruction`?
1977 // Returns false if this instruction and `other_instruction` are the same.
1978 // Aborts if this instruction and `other_instruction` are both phis.
1979 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001980
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001981 int GetId() const { return id_; }
1982 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001984 int GetSsaIndex() const { return ssa_index_; }
1985 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1986 bool HasSsaIndex() const { return ssa_index_ != -1; }
1987
1988 bool HasEnvironment() const { return environment_ != nullptr; }
1989 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001990 // Set the `environment_` field. Raw because this method does not
1991 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001992 void SetRawEnvironment(HEnvironment* environment) {
1993 DCHECK(environment_ == nullptr);
1994 DCHECK_EQ(environment->GetHolder(), this);
1995 environment_ = environment;
1996 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001997
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001998 void RemoveEnvironment();
1999
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002000 // Set the environment of this instruction, copying it from `environment`. While
2001 // copying, the uses lists are being updated.
2002 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002003 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002004 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002005 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002006 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002007 if (environment->GetParent() != nullptr) {
2008 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2009 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002010 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002012 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2013 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002014 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002015 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002016 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002017 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002018 if (environment->GetParent() != nullptr) {
2019 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2020 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002021 }
2022
Nicolas Geoffray39468442014-09-02 15:17:15 +01002023 // Returns the number of entries in the environment. Typically, that is the
2024 // number of dex registers in a method. It could be more in case of inlining.
2025 size_t EnvironmentSize() const;
2026
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002027 LocationSummary* GetLocations() const { return locations_; }
2028 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002029
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002030 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002031 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002032
Alexandre Rames188d4312015-04-09 18:30:21 +01002033 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2034 // uses of this instruction by `other` are *not* updated.
2035 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2036 ReplaceWith(other);
2037 other->ReplaceInput(this, use_index);
2038 }
2039
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002040 // Move `this` instruction before `cursor`.
2041 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002042
Vladimir Markofb337ea2015-11-25 15:25:10 +00002043 // Move `this` before its first user and out of any loops. If there is no
2044 // out-of-loop user that dominates all other users, move the instruction
2045 // to the end of the out-of-loop common dominator of the user's blocks.
2046 //
2047 // This can be used only on non-throwing instructions with no side effects that
2048 // have at least one use but no environment uses.
2049 void MoveBeforeFirstUserAndOutOfLoops();
2050
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002051#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002052 bool Is##type() const; \
2053 const H##type* As##type() const; \
2054 H##type* As##type();
2055
2056 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2057#undef INSTRUCTION_TYPE_CHECK
2058
2059#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002060 bool Is##type() const { return (As##type() != nullptr); } \
2061 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002062 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002063 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002064#undef INSTRUCTION_TYPE_CHECK
2065
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002066 // Returns whether the instruction can be moved within the graph.
2067 virtual bool CanBeMoved() const { return false; }
2068
2069 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002070 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002071 return false;
2072 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002073
2074 // Returns whether any data encoded in the two instructions is equal.
2075 // This method does not look at the inputs. Both instructions must be
2076 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002077 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002078 return false;
2079 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002080
2081 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002082 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002083 // 2) Their inputs are identical.
2084 bool Equals(HInstruction* other) const;
2085
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002086 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2087 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2088 // the virtual function because the __attribute__((__pure__)) doesn't really
2089 // apply the strong requirement for virtual functions, preventing optimizations.
2090 InstructionKind GetKind() const PURE;
2091 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002092
2093 virtual size_t ComputeHashCode() const {
2094 size_t result = GetKind();
2095 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2096 result = (result * 31) + InputAt(i)->GetId();
2097 }
2098 return result;
2099 }
2100
2101 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002102 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002103 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002104
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002105 size_t GetLifetimePosition() const { return lifetime_position_; }
2106 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2107 LiveInterval* GetLiveInterval() const { return live_interval_; }
2108 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2109 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2110
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002111 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2112
2113 // Returns whether the code generation of the instruction will require to have access
2114 // to the current method. Such instructions are:
2115 // (1): Instructions that require an environment, as calling the runtime requires
2116 // to walk the stack and have the current method stored at a specific stack address.
2117 // (2): Object literals like classes and strings, that are loaded from the dex cache
2118 // fields of the current method.
2119 bool NeedsCurrentMethod() const {
2120 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2121 }
2122
Vladimir Markodc151b22015-10-15 18:02:30 +01002123 // Returns whether the code generation of the instruction will require to have access
2124 // to the dex cache of the current method's declaring class via the current method.
2125 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002126
Mark Mendellc4701932015-04-10 13:18:51 -04002127 // Does this instruction have any use in an environment before
2128 // control flow hits 'other'?
2129 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2130
2131 // Remove all references to environment uses of this instruction.
2132 // The caller must ensure that this is safe to do.
2133 void RemoveEnvironmentUsers();
2134
Vladimir Markoa1de9182016-02-25 11:37:38 +00002135 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2136 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002137
David Brazdil1abb4192015-02-17 18:33:36 +00002138 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002139 // If set, the machine code for this instruction is assumed to be generated by
2140 // its users. Used by liveness analysis to compute use positions accordingly.
2141 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2142 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2143 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2144 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2145
David Brazdil1abb4192015-02-17 18:33:36 +00002146 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2147 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2148
Vladimir Markoa1de9182016-02-25 11:37:38 +00002149 uint32_t GetPackedFields() const {
2150 return packed_fields_;
2151 }
2152
2153 template <size_t flag>
2154 bool GetPackedFlag() const {
2155 return (packed_fields_ & (1u << flag)) != 0u;
2156 }
2157
2158 template <size_t flag>
2159 void SetPackedFlag(bool value = true) {
2160 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2161 }
2162
2163 template <typename BitFieldType>
2164 typename BitFieldType::value_type GetPackedField() const {
2165 return BitFieldType::Decode(packed_fields_);
2166 }
2167
2168 template <typename BitFieldType>
2169 void SetPackedField(typename BitFieldType::value_type value) {
2170 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2171 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2172 }
2173
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002175 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2176
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002177 HInstruction* previous_;
2178 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002179 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002180 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002182 // An instruction gets an id when it is added to the graph.
2183 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002184 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185 int id_;
2186
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002187 // When doing liveness analysis, instructions that have uses get an SSA index.
2188 int ssa_index_;
2189
Vladimir Markoa1de9182016-02-25 11:37:38 +00002190 // Packed fields.
2191 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002192
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002194 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002195
2196 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002197 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002198
Nicolas Geoffray39468442014-09-02 15:17:15 +01002199 // The environment associated with this instruction. Not null if the instruction
2200 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002201 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002202
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002203 // Set by the code generator.
2204 LocationSummary* locations_;
2205
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002206 // Set by the liveness analysis.
2207 LiveInterval* live_interval_;
2208
2209 // Set by the liveness analysis, this is the position in a linear
2210 // order of blocks where this instruction's live interval start.
2211 size_t lifetime_position_;
2212
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002213 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002214
Vladimir Markoa1de9182016-02-25 11:37:38 +00002215 // The reference handle part of the reference type info.
2216 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002217 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002218 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002219
David Brazdil1abb4192015-02-17 18:33:36 +00002220 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002222 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002223 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002224 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225
2226 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2227};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002228std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002230class HInputIterator : public ValueObject {
2231 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002232 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002233
2234 bool Done() const { return index_ == instruction_->InputCount(); }
2235 HInstruction* Current() const { return instruction_->InputAt(index_); }
2236 void Advance() { index_++; }
2237
2238 private:
2239 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002240 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241
2242 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2243};
2244
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002245class HInstructionIterator : public ValueObject {
2246 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247 explicit HInstructionIterator(const HInstructionList& instructions)
2248 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002249 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002250 }
2251
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002252 bool Done() const { return instruction_ == nullptr; }
2253 HInstruction* Current() const { return instruction_; }
2254 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002256 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257 }
2258
2259 private:
2260 HInstruction* instruction_;
2261 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002262
2263 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002264};
2265
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002266class HBackwardInstructionIterator : public ValueObject {
2267 public:
2268 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2269 : instruction_(instructions.last_instruction_) {
2270 next_ = Done() ? nullptr : instruction_->GetPrevious();
2271 }
2272
2273 bool Done() const { return instruction_ == nullptr; }
2274 HInstruction* Current() const { return instruction_; }
2275 void Advance() {
2276 instruction_ = next_;
2277 next_ = Done() ? nullptr : instruction_->GetPrevious();
2278 }
2279
2280 private:
2281 HInstruction* instruction_;
2282 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002283
2284 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002285};
2286
Vladimir Markof9f64412015-09-02 14:05:49 +01002287template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002288class HTemplateInstruction: public HInstruction {
2289 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002290 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002291 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002292 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002294 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002295
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002296 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002297 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2298 DCHECK_LT(i, N);
2299 return inputs_[i];
2300 }
David Brazdil1abb4192015-02-17 18:33:36 +00002301
2302 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002303 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002304 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002305 }
2306
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002308 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002309
2310 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311};
2312
Vladimir Markof9f64412015-09-02 14:05:49 +01002313// HTemplateInstruction specialization for N=0.
2314template<>
2315class HTemplateInstruction<0>: public HInstruction {
2316 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002317 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 : HInstruction(side_effects, dex_pc) {}
2319
Vladimir Markof9f64412015-09-02 14:05:49 +01002320 virtual ~HTemplateInstruction() {}
2321
2322 size_t InputCount() const OVERRIDE { return 0; }
2323
2324 protected:
2325 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2326 LOG(FATAL) << "Unreachable";
2327 UNREACHABLE();
2328 }
2329
2330 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2331 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2332 LOG(FATAL) << "Unreachable";
2333 UNREACHABLE();
2334 }
2335
2336 private:
2337 friend class SsaBuilder;
2338};
2339
Dave Allison20dfc792014-06-16 20:44:29 -07002340template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002341class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002342 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002343 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002344 : HTemplateInstruction<N>(side_effects, dex_pc) {
2345 this->template SetPackedField<TypeField>(type);
2346 }
Dave Allison20dfc792014-06-16 20:44:29 -07002347 virtual ~HExpression() {}
2348
Vladimir Markoa1de9182016-02-25 11:37:38 +00002349 Primitive::Type GetType() const OVERRIDE {
2350 return TypeField::Decode(this->GetPackedFields());
2351 }
Dave Allison20dfc792014-06-16 20:44:29 -07002352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002353 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002354 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2355 static constexpr size_t kFieldTypeSize =
2356 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2357 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2358 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2359 "Too many packed fields.");
2360 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002361};
2362
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002363// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2364// instruction that branches to the exit block.
2365class HReturnVoid : public HTemplateInstruction<0> {
2366 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002367 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2368 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002370 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002371
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002372 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002373
2374 private:
2375 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2376};
2377
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002378// Represents dex's RETURN opcodes. A HReturn is a control flow
2379// instruction that branches to the exit block.
2380class HReturn : public HTemplateInstruction<1> {
2381 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002382 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2383 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002384 SetRawInputAt(0, value);
2385 }
2386
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002387 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002388
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002389 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002390
2391 private:
2392 DISALLOW_COPY_AND_ASSIGN(HReturn);
2393};
2394
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002395// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002396// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002397// exit block.
2398class HExit : public HTemplateInstruction<0> {
2399 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002400 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002402 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002403
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002404 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002405
2406 private:
2407 DISALLOW_COPY_AND_ASSIGN(HExit);
2408};
2409
2410// Jumps from one block to another.
2411class HGoto : public HTemplateInstruction<0> {
2412 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002413 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002414
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002415 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002416
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002417 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002418 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002419 }
2420
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002421 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002422
2423 private:
2424 DISALLOW_COPY_AND_ASSIGN(HGoto);
2425};
2426
Roland Levillain9867bc72015-08-05 10:21:34 +01002427class HConstant : public HExpression<0> {
2428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002429 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2430 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002431
2432 bool CanBeMoved() const OVERRIDE { return true; }
2433
Roland Levillain1a653882016-03-18 18:05:57 +00002434 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002435 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002436 // Is this constant 0 in the arithmetic sense?
2437 virtual bool IsArithmeticZero() const { return false; }
2438 // Is this constant a 0-bit pattern?
2439 virtual bool IsZeroBitPattern() const { return false; }
2440 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002441 virtual bool IsOne() const { return false; }
2442
David Brazdil77a48ae2015-09-15 12:34:04 +00002443 virtual uint64_t GetValueAsUint64() const = 0;
2444
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002445 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002446
2447 private:
2448 DISALLOW_COPY_AND_ASSIGN(HConstant);
2449};
2450
2451class HNullConstant : public HConstant {
2452 public:
2453 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2454 return true;
2455 }
2456
David Brazdil77a48ae2015-09-15 12:34:04 +00002457 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2458
Roland Levillain9867bc72015-08-05 10:21:34 +01002459 size_t ComputeHashCode() const OVERRIDE { return 0; }
2460
Roland Levillain1a653882016-03-18 18:05:57 +00002461 // The null constant representation is a 0-bit pattern.
2462 virtual bool IsZeroBitPattern() const { return true; }
2463
Roland Levillain9867bc72015-08-05 10:21:34 +01002464 DECLARE_INSTRUCTION(NullConstant);
2465
2466 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002467 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002468
2469 friend class HGraph;
2470 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2471};
2472
2473// Constants of the type int. Those can be from Dex instructions, or
2474// synthesized (for example with the if-eqz instruction).
2475class HIntConstant : public HConstant {
2476 public:
2477 int32_t GetValue() const { return value_; }
2478
David Brazdil9f389d42015-10-01 14:32:56 +01002479 uint64_t GetValueAsUint64() const OVERRIDE {
2480 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2481 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002482
Roland Levillain9867bc72015-08-05 10:21:34 +01002483 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002484 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002485 return other->AsIntConstant()->value_ == value_;
2486 }
2487
2488 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2489
2490 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002491 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2492 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002493 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2494
Roland Levillain1a653882016-03-18 18:05:57 +00002495 // Integer constants are used to encode Boolean values as well,
2496 // where 1 means true and 0 means false.
2497 bool IsTrue() const { return GetValue() == 1; }
2498 bool IsFalse() const { return GetValue() == 0; }
2499
Roland Levillain9867bc72015-08-05 10:21:34 +01002500 DECLARE_INSTRUCTION(IntConstant);
2501
2502 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002503 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2504 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2505 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2506 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002507
2508 const int32_t value_;
2509
2510 friend class HGraph;
2511 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2512 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2513 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2514};
2515
2516class HLongConstant : public HConstant {
2517 public:
2518 int64_t GetValue() const { return value_; }
2519
David Brazdil77a48ae2015-09-15 12:34:04 +00002520 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2521
Roland Levillain9867bc72015-08-05 10:21:34 +01002522 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002523 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002524 return other->AsLongConstant()->value_ == value_;
2525 }
2526
2527 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2528
2529 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002530 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2531 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002532 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2533
2534 DECLARE_INSTRUCTION(LongConstant);
2535
2536 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002537 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2538 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002539
2540 const int64_t value_;
2541
2542 friend class HGraph;
2543 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2544};
Dave Allison20dfc792014-06-16 20:44:29 -07002545
Roland Levillain31dd3d62016-02-16 12:21:02 +00002546class HFloatConstant : public HConstant {
2547 public:
2548 float GetValue() const { return value_; }
2549
2550 uint64_t GetValueAsUint64() const OVERRIDE {
2551 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2552 }
2553
2554 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2555 DCHECK(other->IsFloatConstant()) << other->DebugName();
2556 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2557 }
2558
2559 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2560
2561 bool IsMinusOne() const OVERRIDE {
2562 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2563 }
Roland Levillain1a653882016-03-18 18:05:57 +00002564 bool IsArithmeticZero() const OVERRIDE {
2565 return std::fpclassify(value_) == FP_ZERO;
2566 }
2567 bool IsArithmeticPositiveZero() const {
2568 return IsArithmeticZero() && !std::signbit(value_);
2569 }
2570 bool IsArithmeticNegativeZero() const {
2571 return IsArithmeticZero() && std::signbit(value_);
2572 }
2573 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002574 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002575 }
2576 bool IsOne() const OVERRIDE {
2577 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2578 }
2579 bool IsNaN() const {
2580 return std::isnan(value_);
2581 }
2582
2583 DECLARE_INSTRUCTION(FloatConstant);
2584
2585 private:
2586 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2587 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2588 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2589 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2590
2591 const float value_;
2592
2593 // Only the SsaBuilder and HGraph can create floating-point constants.
2594 friend class SsaBuilder;
2595 friend class HGraph;
2596 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2597};
2598
2599class HDoubleConstant : public HConstant {
2600 public:
2601 double GetValue() const { return value_; }
2602
2603 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2604
2605 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2606 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2607 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2608 }
2609
2610 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2611
2612 bool IsMinusOne() const OVERRIDE {
2613 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2614 }
Roland Levillain1a653882016-03-18 18:05:57 +00002615 bool IsArithmeticZero() const OVERRIDE {
2616 return std::fpclassify(value_) == FP_ZERO;
2617 }
2618 bool IsArithmeticPositiveZero() const {
2619 return IsArithmeticZero() && !std::signbit(value_);
2620 }
2621 bool IsArithmeticNegativeZero() const {
2622 return IsArithmeticZero() && std::signbit(value_);
2623 }
2624 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002625 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002626 }
2627 bool IsOne() const OVERRIDE {
2628 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2629 }
2630 bool IsNaN() const {
2631 return std::isnan(value_);
2632 }
2633
2634 DECLARE_INSTRUCTION(DoubleConstant);
2635
2636 private:
2637 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2638 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2639 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2640 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2641
2642 const double value_;
2643
2644 // Only the SsaBuilder and HGraph can create floating-point constants.
2645 friend class SsaBuilder;
2646 friend class HGraph;
2647 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2648};
2649
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002650// Conditional branch. A block ending with an HIf instruction must have
2651// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002652class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002653 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2655 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002656 SetRawInputAt(0, input);
2657 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002658
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002659 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002660
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002661 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002662 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002663 }
2664
2665 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002666 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002667 }
2668
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002669 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002670
2671 private:
2672 DISALLOW_COPY_AND_ASSIGN(HIf);
2673};
2674
David Brazdilfc6a86a2015-06-26 10:33:45 +00002675
2676// Abstract instruction which marks the beginning and/or end of a try block and
2677// links it to the respective exception handlers. Behaves the same as a Goto in
2678// non-exceptional control flow.
2679// Normal-flow successor is stored at index zero, exception handlers under
2680// higher indices in no particular order.
2681class HTryBoundary : public HTemplateInstruction<0> {
2682 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002683 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002684 kEntry,
2685 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002686 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002687 };
2688
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002689 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002690 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2691 SetPackedField<BoundaryKindField>(kind);
2692 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002693
2694 bool IsControlFlow() const OVERRIDE { return true; }
2695
2696 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002697 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002698
David Brazdild26a4112015-11-10 11:07:31 +00002699 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2700 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2701 }
2702
David Brazdilfc6a86a2015-06-26 10:33:45 +00002703 // Returns whether `handler` is among its exception handlers (non-zero index
2704 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002705 bool HasExceptionHandler(const HBasicBlock& handler) const {
2706 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002707 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002708 }
2709
2710 // If not present already, adds `handler` to its block's list of exception
2711 // handlers.
2712 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002713 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002714 GetBlock()->AddSuccessor(handler);
2715 }
2716 }
2717
Vladimir Markoa1de9182016-02-25 11:37:38 +00002718 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2719 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002720
David Brazdilffee3d32015-07-06 11:48:53 +01002721 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2722
David Brazdilfc6a86a2015-06-26 10:33:45 +00002723 DECLARE_INSTRUCTION(TryBoundary);
2724
2725 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002726 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2727 static constexpr size_t kFieldBoundaryKindSize =
2728 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2729 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2730 kFieldBoundaryKind + kFieldBoundaryKindSize;
2731 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2732 "Too many packed fields.");
2733 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002734
2735 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2736};
2737
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002738// Deoptimize to interpreter, upon checking a condition.
2739class HDeoptimize : public HTemplateInstruction<1> {
2740 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002741 // We set CanTriggerGC to prevent any intermediate address to be live
2742 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002743 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002744 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002745 SetRawInputAt(0, cond);
2746 }
2747
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002748 bool CanBeMoved() const OVERRIDE { return true; }
2749 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2750 return true;
2751 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002752 bool NeedsEnvironment() const OVERRIDE { return true; }
2753 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002754
2755 DECLARE_INSTRUCTION(Deoptimize);
2756
2757 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002758 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2759};
2760
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002761// Represents the ArtMethod that was passed as a first argument to
2762// the method. It is used by instructions that depend on it, like
2763// instructions that work with the dex cache.
2764class HCurrentMethod : public HExpression<0> {
2765 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2767 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002768
2769 DECLARE_INSTRUCTION(CurrentMethod);
2770
2771 private:
2772 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2773};
2774
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002775// Fetches an ArtMethod from the virtual table or the interface method table
2776// of a class.
2777class HClassTableGet : public HExpression<1> {
2778 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002779 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002780 kVTable,
2781 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002782 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002783 };
2784 HClassTableGet(HInstruction* cls,
2785 Primitive::Type type,
2786 TableKind kind,
2787 size_t index,
2788 uint32_t dex_pc)
2789 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002790 index_(index) {
2791 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002792 SetRawInputAt(0, cls);
2793 }
2794
2795 bool CanBeMoved() const OVERRIDE { return true; }
2796 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2797 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002798 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002799 }
2800
Vladimir Markoa1de9182016-02-25 11:37:38 +00002801 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002802 size_t GetIndex() const { return index_; }
2803
2804 DECLARE_INSTRUCTION(ClassTableGet);
2805
2806 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002807 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2808 static constexpr size_t kFieldTableKindSize =
2809 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2810 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2811 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2812 "Too many packed fields.");
2813 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2814
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002815 // The index of the ArtMethod in the table.
2816 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002817
2818 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2819};
2820
Mark Mendellfe57faa2015-09-18 09:26:15 -04002821// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2822// have one successor for each entry in the switch table, and the final successor
2823// will be the block containing the next Dex opcode.
2824class HPackedSwitch : public HTemplateInstruction<1> {
2825 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002826 HPackedSwitch(int32_t start_value,
2827 uint32_t num_entries,
2828 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002829 uint32_t dex_pc = kNoDexPc)
2830 : HTemplateInstruction(SideEffects::None(), dex_pc),
2831 start_value_(start_value),
2832 num_entries_(num_entries) {
2833 SetRawInputAt(0, input);
2834 }
2835
2836 bool IsControlFlow() const OVERRIDE { return true; }
2837
2838 int32_t GetStartValue() const { return start_value_; }
2839
Vladimir Marko211c2112015-09-24 16:52:33 +01002840 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002841
2842 HBasicBlock* GetDefaultBlock() const {
2843 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002844 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002845 }
2846 DECLARE_INSTRUCTION(PackedSwitch);
2847
2848 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002849 const int32_t start_value_;
2850 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002851
2852 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2853};
2854
Roland Levillain88cb1752014-10-20 16:36:47 +01002855class HUnaryOperation : public HExpression<1> {
2856 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002857 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2858 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002859 SetRawInputAt(0, input);
2860 }
2861
2862 HInstruction* GetInput() const { return InputAt(0); }
2863 Primitive::Type GetResultType() const { return GetType(); }
2864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002865 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002866 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002867 return true;
2868 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002869
Roland Levillain31dd3d62016-02-16 12:21:02 +00002870 // Try to statically evaluate `this` and return a HConstant
2871 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002872 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002873 HConstant* TryStaticEvaluation() const;
2874
2875 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002876 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2877 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002878 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2879 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002880
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002881 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002882
2883 private:
2884 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2885};
2886
Dave Allison20dfc792014-06-16 20:44:29 -07002887class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002888 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002889 HBinaryOperation(Primitive::Type result_type,
2890 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002891 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002892 SideEffects side_effects = SideEffects::None(),
2893 uint32_t dex_pc = kNoDexPc)
2894 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002895 SetRawInputAt(0, left);
2896 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002897 }
2898
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002899 HInstruction* GetLeft() const { return InputAt(0); }
2900 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002901 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002902
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002903 virtual bool IsCommutative() const { return false; }
2904
2905 // Put constant on the right.
2906 // Returns whether order is changed.
2907 bool OrderInputsWithConstantOnTheRight() {
2908 HInstruction* left = InputAt(0);
2909 HInstruction* right = InputAt(1);
2910 if (left->IsConstant() && !right->IsConstant()) {
2911 ReplaceInput(right, 0);
2912 ReplaceInput(left, 1);
2913 return true;
2914 }
2915 return false;
2916 }
2917
2918 // Order inputs by instruction id, but favor constant on the right side.
2919 // This helps GVN for commutative ops.
2920 void OrderInputs() {
2921 DCHECK(IsCommutative());
2922 HInstruction* left = InputAt(0);
2923 HInstruction* right = InputAt(1);
2924 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2925 return;
2926 }
2927 if (OrderInputsWithConstantOnTheRight()) {
2928 return;
2929 }
2930 // Order according to instruction id.
2931 if (left->GetId() > right->GetId()) {
2932 ReplaceInput(right, 0);
2933 ReplaceInput(left, 1);
2934 }
2935 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002936
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002937 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002938 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002939 return true;
2940 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002941
Roland Levillain31dd3d62016-02-16 12:21:02 +00002942 // Try to statically evaluate `this` and return a HConstant
2943 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002944 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002945 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002946
2947 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002948 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2949 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002950 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2951 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002952 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002953 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2954 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002955 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2956 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002957 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2958 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002959 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002960 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2961 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002962
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002963 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002964 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002965 HConstant* GetConstantRight() const;
2966
2967 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002968 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002969 HInstruction* GetLeastConstantLeft() const;
2970
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002971 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002972
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002973 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002974 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2975};
2976
Mark Mendellc4701932015-04-10 13:18:51 -04002977// The comparison bias applies for floating point operations and indicates how NaN
2978// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002979enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002980 kNoBias, // bias is not applicable (i.e. for long operation)
2981 kGtBias, // return 1 for NaN comparisons
2982 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002983 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002984};
2985
Roland Levillain31dd3d62016-02-16 12:21:02 +00002986std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2987
Dave Allison20dfc792014-06-16 20:44:29 -07002988class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002989 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002990 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002991 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2992 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2993 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002994
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002995 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002996 // `instruction`, and disregard moves in between.
2997 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002998
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002999 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003000
3001 virtual IfCondition GetCondition() const = 0;
3002
Mark Mendellc4701932015-04-10 13:18:51 -04003003 virtual IfCondition GetOppositeCondition() const = 0;
3004
Vladimir Markoa1de9182016-02-25 11:37:38 +00003005 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003006 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3007
Vladimir Markoa1de9182016-02-25 11:37:38 +00003008 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3009 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003010
3011 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003012 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003013 }
3014
Roland Levillain4fa13f62015-07-06 18:11:54 +01003015 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003016 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003017 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003018 if (if_cond == kCondNE) {
3019 return true;
3020 } else if (if_cond == kCondEQ) {
3021 return false;
3022 }
3023 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003024 }
3025
3026 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003028 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003029 if (if_cond == kCondEQ) {
3030 return true;
3031 } else if (if_cond == kCondNE) {
3032 return false;
3033 }
3034 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003035 }
3036
Roland Levillain31dd3d62016-02-16 12:21:02 +00003037 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003038 // Needed if we merge a HCompare into a HCondition.
3039 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3040 static constexpr size_t kFieldComparisonBiasSize =
3041 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3042 static constexpr size_t kNumberOfConditionPackedBits =
3043 kFieldComparisonBias + kFieldComparisonBiasSize;
3044 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3045 using ComparisonBiasField =
3046 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3047
Roland Levillain31dd3d62016-02-16 12:21:02 +00003048 template <typename T>
3049 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3050
3051 template <typename T>
3052 int32_t CompareFP(T x, T y) const {
3053 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3054 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3055 // Handle the bias.
3056 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3057 }
3058
3059 // Return an integer constant containing the result of a condition evaluated at compile time.
3060 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3061 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3062 }
3063
Dave Allison20dfc792014-06-16 20:44:29 -07003064 private:
3065 DISALLOW_COPY_AND_ASSIGN(HCondition);
3066};
3067
3068// Instruction to check if two inputs are equal to each other.
3069class HEqual : public HCondition {
3070 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003071 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3072 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003073
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003074 bool IsCommutative() const OVERRIDE { return true; }
3075
Vladimir Marko9e23df52015-11-10 17:14:35 +00003076 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3077 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 return MakeConstantCondition(true, GetDexPc());
3079 }
3080 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3081 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3082 }
3083 // In the following Evaluate methods, a HCompare instruction has
3084 // been merged into this HEqual instruction; evaluate it as
3085 // `Compare(x, y) == 0`.
3086 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3087 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3088 GetDexPc());
3089 }
3090 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3091 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3092 }
3093 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3094 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003095 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003096
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003097 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003099 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003100 return kCondEQ;
3101 }
3102
Mark Mendellc4701932015-04-10 13:18:51 -04003103 IfCondition GetOppositeCondition() const OVERRIDE {
3104 return kCondNE;
3105 }
3106
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003107 private:
Aart Bike9f37602015-10-09 11:15:55 -07003108 template <typename T> bool Compute(T x, T y) const { return x == y; }
3109
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003110 DISALLOW_COPY_AND_ASSIGN(HEqual);
3111};
3112
Dave Allison20dfc792014-06-16 20:44:29 -07003113class HNotEqual : public HCondition {
3114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003115 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3116 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003117
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003118 bool IsCommutative() const OVERRIDE { return true; }
3119
Vladimir Marko9e23df52015-11-10 17:14:35 +00003120 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3121 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003122 return MakeConstantCondition(false, GetDexPc());
3123 }
3124 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3125 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3126 }
3127 // In the following Evaluate methods, a HCompare instruction has
3128 // been merged into this HNotEqual instruction; evaluate it as
3129 // `Compare(x, y) != 0`.
3130 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3131 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3132 }
3133 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3134 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3135 }
3136 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3137 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003138 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003139
Dave Allison20dfc792014-06-16 20:44:29 -07003140 DECLARE_INSTRUCTION(NotEqual);
3141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003142 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003143 return kCondNE;
3144 }
3145
Mark Mendellc4701932015-04-10 13:18:51 -04003146 IfCondition GetOppositeCondition() const OVERRIDE {
3147 return kCondEQ;
3148 }
3149
Dave Allison20dfc792014-06-16 20:44:29 -07003150 private:
Aart Bike9f37602015-10-09 11:15:55 -07003151 template <typename T> bool Compute(T x, T y) const { return x != y; }
3152
Dave Allison20dfc792014-06-16 20:44:29 -07003153 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3154};
3155
3156class HLessThan : public HCondition {
3157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003158 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3159 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003160
Roland Levillain9867bc72015-08-05 10:21:34 +01003161 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003163 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164 // In the following Evaluate methods, a HCompare instruction has
3165 // been merged into this HLessThan instruction; evaluate it as
3166 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003167 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003168 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3169 }
3170 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3171 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3172 }
3173 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3174 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003175 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003176
Dave Allison20dfc792014-06-16 20:44:29 -07003177 DECLARE_INSTRUCTION(LessThan);
3178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003179 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003180 return kCondLT;
3181 }
3182
Mark Mendellc4701932015-04-10 13:18:51 -04003183 IfCondition GetOppositeCondition() const OVERRIDE {
3184 return kCondGE;
3185 }
3186
Dave Allison20dfc792014-06-16 20:44:29 -07003187 private:
Aart Bike9f37602015-10-09 11:15:55 -07003188 template <typename T> bool Compute(T x, T y) const { return x < y; }
3189
Dave Allison20dfc792014-06-16 20:44:29 -07003190 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3191};
3192
3193class HLessThanOrEqual : public HCondition {
3194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003195 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3196 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003197
Roland Levillain9867bc72015-08-05 10:21:34 +01003198 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003200 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 // In the following Evaluate methods, a HCompare instruction has
3202 // been merged into this HLessThanOrEqual instruction; evaluate it as
3203 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003204 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3206 }
3207 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3208 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3209 }
3210 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3211 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003212 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003213
Dave Allison20dfc792014-06-16 20:44:29 -07003214 DECLARE_INSTRUCTION(LessThanOrEqual);
3215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003216 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003217 return kCondLE;
3218 }
3219
Mark Mendellc4701932015-04-10 13:18:51 -04003220 IfCondition GetOppositeCondition() const OVERRIDE {
3221 return kCondGT;
3222 }
3223
Dave Allison20dfc792014-06-16 20:44:29 -07003224 private:
Aart Bike9f37602015-10-09 11:15:55 -07003225 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3226
Dave Allison20dfc792014-06-16 20:44:29 -07003227 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3228};
3229
3230class HGreaterThan : public HCondition {
3231 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003232 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3233 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003234
Roland Levillain9867bc72015-08-05 10:21:34 +01003235 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003237 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 // In the following Evaluate methods, a HCompare instruction has
3239 // been merged into this HGreaterThan instruction; evaluate it as
3240 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003241 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003242 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3243 }
3244 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3245 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3246 }
3247 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3248 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003249 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003250
Dave Allison20dfc792014-06-16 20:44:29 -07003251 DECLARE_INSTRUCTION(GreaterThan);
3252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003253 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003254 return kCondGT;
3255 }
3256
Mark Mendellc4701932015-04-10 13:18:51 -04003257 IfCondition GetOppositeCondition() const OVERRIDE {
3258 return kCondLE;
3259 }
3260
Dave Allison20dfc792014-06-16 20:44:29 -07003261 private:
Aart Bike9f37602015-10-09 11:15:55 -07003262 template <typename T> bool Compute(T x, T y) const { return x > y; }
3263
Dave Allison20dfc792014-06-16 20:44:29 -07003264 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3265};
3266
3267class HGreaterThanOrEqual : public HCondition {
3268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003269 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3270 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003271
Roland Levillain9867bc72015-08-05 10:21:34 +01003272 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003274 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 // In the following Evaluate methods, a HCompare instruction has
3276 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3277 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003278 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3280 }
3281 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3282 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3283 }
3284 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3285 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003286 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003287
Dave Allison20dfc792014-06-16 20:44:29 -07003288 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3289
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003290 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003291 return kCondGE;
3292 }
3293
Mark Mendellc4701932015-04-10 13:18:51 -04003294 IfCondition GetOppositeCondition() const OVERRIDE {
3295 return kCondLT;
3296 }
3297
Dave Allison20dfc792014-06-16 20:44:29 -07003298 private:
Aart Bike9f37602015-10-09 11:15:55 -07003299 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3300
Dave Allison20dfc792014-06-16 20:44:29 -07003301 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3302};
3303
Aart Bike9f37602015-10-09 11:15:55 -07003304class HBelow : public HCondition {
3305 public:
3306 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3307 : HCondition(first, second, dex_pc) {}
3308
3309 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003311 }
3312 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003313 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3314 }
3315 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3316 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3317 LOG(FATAL) << DebugName() << " is not defined for float values";
3318 UNREACHABLE();
3319 }
3320 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3321 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3322 LOG(FATAL) << DebugName() << " is not defined for double values";
3323 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003324 }
3325
3326 DECLARE_INSTRUCTION(Below);
3327
3328 IfCondition GetCondition() const OVERRIDE {
3329 return kCondB;
3330 }
3331
3332 IfCondition GetOppositeCondition() const OVERRIDE {
3333 return kCondAE;
3334 }
3335
3336 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003337 template <typename T> bool Compute(T x, T y) const {
3338 return MakeUnsigned(x) < MakeUnsigned(y);
3339 }
Aart Bike9f37602015-10-09 11:15:55 -07003340
3341 DISALLOW_COPY_AND_ASSIGN(HBelow);
3342};
3343
3344class HBelowOrEqual : public HCondition {
3345 public:
3346 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3347 : HCondition(first, second, dex_pc) {}
3348
3349 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003350 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003351 }
3352 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3354 }
3355 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3356 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3357 LOG(FATAL) << DebugName() << " is not defined for float values";
3358 UNREACHABLE();
3359 }
3360 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3361 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3362 LOG(FATAL) << DebugName() << " is not defined for double values";
3363 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003364 }
3365
3366 DECLARE_INSTRUCTION(BelowOrEqual);
3367
3368 IfCondition GetCondition() const OVERRIDE {
3369 return kCondBE;
3370 }
3371
3372 IfCondition GetOppositeCondition() const OVERRIDE {
3373 return kCondA;
3374 }
3375
3376 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003377 template <typename T> bool Compute(T x, T y) const {
3378 return MakeUnsigned(x) <= MakeUnsigned(y);
3379 }
Aart Bike9f37602015-10-09 11:15:55 -07003380
3381 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3382};
3383
3384class HAbove : public HCondition {
3385 public:
3386 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3387 : HCondition(first, second, dex_pc) {}
3388
3389 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003391 }
3392 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003393 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3394 }
3395 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3396 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3397 LOG(FATAL) << DebugName() << " is not defined for float values";
3398 UNREACHABLE();
3399 }
3400 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3401 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3402 LOG(FATAL) << DebugName() << " is not defined for double values";
3403 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003404 }
3405
3406 DECLARE_INSTRUCTION(Above);
3407
3408 IfCondition GetCondition() const OVERRIDE {
3409 return kCondA;
3410 }
3411
3412 IfCondition GetOppositeCondition() const OVERRIDE {
3413 return kCondBE;
3414 }
3415
3416 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003417 template <typename T> bool Compute(T x, T y) const {
3418 return MakeUnsigned(x) > MakeUnsigned(y);
3419 }
Aart Bike9f37602015-10-09 11:15:55 -07003420
3421 DISALLOW_COPY_AND_ASSIGN(HAbove);
3422};
3423
3424class HAboveOrEqual : public HCondition {
3425 public:
3426 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3427 : HCondition(first, second, dex_pc) {}
3428
3429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003431 }
3432 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003433 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3434 }
3435 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3436 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3437 LOG(FATAL) << DebugName() << " is not defined for float values";
3438 UNREACHABLE();
3439 }
3440 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3441 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3442 LOG(FATAL) << DebugName() << " is not defined for double values";
3443 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003444 }
3445
3446 DECLARE_INSTRUCTION(AboveOrEqual);
3447
3448 IfCondition GetCondition() const OVERRIDE {
3449 return kCondAE;
3450 }
3451
3452 IfCondition GetOppositeCondition() const OVERRIDE {
3453 return kCondB;
3454 }
3455
3456 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003457 template <typename T> bool Compute(T x, T y) const {
3458 return MakeUnsigned(x) >= MakeUnsigned(y);
3459 }
Aart Bike9f37602015-10-09 11:15:55 -07003460
3461 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3462};
Dave Allison20dfc792014-06-16 20:44:29 -07003463
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003464// Instruction to check how two inputs compare to each other.
3465// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3466class HCompare : public HBinaryOperation {
3467 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003468 // Note that `comparison_type` is the type of comparison performed
3469 // between the comparison's inputs, not the type of the instantiated
3470 // HCompare instruction (which is always Primitive::kPrimInt).
3471 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003472 HInstruction* first,
3473 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003474 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003475 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003476 : HBinaryOperation(Primitive::kPrimInt,
3477 first,
3478 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003479 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003480 dex_pc) {
3481 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003482 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3483 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003484 }
3485
Roland Levillain9867bc72015-08-05 10:21:34 +01003486 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003487 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3488
3489 template <typename T>
3490 int32_t ComputeFP(T x, T y) const {
3491 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3492 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3493 // Handle the bias.
3494 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3495 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003496
Roland Levillain9867bc72015-08-05 10:21:34 +01003497 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3499 // reach this code path when processing a freshly built HIR
3500 // graph. However HCompare integer instructions can be synthesized
3501 // by the instruction simplifier to implement IntegerCompare and
3502 // IntegerSignum intrinsics, so we have to handle this case.
3503 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003504 }
3505 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003506 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3507 }
3508 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3509 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3510 }
3511 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3512 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003513 }
3514
Calin Juravleddb7df22014-11-25 20:56:51 +00003515 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003516 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003517 }
3518
Vladimir Markoa1de9182016-02-25 11:37:38 +00003519 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003520
Roland Levillain31dd3d62016-02-16 12:21:02 +00003521 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003522 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003523 bool IsGtBias() const {
3524 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003525 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003526 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003527
Roland Levillain1693a1f2016-03-15 14:57:31 +00003528 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3529 // Comparisons do not require a runtime call in any back end.
3530 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003531 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003532
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003533 DECLARE_INSTRUCTION(Compare);
3534
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003536 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3537 static constexpr size_t kFieldComparisonBiasSize =
3538 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3539 static constexpr size_t kNumberOfComparePackedBits =
3540 kFieldComparisonBias + kFieldComparisonBiasSize;
3541 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3542 using ComparisonBiasField =
3543 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3544
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 // Return an integer constant containing the result of a comparison evaluated at compile time.
3546 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3547 DCHECK(value == -1 || value == 0 || value == 1) << value;
3548 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3549 }
3550
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003551 private:
3552 DISALLOW_COPY_AND_ASSIGN(HCompare);
3553};
3554
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003555// A local in the graph. Corresponds to a Dex register.
3556class HLocal : public HTemplateInstruction<0> {
3557 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003558 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003559 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003560
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003561 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003562
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003563 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003564
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003565 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003566 // The Dex register number.
3567 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003568
3569 DISALLOW_COPY_AND_ASSIGN(HLocal);
3570};
3571
3572// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003573class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003574 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003575 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3576 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003577 SetRawInputAt(0, local);
3578 }
3579
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003580 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3581
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003582 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003583
3584 private:
3585 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3586};
3587
3588// Store a value in a given local. This instruction has two inputs: the value
3589// and the local.
3590class HStoreLocal : public HTemplateInstruction<2> {
3591 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003592 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3593 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003594 SetRawInputAt(0, local);
3595 SetRawInputAt(1, value);
3596 }
3597
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003598 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3599
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003600 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003601
3602 private:
3603 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3604};
3605
David Brazdil6de19382016-01-08 17:37:10 +00003606class HNewInstance : public HExpression<2> {
3607 public:
3608 HNewInstance(HInstruction* cls,
3609 HCurrentMethod* current_method,
3610 uint32_t dex_pc,
3611 uint16_t type_index,
3612 const DexFile& dex_file,
3613 bool can_throw,
3614 bool finalizable,
3615 QuickEntrypointEnum entrypoint)
3616 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3617 type_index_(type_index),
3618 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003619 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003620 SetPackedFlag<kFlagCanThrow>(can_throw);
3621 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003622 SetRawInputAt(0, cls);
3623 SetRawInputAt(1, current_method);
3624 }
3625
3626 uint16_t GetTypeIndex() const { return type_index_; }
3627 const DexFile& GetDexFile() const { return dex_file_; }
3628
3629 // Calls runtime so needs an environment.
3630 bool NeedsEnvironment() const OVERRIDE { return true; }
3631
3632 // It may throw when called on type that's not instantiable/accessible.
3633 // It can throw OOME.
3634 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003635 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003636
Vladimir Markoa1de9182016-02-25 11:37:38 +00003637 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003638
3639 bool CanBeNull() const OVERRIDE { return false; }
3640
3641 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3642
3643 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3644 entrypoint_ = entrypoint;
3645 }
3646
3647 bool IsStringAlloc() const;
3648
3649 DECLARE_INSTRUCTION(NewInstance);
3650
3651 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003652 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3653 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3654 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3655 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3656 "Too many packed fields.");
3657
David Brazdil6de19382016-01-08 17:37:10 +00003658 const uint16_t type_index_;
3659 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003660 QuickEntrypointEnum entrypoint_;
3661
3662 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3663};
3664
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003665enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003666#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3667 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003668#include "intrinsics_list.h"
3669 kNone,
3670 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3671#undef INTRINSICS_LIST
3672#undef OPTIMIZING_INTRINSICS
3673};
3674std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3675
Agi Csaki05f20562015-08-19 14:58:14 -07003676enum IntrinsicNeedsEnvironmentOrCache {
3677 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3678 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003679};
3680
Aart Bik5d75afe2015-12-14 11:57:01 -08003681enum IntrinsicSideEffects {
3682 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3683 kReadSideEffects, // Intrinsic may read heap memory.
3684 kWriteSideEffects, // Intrinsic may write heap memory.
3685 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3686};
3687
3688enum IntrinsicExceptions {
3689 kNoThrow, // Intrinsic does not throw any exceptions.
3690 kCanThrow // Intrinsic may throw exceptions.
3691};
3692
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003693class HInvoke : public HInstruction {
3694 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003695 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003696
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003697 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003698
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003699 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003700 SetRawInputAt(index, argument);
3701 }
3702
Roland Levillain3e3d7332015-04-28 11:00:54 +01003703 // Return the number of arguments. This number can be lower than
3704 // the number of inputs returned by InputCount(), as some invoke
3705 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3706 // inputs at the end of their list of inputs.
3707 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3708
Vladimir Markoa1de9182016-02-25 11:37:38 +00003709 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003710
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003711 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003712 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003713
Vladimir Markoa1de9182016-02-25 11:37:38 +00003714 InvokeType GetOriginalInvokeType() const {
3715 return GetPackedField<OriginalInvokeTypeField>();
3716 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003717
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003718 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003719 return intrinsic_;
3720 }
3721
Aart Bik5d75afe2015-12-14 11:57:01 -08003722 void SetIntrinsic(Intrinsics intrinsic,
3723 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3724 IntrinsicSideEffects side_effects,
3725 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003726
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003727 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003728 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003729 }
3730
Vladimir Markoa1de9182016-02-25 11:37:38 +00003731 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003732
3733 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3734
3735 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3736 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3737 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003738
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003739 uint32_t* GetIntrinsicOptimizations() {
3740 return &intrinsic_optimizations_;
3741 }
3742
3743 const uint32_t* GetIntrinsicOptimizations() const {
3744 return &intrinsic_optimizations_;
3745 }
3746
3747 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3748
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003749 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003750
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003751 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003752 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3753 static constexpr size_t kFieldOriginalInvokeTypeSize =
3754 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3755 static constexpr size_t kFieldReturnType =
3756 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3757 static constexpr size_t kFieldReturnTypeSize =
3758 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3759 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3760 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3761 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3762 using OriginalInvokeTypeField =
3763 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3764 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3765
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003766 HInvoke(ArenaAllocator* arena,
3767 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003768 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003769 Primitive::Type return_type,
3770 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003771 uint32_t dex_method_index,
3772 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003773 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003774 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003775 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003776 inputs_(number_of_arguments + number_of_other_inputs,
3777 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003778 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003779 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003780 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003781 SetPackedField<ReturnTypeField>(return_type);
3782 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3783 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003784 }
3785
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003786 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003787 return inputs_[index];
3788 }
3789
David Brazdil1abb4192015-02-17 18:33:36 +00003790 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003791 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003792 }
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
Calin Juravle175dc732015-08-25 15:42:32 +01003808class HInvokeUnresolved : public HInvoke {
3809 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
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003831class HInvokeStaticOrDirect : 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) +
3926 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3927 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003928 return_type,
3929 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003930 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003931 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003932 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003933 dispatch_info_(dispatch_info) {
3934 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3935 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3936 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003937
Vladimir Markodc151b22015-10-15 18:02:30 +01003938 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003939 bool had_current_method_input = HasCurrentMethodInput();
3940 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3941
3942 // Using the current method is the default and once we find a better
3943 // method load kind, we should not go back to using the current method.
3944 DCHECK(had_current_method_input || !needs_current_method_input);
3945
3946 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003947 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3948 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003949 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003950 dispatch_info_ = dispatch_info;
3951 }
3952
Vladimir Markoc53c0792015-11-19 15:48:33 +00003953 void AddSpecialInput(HInstruction* input) {
3954 // We allow only one special input.
3955 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3956 DCHECK(InputCount() == GetSpecialInputIndex() ||
3957 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3958 InsertInputAt(GetSpecialInputIndex(), input);
3959 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003960
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003961 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003962 // We access the method via the dex cache so we can't do an implicit null check.
3963 // TODO: for intrinsics we can generate implicit null checks.
3964 return false;
3965 }
3966
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003967 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003968 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003969 }
3970
Vladimir Markoc53c0792015-11-19 15:48:33 +00003971 // Get the index of the special input, if any.
3972 //
David Brazdil6de19382016-01-08 17:37:10 +00003973 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3974 // method pointer; otherwise there may be one platform-specific special input,
3975 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003976 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003977 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003978
Vladimir Markoa1de9182016-02-25 11:37:38 +00003979 InvokeType GetOptimizedInvokeType() const {
3980 return GetPackedField<OptimizedInvokeTypeField>();
3981 }
3982
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003983 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003984 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003985 }
3986
Vladimir Marko58155012015-08-19 12:49:41 +00003987 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3988 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3989 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003990 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003991 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003992 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003993 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003994 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3995 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003996 bool HasCurrentMethodInput() const {
3997 // This function can be called only after the invoke has been fully initialized by the builder.
3998 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003999 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004000 return true;
4001 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004002 DCHECK(InputCount() == GetSpecialInputIndex() ||
4003 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004004 return false;
4005 }
4006 }
Vladimir Marko58155012015-08-19 12:49:41 +00004007 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4008 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004009 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004010
4011 int32_t GetStringInitOffset() const {
4012 DCHECK(IsStringInit());
4013 return dispatch_info_.method_load_data;
4014 }
4015
4016 uint64_t GetMethodAddress() const {
4017 DCHECK(HasMethodAddress());
4018 return dispatch_info_.method_load_data;
4019 }
4020
4021 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004022 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004023 return dispatch_info_.method_load_data;
4024 }
4025
4026 uint64_t GetDirectCodePtr() const {
4027 DCHECK(HasDirectCodePtr());
4028 return dispatch_info_.direct_code_ptr;
4029 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004030
Vladimir Markoa1de9182016-02-25 11:37:38 +00004031 ClinitCheckRequirement GetClinitCheckRequirement() const {
4032 return GetPackedField<ClinitCheckRequirementField>();
4033 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004034
Roland Levillain4c0eb422015-04-24 16:43:49 +01004035 // Is this instruction a call to a static method?
4036 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004037 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004038 }
4039
Vladimir Markofbb184a2015-11-13 14:47:00 +00004040 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4041 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4042 // instruction; only relevant for static calls with explicit clinit check.
4043 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004044 DCHECK(IsStaticWithExplicitClinitCheck());
4045 size_t last_input_index = InputCount() - 1;
4046 HInstruction* last_input = InputAt(last_input_index);
4047 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004048 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004049 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004050 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004051 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004052 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004053 }
4054
David Brazdilbc9ab162016-01-20 14:50:19 +00004055 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004056 DCHECK(IsStringInit());
4057 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004058 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004059 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004060 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004061 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004062 }
4063
Roland Levillain4c0eb422015-04-24 16:43:49 +01004064 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004065 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004066 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004067 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004068 }
4069
4070 // Is this a call to a static method whose declaring class has an
4071 // implicit intialization check requirement?
4072 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004073 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004074 }
4075
Vladimir Markob554b5a2015-11-06 12:57:55 +00004076 // Does this method load kind need the current method as an input?
4077 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4078 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4079 }
4080
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004081 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004082
Roland Levillain4c0eb422015-04-24 16:43:49 +01004083 protected:
4084 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4085 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4086 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4087 HInstruction* input = input_record.GetInstruction();
4088 // `input` is the last input of a static invoke marked as having
4089 // an explicit clinit check. It must either be:
4090 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4091 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4092 DCHECK(input != nullptr);
4093 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4094 }
4095 return input_record;
4096 }
4097
Vladimir Markoc53c0792015-11-19 15:48:33 +00004098 void InsertInputAt(size_t index, HInstruction* input);
4099 void RemoveInputAt(size_t index);
4100
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004101 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004102 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4103 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4104 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4105 static constexpr size_t kFieldClinitCheckRequirement =
4106 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4107 static constexpr size_t kFieldClinitCheckRequirementSize =
4108 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4109 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4110 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4111 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4112 "Too many packed fields.");
4113 using OptimizedInvokeTypeField =
4114 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4115 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4116 kFieldClinitCheckRequirement,
4117 kFieldClinitCheckRequirementSize>;
4118
Vladimir Marko58155012015-08-19 12:49:41 +00004119 // The target method may refer to different dex file or method index than the original
4120 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4121 // in derived class to increase visibility.
4122 MethodReference target_method_;
4123 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004124
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004125 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004126};
Vladimir Markof64242a2015-12-01 14:58:23 +00004127std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004128std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004129
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004130class HInvokeVirtual : public HInvoke {
4131 public:
4132 HInvokeVirtual(ArenaAllocator* arena,
4133 uint32_t number_of_arguments,
4134 Primitive::Type return_type,
4135 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004136 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004137 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004138 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004139 vtable_index_(vtable_index) {}
4140
Calin Juravle641547a2015-04-21 22:08:51 +01004141 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004142 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004143 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004144 }
4145
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004146 uint32_t GetVTableIndex() const { return vtable_index_; }
4147
4148 DECLARE_INSTRUCTION(InvokeVirtual);
4149
4150 private:
4151 const uint32_t vtable_index_;
4152
4153 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4154};
4155
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004156class HInvokeInterface : public HInvoke {
4157 public:
4158 HInvokeInterface(ArenaAllocator* arena,
4159 uint32_t number_of_arguments,
4160 Primitive::Type return_type,
4161 uint32_t dex_pc,
4162 uint32_t dex_method_index,
4163 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004164 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004165 imt_index_(imt_index) {}
4166
Calin Juravle641547a2015-04-21 22:08:51 +01004167 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004168 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004169 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004170 }
4171
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004172 uint32_t GetImtIndex() const { return imt_index_; }
4173 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4174
4175 DECLARE_INSTRUCTION(InvokeInterface);
4176
4177 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004178 const uint32_t imt_index_;
4179
4180 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4181};
4182
Roland Levillain88cb1752014-10-20 16:36:47 +01004183class HNeg : public HUnaryOperation {
4184 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004185 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004186 : HUnaryOperation(result_type, input, dex_pc) {
4187 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4188 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004189
Roland Levillain9867bc72015-08-05 10:21:34 +01004190 template <typename T> T Compute(T x) const { return -x; }
4191
4192 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004193 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004194 }
4195 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004196 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004197 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004198 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4199 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4200 }
4201 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4202 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4203 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004204
Roland Levillain88cb1752014-10-20 16:36:47 +01004205 DECLARE_INSTRUCTION(Neg);
4206
4207 private:
4208 DISALLOW_COPY_AND_ASSIGN(HNeg);
4209};
4210
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004211class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004212 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004213 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004214 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004215 uint32_t dex_pc,
4216 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004217 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004218 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004219 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004220 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004221 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004222 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004223 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004224 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004225 }
4226
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004227 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004228 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004229
4230 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004231 bool NeedsEnvironment() const OVERRIDE { return true; }
4232
Mingyao Yang0c365e62015-03-31 15:09:29 -07004233 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4234 bool CanThrow() const OVERRIDE { return true; }
4235
Calin Juravle10e244f2015-01-26 18:54:32 +00004236 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004237
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004238 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4239
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004240 DECLARE_INSTRUCTION(NewArray);
4241
4242 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004243 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004244 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004245 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004246
4247 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4248};
4249
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004250class HAdd : public HBinaryOperation {
4251 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004252 HAdd(Primitive::Type result_type,
4253 HInstruction* left,
4254 HInstruction* right,
4255 uint32_t dex_pc = kNoDexPc)
4256 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004257
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004258 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004259
Roland Levillain9867bc72015-08-05 10:21:34 +01004260 template <typename T> T Compute(T x, T y) const { return x + y; }
4261
4262 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 return GetBlock()->GetGraph()->GetIntConstant(
4264 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004265 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004266 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004267 return GetBlock()->GetGraph()->GetLongConstant(
4268 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004269 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004270 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4271 return GetBlock()->GetGraph()->GetFloatConstant(
4272 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4273 }
4274 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4275 return GetBlock()->GetGraph()->GetDoubleConstant(
4276 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4277 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004278
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004279 DECLARE_INSTRUCTION(Add);
4280
4281 private:
4282 DISALLOW_COPY_AND_ASSIGN(HAdd);
4283};
4284
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004285class HSub : public HBinaryOperation {
4286 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004287 HSub(Primitive::Type result_type,
4288 HInstruction* left,
4289 HInstruction* right,
4290 uint32_t dex_pc = kNoDexPc)
4291 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004292
Roland Levillain9867bc72015-08-05 10:21:34 +01004293 template <typename T> T Compute(T x, T y) const { return x - y; }
4294
4295 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004296 return GetBlock()->GetGraph()->GetIntConstant(
4297 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004298 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004299 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004300 return GetBlock()->GetGraph()->GetLongConstant(
4301 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004302 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004303 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4304 return GetBlock()->GetGraph()->GetFloatConstant(
4305 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4306 }
4307 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4308 return GetBlock()->GetGraph()->GetDoubleConstant(
4309 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4310 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004311
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004312 DECLARE_INSTRUCTION(Sub);
4313
4314 private:
4315 DISALLOW_COPY_AND_ASSIGN(HSub);
4316};
4317
Calin Juravle34bacdf2014-10-07 20:23:36 +01004318class HMul : public HBinaryOperation {
4319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 HMul(Primitive::Type result_type,
4321 HInstruction* left,
4322 HInstruction* right,
4323 uint32_t dex_pc = kNoDexPc)
4324 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004325
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004326 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004327
Roland Levillain9867bc72015-08-05 10:21:34 +01004328 template <typename T> T Compute(T x, T y) const { return x * y; }
4329
4330 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004331 return GetBlock()->GetGraph()->GetIntConstant(
4332 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004333 }
4334 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004335 return GetBlock()->GetGraph()->GetLongConstant(
4336 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004337 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004338 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4339 return GetBlock()->GetGraph()->GetFloatConstant(
4340 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4341 }
4342 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4343 return GetBlock()->GetGraph()->GetDoubleConstant(
4344 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4345 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004346
4347 DECLARE_INSTRUCTION(Mul);
4348
4349 private:
4350 DISALLOW_COPY_AND_ASSIGN(HMul);
4351};
4352
Calin Juravle7c4954d2014-10-28 16:57:40 +00004353class HDiv : public HBinaryOperation {
4354 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 HDiv(Primitive::Type result_type,
4356 HInstruction* left,
4357 HInstruction* right,
4358 uint32_t dex_pc)
4359 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004360
Roland Levillain9867bc72015-08-05 10:21:34 +01004361 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004362 T ComputeIntegral(T x, T y) const {
4363 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004364 // Our graph structure ensures we never have 0 for `y` during
4365 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004366 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004367 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004368 return (y == -1) ? -x : x / y;
4369 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004370
Roland Levillain31dd3d62016-02-16 12:21:02 +00004371 template <typename T>
4372 T ComputeFP(T x, T y) const {
4373 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4374 return x / y;
4375 }
4376
Roland Levillain9867bc72015-08-05 10:21:34 +01004377 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004378 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004379 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004380 }
4381 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004382 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004383 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4384 }
4385 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4386 return GetBlock()->GetGraph()->GetFloatConstant(
4387 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4388 }
4389 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4390 return GetBlock()->GetGraph()->GetDoubleConstant(
4391 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004392 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004393
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004394 static SideEffects SideEffectsForArchRuntimeCalls() {
4395 // The generated code can use a runtime call.
4396 return SideEffects::CanTriggerGC();
4397 }
4398
Calin Juravle7c4954d2014-10-28 16:57:40 +00004399 DECLARE_INSTRUCTION(Div);
4400
4401 private:
4402 DISALLOW_COPY_AND_ASSIGN(HDiv);
4403};
4404
Calin Juravlebacfec32014-11-14 15:54:36 +00004405class HRem : public HBinaryOperation {
4406 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004407 HRem(Primitive::Type result_type,
4408 HInstruction* left,
4409 HInstruction* right,
4410 uint32_t dex_pc)
4411 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004412
Roland Levillain9867bc72015-08-05 10:21:34 +01004413 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004414 T ComputeIntegral(T x, T y) const {
4415 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004416 // Our graph structure ensures we never have 0 for `y` during
4417 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004418 DCHECK_NE(y, 0);
4419 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4420 return (y == -1) ? 0 : x % y;
4421 }
4422
Roland Levillain31dd3d62016-02-16 12:21:02 +00004423 template <typename T>
4424 T ComputeFP(T x, T y) const {
4425 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4426 return std::fmod(x, y);
4427 }
4428
Roland Levillain9867bc72015-08-05 10:21:34 +01004429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004430 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004431 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004432 }
4433 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004434 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004435 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004436 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004437 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4438 return GetBlock()->GetGraph()->GetFloatConstant(
4439 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4440 }
4441 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4442 return GetBlock()->GetGraph()->GetDoubleConstant(
4443 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4444 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004445
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004446 static SideEffects SideEffectsForArchRuntimeCalls() {
4447 return SideEffects::CanTriggerGC();
4448 }
4449
Calin Juravlebacfec32014-11-14 15:54:36 +00004450 DECLARE_INSTRUCTION(Rem);
4451
4452 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004453 DISALLOW_COPY_AND_ASSIGN(HRem);
4454};
4455
Calin Juravled0d48522014-11-04 16:40:20 +00004456class HDivZeroCheck : public HExpression<1> {
4457 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004458 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4459 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004460 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004461 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004462 SetRawInputAt(0, value);
4463 }
4464
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004465 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4466
Calin Juravled0d48522014-11-04 16:40:20 +00004467 bool CanBeMoved() const OVERRIDE { return true; }
4468
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004469 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004470 return true;
4471 }
4472
4473 bool NeedsEnvironment() const OVERRIDE { return true; }
4474 bool CanThrow() const OVERRIDE { return true; }
4475
Calin Juravled0d48522014-11-04 16:40:20 +00004476 DECLARE_INSTRUCTION(DivZeroCheck);
4477
4478 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004479 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4480};
4481
Calin Juravle9aec02f2014-11-18 23:06:35 +00004482class HShl : public HBinaryOperation {
4483 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004484 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004485 HInstruction* value,
4486 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004487 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004488 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4489 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4490 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004491 }
4492
Roland Levillain5b5b9312016-03-22 14:57:31 +00004493 template <typename T>
4494 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4495 return value << (distance & max_shift_distance);
4496 }
4497
4498 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004500 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004501 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004503 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004504 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004505 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004506 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4507 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4508 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4509 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4512 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004513 LOG(FATAL) << DebugName() << " is not defined for float values";
4514 UNREACHABLE();
4515 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004516 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4517 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004518 LOG(FATAL) << DebugName() << " is not defined for double values";
4519 UNREACHABLE();
4520 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004521
4522 DECLARE_INSTRUCTION(Shl);
4523
4524 private:
4525 DISALLOW_COPY_AND_ASSIGN(HShl);
4526};
4527
4528class HShr : public HBinaryOperation {
4529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004530 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004531 HInstruction* value,
4532 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004533 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004534 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4535 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4536 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004537 }
4538
Roland Levillain5b5b9312016-03-22 14:57:31 +00004539 template <typename T>
4540 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4541 return value >> (distance & max_shift_distance);
4542 }
4543
4544 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004545 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004549 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004550 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004551 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004552 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4553 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4554 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4555 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4558 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004559 LOG(FATAL) << DebugName() << " is not defined for float values";
4560 UNREACHABLE();
4561 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004562 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4563 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004564 LOG(FATAL) << DebugName() << " is not defined for double values";
4565 UNREACHABLE();
4566 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004567
4568 DECLARE_INSTRUCTION(Shr);
4569
4570 private:
4571 DISALLOW_COPY_AND_ASSIGN(HShr);
4572};
4573
4574class HUShr : public HBinaryOperation {
4575 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004577 HInstruction* value,
4578 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004579 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004580 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4581 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4582 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004583 }
4584
Roland Levillain5b5b9312016-03-22 14:57:31 +00004585 template <typename T>
4586 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4587 typedef typename std::make_unsigned<T>::type V;
4588 V ux = static_cast<V>(value);
4589 return static_cast<T>(ux >> (distance & max_shift_distance));
4590 }
4591
4592 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004593 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004595 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004597 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004598 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004599 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004600 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4601 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4602 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4603 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004604 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4606 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004607 LOG(FATAL) << DebugName() << " is not defined for float values";
4608 UNREACHABLE();
4609 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004610 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4611 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004612 LOG(FATAL) << DebugName() << " is not defined for double values";
4613 UNREACHABLE();
4614 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004615
4616 DECLARE_INSTRUCTION(UShr);
4617
4618 private:
4619 DISALLOW_COPY_AND_ASSIGN(HUShr);
4620};
4621
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004622class HAnd : public HBinaryOperation {
4623 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004624 HAnd(Primitive::Type result_type,
4625 HInstruction* left,
4626 HInstruction* right,
4627 uint32_t dex_pc = kNoDexPc)
4628 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004629
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004630 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004631
Roland Levillaine53bd812016-02-24 14:54:18 +00004632 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004633
4634 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004635 return GetBlock()->GetGraph()->GetIntConstant(
4636 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004637 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004638 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004639 return GetBlock()->GetGraph()->GetLongConstant(
4640 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004641 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004642 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4643 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4644 LOG(FATAL) << DebugName() << " is not defined for float values";
4645 UNREACHABLE();
4646 }
4647 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4648 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4649 LOG(FATAL) << DebugName() << " is not defined for double values";
4650 UNREACHABLE();
4651 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004652
4653 DECLARE_INSTRUCTION(And);
4654
4655 private:
4656 DISALLOW_COPY_AND_ASSIGN(HAnd);
4657};
4658
4659class HOr : public HBinaryOperation {
4660 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004661 HOr(Primitive::Type result_type,
4662 HInstruction* left,
4663 HInstruction* right,
4664 uint32_t dex_pc = kNoDexPc)
4665 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004666
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004667 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004668
Roland Levillaine53bd812016-02-24 14:54:18 +00004669 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004670
4671 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004672 return GetBlock()->GetGraph()->GetIntConstant(
4673 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004674 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004675 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004676 return GetBlock()->GetGraph()->GetLongConstant(
4677 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004679 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4680 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4681 LOG(FATAL) << DebugName() << " is not defined for float values";
4682 UNREACHABLE();
4683 }
4684 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4685 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4686 LOG(FATAL) << DebugName() << " is not defined for double values";
4687 UNREACHABLE();
4688 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004689
4690 DECLARE_INSTRUCTION(Or);
4691
4692 private:
4693 DISALLOW_COPY_AND_ASSIGN(HOr);
4694};
4695
4696class HXor : public HBinaryOperation {
4697 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004698 HXor(Primitive::Type result_type,
4699 HInstruction* left,
4700 HInstruction* right,
4701 uint32_t dex_pc = kNoDexPc)
4702 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004703
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004704 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004705
Roland Levillaine53bd812016-02-24 14:54:18 +00004706 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004707
4708 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004709 return GetBlock()->GetGraph()->GetIntConstant(
4710 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004711 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004712 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004713 return GetBlock()->GetGraph()->GetLongConstant(
4714 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004715 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004716 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4717 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4718 LOG(FATAL) << DebugName() << " is not defined for float values";
4719 UNREACHABLE();
4720 }
4721 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4722 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4723 LOG(FATAL) << DebugName() << " is not defined for double values";
4724 UNREACHABLE();
4725 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004726
4727 DECLARE_INSTRUCTION(Xor);
4728
4729 private:
4730 DISALLOW_COPY_AND_ASSIGN(HXor);
4731};
4732
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004733class HRor : public HBinaryOperation {
4734 public:
4735 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004736 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004737 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4738 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004739 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004740
Roland Levillain5b5b9312016-03-22 14:57:31 +00004741 template <typename T>
4742 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4743 typedef typename std::make_unsigned<T>::type V;
4744 V ux = static_cast<V>(value);
4745 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004746 return static_cast<T>(ux);
4747 } else {
4748 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 return static_cast<T>(ux >> (distance & max_shift_value)) |
4750 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004751 }
4752 }
4753
Roland Levillain5b5b9312016-03-22 14:57:31 +00004754 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004755 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004756 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004757 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004759 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004761 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4763 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4764 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4765 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4768 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004769 LOG(FATAL) << DebugName() << " is not defined for float values";
4770 UNREACHABLE();
4771 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4773 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004774 LOG(FATAL) << DebugName() << " is not defined for double values";
4775 UNREACHABLE();
4776 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004777
4778 DECLARE_INSTRUCTION(Ror);
4779
4780 private:
4781 DISALLOW_COPY_AND_ASSIGN(HRor);
4782};
4783
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004784// The value of a parameter in this method. Its location depends on
4785// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004786class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004787 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004788 HParameterValue(const DexFile& dex_file,
4789 uint16_t type_index,
4790 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004791 Primitive::Type parameter_type,
4792 bool is_this = false)
4793 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004794 dex_file_(dex_file),
4795 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004796 index_(index) {
4797 SetPackedFlag<kFlagIsThis>(is_this);
4798 SetPackedFlag<kFlagCanBeNull>(!is_this);
4799 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004800
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004801 const DexFile& GetDexFile() const { return dex_file_; }
4802 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004803 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004804 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004805
Vladimir Markoa1de9182016-02-25 11:37:38 +00004806 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4807 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004808
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004809 DECLARE_INSTRUCTION(ParameterValue);
4810
4811 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004812 // Whether or not the parameter value corresponds to 'this' argument.
4813 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4814 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4815 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4816 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4817 "Too many packed fields.");
4818
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004819 const DexFile& dex_file_;
4820 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004821 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004822 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004823 const uint8_t index_;
4824
4825 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4826};
4827
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004828class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004829 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004830 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4831 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004832
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004833 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004834 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004835 return true;
4836 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004837
Roland Levillain9867bc72015-08-05 10:21:34 +01004838 template <typename T> T Compute(T x) const { return ~x; }
4839
4840 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004841 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 }
4843 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004844 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004846 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4847 LOG(FATAL) << DebugName() << " is not defined for float values";
4848 UNREACHABLE();
4849 }
4850 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4851 LOG(FATAL) << DebugName() << " is not defined for double values";
4852 UNREACHABLE();
4853 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004854
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004855 DECLARE_INSTRUCTION(Not);
4856
4857 private:
4858 DISALLOW_COPY_AND_ASSIGN(HNot);
4859};
4860
David Brazdil66d126e2015-04-03 16:02:44 +01004861class HBooleanNot : public HUnaryOperation {
4862 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004863 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4864 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004865
4866 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004867 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004868 return true;
4869 }
4870
Roland Levillain9867bc72015-08-05 10:21:34 +01004871 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004872 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004873 return !x;
4874 }
4875
Roland Levillain9867bc72015-08-05 10:21:34 +01004876 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004877 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004878 }
4879 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4880 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004881 UNREACHABLE();
4882 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004883 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4884 LOG(FATAL) << DebugName() << " is not defined for float values";
4885 UNREACHABLE();
4886 }
4887 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4888 LOG(FATAL) << DebugName() << " is not defined for double values";
4889 UNREACHABLE();
4890 }
David Brazdil66d126e2015-04-03 16:02:44 +01004891
4892 DECLARE_INSTRUCTION(BooleanNot);
4893
4894 private:
4895 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4896};
4897
Roland Levillaindff1f282014-11-05 14:15:05 +00004898class HTypeConversion : public HExpression<1> {
4899 public:
4900 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004901 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 : HExpression(result_type,
4903 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4904 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004905 SetRawInputAt(0, input);
4906 DCHECK_NE(input->GetType(), result_type);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004907 // Invariant: We should never generate a conversion to a Boolean value.
4908 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004909 }
4910
4911 HInstruction* GetInput() const { return InputAt(0); }
4912 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4913 Primitive::Type GetResultType() const { return GetType(); }
4914
4915 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004916 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004917
Mark Mendelle82549b2015-05-06 10:55:34 -04004918 // Try to statically evaluate the conversion and return a HConstant
4919 // containing the result. If the input cannot be converted, return nullptr.
4920 HConstant* TryStaticEvaluation() const;
4921
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004922 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4923 Primitive::Type result_type) {
4924 // Some architectures may not require the 'GC' side effects, but at this point
4925 // in the compilation process we do not know what architecture we will
4926 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004927 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4928 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004929 return SideEffects::CanTriggerGC();
4930 }
4931 return SideEffects::None();
4932 }
4933
Roland Levillaindff1f282014-11-05 14:15:05 +00004934 DECLARE_INSTRUCTION(TypeConversion);
4935
4936 private:
4937 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4938};
4939
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004940static constexpr uint32_t kNoRegNumber = -1;
4941
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004942class HPhi : public HInstruction {
4943 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004944 HPhi(ArenaAllocator* arena,
4945 uint32_t reg_number,
4946 size_t number_of_inputs,
4947 Primitive::Type type,
4948 uint32_t dex_pc = kNoDexPc)
4949 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004950 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004951 reg_number_(reg_number) {
4952 SetPackedField<TypeField>(ToPhiType(type));
4953 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4954 // Phis are constructed live and marked dead if conflicting or unused.
4955 // Individual steps of SsaBuilder should assume that if a phi has been
4956 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4957 SetPackedFlag<kFlagIsLive>(true);
4958 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004959 }
4960
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004961 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4962 static Primitive::Type ToPhiType(Primitive::Type type) {
4963 switch (type) {
4964 case Primitive::kPrimBoolean:
4965 case Primitive::kPrimByte:
4966 case Primitive::kPrimShort:
4967 case Primitive::kPrimChar:
4968 return Primitive::kPrimInt;
4969 default:
4970 return type;
4971 }
4972 }
4973
David Brazdilffee3d32015-07-06 11:48:53 +01004974 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4975
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004976 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004977
4978 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004979 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004980
Vladimir Markoa1de9182016-02-25 11:37:38 +00004981 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004982 void SetType(Primitive::Type new_type) {
4983 // Make sure that only valid type changes occur. The following are allowed:
4984 // (1) int -> float/ref (primitive type propagation),
4985 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004986 DCHECK(GetType() == new_type ||
4987 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4988 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4989 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4990 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004991 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004992
Vladimir Markoa1de9182016-02-25 11:37:38 +00004993 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4994 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004995
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004996 uint32_t GetRegNumber() const { return reg_number_; }
4997
Vladimir Markoa1de9182016-02-25 11:37:38 +00004998 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4999 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
5000 bool IsDead() const { return !IsLive(); }
5001 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01005002
David Brazdil77a48ae2015-09-15 12:34:04 +00005003 bool IsVRegEquivalentOf(HInstruction* other) const {
5004 return other != nullptr
5005 && other->IsPhi()
5006 && other->AsPhi()->GetBlock() == GetBlock()
5007 && other->AsPhi()->GetRegNumber() == GetRegNumber();
5008 }
5009
Calin Juravlea4f88312015-04-16 12:57:19 +01005010 // Returns the next equivalent phi (starting from the current one) or null if there is none.
5011 // An equivalent phi is a phi having the same dex register and type.
5012 // It assumes that phis with the same dex register are adjacent.
5013 HPhi* GetNextEquivalentPhiWithSameType() {
5014 HInstruction* next = GetNext();
5015 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
5016 if (next->GetType() == GetType()) {
5017 return next->AsPhi();
5018 }
5019 next = next->GetNext();
5020 }
5021 return nullptr;
5022 }
5023
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005024 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005025
David Brazdil1abb4192015-02-17 18:33:36 +00005026 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005027 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005028 return inputs_[index];
5029 }
David Brazdil1abb4192015-02-17 18:33:36 +00005030
5031 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005032 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00005033 }
5034
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005035 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005036 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
5037 static constexpr size_t kFieldTypeSize =
5038 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5039 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
5040 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
5041 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
5042 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5043 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
5044
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005045 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005046 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005047
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005048 DISALLOW_COPY_AND_ASSIGN(HPhi);
5049};
5050
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005051class HNullCheck : public HExpression<1> {
5052 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005053 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5054 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005056 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057 SetRawInputAt(0, value);
5058 }
5059
Calin Juravle10e244f2015-01-26 18:54:32 +00005060 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005061 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005062 return true;
5063 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005064
Calin Juravle10e244f2015-01-26 18:54:32 +00005065 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066
Calin Juravle10e244f2015-01-26 18:54:32 +00005067 bool CanThrow() const OVERRIDE { return true; }
5068
5069 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005070
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005071
5072 DECLARE_INSTRUCTION(NullCheck);
5073
5074 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005075 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5076};
5077
5078class FieldInfo : public ValueObject {
5079 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005080 FieldInfo(MemberOffset field_offset,
5081 Primitive::Type field_type,
5082 bool is_volatile,
5083 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005084 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005085 const DexFile& dex_file,
5086 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005087 : field_offset_(field_offset),
5088 field_type_(field_type),
5089 is_volatile_(is_volatile),
5090 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005091 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005092 dex_file_(dex_file),
5093 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005094
5095 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005096 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005097 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005098 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005099 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005100 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005101 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102
5103 private:
5104 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005105 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005106 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005107 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005108 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005109 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005110 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111};
5112
5113class HInstanceFieldGet : public HExpression<1> {
5114 public:
5115 HInstanceFieldGet(HInstruction* value,
5116 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005117 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005118 bool is_volatile,
5119 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005120 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005121 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005122 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005123 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005124 : HExpression(field_type,
5125 SideEffects::FieldReadOfType(field_type, is_volatile),
5126 dex_pc),
5127 field_info_(field_offset,
5128 field_type,
5129 is_volatile,
5130 field_idx,
5131 declaring_class_def_index,
5132 dex_file,
5133 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005134 SetRawInputAt(0, value);
5135 }
5136
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005137 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005138
5139 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5140 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5141 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005142 }
5143
Calin Juravle641547a2015-04-21 22:08:51 +01005144 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5145 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005146 }
5147
5148 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005149 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5150 }
5151
Calin Juravle52c48962014-12-16 17:02:57 +00005152 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005153 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005154 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005155 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005156
5157 DECLARE_INSTRUCTION(InstanceFieldGet);
5158
5159 private:
5160 const FieldInfo field_info_;
5161
5162 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5163};
5164
5165class HInstanceFieldSet : public HTemplateInstruction<2> {
5166 public:
5167 HInstanceFieldSet(HInstruction* object,
5168 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005169 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005170 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005171 bool is_volatile,
5172 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005173 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005174 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005175 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005176 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005177 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5178 dex_pc),
5179 field_info_(field_offset,
5180 field_type,
5181 is_volatile,
5182 field_idx,
5183 declaring_class_def_index,
5184 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005185 dex_cache) {
5186 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187 SetRawInputAt(0, object);
5188 SetRawInputAt(1, value);
5189 }
5190
Calin Juravle641547a2015-04-21 22:08:51 +01005191 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5192 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005193 }
5194
Calin Juravle52c48962014-12-16 17:02:57 +00005195 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005196 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005197 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005198 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005199 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005200 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5201 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005202
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005203 DECLARE_INSTRUCTION(InstanceFieldSet);
5204
5205 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005206 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5207 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5208 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5209 "Too many packed fields.");
5210
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005211 const FieldInfo field_info_;
5212
5213 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5214};
5215
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216class HArrayGet : public HExpression<2> {
5217 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005218 HArrayGet(HInstruction* array,
5219 HInstruction* index,
5220 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005221 uint32_t dex_pc,
5222 SideEffects additional_side_effects = SideEffects::None())
5223 : HExpression(type,
5224 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005225 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226 SetRawInputAt(0, array);
5227 SetRawInputAt(1, index);
5228 }
5229
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005230 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005231 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005232 return true;
5233 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005234 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005235 // TODO: We can be smarter here.
5236 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5237 // which generates the implicit null check. There are cases when these can be removed
5238 // to produce better code. If we ever add optimizations to do so we should allow an
5239 // implicit check here (as long as the address falls in the first page).
5240 return false;
5241 }
5242
David Brazdil4833f5a2015-12-16 10:37:39 +00005243 bool IsEquivalentOf(HArrayGet* other) const {
5244 bool result = (GetDexPc() == other->GetDexPc());
5245 if (kIsDebugBuild && result) {
5246 DCHECK_EQ(GetBlock(), other->GetBlock());
5247 DCHECK_EQ(GetArray(), other->GetArray());
5248 DCHECK_EQ(GetIndex(), other->GetIndex());
5249 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005250 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005251 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005252 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5253 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005254 }
5255 }
5256 return result;
5257 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005258
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005259 HInstruction* GetArray() const { return InputAt(0); }
5260 HInstruction* GetIndex() const { return InputAt(1); }
5261
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262 DECLARE_INSTRUCTION(ArrayGet);
5263
5264 private:
5265 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5266};
5267
5268class HArraySet : public HTemplateInstruction<3> {
5269 public:
5270 HArraySet(HInstruction* array,
5271 HInstruction* index,
5272 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005273 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005274 uint32_t dex_pc,
5275 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005276 : HTemplateInstruction(
5277 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005278 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5279 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005280 dex_pc) {
5281 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5282 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5283 SetPackedFlag<kFlagValueCanBeNull>(true);
5284 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005285 SetRawInputAt(0, array);
5286 SetRawInputAt(1, index);
5287 SetRawInputAt(2, value);
5288 }
5289
Calin Juravle77520bc2015-01-12 18:45:46 +00005290 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005291 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005292 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 }
5294
Mingyao Yang81014cb2015-06-02 03:16:27 -07005295 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005296 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005297
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005298 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005299 // TODO: Same as for ArrayGet.
5300 return false;
5301 }
5302
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005303 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005304 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005305 }
5306
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005307 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005308 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005309 }
5310
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005312 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005313 }
5314
Vladimir Markoa1de9182016-02-25 11:37:38 +00005315 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5316 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5317 bool StaticTypeOfArrayIsObjectArray() const {
5318 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5319 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005320
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005321 HInstruction* GetArray() const { return InputAt(0); }
5322 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005323 HInstruction* GetValue() const { return InputAt(2); }
5324
5325 Primitive::Type GetComponentType() const {
5326 // The Dex format does not type floating point index operations. Since the
5327 // `expected_component_type_` is set during building and can therefore not
5328 // be correct, we also check what is the value type. If it is a floating
5329 // point type, we must use that type.
5330 Primitive::Type value_type = GetValue()->GetType();
5331 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5332 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005333 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005334 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005335
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005336 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005337 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005338 }
5339
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005340 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5341 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5342 }
5343
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344 DECLARE_INSTRUCTION(ArraySet);
5345
5346 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005347 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5348 static constexpr size_t kFieldExpectedComponentTypeSize =
5349 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5350 static constexpr size_t kFlagNeedsTypeCheck =
5351 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5352 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005353 // Cached information for the reference_type_info_ so that codegen
5354 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005355 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5356 static constexpr size_t kNumberOfArraySetPackedBits =
5357 kFlagStaticTypeOfArrayIsObjectArray + 1;
5358 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5359 using ExpectedComponentTypeField =
5360 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361
5362 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5363};
5364
5365class HArrayLength : public HExpression<1> {
5366 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005367 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005368 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005369 // Note that arrays do not change length, so the instruction does not
5370 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371 SetRawInputAt(0, array);
5372 }
5373
Calin Juravle77520bc2015-01-12 18:45:46 +00005374 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005375 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005376 return true;
5377 }
Calin Juravle641547a2015-04-21 22:08:51 +01005378 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5379 return obj == InputAt(0);
5380 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005381
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005382 DECLARE_INSTRUCTION(ArrayLength);
5383
5384 private:
5385 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5386};
5387
5388class HBoundsCheck : public HExpression<2> {
5389 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005390 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5391 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005393 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 DCHECK(index->GetType() == Primitive::kPrimInt);
5395 SetRawInputAt(0, index);
5396 SetRawInputAt(1, length);
5397 }
5398
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005399 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005400 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005401 return true;
5402 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005404 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005406 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005407
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005408 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005409
5410 DECLARE_INSTRUCTION(BoundsCheck);
5411
5412 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5414};
5415
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005416class HSuspendCheck : public HTemplateInstruction<0> {
5417 public:
5418 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005419 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005420
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005421 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005422 return true;
5423 }
5424
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005425 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5426 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005427
5428 DECLARE_INSTRUCTION(SuspendCheck);
5429
5430 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005431 // Only used for code generation, in order to share the same slow path between back edges
5432 // of a same loop.
5433 SlowPathCode* slow_path_;
5434
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005435 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5436};
5437
David Srbecky0cf44932015-12-09 14:09:59 +00005438// Pseudo-instruction which provides the native debugger with mapping information.
5439// It ensures that we can generate line number and local variables at this point.
5440class HNativeDebugInfo : public HTemplateInstruction<0> {
5441 public:
5442 explicit HNativeDebugInfo(uint32_t dex_pc)
5443 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5444
5445 bool NeedsEnvironment() const OVERRIDE {
5446 return true;
5447 }
5448
5449 DECLARE_INSTRUCTION(NativeDebugInfo);
5450
5451 private:
5452 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5453};
5454
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005455/**
5456 * Instruction to load a Class object.
5457 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005458class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005459 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005460 HLoadClass(HCurrentMethod* current_method,
5461 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005462 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005463 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005464 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005465 bool needs_access_check,
5466 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005467 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005468 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005469 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005470 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005471 // Referrers class should not need access check. We never inline unverified
5472 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005473 DCHECK(!is_referrers_class || !needs_access_check);
5474
5475 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5476 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5477 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5478 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005479 SetRawInputAt(0, current_method);
5480 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005481
5482 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005483
5484 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005485 // Note that we don't need to test for generate_clinit_check_.
5486 // Whether or not we need to generate the clinit check is processed in
5487 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005488 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005489 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005490 }
5491
5492 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5493
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005494 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005495 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005496
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005497 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005498 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005499 }
5500
Calin Juravle0ba218d2015-05-19 18:46:01 +01005501 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005502 // The entrypoint the code generator is going to call does not do
5503 // clinit of the class.
5504 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005505 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005506 }
5507
5508 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005509 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005510 (!IsReferrersClass() && !IsInDexCache()) ||
5511 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005512 }
5513
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005514
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005515 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005516 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005517 }
5518
Calin Juravleacf735c2015-02-12 15:25:22 +00005519 ReferenceTypeInfo GetLoadedClassRTI() {
5520 return loaded_class_rti_;
5521 }
5522
5523 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5524 // Make sure we only set exact types (the loaded class should never be merged).
5525 DCHECK(rti.IsExact());
5526 loaded_class_rti_ = rti;
5527 }
5528
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005529 const DexFile& GetDexFile() { return dex_file_; }
5530
Vladimir Markoa1de9182016-02-25 11:37:38 +00005531 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005532
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005533 static SideEffects SideEffectsForArchRuntimeCalls() {
5534 return SideEffects::CanTriggerGC();
5535 }
5536
Vladimir Markoa1de9182016-02-25 11:37:38 +00005537 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5538 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5539 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5540 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005541
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005542 DECLARE_INSTRUCTION(LoadClass);
5543
5544 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005545 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5546 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5547 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005548 // Whether this instruction must generate the initialization check.
5549 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005550 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5551 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5552 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5553
5554 const uint16_t type_index_;
5555 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005556
Calin Juravleacf735c2015-02-12 15:25:22 +00005557 ReferenceTypeInfo loaded_class_rti_;
5558
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005559 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5560};
5561
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005562class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005563 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005564 // Determines how to load the String.
5565 enum class LoadKind {
5566 // Use boot image String* address that will be known at link time.
5567 // Used for boot image strings referenced by boot image code in non-PIC mode.
5568 kBootImageLinkTimeAddress,
5569
5570 // Use PC-relative boot image String* address that will be known at link time.
5571 // Used for boot image strings referenced by boot image code in PIC mode.
5572 kBootImageLinkTimePcRelative,
5573
5574 // Use a known boot image String* address, embedded in the code by the codegen.
5575 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5576 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5577 // GetIncludePatchInformation().
5578 kBootImageAddress,
5579
5580 // Load from the resolved strings array at an absolute address.
5581 // Used for strings outside the boot image referenced by JIT-compiled code.
5582 kDexCacheAddress,
5583
5584 // Load from resolved strings array in the dex cache using a PC-relative load.
5585 // Used for strings outside boot image when we know that we can access
5586 // the dex cache arrays using a PC-relative load.
5587 kDexCachePcRelative,
5588
5589 // Load from resolved strings array accessed through the class loaded from
5590 // the compiled method's own ArtMethod*. This is the default access type when
5591 // all other types are unavailable.
5592 kDexCacheViaMethod,
5593
5594 kLast = kDexCacheViaMethod
5595 };
5596
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005597 HLoadString(HCurrentMethod* current_method,
5598 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005599 const DexFile& dex_file,
5600 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005601 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005602 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005603 SetPackedFlag<kFlagIsInDexCache>(false);
5604 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5605 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005606 SetRawInputAt(0, current_method);
5607 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005608
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005609 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5610 DCHECK(HasAddress(load_kind));
5611 load_data_.address = address;
5612 SetLoadKindInternal(load_kind);
5613 }
5614
5615 void SetLoadKindWithStringReference(LoadKind load_kind,
5616 const DexFile& dex_file,
5617 uint32_t string_index) {
5618 DCHECK(HasStringReference(load_kind));
5619 load_data_.ref.dex_file = &dex_file;
5620 string_index_ = string_index;
5621 SetLoadKindInternal(load_kind);
5622 }
5623
5624 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5625 const DexFile& dex_file,
5626 uint32_t element_index) {
5627 DCHECK(HasDexCacheReference(load_kind));
5628 load_data_.ref.dex_file = &dex_file;
5629 load_data_.ref.dex_cache_element_index = element_index;
5630 SetLoadKindInternal(load_kind);
5631 }
5632
5633 LoadKind GetLoadKind() const {
5634 return GetPackedField<LoadKindField>();
5635 }
5636
5637 const DexFile& GetDexFile() const;
5638
5639 uint32_t GetStringIndex() const {
5640 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5641 return string_index_;
5642 }
5643
5644 uint32_t GetDexCacheElementOffset() const;
5645
5646 uint64_t GetAddress() const {
5647 DCHECK(HasAddress(GetLoadKind()));
5648 return load_data_.address;
5649 }
5650
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005651 bool CanBeMoved() const OVERRIDE { return true; }
5652
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005653 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005654
5655 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5656
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005657 // Will call the runtime if we need to load the string through
5658 // the dex cache and the string is not guaranteed to be there yet.
5659 bool NeedsEnvironment() const OVERRIDE {
5660 LoadKind load_kind = GetLoadKind();
5661 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5662 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5663 load_kind == LoadKind::kBootImageAddress) {
5664 return false;
5665 }
5666 return !IsInDexCache();
5667 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005668
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005669 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5670 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5671 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005672
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005673 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005674 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005675
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005676 static SideEffects SideEffectsForArchRuntimeCalls() {
5677 return SideEffects::CanTriggerGC();
5678 }
5679
Vladimir Markoa1de9182016-02-25 11:37:38 +00005680 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5681
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005682 void MarkInDexCache() {
5683 SetPackedFlag<kFlagIsInDexCache>(true);
5684 DCHECK(!NeedsEnvironment());
5685 RemoveEnvironment();
5686 }
5687
5688 size_t InputCount() const OVERRIDE {
5689 return (InputAt(0) != nullptr) ? 1u : 0u;
5690 }
5691
5692 void AddSpecialInput(HInstruction* special_input);
5693
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005694 DECLARE_INSTRUCTION(LoadString);
5695
5696 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005697 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005698 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5699 static constexpr size_t kFieldLoadKindSize =
5700 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5701 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005702 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005703 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005704
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005705 static bool HasStringReference(LoadKind load_kind) {
5706 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5707 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5708 load_kind == LoadKind::kDexCacheViaMethod;
5709 }
5710
5711 static bool HasAddress(LoadKind load_kind) {
5712 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5713 }
5714
5715 static bool HasDexCacheReference(LoadKind load_kind) {
5716 return load_kind == LoadKind::kDexCachePcRelative;
5717 }
5718
5719 void SetLoadKindInternal(LoadKind load_kind);
5720
5721 // String index serves also as the hash code and it's also needed for slow-paths,
5722 // so it must not be overwritten with other load data.
5723 uint32_t string_index_;
5724
5725 union {
5726 struct {
5727 const DexFile* dex_file; // For string reference and dex cache reference.
5728 uint32_t dex_cache_element_index; // Only for dex cache reference.
5729 } ref;
5730 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5731 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005732
5733 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5734};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005735std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5736
5737// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5738inline const DexFile& HLoadString::GetDexFile() const {
5739 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5740 << GetLoadKind();
5741 return *load_data_.ref.dex_file;
5742}
5743
5744// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5745inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5746 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5747 return load_data_.ref.dex_cache_element_index;
5748}
5749
5750// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5751inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5752 // The special input is used for PC-relative loads on some architectures.
5753 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5754 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5755 DCHECK(InputAt(0) == nullptr);
5756 SetRawInputAt(0u, special_input);
5757 special_input->AddUseAt(this, 0);
5758}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005759
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005760/**
5761 * Performs an initialization check on its Class object input.
5762 */
5763class HClinitCheck : public HExpression<1> {
5764 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005765 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005766 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005767 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005768 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5769 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005770 SetRawInputAt(0, constant);
5771 }
5772
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005773 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005774 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005775 return true;
5776 }
5777
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005778 bool NeedsEnvironment() const OVERRIDE {
5779 // May call runtime to initialize the class.
5780 return true;
5781 }
5782
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005783 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005784
5785 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5786
5787 DECLARE_INSTRUCTION(ClinitCheck);
5788
5789 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005790 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5791};
5792
5793class HStaticFieldGet : public HExpression<1> {
5794 public:
5795 HStaticFieldGet(HInstruction* cls,
5796 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005797 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005798 bool is_volatile,
5799 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005800 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005801 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005802 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005803 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005804 : HExpression(field_type,
5805 SideEffects::FieldReadOfType(field_type, is_volatile),
5806 dex_pc),
5807 field_info_(field_offset,
5808 field_type,
5809 is_volatile,
5810 field_idx,
5811 declaring_class_def_index,
5812 dex_file,
5813 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005814 SetRawInputAt(0, cls);
5815 }
5816
Calin Juravle52c48962014-12-16 17:02:57 +00005817
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005818 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005819
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005820 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005821 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5822 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005823 }
5824
5825 size_t ComputeHashCode() const OVERRIDE {
5826 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5827 }
5828
Calin Juravle52c48962014-12-16 17:02:57 +00005829 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005830 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5831 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005832 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005833
5834 DECLARE_INSTRUCTION(StaticFieldGet);
5835
5836 private:
5837 const FieldInfo field_info_;
5838
5839 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5840};
5841
5842class HStaticFieldSet : public HTemplateInstruction<2> {
5843 public:
5844 HStaticFieldSet(HInstruction* cls,
5845 HInstruction* value,
5846 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005847 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005848 bool is_volatile,
5849 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005850 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005851 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005852 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005853 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005854 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5855 dex_pc),
5856 field_info_(field_offset,
5857 field_type,
5858 is_volatile,
5859 field_idx,
5860 declaring_class_def_index,
5861 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005862 dex_cache) {
5863 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005864 SetRawInputAt(0, cls);
5865 SetRawInputAt(1, value);
5866 }
5867
Calin Juravle52c48962014-12-16 17:02:57 +00005868 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005869 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5870 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005871 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005872
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005873 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005874 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5875 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005876
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005877 DECLARE_INSTRUCTION(StaticFieldSet);
5878
5879 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005880 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5881 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5882 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5883 "Too many packed fields.");
5884
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005885 const FieldInfo field_info_;
5886
5887 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5888};
5889
Calin Juravlee460d1d2015-09-29 04:52:17 +01005890class HUnresolvedInstanceFieldGet : public HExpression<1> {
5891 public:
5892 HUnresolvedInstanceFieldGet(HInstruction* obj,
5893 Primitive::Type field_type,
5894 uint32_t field_index,
5895 uint32_t dex_pc)
5896 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5897 field_index_(field_index) {
5898 SetRawInputAt(0, obj);
5899 }
5900
5901 bool NeedsEnvironment() const OVERRIDE { return true; }
5902 bool CanThrow() const OVERRIDE { return true; }
5903
5904 Primitive::Type GetFieldType() const { return GetType(); }
5905 uint32_t GetFieldIndex() const { return field_index_; }
5906
5907 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5908
5909 private:
5910 const uint32_t field_index_;
5911
5912 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5913};
5914
5915class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5916 public:
5917 HUnresolvedInstanceFieldSet(HInstruction* obj,
5918 HInstruction* value,
5919 Primitive::Type field_type,
5920 uint32_t field_index,
5921 uint32_t dex_pc)
5922 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005923 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005924 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005925 DCHECK_EQ(field_type, value->GetType());
5926 SetRawInputAt(0, obj);
5927 SetRawInputAt(1, value);
5928 }
5929
5930 bool NeedsEnvironment() const OVERRIDE { return true; }
5931 bool CanThrow() const OVERRIDE { return true; }
5932
Vladimir Markoa1de9182016-02-25 11:37:38 +00005933 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005934 uint32_t GetFieldIndex() const { return field_index_; }
5935
5936 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5937
5938 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005939 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5940 static constexpr size_t kFieldFieldTypeSize =
5941 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5942 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5943 kFieldFieldType + kFieldFieldTypeSize;
5944 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5945 "Too many packed fields.");
5946 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5947
Calin Juravlee460d1d2015-09-29 04:52:17 +01005948 const uint32_t field_index_;
5949
5950 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5951};
5952
5953class HUnresolvedStaticFieldGet : public HExpression<0> {
5954 public:
5955 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5956 uint32_t field_index,
5957 uint32_t dex_pc)
5958 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5959 field_index_(field_index) {
5960 }
5961
5962 bool NeedsEnvironment() const OVERRIDE { return true; }
5963 bool CanThrow() const OVERRIDE { return true; }
5964
5965 Primitive::Type GetFieldType() const { return GetType(); }
5966 uint32_t GetFieldIndex() const { return field_index_; }
5967
5968 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5969
5970 private:
5971 const uint32_t field_index_;
5972
5973 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5974};
5975
5976class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5977 public:
5978 HUnresolvedStaticFieldSet(HInstruction* value,
5979 Primitive::Type field_type,
5980 uint32_t field_index,
5981 uint32_t dex_pc)
5982 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005983 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005984 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005985 DCHECK_EQ(field_type, value->GetType());
5986 SetRawInputAt(0, value);
5987 }
5988
5989 bool NeedsEnvironment() const OVERRIDE { return true; }
5990 bool CanThrow() const OVERRIDE { return true; }
5991
Vladimir Markoa1de9182016-02-25 11:37:38 +00005992 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005993 uint32_t GetFieldIndex() const { return field_index_; }
5994
5995 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5996
5997 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005998 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5999 static constexpr size_t kFieldFieldTypeSize =
6000 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6001 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6002 kFieldFieldType + kFieldFieldTypeSize;
6003 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6004 "Too many packed fields.");
6005 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6006
Calin Juravlee460d1d2015-09-29 04:52:17 +01006007 const uint32_t field_index_;
6008
6009 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6010};
6011
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006012// Implement the move-exception DEX instruction.
6013class HLoadException : public HExpression<0> {
6014 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006015 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6016 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006017
David Brazdilbbd733e2015-08-18 17:48:17 +01006018 bool CanBeNull() const OVERRIDE { return false; }
6019
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006020 DECLARE_INSTRUCTION(LoadException);
6021
6022 private:
6023 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6024};
6025
David Brazdilcb1c0552015-08-04 16:22:25 +01006026// Implicit part of move-exception which clears thread-local exception storage.
6027// Must not be removed because the runtime expects the TLS to get cleared.
6028class HClearException : public HTemplateInstruction<0> {
6029 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006030 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6031 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006032
6033 DECLARE_INSTRUCTION(ClearException);
6034
6035 private:
6036 DISALLOW_COPY_AND_ASSIGN(HClearException);
6037};
6038
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006039class HThrow : public HTemplateInstruction<1> {
6040 public:
6041 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006042 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006043 SetRawInputAt(0, exception);
6044 }
6045
6046 bool IsControlFlow() const OVERRIDE { return true; }
6047
6048 bool NeedsEnvironment() const OVERRIDE { return true; }
6049
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006050 bool CanThrow() const OVERRIDE { return true; }
6051
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006052
6053 DECLARE_INSTRUCTION(Throw);
6054
6055 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006056 DISALLOW_COPY_AND_ASSIGN(HThrow);
6057};
6058
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006059/**
6060 * Implementation strategies for the code generator of a HInstanceOf
6061 * or `HCheckCast`.
6062 */
6063enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006064 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006065 kExactCheck, // Can do a single class compare.
6066 kClassHierarchyCheck, // Can just walk the super class chain.
6067 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6068 kInterfaceCheck, // No optimization yet when checking against an interface.
6069 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 kArrayCheck, // No optimization yet when checking against a generic array.
6071 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006072};
6073
Roland Levillain86503782016-02-11 19:07:30 +00006074std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6075
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006076class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006077 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006078 HInstanceOf(HInstruction* object,
6079 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006081 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006082 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006083 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006084 dex_pc) {
6085 SetPackedField<TypeCheckKindField>(check_kind);
6086 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006087 SetRawInputAt(0, object);
6088 SetRawInputAt(1, constant);
6089 }
6090
6091 bool CanBeMoved() const OVERRIDE { return true; }
6092
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006093 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006094 return true;
6095 }
6096
6097 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006098 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006099 }
6100
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006101 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006102 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6103 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6104 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6105 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006106
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006107 static bool CanCallRuntime(TypeCheckKind check_kind) {
6108 // Mips currently does runtime calls for any other checks.
6109 return check_kind != TypeCheckKind::kExactCheck;
6110 }
6111
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006112 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006113 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006114 }
6115
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006116 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006117
6118 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006119 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6120 static constexpr size_t kFieldTypeCheckKindSize =
6121 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6122 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6123 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6124 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6125 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006126
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006127 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6128};
6129
Calin Juravleb1498f62015-02-16 13:13:29 +00006130class HBoundType : public HExpression<1> {
6131 public:
David Brazdilf5552582015-12-27 13:36:12 +00006132 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006133 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6135 SetPackedFlag<kFlagUpperCanBeNull>(true);
6136 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006137 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006138 SetRawInputAt(0, input);
6139 }
6140
David Brazdilf5552582015-12-27 13:36:12 +00006141 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006142 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006143 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006144 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006145
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006146 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006147 DCHECK(GetUpperCanBeNull() || !can_be_null);
6148 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006149 }
6150
Vladimir Markoa1de9182016-02-25 11:37:38 +00006151 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006152
Calin Juravleb1498f62015-02-16 13:13:29 +00006153 DECLARE_INSTRUCTION(BoundType);
6154
6155 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006156 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6157 // is false then CanBeNull() cannot be true).
6158 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6159 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6160 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6161 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6162
Calin Juravleb1498f62015-02-16 13:13:29 +00006163 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006164 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6165 // It is used to bound the type in cases like:
6166 // if (x instanceof ClassX) {
6167 // // uper_bound_ will be ClassX
6168 // }
David Brazdilf5552582015-12-27 13:36:12 +00006169 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006170
6171 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6172};
6173
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006174class HCheckCast : public HTemplateInstruction<2> {
6175 public:
6176 HCheckCast(HInstruction* object,
6177 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006178 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006179 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006180 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6181 SetPackedField<TypeCheckKindField>(check_kind);
6182 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006183 SetRawInputAt(0, object);
6184 SetRawInputAt(1, constant);
6185 }
6186
6187 bool CanBeMoved() const OVERRIDE { return true; }
6188
6189 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6190 return true;
6191 }
6192
6193 bool NeedsEnvironment() const OVERRIDE {
6194 // Instruction may throw a CheckCastError.
6195 return true;
6196 }
6197
6198 bool CanThrow() const OVERRIDE { return true; }
6199
Vladimir Markoa1de9182016-02-25 11:37:38 +00006200 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6201 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6202 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6203 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006204
6205 DECLARE_INSTRUCTION(CheckCast);
6206
6207 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006208 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6209 static constexpr size_t kFieldTypeCheckKindSize =
6210 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6211 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6212 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6213 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6214 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006215
6216 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006217};
6218
Calin Juravle27df7582015-04-17 19:12:31 +01006219class HMemoryBarrier : public HTemplateInstruction<0> {
6220 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006221 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006222 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006223 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6224 SetPackedField<BarrierKindField>(barrier_kind);
6225 }
Calin Juravle27df7582015-04-17 19:12:31 +01006226
Vladimir Markoa1de9182016-02-25 11:37:38 +00006227 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006228
6229 DECLARE_INSTRUCTION(MemoryBarrier);
6230
6231 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6233 static constexpr size_t kFieldBarrierKindSize =
6234 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6235 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6236 kFieldBarrierKind + kFieldBarrierKindSize;
6237 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6238 "Too many packed fields.");
6239 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006240
6241 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6242};
6243
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006244class HMonitorOperation : public HTemplateInstruction<1> {
6245 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006246 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006247 kEnter,
6248 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006249 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006250 };
6251
6252 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006253 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006254 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6255 dex_pc) {
6256 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006257 SetRawInputAt(0, object);
6258 }
6259
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006260 // Instruction may go into runtime, so we need an environment.
6261 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006262
6263 bool CanThrow() const OVERRIDE {
6264 // Verifier guarantees that monitor-exit cannot throw.
6265 // This is important because it allows the HGraphBuilder to remove
6266 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6267 return IsEnter();
6268 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006269
Vladimir Markoa1de9182016-02-25 11:37:38 +00006270 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6271 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006272
6273 DECLARE_INSTRUCTION(MonitorOperation);
6274
Calin Juravle52c48962014-12-16 17:02:57 +00006275 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6277 static constexpr size_t kFieldOperationKindSize =
6278 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6279 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6280 kFieldOperationKind + kFieldOperationKindSize;
6281 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6282 "Too many packed fields.");
6283 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006284
6285 private:
6286 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6287};
6288
David Brazdil74eb1b22015-12-14 11:44:01 +00006289class HSelect : public HExpression<3> {
6290 public:
6291 HSelect(HInstruction* condition,
6292 HInstruction* true_value,
6293 HInstruction* false_value,
6294 uint32_t dex_pc)
6295 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6296 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6297
6298 // First input must be `true_value` or `false_value` to allow codegens to
6299 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6300 // that architectures which implement HSelect as a conditional move also
6301 // will not need to invert the condition.
6302 SetRawInputAt(0, false_value);
6303 SetRawInputAt(1, true_value);
6304 SetRawInputAt(2, condition);
6305 }
6306
6307 HInstruction* GetFalseValue() const { return InputAt(0); }
6308 HInstruction* GetTrueValue() const { return InputAt(1); }
6309 HInstruction* GetCondition() const { return InputAt(2); }
6310
6311 bool CanBeMoved() const OVERRIDE { return true; }
6312 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6313
6314 bool CanBeNull() const OVERRIDE {
6315 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6316 }
6317
6318 DECLARE_INSTRUCTION(Select);
6319
6320 private:
6321 DISALLOW_COPY_AND_ASSIGN(HSelect);
6322};
6323
Vladimir Markof9f64412015-09-02 14:05:49 +01006324class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006325 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006326 MoveOperands(Location source,
6327 Location destination,
6328 Primitive::Type type,
6329 HInstruction* instruction)
6330 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006331
6332 Location GetSource() const { return source_; }
6333 Location GetDestination() const { return destination_; }
6334
6335 void SetSource(Location value) { source_ = value; }
6336 void SetDestination(Location value) { destination_ = value; }
6337
6338 // The parallel move resolver marks moves as "in-progress" by clearing the
6339 // destination (but not the source).
6340 Location MarkPending() {
6341 DCHECK(!IsPending());
6342 Location dest = destination_;
6343 destination_ = Location::NoLocation();
6344 return dest;
6345 }
6346
6347 void ClearPending(Location dest) {
6348 DCHECK(IsPending());
6349 destination_ = dest;
6350 }
6351
6352 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006353 DCHECK(source_.IsValid() || destination_.IsInvalid());
6354 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006355 }
6356
6357 // True if this blocks a move from the given location.
6358 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006359 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006360 }
6361
6362 // A move is redundant if it's been eliminated, if its source and
6363 // destination are the same, or if its destination is unneeded.
6364 bool IsRedundant() const {
6365 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6366 }
6367
6368 // We clear both operands to indicate move that's been eliminated.
6369 void Eliminate() {
6370 source_ = destination_ = Location::NoLocation();
6371 }
6372
6373 bool IsEliminated() const {
6374 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6375 return source_.IsInvalid();
6376 }
6377
Alexey Frunze4dda3372015-06-01 18:31:49 -07006378 Primitive::Type GetType() const { return type_; }
6379
Nicolas Geoffray90218252015-04-15 11:56:51 +01006380 bool Is64BitMove() const {
6381 return Primitive::Is64BitType(type_);
6382 }
6383
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006384 HInstruction* GetInstruction() const { return instruction_; }
6385
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006386 private:
6387 Location source_;
6388 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006389 // The type this move is for.
6390 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006391 // The instruction this move is assocatied with. Null when this move is
6392 // for moving an input in the expected locations of user (including a phi user).
6393 // This is only used in debug mode, to ensure we do not connect interval siblings
6394 // in the same parallel move.
6395 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006396};
6397
Roland Levillainc9285912015-12-18 10:38:42 +00006398std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6399
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006400static constexpr size_t kDefaultNumberOfMoves = 4;
6401
6402class HParallelMove : public HTemplateInstruction<0> {
6403 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006404 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006405 : HTemplateInstruction(SideEffects::None(), dex_pc),
6406 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6407 moves_.reserve(kDefaultNumberOfMoves);
6408 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006409
Nicolas Geoffray90218252015-04-15 11:56:51 +01006410 void AddMove(Location source,
6411 Location destination,
6412 Primitive::Type type,
6413 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006414 DCHECK(source.IsValid());
6415 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006416 if (kIsDebugBuild) {
6417 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006418 for (const MoveOperands& move : moves_) {
6419 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006420 // Special case the situation where the move is for the spill slot
6421 // of the instruction.
6422 if ((GetPrevious() == instruction)
6423 || ((GetPrevious() == nullptr)
6424 && instruction->IsPhi()
6425 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006426 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006427 << "Doing parallel moves for the same instruction.";
6428 } else {
6429 DCHECK(false) << "Doing parallel moves for the same instruction.";
6430 }
6431 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006432 }
6433 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006434 for (const MoveOperands& move : moves_) {
6435 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006436 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006437 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006438 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006439 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006440 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006441 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006442 }
6443
Vladimir Marko225b6462015-09-28 12:17:40 +01006444 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006445 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006446 }
6447
Vladimir Marko225b6462015-09-28 12:17:40 +01006448 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006449
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006450 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006451
6452 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006453 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006454
6455 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6456};
6457
Mark Mendell0616ae02015-04-17 12:49:27 -04006458} // namespace art
6459
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006460#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6461#include "nodes_shared.h"
6462#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006463#ifdef ART_ENABLE_CODEGEN_arm
6464#include "nodes_arm.h"
6465#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006466#ifdef ART_ENABLE_CODEGEN_arm64
6467#include "nodes_arm64.h"
6468#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006469#ifdef ART_ENABLE_CODEGEN_x86
6470#include "nodes_x86.h"
6471#endif
6472
6473namespace art {
6474
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006475class HGraphVisitor : public ValueObject {
6476 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006477 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6478 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006479
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006480 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006481 virtual void VisitBasicBlock(HBasicBlock* block);
6482
Roland Levillain633021e2014-10-01 14:12:25 +01006483 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006484 void VisitInsertionOrder();
6485
Roland Levillain633021e2014-10-01 14:12:25 +01006486 // Visit the graph following dominator tree reverse post-order.
6487 void VisitReversePostOrder();
6488
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006489 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006490
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006491 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006492#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006493 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6494
6495 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6496
6497#undef DECLARE_VISIT_INSTRUCTION
6498
6499 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006500 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006501
6502 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6503};
6504
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006505class HGraphDelegateVisitor : public HGraphVisitor {
6506 public:
6507 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6508 virtual ~HGraphDelegateVisitor() {}
6509
6510 // Visit functions that delegate to to super class.
6511#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006512 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006513
6514 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6515
6516#undef DECLARE_VISIT_INSTRUCTION
6517
6518 private:
6519 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6520};
6521
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006522class HInsertionOrderIterator : public ValueObject {
6523 public:
6524 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6525
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006526 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006527 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006528 void Advance() { ++index_; }
6529
6530 private:
6531 const HGraph& graph_;
6532 size_t index_;
6533
6534 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6535};
6536
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006537class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006538 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006539 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6540 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006541 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006542 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006543
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006544 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6545 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006546 void Advance() { ++index_; }
6547
6548 private:
6549 const HGraph& graph_;
6550 size_t index_;
6551
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006552 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006553};
6554
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006555class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006556 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006557 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006558 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006559 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006560 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006561 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006562
6563 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006564 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006565 void Advance() { --index_; }
6566
6567 private:
6568 const HGraph& graph_;
6569 size_t index_;
6570
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006571 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006572};
6573
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006574class HLinearPostOrderIterator : public ValueObject {
6575 public:
6576 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006577 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006578
6579 bool Done() const { return index_ == 0; }
6580
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006581 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006582
6583 void Advance() {
6584 --index_;
6585 DCHECK_GE(index_, 0U);
6586 }
6587
6588 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006589 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006590 size_t index_;
6591
6592 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6593};
6594
6595class HLinearOrderIterator : public ValueObject {
6596 public:
6597 explicit HLinearOrderIterator(const HGraph& graph)
6598 : order_(graph.GetLinearOrder()), index_(0) {}
6599
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006600 bool Done() const { return index_ == order_.size(); }
6601 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006602 void Advance() { ++index_; }
6603
6604 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006605 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006606 size_t index_;
6607
6608 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6609};
6610
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006611// Iterator over the blocks that art part of the loop. Includes blocks part
6612// of an inner loop. The order in which the blocks are iterated is on their
6613// block id.
6614class HBlocksInLoopIterator : public ValueObject {
6615 public:
6616 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6617 : blocks_in_loop_(info.GetBlocks()),
6618 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6619 index_(0) {
6620 if (!blocks_in_loop_.IsBitSet(index_)) {
6621 Advance();
6622 }
6623 }
6624
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006625 bool Done() const { return index_ == blocks_.size(); }
6626 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006627 void Advance() {
6628 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006629 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006630 if (blocks_in_loop_.IsBitSet(index_)) {
6631 break;
6632 }
6633 }
6634 }
6635
6636 private:
6637 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006638 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006639 size_t index_;
6640
6641 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6642};
6643
Mingyao Yang3584bce2015-05-19 16:01:59 -07006644// Iterator over the blocks that art part of the loop. Includes blocks part
6645// of an inner loop. The order in which the blocks are iterated is reverse
6646// post order.
6647class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6648 public:
6649 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6650 : blocks_in_loop_(info.GetBlocks()),
6651 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6652 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006653 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006654 Advance();
6655 }
6656 }
6657
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006658 bool Done() const { return index_ == blocks_.size(); }
6659 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006660 void Advance() {
6661 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006662 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6663 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006664 break;
6665 }
6666 }
6667 }
6668
6669 private:
6670 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006671 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006672 size_t index_;
6673
6674 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6675};
6676
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006677inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006678 if (constant->IsIntConstant()) {
6679 return constant->AsIntConstant()->GetValue();
6680 } else if (constant->IsLongConstant()) {
6681 return constant->AsLongConstant()->GetValue();
6682 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006683 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006684 return 0;
6685 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006686}
6687
Vladimir Marko58155012015-08-19 12:49:41 +00006688inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6689 // For the purposes of the compiler, the dex files must actually be the same object
6690 // if we want to safely treat them as the same. This is especially important for JIT
6691 // as custom class loaders can open the same underlying file (or memory) multiple
6692 // times and provide different class resolution but no two class loaders should ever
6693 // use the same DexFile object - doing so is an unsupported hack that can lead to
6694 // all sorts of weird failures.
6695 return &lhs == &rhs;
6696}
6697
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006698#define INSTRUCTION_TYPE_CHECK(type, super) \
6699 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6700 inline const H##type* HInstruction::As##type() const { \
6701 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6702 } \
6703 inline H##type* HInstruction::As##type() { \
6704 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6705 }
6706
6707 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6708#undef INSTRUCTION_TYPE_CHECK
6709
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006710class SwitchTable : public ValueObject {
6711 public:
6712 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6713 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6714 int32_t table_offset = instruction.VRegB_31t();
6715 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6716 if (sparse) {
6717 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6718 } else {
6719 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6720 }
6721 num_entries_ = table[1];
6722 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6723 }
6724
6725 uint16_t GetNumEntries() const {
6726 return num_entries_;
6727 }
6728
6729 void CheckIndex(size_t index) const {
6730 if (sparse_) {
6731 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6732 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6733 } else {
6734 // In a packed table, we have the starting key and num_entries_ values.
6735 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6736 }
6737 }
6738
6739 int32_t GetEntryAt(size_t index) const {
6740 CheckIndex(index);
6741 return values_[index];
6742 }
6743
6744 uint32_t GetDexPcForIndex(size_t index) const {
6745 CheckIndex(index);
6746 return dex_pc_ +
6747 (reinterpret_cast<const int16_t*>(values_ + index) -
6748 reinterpret_cast<const int16_t*>(&instruction_));
6749 }
6750
6751 // Index of the first value in the table.
6752 size_t GetFirstValueIndex() const {
6753 if (sparse_) {
6754 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6755 return num_entries_;
6756 } else {
6757 // In a packed table, we have the starting key and num_entries_ values.
6758 return 1;
6759 }
6760 }
6761
6762 private:
6763 const Instruction& instruction_;
6764 const uint32_t dex_pc_;
6765
6766 // Whether this is a sparse-switch table (or a packed-switch one).
6767 const bool sparse_;
6768
6769 // This can't be const as it needs to be computed off of the given instruction, and complicated
6770 // expressions in the initializer list seemed very ugly.
6771 uint16_t num_entries_;
6772
6773 const int32_t* values_;
6774
6775 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6776};
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_