blob: 53da0691edef5d66be1723db05958ff7516acce3 [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
1998 // Set the environment of this instruction, copying it from `environment`. While
1999 // copying, the uses lists are being updated.
2000 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002001 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002002 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002003 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002004 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002005 if (environment->GetParent() != nullptr) {
2006 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2007 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002008 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002010 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2011 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002012 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002013 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002014 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002015 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002016 if (environment->GetParent() != nullptr) {
2017 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2018 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002019 }
2020
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 // Returns the number of entries in the environment. Typically, that is the
2022 // number of dex registers in a method. It could be more in case of inlining.
2023 size_t EnvironmentSize() const;
2024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002025 LocationSummary* GetLocations() const { return locations_; }
2026 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002027
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002028 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002029 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002030
Alexandre Rames188d4312015-04-09 18:30:21 +01002031 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2032 // uses of this instruction by `other` are *not* updated.
2033 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2034 ReplaceWith(other);
2035 other->ReplaceInput(this, use_index);
2036 }
2037
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002038 // Move `this` instruction before `cursor`.
2039 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002040
Vladimir Markofb337ea2015-11-25 15:25:10 +00002041 // Move `this` before its first user and out of any loops. If there is no
2042 // out-of-loop user that dominates all other users, move the instruction
2043 // to the end of the out-of-loop common dominator of the user's blocks.
2044 //
2045 // This can be used only on non-throwing instructions with no side effects that
2046 // have at least one use but no environment uses.
2047 void MoveBeforeFirstUserAndOutOfLoops();
2048
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002049#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002050 bool Is##type() const; \
2051 const H##type* As##type() const; \
2052 H##type* As##type();
2053
2054 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2055#undef INSTRUCTION_TYPE_CHECK
2056
2057#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002058 bool Is##type() const { return (As##type() != nullptr); } \
2059 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002060 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002061 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002062#undef INSTRUCTION_TYPE_CHECK
2063
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002064 // Returns whether the instruction can be moved within the graph.
2065 virtual bool CanBeMoved() const { return false; }
2066
2067 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002068 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002069 return false;
2070 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002071
2072 // Returns whether any data encoded in the two instructions is equal.
2073 // This method does not look at the inputs. Both instructions must be
2074 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002075 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002076 return false;
2077 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002078
2079 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002080 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002081 // 2) Their inputs are identical.
2082 bool Equals(HInstruction* other) const;
2083
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002084 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2085 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2086 // the virtual function because the __attribute__((__pure__)) doesn't really
2087 // apply the strong requirement for virtual functions, preventing optimizations.
2088 InstructionKind GetKind() const PURE;
2089 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002090
2091 virtual size_t ComputeHashCode() const {
2092 size_t result = GetKind();
2093 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2094 result = (result * 31) + InputAt(i)->GetId();
2095 }
2096 return result;
2097 }
2098
2099 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002100 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002101 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002102
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002103 size_t GetLifetimePosition() const { return lifetime_position_; }
2104 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2105 LiveInterval* GetLiveInterval() const { return live_interval_; }
2106 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2107 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2108
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002109 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2110
2111 // Returns whether the code generation of the instruction will require to have access
2112 // to the current method. Such instructions are:
2113 // (1): Instructions that require an environment, as calling the runtime requires
2114 // to walk the stack and have the current method stored at a specific stack address.
2115 // (2): Object literals like classes and strings, that are loaded from the dex cache
2116 // fields of the current method.
2117 bool NeedsCurrentMethod() const {
2118 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2119 }
2120
Vladimir Markodc151b22015-10-15 18:02:30 +01002121 // Returns whether the code generation of the instruction will require to have access
2122 // to the dex cache of the current method's declaring class via the current method.
2123 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002124
Mark Mendellc4701932015-04-10 13:18:51 -04002125 // Does this instruction have any use in an environment before
2126 // control flow hits 'other'?
2127 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2128
2129 // Remove all references to environment uses of this instruction.
2130 // The caller must ensure that this is safe to do.
2131 void RemoveEnvironmentUsers();
2132
Vladimir Markoa1de9182016-02-25 11:37:38 +00002133 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2134 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002135
David Brazdil1abb4192015-02-17 18:33:36 +00002136 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002137 // If set, the machine code for this instruction is assumed to be generated by
2138 // its users. Used by liveness analysis to compute use positions accordingly.
2139 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2140 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2141 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2142 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2143
David Brazdil1abb4192015-02-17 18:33:36 +00002144 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2145 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2146
Vladimir Markoa1de9182016-02-25 11:37:38 +00002147 uint32_t GetPackedFields() const {
2148 return packed_fields_;
2149 }
2150
2151 template <size_t flag>
2152 bool GetPackedFlag() const {
2153 return (packed_fields_ & (1u << flag)) != 0u;
2154 }
2155
2156 template <size_t flag>
2157 void SetPackedFlag(bool value = true) {
2158 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2159 }
2160
2161 template <typename BitFieldType>
2162 typename BitFieldType::value_type GetPackedField() const {
2163 return BitFieldType::Decode(packed_fields_);
2164 }
2165
2166 template <typename BitFieldType>
2167 void SetPackedField(typename BitFieldType::value_type value) {
2168 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2169 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2170 }
2171
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002172 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002173 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2174
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175 HInstruction* previous_;
2176 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002177 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002178 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002180 // An instruction gets an id when it is added to the graph.
2181 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002182 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183 int id_;
2184
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002185 // When doing liveness analysis, instructions that have uses get an SSA index.
2186 int ssa_index_;
2187
Vladimir Markoa1de9182016-02-25 11:37:38 +00002188 // Packed fields.
2189 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002190
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002192 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193
2194 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002195 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002196
Nicolas Geoffray39468442014-09-02 15:17:15 +01002197 // The environment associated with this instruction. Not null if the instruction
2198 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002199 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002200
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002201 // Set by the code generator.
2202 LocationSummary* locations_;
2203
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002204 // Set by the liveness analysis.
2205 LiveInterval* live_interval_;
2206
2207 // Set by the liveness analysis, this is the position in a linear
2208 // order of blocks where this instruction's live interval start.
2209 size_t lifetime_position_;
2210
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002211 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002212
Vladimir Markoa1de9182016-02-25 11:37:38 +00002213 // The reference handle part of the reference type info.
2214 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002215 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002216 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002217
David Brazdil1abb4192015-02-17 18:33:36 +00002218 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002220 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002221 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002222 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223
2224 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2225};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002226std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002228class HInputIterator : public ValueObject {
2229 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002230 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002231
2232 bool Done() const { return index_ == instruction_->InputCount(); }
2233 HInstruction* Current() const { return instruction_->InputAt(index_); }
2234 void Advance() { index_++; }
2235
2236 private:
2237 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002238 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002239
2240 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2241};
2242
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002243class HInstructionIterator : public ValueObject {
2244 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002245 explicit HInstructionIterator(const HInstructionList& instructions)
2246 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002247 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002248 }
2249
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002250 bool Done() const { return instruction_ == nullptr; }
2251 HInstruction* Current() const { return instruction_; }
2252 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002253 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002254 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255 }
2256
2257 private:
2258 HInstruction* instruction_;
2259 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002260
2261 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262};
2263
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002264class HBackwardInstructionIterator : public ValueObject {
2265 public:
2266 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2267 : instruction_(instructions.last_instruction_) {
2268 next_ = Done() ? nullptr : instruction_->GetPrevious();
2269 }
2270
2271 bool Done() const { return instruction_ == nullptr; }
2272 HInstruction* Current() const { return instruction_; }
2273 void Advance() {
2274 instruction_ = next_;
2275 next_ = Done() ? nullptr : instruction_->GetPrevious();
2276 }
2277
2278 private:
2279 HInstruction* instruction_;
2280 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002281
2282 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002283};
2284
Vladimir Markof9f64412015-09-02 14:05:49 +01002285template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286class HTemplateInstruction: public HInstruction {
2287 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002288 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002289 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002290 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002292 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002294 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002295 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2296 DCHECK_LT(i, N);
2297 return inputs_[i];
2298 }
David Brazdil1abb4192015-02-17 18:33:36 +00002299
2300 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002301 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002302 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002303 }
2304
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002305 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002306 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002307
2308 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002309};
2310
Vladimir Markof9f64412015-09-02 14:05:49 +01002311// HTemplateInstruction specialization for N=0.
2312template<>
2313class HTemplateInstruction<0>: public HInstruction {
2314 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002315 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002316 : HInstruction(side_effects, dex_pc) {}
2317
Vladimir Markof9f64412015-09-02 14:05:49 +01002318 virtual ~HTemplateInstruction() {}
2319
2320 size_t InputCount() const OVERRIDE { return 0; }
2321
2322 protected:
2323 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2324 LOG(FATAL) << "Unreachable";
2325 UNREACHABLE();
2326 }
2327
2328 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2329 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2330 LOG(FATAL) << "Unreachable";
2331 UNREACHABLE();
2332 }
2333
2334 private:
2335 friend class SsaBuilder;
2336};
2337
Dave Allison20dfc792014-06-16 20:44:29 -07002338template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002339class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002340 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002341 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002342 : HTemplateInstruction<N>(side_effects, dex_pc) {
2343 this->template SetPackedField<TypeField>(type);
2344 }
Dave Allison20dfc792014-06-16 20:44:29 -07002345 virtual ~HExpression() {}
2346
Vladimir Markoa1de9182016-02-25 11:37:38 +00002347 Primitive::Type GetType() const OVERRIDE {
2348 return TypeField::Decode(this->GetPackedFields());
2349 }
Dave Allison20dfc792014-06-16 20:44:29 -07002350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002351 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002352 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2353 static constexpr size_t kFieldTypeSize =
2354 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2355 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2356 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2357 "Too many packed fields.");
2358 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002359};
2360
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002361// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2362// instruction that branches to the exit block.
2363class HReturnVoid : public HTemplateInstruction<0> {
2364 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002365 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2366 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002367
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002368 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002369
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002370 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002371
2372 private:
2373 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2374};
2375
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002376// Represents dex's RETURN opcodes. A HReturn is a control flow
2377// instruction that branches to the exit block.
2378class HReturn : public HTemplateInstruction<1> {
2379 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002380 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2381 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002382 SetRawInputAt(0, value);
2383 }
2384
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002385 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002386
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002387 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002388
2389 private:
2390 DISALLOW_COPY_AND_ASSIGN(HReturn);
2391};
2392
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002393// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002394// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002395// exit block.
2396class HExit : public HTemplateInstruction<0> {
2397 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002398 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002399
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002400 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002401
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002402 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002403
2404 private:
2405 DISALLOW_COPY_AND_ASSIGN(HExit);
2406};
2407
2408// Jumps from one block to another.
2409class HGoto : public HTemplateInstruction<0> {
2410 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002411 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002412
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002413 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002414
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002415 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002416 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002417 }
2418
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002419 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002420
2421 private:
2422 DISALLOW_COPY_AND_ASSIGN(HGoto);
2423};
2424
Roland Levillain9867bc72015-08-05 10:21:34 +01002425class HConstant : public HExpression<0> {
2426 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002427 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2428 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002429
2430 bool CanBeMoved() const OVERRIDE { return true; }
2431
2432 virtual bool IsMinusOne() const { return false; }
2433 virtual bool IsZero() const { return false; }
2434 virtual bool IsOne() const { return false; }
2435
David Brazdil77a48ae2015-09-15 12:34:04 +00002436 virtual uint64_t GetValueAsUint64() const = 0;
2437
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002438 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002439
2440 private:
2441 DISALLOW_COPY_AND_ASSIGN(HConstant);
2442};
2443
2444class HNullConstant : public HConstant {
2445 public:
2446 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2447 return true;
2448 }
2449
David Brazdil77a48ae2015-09-15 12:34:04 +00002450 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2451
Roland Levillain9867bc72015-08-05 10:21:34 +01002452 size_t ComputeHashCode() const OVERRIDE { return 0; }
2453
2454 DECLARE_INSTRUCTION(NullConstant);
2455
2456 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002457 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002458
2459 friend class HGraph;
2460 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2461};
2462
2463// Constants of the type int. Those can be from Dex instructions, or
2464// synthesized (for example with the if-eqz instruction).
2465class HIntConstant : public HConstant {
2466 public:
2467 int32_t GetValue() const { return value_; }
2468
David Brazdil9f389d42015-10-01 14:32:56 +01002469 uint64_t GetValueAsUint64() const OVERRIDE {
2470 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2471 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002472
Roland Levillain9867bc72015-08-05 10:21:34 +01002473 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002474 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002475 return other->AsIntConstant()->value_ == value_;
2476 }
2477
2478 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2479
2480 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2481 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2482 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2483
2484 DECLARE_INSTRUCTION(IntConstant);
2485
2486 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002487 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2488 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2489 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2490 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002491
2492 const int32_t value_;
2493
2494 friend class HGraph;
2495 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2496 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2497 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2498};
2499
2500class HLongConstant : public HConstant {
2501 public:
2502 int64_t GetValue() const { return value_; }
2503
David Brazdil77a48ae2015-09-15 12:34:04 +00002504 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2505
Roland Levillain9867bc72015-08-05 10:21:34 +01002506 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002507 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002508 return other->AsLongConstant()->value_ == value_;
2509 }
2510
2511 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2512
2513 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2514 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2515 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2516
2517 DECLARE_INSTRUCTION(LongConstant);
2518
2519 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002520 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2521 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002522
2523 const int64_t value_;
2524
2525 friend class HGraph;
2526 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2527};
Dave Allison20dfc792014-06-16 20:44:29 -07002528
Roland Levillain31dd3d62016-02-16 12:21:02 +00002529class HFloatConstant : public HConstant {
2530 public:
2531 float GetValue() const { return value_; }
2532
2533 uint64_t GetValueAsUint64() const OVERRIDE {
2534 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2535 }
2536
2537 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2538 DCHECK(other->IsFloatConstant()) << other->DebugName();
2539 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2540 }
2541
2542 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2543
2544 bool IsMinusOne() const OVERRIDE {
2545 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2546 }
2547 bool IsZero() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002548 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002549 }
2550 bool IsOne() const OVERRIDE {
2551 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2552 }
2553 bool IsNaN() const {
2554 return std::isnan(value_);
2555 }
2556
2557 DECLARE_INSTRUCTION(FloatConstant);
2558
2559 private:
2560 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2561 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2562 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2563 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2564
2565 const float value_;
2566
2567 // Only the SsaBuilder and HGraph can create floating-point constants.
2568 friend class SsaBuilder;
2569 friend class HGraph;
2570 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2571};
2572
2573class HDoubleConstant : public HConstant {
2574 public:
2575 double GetValue() const { return value_; }
2576
2577 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2578
2579 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2580 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2581 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2582 }
2583
2584 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2585
2586 bool IsMinusOne() const OVERRIDE {
2587 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2588 }
2589 bool IsZero() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002590 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002591 }
2592 bool IsOne() const OVERRIDE {
2593 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2594 }
2595 bool IsNaN() const {
2596 return std::isnan(value_);
2597 }
2598
2599 DECLARE_INSTRUCTION(DoubleConstant);
2600
2601 private:
2602 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2603 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2604 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2605 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2606
2607 const double value_;
2608
2609 // Only the SsaBuilder and HGraph can create floating-point constants.
2610 friend class SsaBuilder;
2611 friend class HGraph;
2612 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2613};
2614
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002615// Conditional branch. A block ending with an HIf instruction must have
2616// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002617class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002619 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2620 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002621 SetRawInputAt(0, input);
2622 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002623
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002624 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002625
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002626 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002627 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002628 }
2629
2630 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002631 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002632 }
2633
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002634 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002635
2636 private:
2637 DISALLOW_COPY_AND_ASSIGN(HIf);
2638};
2639
David Brazdilfc6a86a2015-06-26 10:33:45 +00002640
2641// Abstract instruction which marks the beginning and/or end of a try block and
2642// links it to the respective exception handlers. Behaves the same as a Goto in
2643// non-exceptional control flow.
2644// Normal-flow successor is stored at index zero, exception handlers under
2645// higher indices in no particular order.
2646class HTryBoundary : public HTemplateInstruction<0> {
2647 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002648 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002649 kEntry,
2650 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002651 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002652 };
2653
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002655 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2656 SetPackedField<BoundaryKindField>(kind);
2657 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002658
2659 bool IsControlFlow() const OVERRIDE { return true; }
2660
2661 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002662 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002663
David Brazdild26a4112015-11-10 11:07:31 +00002664 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2665 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2666 }
2667
David Brazdilfc6a86a2015-06-26 10:33:45 +00002668 // Returns whether `handler` is among its exception handlers (non-zero index
2669 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002670 bool HasExceptionHandler(const HBasicBlock& handler) const {
2671 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002672 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002673 }
2674
2675 // If not present already, adds `handler` to its block's list of exception
2676 // handlers.
2677 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002678 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002679 GetBlock()->AddSuccessor(handler);
2680 }
2681 }
2682
Vladimir Markoa1de9182016-02-25 11:37:38 +00002683 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2684 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002685
David Brazdilffee3d32015-07-06 11:48:53 +01002686 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2687
David Brazdilfc6a86a2015-06-26 10:33:45 +00002688 DECLARE_INSTRUCTION(TryBoundary);
2689
2690 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002691 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2692 static constexpr size_t kFieldBoundaryKindSize =
2693 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2694 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2695 kFieldBoundaryKind + kFieldBoundaryKindSize;
2696 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2697 "Too many packed fields.");
2698 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002699
2700 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2701};
2702
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002703// Deoptimize to interpreter, upon checking a condition.
2704class HDeoptimize : public HTemplateInstruction<1> {
2705 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002706 // We set CanTriggerGC to prevent any intermediate address to be live
2707 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002708 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002709 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002710 SetRawInputAt(0, cond);
2711 }
2712
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002713 bool CanBeMoved() const OVERRIDE { return true; }
2714 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2715 return true;
2716 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002717 bool NeedsEnvironment() const OVERRIDE { return true; }
2718 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002719
2720 DECLARE_INSTRUCTION(Deoptimize);
2721
2722 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002723 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2724};
2725
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002726// Represents the ArtMethod that was passed as a first argument to
2727// the method. It is used by instructions that depend on it, like
2728// instructions that work with the dex cache.
2729class HCurrentMethod : public HExpression<0> {
2730 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002731 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2732 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002733
2734 DECLARE_INSTRUCTION(CurrentMethod);
2735
2736 private:
2737 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2738};
2739
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002740// Fetches an ArtMethod from the virtual table or the interface method table
2741// of a class.
2742class HClassTableGet : public HExpression<1> {
2743 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002744 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002745 kVTable,
2746 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002747 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002748 };
2749 HClassTableGet(HInstruction* cls,
2750 Primitive::Type type,
2751 TableKind kind,
2752 size_t index,
2753 uint32_t dex_pc)
2754 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002755 index_(index) {
2756 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002757 SetRawInputAt(0, cls);
2758 }
2759
2760 bool CanBeMoved() const OVERRIDE { return true; }
2761 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2762 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002763 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002764 }
2765
Vladimir Markoa1de9182016-02-25 11:37:38 +00002766 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002767 size_t GetIndex() const { return index_; }
2768
2769 DECLARE_INSTRUCTION(ClassTableGet);
2770
2771 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002772 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2773 static constexpr size_t kFieldTableKindSize =
2774 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2775 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2776 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2777 "Too many packed fields.");
2778 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2779
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002780 // The index of the ArtMethod in the table.
2781 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002782
2783 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2784};
2785
Mark Mendellfe57faa2015-09-18 09:26:15 -04002786// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2787// have one successor for each entry in the switch table, and the final successor
2788// will be the block containing the next Dex opcode.
2789class HPackedSwitch : public HTemplateInstruction<1> {
2790 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002791 HPackedSwitch(int32_t start_value,
2792 uint32_t num_entries,
2793 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002794 uint32_t dex_pc = kNoDexPc)
2795 : HTemplateInstruction(SideEffects::None(), dex_pc),
2796 start_value_(start_value),
2797 num_entries_(num_entries) {
2798 SetRawInputAt(0, input);
2799 }
2800
2801 bool IsControlFlow() const OVERRIDE { return true; }
2802
2803 int32_t GetStartValue() const { return start_value_; }
2804
Vladimir Marko211c2112015-09-24 16:52:33 +01002805 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002806
2807 HBasicBlock* GetDefaultBlock() const {
2808 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002809 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002810 }
2811 DECLARE_INSTRUCTION(PackedSwitch);
2812
2813 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002814 const int32_t start_value_;
2815 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002816
2817 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2818};
2819
Roland Levillain88cb1752014-10-20 16:36:47 +01002820class HUnaryOperation : public HExpression<1> {
2821 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002822 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2823 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002824 SetRawInputAt(0, input);
2825 }
2826
2827 HInstruction* GetInput() const { return InputAt(0); }
2828 Primitive::Type GetResultType() const { return GetType(); }
2829
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002830 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002831 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002832 return true;
2833 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002834
Roland Levillain31dd3d62016-02-16 12:21:02 +00002835 // Try to statically evaluate `this` and return a HConstant
2836 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002837 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002838 HConstant* TryStaticEvaluation() const;
2839
2840 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002841 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2842 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002843 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2844 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002845
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002846 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002847
2848 private:
2849 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2850};
2851
Dave Allison20dfc792014-06-16 20:44:29 -07002852class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002853 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002854 HBinaryOperation(Primitive::Type result_type,
2855 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002856 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002857 SideEffects side_effects = SideEffects::None(),
2858 uint32_t dex_pc = kNoDexPc)
2859 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002860 SetRawInputAt(0, left);
2861 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002862 }
2863
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002864 HInstruction* GetLeft() const { return InputAt(0); }
2865 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002866 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002867
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002868 virtual bool IsCommutative() const { return false; }
2869
2870 // Put constant on the right.
2871 // Returns whether order is changed.
2872 bool OrderInputsWithConstantOnTheRight() {
2873 HInstruction* left = InputAt(0);
2874 HInstruction* right = InputAt(1);
2875 if (left->IsConstant() && !right->IsConstant()) {
2876 ReplaceInput(right, 0);
2877 ReplaceInput(left, 1);
2878 return true;
2879 }
2880 return false;
2881 }
2882
2883 // Order inputs by instruction id, but favor constant on the right side.
2884 // This helps GVN for commutative ops.
2885 void OrderInputs() {
2886 DCHECK(IsCommutative());
2887 HInstruction* left = InputAt(0);
2888 HInstruction* right = InputAt(1);
2889 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2890 return;
2891 }
2892 if (OrderInputsWithConstantOnTheRight()) {
2893 return;
2894 }
2895 // Order according to instruction id.
2896 if (left->GetId() > right->GetId()) {
2897 ReplaceInput(right, 0);
2898 ReplaceInput(left, 1);
2899 }
2900 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002901
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002902 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002903 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002904 return true;
2905 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002906
Roland Levillain31dd3d62016-02-16 12:21:02 +00002907 // Try to statically evaluate `this` and return a HConstant
2908 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002909 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002910 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002911
2912 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002913 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2914 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002915 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2916 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002917 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002918 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2919 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002920 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2921 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002922 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2923 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002924 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002925 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2926 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002927
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002928 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002929 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002930 HConstant* GetConstantRight() const;
2931
2932 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002933 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002934 HInstruction* GetLeastConstantLeft() const;
2935
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002936 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002937
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002938 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002939 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2940};
2941
Mark Mendellc4701932015-04-10 13:18:51 -04002942// The comparison bias applies for floating point operations and indicates how NaN
2943// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002944enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002945 kNoBias, // bias is not applicable (i.e. for long operation)
2946 kGtBias, // return 1 for NaN comparisons
2947 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002948 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002949};
2950
Roland Levillain31dd3d62016-02-16 12:21:02 +00002951std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2952
Dave Allison20dfc792014-06-16 20:44:29 -07002953class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002954 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002955 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002956 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2957 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2958 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002959
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002960 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002961 // `instruction`, and disregard moves in between.
2962 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002963
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002964 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002965
2966 virtual IfCondition GetCondition() const = 0;
2967
Mark Mendellc4701932015-04-10 13:18:51 -04002968 virtual IfCondition GetOppositeCondition() const = 0;
2969
Vladimir Markoa1de9182016-02-25 11:37:38 +00002970 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06002971 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
2972
Vladimir Markoa1de9182016-02-25 11:37:38 +00002973 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
2974 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04002975
2976 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002977 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04002978 }
2979
Roland Levillain4fa13f62015-07-06 18:11:54 +01002980 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002982 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06002983 if (if_cond == kCondNE) {
2984 return true;
2985 } else if (if_cond == kCondEQ) {
2986 return false;
2987 }
2988 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002989 }
2990
2991 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002992 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002993 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06002994 if (if_cond == kCondEQ) {
2995 return true;
2996 } else if (if_cond == kCondNE) {
2997 return false;
2998 }
2999 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003000 }
3001
Roland Levillain31dd3d62016-02-16 12:21:02 +00003002 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003003 // Needed if we merge a HCompare into a HCondition.
3004 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3005 static constexpr size_t kFieldComparisonBiasSize =
3006 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3007 static constexpr size_t kNumberOfConditionPackedBits =
3008 kFieldComparisonBias + kFieldComparisonBiasSize;
3009 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3010 using ComparisonBiasField =
3011 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3012
Roland Levillain31dd3d62016-02-16 12:21:02 +00003013 template <typename T>
3014 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3015
3016 template <typename T>
3017 int32_t CompareFP(T x, T y) const {
3018 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3019 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3020 // Handle the bias.
3021 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3022 }
3023
3024 // Return an integer constant containing the result of a condition evaluated at compile time.
3025 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3026 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3027 }
3028
Dave Allison20dfc792014-06-16 20:44:29 -07003029 private:
3030 DISALLOW_COPY_AND_ASSIGN(HCondition);
3031};
3032
3033// Instruction to check if two inputs are equal to each other.
3034class HEqual : public HCondition {
3035 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003036 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3037 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003038
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003039 bool IsCommutative() const OVERRIDE { return true; }
3040
Vladimir Marko9e23df52015-11-10 17:14:35 +00003041 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3042 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003043 return MakeConstantCondition(true, GetDexPc());
3044 }
3045 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3046 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3047 }
3048 // In the following Evaluate methods, a HCompare instruction has
3049 // been merged into this HEqual instruction; evaluate it as
3050 // `Compare(x, y) == 0`.
3051 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3052 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3053 GetDexPc());
3054 }
3055 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3056 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3057 }
3058 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3059 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003060 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003061
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003062 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003063
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003064 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003065 return kCondEQ;
3066 }
3067
Mark Mendellc4701932015-04-10 13:18:51 -04003068 IfCondition GetOppositeCondition() const OVERRIDE {
3069 return kCondNE;
3070 }
3071
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003072 private:
Aart Bike9f37602015-10-09 11:15:55 -07003073 template <typename T> bool Compute(T x, T y) const { return x == y; }
3074
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003075 DISALLOW_COPY_AND_ASSIGN(HEqual);
3076};
3077
Dave Allison20dfc792014-06-16 20:44:29 -07003078class HNotEqual : public HCondition {
3079 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003080 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3081 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003082
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003083 bool IsCommutative() const OVERRIDE { return true; }
3084
Vladimir Marko9e23df52015-11-10 17:14:35 +00003085 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3086 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003087 return MakeConstantCondition(false, GetDexPc());
3088 }
3089 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3090 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3091 }
3092 // In the following Evaluate methods, a HCompare instruction has
3093 // been merged into this HNotEqual instruction; evaluate it as
3094 // `Compare(x, y) != 0`.
3095 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3096 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3097 }
3098 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3099 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3100 }
3101 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3102 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003103 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003104
Dave Allison20dfc792014-06-16 20:44:29 -07003105 DECLARE_INSTRUCTION(NotEqual);
3106
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003107 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003108 return kCondNE;
3109 }
3110
Mark Mendellc4701932015-04-10 13:18:51 -04003111 IfCondition GetOppositeCondition() const OVERRIDE {
3112 return kCondEQ;
3113 }
3114
Dave Allison20dfc792014-06-16 20:44:29 -07003115 private:
Aart Bike9f37602015-10-09 11:15:55 -07003116 template <typename T> bool Compute(T x, T y) const { return x != y; }
3117
Dave Allison20dfc792014-06-16 20:44:29 -07003118 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3119};
3120
3121class HLessThan : public HCondition {
3122 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003123 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3124 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003125
Roland Levillain9867bc72015-08-05 10:21:34 +01003126 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003127 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003128 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003129 // In the following Evaluate methods, a HCompare instruction has
3130 // been merged into this HLessThan instruction; evaluate it as
3131 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003132 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003133 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3134 }
3135 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3136 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3137 }
3138 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3139 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003140 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003141
Dave Allison20dfc792014-06-16 20:44:29 -07003142 DECLARE_INSTRUCTION(LessThan);
3143
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003144 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003145 return kCondLT;
3146 }
3147
Mark Mendellc4701932015-04-10 13:18:51 -04003148 IfCondition GetOppositeCondition() const OVERRIDE {
3149 return kCondGE;
3150 }
3151
Dave Allison20dfc792014-06-16 20:44:29 -07003152 private:
Aart Bike9f37602015-10-09 11:15:55 -07003153 template <typename T> bool Compute(T x, T y) const { return x < y; }
3154
Dave Allison20dfc792014-06-16 20:44:29 -07003155 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3156};
3157
3158class HLessThanOrEqual : public HCondition {
3159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003160 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3161 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003162
Roland Levillain9867bc72015-08-05 10:21:34 +01003163 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003165 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003166 // In the following Evaluate methods, a HCompare instruction has
3167 // been merged into this HLessThanOrEqual instruction; evaluate it as
3168 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003169 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003170 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3171 }
3172 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3173 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3174 }
3175 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3176 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003177 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003178
Dave Allison20dfc792014-06-16 20:44:29 -07003179 DECLARE_INSTRUCTION(LessThanOrEqual);
3180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003181 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003182 return kCondLE;
3183 }
3184
Mark Mendellc4701932015-04-10 13:18:51 -04003185 IfCondition GetOppositeCondition() const OVERRIDE {
3186 return kCondGT;
3187 }
3188
Dave Allison20dfc792014-06-16 20:44:29 -07003189 private:
Aart Bike9f37602015-10-09 11:15:55 -07003190 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3191
Dave Allison20dfc792014-06-16 20:44:29 -07003192 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3193};
3194
3195class HGreaterThan : public HCondition {
3196 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003197 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3198 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003199
Roland Levillain9867bc72015-08-05 10:21:34 +01003200 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003202 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003203 // In the following Evaluate methods, a HCompare instruction has
3204 // been merged into this HGreaterThan instruction; evaluate it as
3205 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003206 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003207 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3208 }
3209 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3210 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3211 }
3212 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3213 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003214 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003215
Dave Allison20dfc792014-06-16 20:44:29 -07003216 DECLARE_INSTRUCTION(GreaterThan);
3217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003218 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003219 return kCondGT;
3220 }
3221
Mark Mendellc4701932015-04-10 13:18:51 -04003222 IfCondition GetOppositeCondition() const OVERRIDE {
3223 return kCondLE;
3224 }
3225
Dave Allison20dfc792014-06-16 20:44:29 -07003226 private:
Aart Bike9f37602015-10-09 11:15:55 -07003227 template <typename T> bool Compute(T x, T y) const { return x > y; }
3228
Dave Allison20dfc792014-06-16 20:44:29 -07003229 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3230};
3231
3232class HGreaterThanOrEqual : public HCondition {
3233 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003234 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3235 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003236
Roland Levillain9867bc72015-08-05 10:21:34 +01003237 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003239 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 // In the following Evaluate methods, a HCompare instruction has
3241 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3242 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003243 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003244 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3245 }
3246 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3247 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3248 }
3249 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3250 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003251 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003252
Dave Allison20dfc792014-06-16 20:44:29 -07003253 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003255 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003256 return kCondGE;
3257 }
3258
Mark Mendellc4701932015-04-10 13:18:51 -04003259 IfCondition GetOppositeCondition() const OVERRIDE {
3260 return kCondLT;
3261 }
3262
Dave Allison20dfc792014-06-16 20:44:29 -07003263 private:
Aart Bike9f37602015-10-09 11:15:55 -07003264 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3265
Dave Allison20dfc792014-06-16 20:44:29 -07003266 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3267};
3268
Aart Bike9f37602015-10-09 11:15:55 -07003269class HBelow : public HCondition {
3270 public:
3271 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3272 : HCondition(first, second, dex_pc) {}
3273
3274 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003276 }
3277 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003278 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3279 }
3280 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3281 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3282 LOG(FATAL) << DebugName() << " is not defined for float values";
3283 UNREACHABLE();
3284 }
3285 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3286 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3287 LOG(FATAL) << DebugName() << " is not defined for double values";
3288 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003289 }
3290
3291 DECLARE_INSTRUCTION(Below);
3292
3293 IfCondition GetCondition() const OVERRIDE {
3294 return kCondB;
3295 }
3296
3297 IfCondition GetOppositeCondition() const OVERRIDE {
3298 return kCondAE;
3299 }
3300
3301 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003302 template <typename T> bool Compute(T x, T y) const {
3303 return MakeUnsigned(x) < MakeUnsigned(y);
3304 }
Aart Bike9f37602015-10-09 11:15:55 -07003305
3306 DISALLOW_COPY_AND_ASSIGN(HBelow);
3307};
3308
3309class HBelowOrEqual : public HCondition {
3310 public:
3311 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3312 : HCondition(first, second, dex_pc) {}
3313
3314 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003315 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003316 }
3317 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3319 }
3320 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3321 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3322 LOG(FATAL) << DebugName() << " is not defined for float values";
3323 UNREACHABLE();
3324 }
3325 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3326 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3327 LOG(FATAL) << DebugName() << " is not defined for double values";
3328 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003329 }
3330
3331 DECLARE_INSTRUCTION(BelowOrEqual);
3332
3333 IfCondition GetCondition() const OVERRIDE {
3334 return kCondBE;
3335 }
3336
3337 IfCondition GetOppositeCondition() const OVERRIDE {
3338 return kCondA;
3339 }
3340
3341 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 template <typename T> bool Compute(T x, T y) const {
3343 return MakeUnsigned(x) <= MakeUnsigned(y);
3344 }
Aart Bike9f37602015-10-09 11:15:55 -07003345
3346 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3347};
3348
3349class HAbove : public HCondition {
3350 public:
3351 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3352 : HCondition(first, second, dex_pc) {}
3353
3354 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003355 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003356 }
3357 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003358 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3359 }
3360 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3361 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3362 LOG(FATAL) << DebugName() << " is not defined for float values";
3363 UNREACHABLE();
3364 }
3365 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3366 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3367 LOG(FATAL) << DebugName() << " is not defined for double values";
3368 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003369 }
3370
3371 DECLARE_INSTRUCTION(Above);
3372
3373 IfCondition GetCondition() const OVERRIDE {
3374 return kCondA;
3375 }
3376
3377 IfCondition GetOppositeCondition() const OVERRIDE {
3378 return kCondBE;
3379 }
3380
3381 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 template <typename T> bool Compute(T x, T y) const {
3383 return MakeUnsigned(x) > MakeUnsigned(y);
3384 }
Aart Bike9f37602015-10-09 11:15:55 -07003385
3386 DISALLOW_COPY_AND_ASSIGN(HAbove);
3387};
3388
3389class HAboveOrEqual : public HCondition {
3390 public:
3391 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3392 : HCondition(first, second, dex_pc) {}
3393
3394 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003395 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003396 }
3397 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003398 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3399 }
3400 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3401 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3402 LOG(FATAL) << DebugName() << " is not defined for float values";
3403 UNREACHABLE();
3404 }
3405 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3406 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3407 LOG(FATAL) << DebugName() << " is not defined for double values";
3408 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003409 }
3410
3411 DECLARE_INSTRUCTION(AboveOrEqual);
3412
3413 IfCondition GetCondition() const OVERRIDE {
3414 return kCondAE;
3415 }
3416
3417 IfCondition GetOppositeCondition() const OVERRIDE {
3418 return kCondB;
3419 }
3420
3421 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003422 template <typename T> bool Compute(T x, T y) const {
3423 return MakeUnsigned(x) >= MakeUnsigned(y);
3424 }
Aart Bike9f37602015-10-09 11:15:55 -07003425
3426 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3427};
Dave Allison20dfc792014-06-16 20:44:29 -07003428
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003429// Instruction to check how two inputs compare to each other.
3430// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3431class HCompare : public HBinaryOperation {
3432 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003433 // Note that `comparison_type` is the type of comparison performed
3434 // between the comparison's inputs, not the type of the instantiated
3435 // HCompare instruction (which is always Primitive::kPrimInt).
3436 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003437 HInstruction* first,
3438 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003439 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003440 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003441 : HBinaryOperation(Primitive::kPrimInt,
3442 first,
3443 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003444 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003445 dex_pc) {
3446 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003447 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3448 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003449 }
3450
Roland Levillain9867bc72015-08-05 10:21:34 +01003451 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003452 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3453
3454 template <typename T>
3455 int32_t ComputeFP(T x, T y) const {
3456 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3457 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3458 // Handle the bias.
3459 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3460 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003461
Roland Levillain9867bc72015-08-05 10:21:34 +01003462 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3464 // reach this code path when processing a freshly built HIR
3465 // graph. However HCompare integer instructions can be synthesized
3466 // by the instruction simplifier to implement IntegerCompare and
3467 // IntegerSignum intrinsics, so we have to handle this case.
3468 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003469 }
3470 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3472 }
3473 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3474 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3475 }
3476 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3477 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003478 }
3479
Calin Juravleddb7df22014-11-25 20:56:51 +00003480 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003481 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003482 }
3483
Vladimir Markoa1de9182016-02-25 11:37:38 +00003484 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003485
Roland Levillain31dd3d62016-02-16 12:21:02 +00003486 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003487 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003488 bool IsGtBias() const {
3489 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003490 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003491 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003492
Roland Levillain1693a1f2016-03-15 14:57:31 +00003493 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3494 // Comparisons do not require a runtime call in any back end.
3495 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003496 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003497
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003498 DECLARE_INSTRUCTION(Compare);
3499
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003501 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3502 static constexpr size_t kFieldComparisonBiasSize =
3503 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3504 static constexpr size_t kNumberOfComparePackedBits =
3505 kFieldComparisonBias + kFieldComparisonBiasSize;
3506 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3507 using ComparisonBiasField =
3508 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3509
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 // Return an integer constant containing the result of a comparison evaluated at compile time.
3511 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3512 DCHECK(value == -1 || value == 0 || value == 1) << value;
3513 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3514 }
3515
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003516 private:
3517 DISALLOW_COPY_AND_ASSIGN(HCompare);
3518};
3519
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003520// A local in the graph. Corresponds to a Dex register.
3521class HLocal : public HTemplateInstruction<0> {
3522 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003523 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003524 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003526 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003527
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003528 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003529
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003530 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003531 // The Dex register number.
3532 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003533
3534 DISALLOW_COPY_AND_ASSIGN(HLocal);
3535};
3536
3537// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003538class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003539 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003540 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3541 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003542 SetRawInputAt(0, local);
3543 }
3544
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003545 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3546
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003547 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003548
3549 private:
3550 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3551};
3552
3553// Store a value in a given local. This instruction has two inputs: the value
3554// and the local.
3555class HStoreLocal : public HTemplateInstruction<2> {
3556 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003557 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3558 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003559 SetRawInputAt(0, local);
3560 SetRawInputAt(1, value);
3561 }
3562
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003563 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3564
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003565 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003566
3567 private:
3568 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3569};
3570
David Brazdil6de19382016-01-08 17:37:10 +00003571class HNewInstance : public HExpression<2> {
3572 public:
3573 HNewInstance(HInstruction* cls,
3574 HCurrentMethod* current_method,
3575 uint32_t dex_pc,
3576 uint16_t type_index,
3577 const DexFile& dex_file,
3578 bool can_throw,
3579 bool finalizable,
3580 QuickEntrypointEnum entrypoint)
3581 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3582 type_index_(type_index),
3583 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003584 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003585 SetPackedFlag<kFlagCanThrow>(can_throw);
3586 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003587 SetRawInputAt(0, cls);
3588 SetRawInputAt(1, current_method);
3589 }
3590
3591 uint16_t GetTypeIndex() const { return type_index_; }
3592 const DexFile& GetDexFile() const { return dex_file_; }
3593
3594 // Calls runtime so needs an environment.
3595 bool NeedsEnvironment() const OVERRIDE { return true; }
3596
3597 // It may throw when called on type that's not instantiable/accessible.
3598 // It can throw OOME.
3599 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003600 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003601
Vladimir Markoa1de9182016-02-25 11:37:38 +00003602 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003603
3604 bool CanBeNull() const OVERRIDE { return false; }
3605
3606 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3607
3608 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3609 entrypoint_ = entrypoint;
3610 }
3611
3612 bool IsStringAlloc() const;
3613
3614 DECLARE_INSTRUCTION(NewInstance);
3615
3616 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003617 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3618 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3619 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3620 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3621 "Too many packed fields.");
3622
David Brazdil6de19382016-01-08 17:37:10 +00003623 const uint16_t type_index_;
3624 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003625 QuickEntrypointEnum entrypoint_;
3626
3627 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3628};
3629
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003630enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003631#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3632 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003633#include "intrinsics_list.h"
3634 kNone,
3635 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3636#undef INTRINSICS_LIST
3637#undef OPTIMIZING_INTRINSICS
3638};
3639std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3640
Agi Csaki05f20562015-08-19 14:58:14 -07003641enum IntrinsicNeedsEnvironmentOrCache {
3642 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3643 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003644};
3645
Aart Bik5d75afe2015-12-14 11:57:01 -08003646enum IntrinsicSideEffects {
3647 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3648 kReadSideEffects, // Intrinsic may read heap memory.
3649 kWriteSideEffects, // Intrinsic may write heap memory.
3650 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3651};
3652
3653enum IntrinsicExceptions {
3654 kNoThrow, // Intrinsic does not throw any exceptions.
3655 kCanThrow // Intrinsic may throw exceptions.
3656};
3657
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003658class HInvoke : public HInstruction {
3659 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003660 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003661
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003662 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003663
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003664 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003665 SetRawInputAt(index, argument);
3666 }
3667
Roland Levillain3e3d7332015-04-28 11:00:54 +01003668 // Return the number of arguments. This number can be lower than
3669 // the number of inputs returned by InputCount(), as some invoke
3670 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3671 // inputs at the end of their list of inputs.
3672 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3673
Vladimir Markoa1de9182016-02-25 11:37:38 +00003674 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003675
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003676 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003677 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003678
Vladimir Markoa1de9182016-02-25 11:37:38 +00003679 InvokeType GetOriginalInvokeType() const {
3680 return GetPackedField<OriginalInvokeTypeField>();
3681 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003682
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003683 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003684 return intrinsic_;
3685 }
3686
Aart Bik5d75afe2015-12-14 11:57:01 -08003687 void SetIntrinsic(Intrinsics intrinsic,
3688 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3689 IntrinsicSideEffects side_effects,
3690 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003691
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003692 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003693 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003694 }
3695
Vladimir Markoa1de9182016-02-25 11:37:38 +00003696 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003697
3698 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3699
3700 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3701 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3702 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003703
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003704 uint32_t* GetIntrinsicOptimizations() {
3705 return &intrinsic_optimizations_;
3706 }
3707
3708 const uint32_t* GetIntrinsicOptimizations() const {
3709 return &intrinsic_optimizations_;
3710 }
3711
3712 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3713
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003714 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003715
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003716 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003717 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3718 static constexpr size_t kFieldOriginalInvokeTypeSize =
3719 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3720 static constexpr size_t kFieldReturnType =
3721 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3722 static constexpr size_t kFieldReturnTypeSize =
3723 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3724 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3725 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3726 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3727 using OriginalInvokeTypeField =
3728 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3729 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3730
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003731 HInvoke(ArenaAllocator* arena,
3732 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003733 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003734 Primitive::Type return_type,
3735 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003736 uint32_t dex_method_index,
3737 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003738 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003739 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003740 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003741 inputs_(number_of_arguments + number_of_other_inputs,
3742 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003743 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003744 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003745 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003746 SetPackedField<ReturnTypeField>(return_type);
3747 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3748 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003749 }
3750
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003751 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003752 return inputs_[index];
3753 }
3754
David Brazdil1abb4192015-02-17 18:33:36 +00003755 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003756 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003757 }
3758
Vladimir Markoa1de9182016-02-25 11:37:38 +00003759 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003760
Roland Levillain3e3d7332015-04-28 11:00:54 +01003761 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003762 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003763 const uint32_t dex_method_index_;
3764 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003765
3766 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3767 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003768
3769 private:
3770 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3771};
3772
Calin Juravle175dc732015-08-25 15:42:32 +01003773class HInvokeUnresolved : public HInvoke {
3774 public:
3775 HInvokeUnresolved(ArenaAllocator* arena,
3776 uint32_t number_of_arguments,
3777 Primitive::Type return_type,
3778 uint32_t dex_pc,
3779 uint32_t dex_method_index,
3780 InvokeType invoke_type)
3781 : HInvoke(arena,
3782 number_of_arguments,
3783 0u /* number_of_other_inputs */,
3784 return_type,
3785 dex_pc,
3786 dex_method_index,
3787 invoke_type) {
3788 }
3789
3790 DECLARE_INSTRUCTION(InvokeUnresolved);
3791
3792 private:
3793 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3794};
3795
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003796class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003797 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003798 // Requirements of this method call regarding the class
3799 // initialization (clinit) check of its declaring class.
3800 enum class ClinitCheckRequirement {
3801 kNone, // Class already initialized.
3802 kExplicit, // Static call having explicit clinit check as last input.
3803 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003804 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003805 };
3806
Vladimir Marko58155012015-08-19 12:49:41 +00003807 // Determines how to load the target ArtMethod*.
3808 enum class MethodLoadKind {
3809 // Use a String init ArtMethod* loaded from Thread entrypoints.
3810 kStringInit,
3811
3812 // Use the method's own ArtMethod* loaded by the register allocator.
3813 kRecursive,
3814
3815 // Use ArtMethod* at a known address, embed the direct address in the code.
3816 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3817 kDirectAddress,
3818
3819 // Use ArtMethod* at an address that will be known at link time, embed the direct
3820 // address in the code. If the image is relocatable, emit .patch_oat entry.
3821 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3822 // the image relocatable or not.
3823 kDirectAddressWithFixup,
3824
Vladimir Markoa1de9182016-02-25 11:37:38 +00003825 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003826 // Used when we need to use the dex cache, for example for invoke-static that
3827 // may cause class initialization (the entry may point to a resolution method),
3828 // and we know that we can access the dex cache arrays using a PC-relative load.
3829 kDexCachePcRelative,
3830
3831 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3832 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3833 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3834 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3835 kDexCacheViaMethod,
3836 };
3837
3838 // Determines the location of the code pointer.
3839 enum class CodePtrLocation {
3840 // Recursive call, use local PC-relative call instruction.
3841 kCallSelf,
3842
3843 // Use PC-relative call instruction patched at link time.
3844 // Used for calls within an oat file, boot->boot or app->app.
3845 kCallPCRelative,
3846
3847 // Call to a known target address, embed the direct address in code.
3848 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3849 kCallDirect,
3850
3851 // Call to a target address that will be known at link time, embed the direct
3852 // address in code. If the image is relocatable, emit .patch_oat entry.
3853 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3854 // the image relocatable or not.
3855 kCallDirectWithFixup,
3856
3857 // Use code pointer from the ArtMethod*.
3858 // Used when we don't know the target code. This is also the last-resort-kind used when
3859 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3860 kCallArtMethod,
3861 };
3862
3863 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003864 MethodLoadKind method_load_kind;
3865 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003866 // The method load data holds
3867 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3868 // Note that there are multiple string init methods, each having its own offset.
3869 // - the method address for kDirectAddress
3870 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003871 uint64_t method_load_data;
3872 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003873 };
3874
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003875 HInvokeStaticOrDirect(ArenaAllocator* arena,
3876 uint32_t number_of_arguments,
3877 Primitive::Type return_type,
3878 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003879 uint32_t method_index,
3880 MethodReference target_method,
3881 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003882 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003883 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003884 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003885 : HInvoke(arena,
3886 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003887 // There is potentially one extra argument for the HCurrentMethod node, and
3888 // potentially one other if the clinit check is explicit, and potentially
3889 // one other if the method is a string factory.
3890 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3891 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3892 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003893 return_type,
3894 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003895 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003896 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003897 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003898 dispatch_info_(dispatch_info) {
3899 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3900 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3901 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003902
Vladimir Markodc151b22015-10-15 18:02:30 +01003903 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003904 bool had_current_method_input = HasCurrentMethodInput();
3905 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3906
3907 // Using the current method is the default and once we find a better
3908 // method load kind, we should not go back to using the current method.
3909 DCHECK(had_current_method_input || !needs_current_method_input);
3910
3911 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003912 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3913 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003914 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003915 dispatch_info_ = dispatch_info;
3916 }
3917
Vladimir Markoc53c0792015-11-19 15:48:33 +00003918 void AddSpecialInput(HInstruction* input) {
3919 // We allow only one special input.
3920 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3921 DCHECK(InputCount() == GetSpecialInputIndex() ||
3922 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3923 InsertInputAt(GetSpecialInputIndex(), input);
3924 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003925
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003926 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003927 // We access the method via the dex cache so we can't do an implicit null check.
3928 // TODO: for intrinsics we can generate implicit null checks.
3929 return false;
3930 }
3931
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003932 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003933 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003934 }
3935
Vladimir Markoc53c0792015-11-19 15:48:33 +00003936 // Get the index of the special input, if any.
3937 //
David Brazdil6de19382016-01-08 17:37:10 +00003938 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3939 // method pointer; otherwise there may be one platform-specific special input,
3940 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003941 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003942 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003943
Vladimir Markoa1de9182016-02-25 11:37:38 +00003944 InvokeType GetOptimizedInvokeType() const {
3945 return GetPackedField<OptimizedInvokeTypeField>();
3946 }
3947
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003948 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003949 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003950 }
3951
Vladimir Marko58155012015-08-19 12:49:41 +00003952 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3953 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3954 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003955 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003956 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003957 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003958 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003959 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3960 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003961 bool HasCurrentMethodInput() const {
3962 // This function can be called only after the invoke has been fully initialized by the builder.
3963 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003964 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003965 return true;
3966 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003967 DCHECK(InputCount() == GetSpecialInputIndex() ||
3968 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003969 return false;
3970 }
3971 }
Vladimir Marko58155012015-08-19 12:49:41 +00003972 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3973 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003974 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003975
3976 int32_t GetStringInitOffset() const {
3977 DCHECK(IsStringInit());
3978 return dispatch_info_.method_load_data;
3979 }
3980
3981 uint64_t GetMethodAddress() const {
3982 DCHECK(HasMethodAddress());
3983 return dispatch_info_.method_load_data;
3984 }
3985
3986 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003987 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003988 return dispatch_info_.method_load_data;
3989 }
3990
3991 uint64_t GetDirectCodePtr() const {
3992 DCHECK(HasDirectCodePtr());
3993 return dispatch_info_.direct_code_ptr;
3994 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003995
Vladimir Markoa1de9182016-02-25 11:37:38 +00003996 ClinitCheckRequirement GetClinitCheckRequirement() const {
3997 return GetPackedField<ClinitCheckRequirementField>();
3998 }
Calin Juravle68ad6492015-08-18 17:08:12 +01003999
Roland Levillain4c0eb422015-04-24 16:43:49 +01004000 // Is this instruction a call to a static method?
4001 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004002 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004003 }
4004
Vladimir Markofbb184a2015-11-13 14:47:00 +00004005 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4006 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4007 // instruction; only relevant for static calls with explicit clinit check.
4008 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004009 DCHECK(IsStaticWithExplicitClinitCheck());
4010 size_t last_input_index = InputCount() - 1;
4011 HInstruction* last_input = InputAt(last_input_index);
4012 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004013 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004014 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004015 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004016 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004017 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004018 }
4019
David Brazdilbc9ab162016-01-20 14:50:19 +00004020 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004021 DCHECK(IsStringInit());
4022 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004023 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004024 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004025 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004026 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004027 }
4028
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004030 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004031 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004032 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004033 }
4034
4035 // Is this a call to a static method whose declaring class has an
4036 // implicit intialization check requirement?
4037 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004038 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004039 }
4040
Vladimir Markob554b5a2015-11-06 12:57:55 +00004041 // Does this method load kind need the current method as an input?
4042 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4043 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4044 }
4045
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004046 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004047
Roland Levillain4c0eb422015-04-24 16:43:49 +01004048 protected:
4049 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4050 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4051 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4052 HInstruction* input = input_record.GetInstruction();
4053 // `input` is the last input of a static invoke marked as having
4054 // an explicit clinit check. It must either be:
4055 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4056 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4057 DCHECK(input != nullptr);
4058 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4059 }
4060 return input_record;
4061 }
4062
Vladimir Markoc53c0792015-11-19 15:48:33 +00004063 void InsertInputAt(size_t index, HInstruction* input);
4064 void RemoveInputAt(size_t index);
4065
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004066 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004067 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4068 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4069 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4070 static constexpr size_t kFieldClinitCheckRequirement =
4071 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4072 static constexpr size_t kFieldClinitCheckRequirementSize =
4073 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4074 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4075 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4076 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4077 "Too many packed fields.");
4078 using OptimizedInvokeTypeField =
4079 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4080 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4081 kFieldClinitCheckRequirement,
4082 kFieldClinitCheckRequirementSize>;
4083
Vladimir Marko58155012015-08-19 12:49:41 +00004084 // The target method may refer to different dex file or method index than the original
4085 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4086 // in derived class to increase visibility.
4087 MethodReference target_method_;
4088 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004089
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004090 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004091};
Vladimir Markof64242a2015-12-01 14:58:23 +00004092std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004093std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004094
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004095class HInvokeVirtual : public HInvoke {
4096 public:
4097 HInvokeVirtual(ArenaAllocator* arena,
4098 uint32_t number_of_arguments,
4099 Primitive::Type return_type,
4100 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004101 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004102 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004103 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004104 vtable_index_(vtable_index) {}
4105
Calin Juravle641547a2015-04-21 22:08:51 +01004106 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004107 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004108 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004109 }
4110
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004111 uint32_t GetVTableIndex() const { return vtable_index_; }
4112
4113 DECLARE_INSTRUCTION(InvokeVirtual);
4114
4115 private:
4116 const uint32_t vtable_index_;
4117
4118 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4119};
4120
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004121class HInvokeInterface : public HInvoke {
4122 public:
4123 HInvokeInterface(ArenaAllocator* arena,
4124 uint32_t number_of_arguments,
4125 Primitive::Type return_type,
4126 uint32_t dex_pc,
4127 uint32_t dex_method_index,
4128 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004129 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004130 imt_index_(imt_index) {}
4131
Calin Juravle641547a2015-04-21 22:08:51 +01004132 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004133 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004134 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004135 }
4136
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004137 uint32_t GetImtIndex() const { return imt_index_; }
4138 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4139
4140 DECLARE_INSTRUCTION(InvokeInterface);
4141
4142 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004143 const uint32_t imt_index_;
4144
4145 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4146};
4147
Roland Levillain88cb1752014-10-20 16:36:47 +01004148class HNeg : public HUnaryOperation {
4149 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004150 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4151 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004152
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 template <typename T> T Compute(T x) const { return -x; }
4154
4155 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 }
4158 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004159 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004160 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004161 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4162 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4163 }
4164 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4165 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4166 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004167
Roland Levillain88cb1752014-10-20 16:36:47 +01004168 DECLARE_INSTRUCTION(Neg);
4169
4170 private:
4171 DISALLOW_COPY_AND_ASSIGN(HNeg);
4172};
4173
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004174class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004175 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004176 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004177 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004178 uint32_t dex_pc,
4179 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004180 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004181 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004182 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004183 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004184 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004185 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004186 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004187 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004188 }
4189
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004190 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004191 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004192
4193 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004194 bool NeedsEnvironment() const OVERRIDE { return true; }
4195
Mingyao Yang0c365e62015-03-31 15:09:29 -07004196 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4197 bool CanThrow() const OVERRIDE { return true; }
4198
Calin Juravle10e244f2015-01-26 18:54:32 +00004199 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004200
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004201 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4202
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 DECLARE_INSTRUCTION(NewArray);
4204
4205 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004206 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004207 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004208 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004209
4210 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4211};
4212
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004213class HAdd : public HBinaryOperation {
4214 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004215 HAdd(Primitive::Type result_type,
4216 HInstruction* left,
4217 HInstruction* right,
4218 uint32_t dex_pc = kNoDexPc)
4219 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004220
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004221 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004222
Roland Levillain9867bc72015-08-05 10:21:34 +01004223 template <typename T> T Compute(T x, T y) const { return x + y; }
4224
4225 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 return GetBlock()->GetGraph()->GetIntConstant(
4227 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004228 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004229 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 return GetBlock()->GetGraph()->GetLongConstant(
4231 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004232 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004233 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4234 return GetBlock()->GetGraph()->GetFloatConstant(
4235 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4236 }
4237 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4238 return GetBlock()->GetGraph()->GetDoubleConstant(
4239 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4240 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004241
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004242 DECLARE_INSTRUCTION(Add);
4243
4244 private:
4245 DISALLOW_COPY_AND_ASSIGN(HAdd);
4246};
4247
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004248class HSub : public HBinaryOperation {
4249 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004250 HSub(Primitive::Type result_type,
4251 HInstruction* left,
4252 HInstruction* right,
4253 uint32_t dex_pc = kNoDexPc)
4254 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004255
Roland Levillain9867bc72015-08-05 10:21:34 +01004256 template <typename T> T Compute(T x, T y) const { return x - y; }
4257
4258 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004259 return GetBlock()->GetGraph()->GetIntConstant(
4260 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004261 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 return GetBlock()->GetGraph()->GetLongConstant(
4264 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004265 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004266 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4267 return GetBlock()->GetGraph()->GetFloatConstant(
4268 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4269 }
4270 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4271 return GetBlock()->GetGraph()->GetDoubleConstant(
4272 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4273 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004274
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004275 DECLARE_INSTRUCTION(Sub);
4276
4277 private:
4278 DISALLOW_COPY_AND_ASSIGN(HSub);
4279};
4280
Calin Juravle34bacdf2014-10-07 20:23:36 +01004281class HMul : public HBinaryOperation {
4282 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004283 HMul(Primitive::Type result_type,
4284 HInstruction* left,
4285 HInstruction* right,
4286 uint32_t dex_pc = kNoDexPc)
4287 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004288
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004289 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004290
Roland Levillain9867bc72015-08-05 10:21:34 +01004291 template <typename T> T Compute(T x, T y) const { return x * y; }
4292
4293 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004294 return GetBlock()->GetGraph()->GetIntConstant(
4295 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004296 }
4297 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 return GetBlock()->GetGraph()->GetLongConstant(
4299 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004300 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004301 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4302 return GetBlock()->GetGraph()->GetFloatConstant(
4303 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4304 }
4305 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4306 return GetBlock()->GetGraph()->GetDoubleConstant(
4307 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4308 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004309
4310 DECLARE_INSTRUCTION(Mul);
4311
4312 private:
4313 DISALLOW_COPY_AND_ASSIGN(HMul);
4314};
4315
Calin Juravle7c4954d2014-10-28 16:57:40 +00004316class HDiv : public HBinaryOperation {
4317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004318 HDiv(Primitive::Type result_type,
4319 HInstruction* left,
4320 HInstruction* right,
4321 uint32_t dex_pc)
4322 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004323
Roland Levillain9867bc72015-08-05 10:21:34 +01004324 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004325 T ComputeIntegral(T x, T y) const {
4326 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004327 // Our graph structure ensures we never have 0 for `y` during
4328 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004329 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004330 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004331 return (y == -1) ? -x : x / y;
4332 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004333
Roland Levillain31dd3d62016-02-16 12:21:02 +00004334 template <typename T>
4335 T ComputeFP(T x, T y) const {
4336 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4337 return x / y;
4338 }
4339
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004342 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004343 }
4344 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004345 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004346 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4347 }
4348 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4349 return GetBlock()->GetGraph()->GetFloatConstant(
4350 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4351 }
4352 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4353 return GetBlock()->GetGraph()->GetDoubleConstant(
4354 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004355 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004356
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004357 static SideEffects SideEffectsForArchRuntimeCalls() {
4358 // The generated code can use a runtime call.
4359 return SideEffects::CanTriggerGC();
4360 }
4361
Calin Juravle7c4954d2014-10-28 16:57:40 +00004362 DECLARE_INSTRUCTION(Div);
4363
4364 private:
4365 DISALLOW_COPY_AND_ASSIGN(HDiv);
4366};
4367
Calin Juravlebacfec32014-11-14 15:54:36 +00004368class HRem : public HBinaryOperation {
4369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 HRem(Primitive::Type result_type,
4371 HInstruction* left,
4372 HInstruction* right,
4373 uint32_t dex_pc)
4374 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004375
Roland Levillain9867bc72015-08-05 10:21:34 +01004376 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004377 T ComputeIntegral(T x, T y) const {
4378 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 // Our graph structure ensures we never have 0 for `y` during
4380 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004381 DCHECK_NE(y, 0);
4382 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4383 return (y == -1) ? 0 : x % y;
4384 }
4385
Roland Levillain31dd3d62016-02-16 12:21:02 +00004386 template <typename T>
4387 T ComputeFP(T x, T y) const {
4388 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4389 return std::fmod(x, y);
4390 }
4391
Roland Levillain9867bc72015-08-05 10:21:34 +01004392 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004393 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004394 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004395 }
4396 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004397 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004398 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004399 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004400 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4401 return GetBlock()->GetGraph()->GetFloatConstant(
4402 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4403 }
4404 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4405 return GetBlock()->GetGraph()->GetDoubleConstant(
4406 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4407 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004408
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004409 static SideEffects SideEffectsForArchRuntimeCalls() {
4410 return SideEffects::CanTriggerGC();
4411 }
4412
Calin Juravlebacfec32014-11-14 15:54:36 +00004413 DECLARE_INSTRUCTION(Rem);
4414
4415 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004416 DISALLOW_COPY_AND_ASSIGN(HRem);
4417};
4418
Calin Juravled0d48522014-11-04 16:40:20 +00004419class HDivZeroCheck : public HExpression<1> {
4420 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004421 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4422 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004423 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004424 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004425 SetRawInputAt(0, value);
4426 }
4427
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004428 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4429
Calin Juravled0d48522014-11-04 16:40:20 +00004430 bool CanBeMoved() const OVERRIDE { return true; }
4431
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004432 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004433 return true;
4434 }
4435
4436 bool NeedsEnvironment() const OVERRIDE { return true; }
4437 bool CanThrow() const OVERRIDE { return true; }
4438
Calin Juravled0d48522014-11-04 16:40:20 +00004439 DECLARE_INSTRUCTION(DivZeroCheck);
4440
4441 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004442 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4443};
4444
Calin Juravle9aec02f2014-11-18 23:06:35 +00004445class HShl : public HBinaryOperation {
4446 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004447 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004448 HInstruction* value,
4449 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004450 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004451 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4452 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4453 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004454 }
4455
Roland Levillain5b5b9312016-03-22 14:57:31 +00004456 template <typename T>
4457 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4458 return value << (distance & max_shift_distance);
4459 }
4460
4461 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004462 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004463 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004464 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004465 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004466 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004467 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004468 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004469 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4470 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4471 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4472 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004473 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004474 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4475 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004476 LOG(FATAL) << DebugName() << " is not defined for float values";
4477 UNREACHABLE();
4478 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004479 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4480 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004481 LOG(FATAL) << DebugName() << " is not defined for double values";
4482 UNREACHABLE();
4483 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004484
4485 DECLARE_INSTRUCTION(Shl);
4486
4487 private:
4488 DISALLOW_COPY_AND_ASSIGN(HShl);
4489};
4490
4491class HShr : public HBinaryOperation {
4492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004494 HInstruction* value,
4495 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004496 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004497 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4498 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4499 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 }
4501
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 template <typename T>
4503 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4504 return value >> (distance & max_shift_distance);
4505 }
4506
4507 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004509 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004512 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004513 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004514 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4516 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4517 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4518 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004520 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4521 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004522 LOG(FATAL) << DebugName() << " is not defined for float values";
4523 UNREACHABLE();
4524 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004525 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4526 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004527 LOG(FATAL) << DebugName() << " is not defined for double values";
4528 UNREACHABLE();
4529 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004530
4531 DECLARE_INSTRUCTION(Shr);
4532
4533 private:
4534 DISALLOW_COPY_AND_ASSIGN(HShr);
4535};
4536
4537class HUShr : public HBinaryOperation {
4538 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004539 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004540 HInstruction* value,
4541 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004542 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4544 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4545 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004546 }
4547
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 template <typename T>
4549 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4550 typedef typename std::make_unsigned<T>::type V;
4551 V ux = static_cast<V>(value);
4552 return static_cast<T>(ux >> (distance & max_shift_distance));
4553 }
4554
4555 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004560 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004562 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4564 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4565 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4566 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004567 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004568 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4569 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004570 LOG(FATAL) << DebugName() << " is not defined for float values";
4571 UNREACHABLE();
4572 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004573 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4574 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004575 LOG(FATAL) << DebugName() << " is not defined for double values";
4576 UNREACHABLE();
4577 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004578
4579 DECLARE_INSTRUCTION(UShr);
4580
4581 private:
4582 DISALLOW_COPY_AND_ASSIGN(HUShr);
4583};
4584
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004585class HAnd : public HBinaryOperation {
4586 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004587 HAnd(Primitive::Type result_type,
4588 HInstruction* left,
4589 HInstruction* right,
4590 uint32_t dex_pc = kNoDexPc)
4591 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004592
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004593 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004594
Roland Levillaine53bd812016-02-24 14:54:18 +00004595 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004596
4597 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004598 return GetBlock()->GetGraph()->GetIntConstant(
4599 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004600 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004601 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004602 return GetBlock()->GetGraph()->GetLongConstant(
4603 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004605 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4606 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4607 LOG(FATAL) << DebugName() << " is not defined for float values";
4608 UNREACHABLE();
4609 }
4610 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4611 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4612 LOG(FATAL) << DebugName() << " is not defined for double values";
4613 UNREACHABLE();
4614 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004615
4616 DECLARE_INSTRUCTION(And);
4617
4618 private:
4619 DISALLOW_COPY_AND_ASSIGN(HAnd);
4620};
4621
4622class HOr : public HBinaryOperation {
4623 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004624 HOr(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(Or);
4654
4655 private:
4656 DISALLOW_COPY_AND_ASSIGN(HOr);
4657};
4658
4659class HXor : public HBinaryOperation {
4660 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004661 HXor(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(Xor);
4691
4692 private:
4693 DISALLOW_COPY_AND_ASSIGN(HXor);
4694};
4695
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004696class HRor : public HBinaryOperation {
4697 public:
4698 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004699 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004700 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4701 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004702 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004703
Roland Levillain5b5b9312016-03-22 14:57:31 +00004704 template <typename T>
4705 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4706 typedef typename std::make_unsigned<T>::type V;
4707 V ux = static_cast<V>(value);
4708 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004709 return static_cast<T>(ux);
4710 } else {
4711 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004712 return static_cast<T>(ux >> (distance & max_shift_value)) |
4713 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004714 }
4715 }
4716
Roland Levillain5b5b9312016-03-22 14:57:31 +00004717 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004718 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004719 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004720 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004721 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004722 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004723 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004724 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4726 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4727 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4728 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004729 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4731 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004732 LOG(FATAL) << DebugName() << " is not defined for float values";
4733 UNREACHABLE();
4734 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004735 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4736 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004737 LOG(FATAL) << DebugName() << " is not defined for double values";
4738 UNREACHABLE();
4739 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004740
4741 DECLARE_INSTRUCTION(Ror);
4742
4743 private:
4744 DISALLOW_COPY_AND_ASSIGN(HRor);
4745};
4746
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004747// The value of a parameter in this method. Its location depends on
4748// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004749class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004750 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004751 HParameterValue(const DexFile& dex_file,
4752 uint16_t type_index,
4753 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004754 Primitive::Type parameter_type,
4755 bool is_this = false)
4756 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004757 dex_file_(dex_file),
4758 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004759 index_(index) {
4760 SetPackedFlag<kFlagIsThis>(is_this);
4761 SetPackedFlag<kFlagCanBeNull>(!is_this);
4762 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004763
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004764 const DexFile& GetDexFile() const { return dex_file_; }
4765 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004766 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004767 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004768
Vladimir Markoa1de9182016-02-25 11:37:38 +00004769 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4770 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004771
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004772 DECLARE_INSTRUCTION(ParameterValue);
4773
4774 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004775 // Whether or not the parameter value corresponds to 'this' argument.
4776 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4777 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4778 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4779 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4780 "Too many packed fields.");
4781
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004782 const DexFile& dex_file_;
4783 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004784 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004785 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004786 const uint8_t index_;
4787
4788 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4789};
4790
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004791class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004792 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004793 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4794 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004796 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004797 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004798 return true;
4799 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004800
Roland Levillain9867bc72015-08-05 10:21:34 +01004801 template <typename T> T Compute(T x) const { return ~x; }
4802
4803 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004804 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 }
4806 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004807 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004808 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004809 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4810 LOG(FATAL) << DebugName() << " is not defined for float values";
4811 UNREACHABLE();
4812 }
4813 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4814 LOG(FATAL) << DebugName() << " is not defined for double values";
4815 UNREACHABLE();
4816 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004817
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004818 DECLARE_INSTRUCTION(Not);
4819
4820 private:
4821 DISALLOW_COPY_AND_ASSIGN(HNot);
4822};
4823
David Brazdil66d126e2015-04-03 16:02:44 +01004824class HBooleanNot : public HUnaryOperation {
4825 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004826 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4827 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004828
4829 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004830 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004831 return true;
4832 }
4833
Roland Levillain9867bc72015-08-05 10:21:34 +01004834 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004835 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004836 return !x;
4837 }
4838
Roland Levillain9867bc72015-08-05 10:21:34 +01004839 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004840 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004841 }
4842 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4843 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004844 UNREACHABLE();
4845 }
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 }
David Brazdil66d126e2015-04-03 16:02:44 +01004854
4855 DECLARE_INSTRUCTION(BooleanNot);
4856
4857 private:
4858 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4859};
4860
Roland Levillaindff1f282014-11-05 14:15:05 +00004861class HTypeConversion : public HExpression<1> {
4862 public:
4863 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004864 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 : HExpression(result_type,
4866 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4867 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004868 SetRawInputAt(0, input);
4869 DCHECK_NE(input->GetType(), result_type);
4870 }
4871
4872 HInstruction* GetInput() const { return InputAt(0); }
4873 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4874 Primitive::Type GetResultType() const { return GetType(); }
4875
4876 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004877 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004878
Mark Mendelle82549b2015-05-06 10:55:34 -04004879 // Try to statically evaluate the conversion and return a HConstant
4880 // containing the result. If the input cannot be converted, return nullptr.
4881 HConstant* TryStaticEvaluation() const;
4882
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004883 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4884 Primitive::Type result_type) {
4885 // Some architectures may not require the 'GC' side effects, but at this point
4886 // in the compilation process we do not know what architecture we will
4887 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004888 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4889 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004890 return SideEffects::CanTriggerGC();
4891 }
4892 return SideEffects::None();
4893 }
4894
Roland Levillaindff1f282014-11-05 14:15:05 +00004895 DECLARE_INSTRUCTION(TypeConversion);
4896
4897 private:
4898 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4899};
4900
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004901static constexpr uint32_t kNoRegNumber = -1;
4902
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004903class HPhi : public HInstruction {
4904 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004905 HPhi(ArenaAllocator* arena,
4906 uint32_t reg_number,
4907 size_t number_of_inputs,
4908 Primitive::Type type,
4909 uint32_t dex_pc = kNoDexPc)
4910 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004911 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004912 reg_number_(reg_number) {
4913 SetPackedField<TypeField>(ToPhiType(type));
4914 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4915 // Phis are constructed live and marked dead if conflicting or unused.
4916 // Individual steps of SsaBuilder should assume that if a phi has been
4917 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4918 SetPackedFlag<kFlagIsLive>(true);
4919 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004920 }
4921
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004922 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4923 static Primitive::Type ToPhiType(Primitive::Type type) {
4924 switch (type) {
4925 case Primitive::kPrimBoolean:
4926 case Primitive::kPrimByte:
4927 case Primitive::kPrimShort:
4928 case Primitive::kPrimChar:
4929 return Primitive::kPrimInt;
4930 default:
4931 return type;
4932 }
4933 }
4934
David Brazdilffee3d32015-07-06 11:48:53 +01004935 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4936
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004937 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004938
4939 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004940 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004941
Vladimir Markoa1de9182016-02-25 11:37:38 +00004942 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004943 void SetType(Primitive::Type new_type) {
4944 // Make sure that only valid type changes occur. The following are allowed:
4945 // (1) int -> float/ref (primitive type propagation),
4946 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004947 DCHECK(GetType() == new_type ||
4948 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4949 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4950 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4951 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004952 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004953
Vladimir Markoa1de9182016-02-25 11:37:38 +00004954 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4955 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004956
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004957 uint32_t GetRegNumber() const { return reg_number_; }
4958
Vladimir Markoa1de9182016-02-25 11:37:38 +00004959 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4960 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4961 bool IsDead() const { return !IsLive(); }
4962 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004963
David Brazdil77a48ae2015-09-15 12:34:04 +00004964 bool IsVRegEquivalentOf(HInstruction* other) const {
4965 return other != nullptr
4966 && other->IsPhi()
4967 && other->AsPhi()->GetBlock() == GetBlock()
4968 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4969 }
4970
Calin Juravlea4f88312015-04-16 12:57:19 +01004971 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4972 // An equivalent phi is a phi having the same dex register and type.
4973 // It assumes that phis with the same dex register are adjacent.
4974 HPhi* GetNextEquivalentPhiWithSameType() {
4975 HInstruction* next = GetNext();
4976 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4977 if (next->GetType() == GetType()) {
4978 return next->AsPhi();
4979 }
4980 next = next->GetNext();
4981 }
4982 return nullptr;
4983 }
4984
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004985 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004986
David Brazdil1abb4192015-02-17 18:33:36 +00004987 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004988 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004989 return inputs_[index];
4990 }
David Brazdil1abb4192015-02-17 18:33:36 +00004991
4992 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004993 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004994 }
4995
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004996 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004997 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4998 static constexpr size_t kFieldTypeSize =
4999 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5000 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
5001 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
5002 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
5003 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5004 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
5005
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005006 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005007 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005008
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005009 DISALLOW_COPY_AND_ASSIGN(HPhi);
5010};
5011
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012class HNullCheck : public HExpression<1> {
5013 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005014 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5015 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005016 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005017 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005018 SetRawInputAt(0, value);
5019 }
5020
Calin Juravle10e244f2015-01-26 18:54:32 +00005021 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005022 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005023 return true;
5024 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005025
Calin Juravle10e244f2015-01-26 18:54:32 +00005026 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005027
Calin Juravle10e244f2015-01-26 18:54:32 +00005028 bool CanThrow() const OVERRIDE { return true; }
5029
5030 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005031
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005032
5033 DECLARE_INSTRUCTION(NullCheck);
5034
5035 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005036 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5037};
5038
5039class FieldInfo : public ValueObject {
5040 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005041 FieldInfo(MemberOffset field_offset,
5042 Primitive::Type field_type,
5043 bool is_volatile,
5044 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005045 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005046 const DexFile& dex_file,
5047 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005048 : field_offset_(field_offset),
5049 field_type_(field_type),
5050 is_volatile_(is_volatile),
5051 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005052 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005053 dex_file_(dex_file),
5054 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055
5056 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005057 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005058 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005059 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005060 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005061 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005062 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005063
5064 private:
5065 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005066 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005067 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005068 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005069 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005070 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005071 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005072};
5073
5074class HInstanceFieldGet : public HExpression<1> {
5075 public:
5076 HInstanceFieldGet(HInstruction* value,
5077 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005078 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005079 bool is_volatile,
5080 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005081 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005082 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005083 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005084 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005085 : HExpression(field_type,
5086 SideEffects::FieldReadOfType(field_type, is_volatile),
5087 dex_pc),
5088 field_info_(field_offset,
5089 field_type,
5090 is_volatile,
5091 field_idx,
5092 declaring_class_def_index,
5093 dex_file,
5094 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005095 SetRawInputAt(0, value);
5096 }
5097
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005098 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005099
5100 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5101 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5102 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005103 }
5104
Calin Juravle641547a2015-04-21 22:08:51 +01005105 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5106 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005107 }
5108
5109 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005110 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5111 }
5112
Calin Juravle52c48962014-12-16 17:02:57 +00005113 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005114 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005115 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005116 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005117
5118 DECLARE_INSTRUCTION(InstanceFieldGet);
5119
5120 private:
5121 const FieldInfo field_info_;
5122
5123 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5124};
5125
5126class HInstanceFieldSet : public HTemplateInstruction<2> {
5127 public:
5128 HInstanceFieldSet(HInstruction* object,
5129 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005130 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005131 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005132 bool is_volatile,
5133 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005134 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005135 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005136 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005137 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005138 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5139 dex_pc),
5140 field_info_(field_offset,
5141 field_type,
5142 is_volatile,
5143 field_idx,
5144 declaring_class_def_index,
5145 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005146 dex_cache) {
5147 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005148 SetRawInputAt(0, object);
5149 SetRawInputAt(1, value);
5150 }
5151
Calin Juravle641547a2015-04-21 22:08:51 +01005152 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5153 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005154 }
5155
Calin Juravle52c48962014-12-16 17:02:57 +00005156 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005157 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005158 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005159 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005160 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005161 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5162 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005163
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164 DECLARE_INSTRUCTION(InstanceFieldSet);
5165
5166 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005167 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5168 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5169 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5170 "Too many packed fields.");
5171
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005172 const FieldInfo field_info_;
5173
5174 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5175};
5176
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005177class HArrayGet : public HExpression<2> {
5178 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005179 HArrayGet(HInstruction* array,
5180 HInstruction* index,
5181 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005182 uint32_t dex_pc,
5183 SideEffects additional_side_effects = SideEffects::None())
5184 : HExpression(type,
5185 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005186 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187 SetRawInputAt(0, array);
5188 SetRawInputAt(1, index);
5189 }
5190
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005191 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005192 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005193 return true;
5194 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005195 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005196 // TODO: We can be smarter here.
5197 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5198 // which generates the implicit null check. There are cases when these can be removed
5199 // to produce better code. If we ever add optimizations to do so we should allow an
5200 // implicit check here (as long as the address falls in the first page).
5201 return false;
5202 }
5203
David Brazdil4833f5a2015-12-16 10:37:39 +00005204 bool IsEquivalentOf(HArrayGet* other) const {
5205 bool result = (GetDexPc() == other->GetDexPc());
5206 if (kIsDebugBuild && result) {
5207 DCHECK_EQ(GetBlock(), other->GetBlock());
5208 DCHECK_EQ(GetArray(), other->GetArray());
5209 DCHECK_EQ(GetIndex(), other->GetIndex());
5210 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005211 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005212 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005213 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5214 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005215 }
5216 }
5217 return result;
5218 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005219
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005220 HInstruction* GetArray() const { return InputAt(0); }
5221 HInstruction* GetIndex() const { return InputAt(1); }
5222
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 DECLARE_INSTRUCTION(ArrayGet);
5224
5225 private:
5226 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5227};
5228
5229class HArraySet : public HTemplateInstruction<3> {
5230 public:
5231 HArraySet(HInstruction* array,
5232 HInstruction* index,
5233 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005234 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005235 uint32_t dex_pc,
5236 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005237 : HTemplateInstruction(
5238 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005239 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5240 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005241 dex_pc) {
5242 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5243 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5244 SetPackedFlag<kFlagValueCanBeNull>(true);
5245 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 SetRawInputAt(0, array);
5247 SetRawInputAt(1, index);
5248 SetRawInputAt(2, value);
5249 }
5250
Calin Juravle77520bc2015-01-12 18:45:46 +00005251 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005252 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005253 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 }
5255
Mingyao Yang81014cb2015-06-02 03:16:27 -07005256 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005257 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005258
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005259 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005260 // TODO: Same as for ArrayGet.
5261 return false;
5262 }
5263
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005264 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005265 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005266 }
5267
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005268 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005269 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005270 }
5271
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005272 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005273 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005274 }
5275
Vladimir Markoa1de9182016-02-25 11:37:38 +00005276 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5277 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5278 bool StaticTypeOfArrayIsObjectArray() const {
5279 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5280 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005281
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005282 HInstruction* GetArray() const { return InputAt(0); }
5283 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 HInstruction* GetValue() const { return InputAt(2); }
5285
5286 Primitive::Type GetComponentType() const {
5287 // The Dex format does not type floating point index operations. Since the
5288 // `expected_component_type_` is set during building and can therefore not
5289 // be correct, we also check what is the value type. If it is a floating
5290 // point type, we must use that type.
5291 Primitive::Type value_type = GetValue()->GetType();
5292 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5293 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005294 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005295 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005296
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005297 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005298 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005299 }
5300
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005301 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5302 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5303 }
5304
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 DECLARE_INSTRUCTION(ArraySet);
5306
5307 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005308 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5309 static constexpr size_t kFieldExpectedComponentTypeSize =
5310 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5311 static constexpr size_t kFlagNeedsTypeCheck =
5312 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5313 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 // Cached information for the reference_type_info_ so that codegen
5315 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005316 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5317 static constexpr size_t kNumberOfArraySetPackedBits =
5318 kFlagStaticTypeOfArrayIsObjectArray + 1;
5319 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5320 using ExpectedComponentTypeField =
5321 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322
5323 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5324};
5325
5326class HArrayLength : public HExpression<1> {
5327 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005328 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005329 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005330 // Note that arrays do not change length, so the instruction does not
5331 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 SetRawInputAt(0, array);
5333 }
5334
Calin Juravle77520bc2015-01-12 18:45:46 +00005335 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005336 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005337 return true;
5338 }
Calin Juravle641547a2015-04-21 22:08:51 +01005339 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5340 return obj == InputAt(0);
5341 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005342
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343 DECLARE_INSTRUCTION(ArrayLength);
5344
5345 private:
5346 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5347};
5348
5349class HBoundsCheck : public HExpression<2> {
5350 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005351 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5352 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005354 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355 DCHECK(index->GetType() == Primitive::kPrimInt);
5356 SetRawInputAt(0, index);
5357 SetRawInputAt(1, length);
5358 }
5359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005360 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005361 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005362 return true;
5363 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005364
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005365 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005367 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005368
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005369 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370
5371 DECLARE_INSTRUCTION(BoundsCheck);
5372
5373 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5375};
5376
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005377class HSuspendCheck : public HTemplateInstruction<0> {
5378 public:
5379 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005380 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005381
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005382 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005383 return true;
5384 }
5385
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005386 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5387 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005388
5389 DECLARE_INSTRUCTION(SuspendCheck);
5390
5391 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005392 // Only used for code generation, in order to share the same slow path between back edges
5393 // of a same loop.
5394 SlowPathCode* slow_path_;
5395
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005396 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5397};
5398
David Srbecky0cf44932015-12-09 14:09:59 +00005399// Pseudo-instruction which provides the native debugger with mapping information.
5400// It ensures that we can generate line number and local variables at this point.
5401class HNativeDebugInfo : public HTemplateInstruction<0> {
5402 public:
5403 explicit HNativeDebugInfo(uint32_t dex_pc)
5404 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5405
5406 bool NeedsEnvironment() const OVERRIDE {
5407 return true;
5408 }
5409
5410 DECLARE_INSTRUCTION(NativeDebugInfo);
5411
5412 private:
5413 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5414};
5415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005416/**
5417 * Instruction to load a Class object.
5418 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005419class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005420 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005421 HLoadClass(HCurrentMethod* current_method,
5422 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005423 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005424 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005425 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005426 bool needs_access_check,
5427 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005428 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005429 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005430 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005431 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005432 // Referrers class should not need access check. We never inline unverified
5433 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005434 DCHECK(!is_referrers_class || !needs_access_check);
5435
5436 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5437 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5438 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5439 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005440 SetRawInputAt(0, current_method);
5441 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005442
5443 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005444
5445 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005446 // Note that we don't need to test for generate_clinit_check_.
5447 // Whether or not we need to generate the clinit check is processed in
5448 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005449 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005450 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005451 }
5452
5453 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5454
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005455 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005456 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005457
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005458 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005459 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005460 }
5461
Calin Juravle0ba218d2015-05-19 18:46:01 +01005462 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005463 // The entrypoint the code generator is going to call does not do
5464 // clinit of the class.
5465 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005466 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005467 }
5468
5469 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005470 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005471 (!IsReferrersClass() && !IsInDexCache()) ||
5472 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005473 }
5474
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005475
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005476 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005477 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005478 }
5479
Calin Juravleacf735c2015-02-12 15:25:22 +00005480 ReferenceTypeInfo GetLoadedClassRTI() {
5481 return loaded_class_rti_;
5482 }
5483
5484 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5485 // Make sure we only set exact types (the loaded class should never be merged).
5486 DCHECK(rti.IsExact());
5487 loaded_class_rti_ = rti;
5488 }
5489
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005490 const DexFile& GetDexFile() { return dex_file_; }
5491
Vladimir Markoa1de9182016-02-25 11:37:38 +00005492 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005493
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005494 static SideEffects SideEffectsForArchRuntimeCalls() {
5495 return SideEffects::CanTriggerGC();
5496 }
5497
Vladimir Markoa1de9182016-02-25 11:37:38 +00005498 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5499 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5500 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5501 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005502
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005503 DECLARE_INSTRUCTION(LoadClass);
5504
5505 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005506 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5507 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5508 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005509 // Whether this instruction must generate the initialization check.
5510 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005511 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5512 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5513 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5514
5515 const uint16_t type_index_;
5516 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005517
Calin Juravleacf735c2015-02-12 15:25:22 +00005518 ReferenceTypeInfo loaded_class_rti_;
5519
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005520 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5521};
5522
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005523class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005524 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005525 HLoadString(HCurrentMethod* current_method,
5526 uint32_t string_index,
5527 uint32_t dex_pc,
5528 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005529 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 string_index_(string_index) {
5531 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005532 SetRawInputAt(0, current_method);
5533 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005534
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005535 bool CanBeMoved() const OVERRIDE { return true; }
5536
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005537 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5538 return other->AsLoadString()->string_index_ == string_index_;
5539 }
5540
5541 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5542
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005543 uint32_t GetStringIndex() const { return string_index_; }
5544
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005545 // Will call the runtime if the string is not already in the dex cache.
5546 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5547
Vladimir Markodc151b22015-10-15 18:02:30 +01005548 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005549 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005550 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005551
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005552 static SideEffects SideEffectsForArchRuntimeCalls() {
5553 return SideEffects::CanTriggerGC();
5554 }
5555
Vladimir Markoa1de9182016-02-25 11:37:38 +00005556 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5557
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005558 DECLARE_INSTRUCTION(LoadString);
5559
5560 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005561 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5562 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5563 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5564
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005565 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005566
5567 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5568};
5569
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005570/**
5571 * Performs an initialization check on its Class object input.
5572 */
5573class HClinitCheck : public HExpression<1> {
5574 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005575 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005576 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005577 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005578 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5579 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005580 SetRawInputAt(0, constant);
5581 }
5582
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005583 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005584 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005585 return true;
5586 }
5587
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005588 bool NeedsEnvironment() const OVERRIDE {
5589 // May call runtime to initialize the class.
5590 return true;
5591 }
5592
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005593 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005594
5595 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5596
5597 DECLARE_INSTRUCTION(ClinitCheck);
5598
5599 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5601};
5602
5603class HStaticFieldGet : public HExpression<1> {
5604 public:
5605 HStaticFieldGet(HInstruction* cls,
5606 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005607 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005608 bool is_volatile,
5609 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005610 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005611 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005612 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005613 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005614 : HExpression(field_type,
5615 SideEffects::FieldReadOfType(field_type, is_volatile),
5616 dex_pc),
5617 field_info_(field_offset,
5618 field_type,
5619 is_volatile,
5620 field_idx,
5621 declaring_class_def_index,
5622 dex_file,
5623 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005624 SetRawInputAt(0, cls);
5625 }
5626
Calin Juravle52c48962014-12-16 17:02:57 +00005627
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005628 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005629
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005630 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005631 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5632 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005633 }
5634
5635 size_t ComputeHashCode() const OVERRIDE {
5636 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5637 }
5638
Calin Juravle52c48962014-12-16 17:02:57 +00005639 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005640 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5641 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005642 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005643
5644 DECLARE_INSTRUCTION(StaticFieldGet);
5645
5646 private:
5647 const FieldInfo field_info_;
5648
5649 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5650};
5651
5652class HStaticFieldSet : public HTemplateInstruction<2> {
5653 public:
5654 HStaticFieldSet(HInstruction* cls,
5655 HInstruction* value,
5656 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005657 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005658 bool is_volatile,
5659 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005660 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005661 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005662 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005663 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005664 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5665 dex_pc),
5666 field_info_(field_offset,
5667 field_type,
5668 is_volatile,
5669 field_idx,
5670 declaring_class_def_index,
5671 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005672 dex_cache) {
5673 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005674 SetRawInputAt(0, cls);
5675 SetRawInputAt(1, value);
5676 }
5677
Calin Juravle52c48962014-12-16 17:02:57 +00005678 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005679 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5680 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005681 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005682
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005683 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005684 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5685 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005686
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005687 DECLARE_INSTRUCTION(StaticFieldSet);
5688
5689 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005690 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5691 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5692 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5693 "Too many packed fields.");
5694
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005695 const FieldInfo field_info_;
5696
5697 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5698};
5699
Calin Juravlee460d1d2015-09-29 04:52:17 +01005700class HUnresolvedInstanceFieldGet : public HExpression<1> {
5701 public:
5702 HUnresolvedInstanceFieldGet(HInstruction* obj,
5703 Primitive::Type field_type,
5704 uint32_t field_index,
5705 uint32_t dex_pc)
5706 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5707 field_index_(field_index) {
5708 SetRawInputAt(0, obj);
5709 }
5710
5711 bool NeedsEnvironment() const OVERRIDE { return true; }
5712 bool CanThrow() const OVERRIDE { return true; }
5713
5714 Primitive::Type GetFieldType() const { return GetType(); }
5715 uint32_t GetFieldIndex() const { return field_index_; }
5716
5717 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5718
5719 private:
5720 const uint32_t field_index_;
5721
5722 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5723};
5724
5725class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5726 public:
5727 HUnresolvedInstanceFieldSet(HInstruction* obj,
5728 HInstruction* value,
5729 Primitive::Type field_type,
5730 uint32_t field_index,
5731 uint32_t dex_pc)
5732 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005733 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005734 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005735 DCHECK_EQ(field_type, value->GetType());
5736 SetRawInputAt(0, obj);
5737 SetRawInputAt(1, value);
5738 }
5739
5740 bool NeedsEnvironment() const OVERRIDE { return true; }
5741 bool CanThrow() const OVERRIDE { return true; }
5742
Vladimir Markoa1de9182016-02-25 11:37:38 +00005743 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005744 uint32_t GetFieldIndex() const { return field_index_; }
5745
5746 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5747
5748 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005749 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5750 static constexpr size_t kFieldFieldTypeSize =
5751 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5752 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5753 kFieldFieldType + kFieldFieldTypeSize;
5754 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5755 "Too many packed fields.");
5756 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5757
Calin Juravlee460d1d2015-09-29 04:52:17 +01005758 const uint32_t field_index_;
5759
5760 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5761};
5762
5763class HUnresolvedStaticFieldGet : public HExpression<0> {
5764 public:
5765 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5766 uint32_t field_index,
5767 uint32_t dex_pc)
5768 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5769 field_index_(field_index) {
5770 }
5771
5772 bool NeedsEnvironment() const OVERRIDE { return true; }
5773 bool CanThrow() const OVERRIDE { return true; }
5774
5775 Primitive::Type GetFieldType() const { return GetType(); }
5776 uint32_t GetFieldIndex() const { return field_index_; }
5777
5778 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5779
5780 private:
5781 const uint32_t field_index_;
5782
5783 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5784};
5785
5786class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5787 public:
5788 HUnresolvedStaticFieldSet(HInstruction* value,
5789 Primitive::Type field_type,
5790 uint32_t field_index,
5791 uint32_t dex_pc)
5792 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005793 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005794 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005795 DCHECK_EQ(field_type, value->GetType());
5796 SetRawInputAt(0, value);
5797 }
5798
5799 bool NeedsEnvironment() const OVERRIDE { return true; }
5800 bool CanThrow() const OVERRIDE { return true; }
5801
Vladimir Markoa1de9182016-02-25 11:37:38 +00005802 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005803 uint32_t GetFieldIndex() const { return field_index_; }
5804
5805 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5806
5807 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005808 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5809 static constexpr size_t kFieldFieldTypeSize =
5810 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5811 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5812 kFieldFieldType + kFieldFieldTypeSize;
5813 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5814 "Too many packed fields.");
5815 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5816
Calin Juravlee460d1d2015-09-29 04:52:17 +01005817 const uint32_t field_index_;
5818
5819 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5820};
5821
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005822// Implement the move-exception DEX instruction.
5823class HLoadException : public HExpression<0> {
5824 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005825 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5826 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005827
David Brazdilbbd733e2015-08-18 17:48:17 +01005828 bool CanBeNull() const OVERRIDE { return false; }
5829
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005830 DECLARE_INSTRUCTION(LoadException);
5831
5832 private:
5833 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5834};
5835
David Brazdilcb1c0552015-08-04 16:22:25 +01005836// Implicit part of move-exception which clears thread-local exception storage.
5837// Must not be removed because the runtime expects the TLS to get cleared.
5838class HClearException : public HTemplateInstruction<0> {
5839 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005840 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5841 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005842
5843 DECLARE_INSTRUCTION(ClearException);
5844
5845 private:
5846 DISALLOW_COPY_AND_ASSIGN(HClearException);
5847};
5848
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005849class HThrow : public HTemplateInstruction<1> {
5850 public:
5851 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005852 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005853 SetRawInputAt(0, exception);
5854 }
5855
5856 bool IsControlFlow() const OVERRIDE { return true; }
5857
5858 bool NeedsEnvironment() const OVERRIDE { return true; }
5859
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005860 bool CanThrow() const OVERRIDE { return true; }
5861
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005862
5863 DECLARE_INSTRUCTION(Throw);
5864
5865 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005866 DISALLOW_COPY_AND_ASSIGN(HThrow);
5867};
5868
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005869/**
5870 * Implementation strategies for the code generator of a HInstanceOf
5871 * or `HCheckCast`.
5872 */
5873enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005874 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005875 kExactCheck, // Can do a single class compare.
5876 kClassHierarchyCheck, // Can just walk the super class chain.
5877 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5878 kInterfaceCheck, // No optimization yet when checking against an interface.
5879 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005880 kArrayCheck, // No optimization yet when checking against a generic array.
5881 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882};
5883
Roland Levillain86503782016-02-11 19:07:30 +00005884std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5885
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005886class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005887 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005888 HInstanceOf(HInstruction* object,
5889 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005891 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005892 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005893 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005894 dex_pc) {
5895 SetPackedField<TypeCheckKindField>(check_kind);
5896 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005897 SetRawInputAt(0, object);
5898 SetRawInputAt(1, constant);
5899 }
5900
5901 bool CanBeMoved() const OVERRIDE { return true; }
5902
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005903 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005904 return true;
5905 }
5906
5907 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005908 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005909 }
5910
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005911 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005912 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5913 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5914 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5915 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005916
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005917 static bool CanCallRuntime(TypeCheckKind check_kind) {
5918 // Mips currently does runtime calls for any other checks.
5919 return check_kind != TypeCheckKind::kExactCheck;
5920 }
5921
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005922 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005923 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005924 }
5925
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005926 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005927
5928 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005929 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5930 static constexpr size_t kFieldTypeCheckKindSize =
5931 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5932 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5933 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5934 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5935 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005936
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005937 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5938};
5939
Calin Juravleb1498f62015-02-16 13:13:29 +00005940class HBoundType : public HExpression<1> {
5941 public:
David Brazdilf5552582015-12-27 13:36:12 +00005942 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005943 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005944 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5945 SetPackedFlag<kFlagUpperCanBeNull>(true);
5946 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005947 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005948 SetRawInputAt(0, input);
5949 }
5950
David Brazdilf5552582015-12-27 13:36:12 +00005951 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005952 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005953 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005954 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005955
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005956 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005957 DCHECK(GetUpperCanBeNull() || !can_be_null);
5958 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005959 }
5960
Vladimir Markoa1de9182016-02-25 11:37:38 +00005961 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005962
Calin Juravleb1498f62015-02-16 13:13:29 +00005963 DECLARE_INSTRUCTION(BoundType);
5964
5965 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005966 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5967 // is false then CanBeNull() cannot be true).
5968 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5969 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5970 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5971 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5972
Calin Juravleb1498f62015-02-16 13:13:29 +00005973 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005974 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5975 // It is used to bound the type in cases like:
5976 // if (x instanceof ClassX) {
5977 // // uper_bound_ will be ClassX
5978 // }
David Brazdilf5552582015-12-27 13:36:12 +00005979 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005980
5981 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5982};
5983
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005984class HCheckCast : public HTemplateInstruction<2> {
5985 public:
5986 HCheckCast(HInstruction* object,
5987 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005989 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005990 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5991 SetPackedField<TypeCheckKindField>(check_kind);
5992 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005993 SetRawInputAt(0, object);
5994 SetRawInputAt(1, constant);
5995 }
5996
5997 bool CanBeMoved() const OVERRIDE { return true; }
5998
5999 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6000 return true;
6001 }
6002
6003 bool NeedsEnvironment() const OVERRIDE {
6004 // Instruction may throw a CheckCastError.
6005 return true;
6006 }
6007
6008 bool CanThrow() const OVERRIDE { return true; }
6009
Vladimir Markoa1de9182016-02-25 11:37:38 +00006010 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6011 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6012 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6013 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006014
6015 DECLARE_INSTRUCTION(CheckCast);
6016
6017 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006018 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6019 static constexpr size_t kFieldTypeCheckKindSize =
6020 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6021 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6022 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6023 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6024 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006025
6026 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006027};
6028
Calin Juravle27df7582015-04-17 19:12:31 +01006029class HMemoryBarrier : public HTemplateInstruction<0> {
6030 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006031 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006032 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006033 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6034 SetPackedField<BarrierKindField>(barrier_kind);
6035 }
Calin Juravle27df7582015-04-17 19:12:31 +01006036
Vladimir Markoa1de9182016-02-25 11:37:38 +00006037 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006038
6039 DECLARE_INSTRUCTION(MemoryBarrier);
6040
6041 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006042 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6043 static constexpr size_t kFieldBarrierKindSize =
6044 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6045 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6046 kFieldBarrierKind + kFieldBarrierKindSize;
6047 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6048 "Too many packed fields.");
6049 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006050
6051 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6052};
6053
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006054class HMonitorOperation : public HTemplateInstruction<1> {
6055 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006057 kEnter,
6058 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006059 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006060 };
6061
6062 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006063 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006064 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6065 dex_pc) {
6066 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006067 SetRawInputAt(0, object);
6068 }
6069
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006070 // Instruction may go into runtime, so we need an environment.
6071 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006072
6073 bool CanThrow() const OVERRIDE {
6074 // Verifier guarantees that monitor-exit cannot throw.
6075 // This is important because it allows the HGraphBuilder to remove
6076 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6077 return IsEnter();
6078 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006079
Vladimir Markoa1de9182016-02-25 11:37:38 +00006080 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6081 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006082
6083 DECLARE_INSTRUCTION(MonitorOperation);
6084
Calin Juravle52c48962014-12-16 17:02:57 +00006085 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006086 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6087 static constexpr size_t kFieldOperationKindSize =
6088 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6089 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6090 kFieldOperationKind + kFieldOperationKindSize;
6091 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6092 "Too many packed fields.");
6093 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006094
6095 private:
6096 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6097};
6098
David Brazdil74eb1b22015-12-14 11:44:01 +00006099class HSelect : public HExpression<3> {
6100 public:
6101 HSelect(HInstruction* condition,
6102 HInstruction* true_value,
6103 HInstruction* false_value,
6104 uint32_t dex_pc)
6105 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6106 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6107
6108 // First input must be `true_value` or `false_value` to allow codegens to
6109 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6110 // that architectures which implement HSelect as a conditional move also
6111 // will not need to invert the condition.
6112 SetRawInputAt(0, false_value);
6113 SetRawInputAt(1, true_value);
6114 SetRawInputAt(2, condition);
6115 }
6116
6117 HInstruction* GetFalseValue() const { return InputAt(0); }
6118 HInstruction* GetTrueValue() const { return InputAt(1); }
6119 HInstruction* GetCondition() const { return InputAt(2); }
6120
6121 bool CanBeMoved() const OVERRIDE { return true; }
6122 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6123
6124 bool CanBeNull() const OVERRIDE {
6125 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6126 }
6127
6128 DECLARE_INSTRUCTION(Select);
6129
6130 private:
6131 DISALLOW_COPY_AND_ASSIGN(HSelect);
6132};
6133
Vladimir Markof9f64412015-09-02 14:05:49 +01006134class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006135 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006136 MoveOperands(Location source,
6137 Location destination,
6138 Primitive::Type type,
6139 HInstruction* instruction)
6140 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006141
6142 Location GetSource() const { return source_; }
6143 Location GetDestination() const { return destination_; }
6144
6145 void SetSource(Location value) { source_ = value; }
6146 void SetDestination(Location value) { destination_ = value; }
6147
6148 // The parallel move resolver marks moves as "in-progress" by clearing the
6149 // destination (but not the source).
6150 Location MarkPending() {
6151 DCHECK(!IsPending());
6152 Location dest = destination_;
6153 destination_ = Location::NoLocation();
6154 return dest;
6155 }
6156
6157 void ClearPending(Location dest) {
6158 DCHECK(IsPending());
6159 destination_ = dest;
6160 }
6161
6162 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006163 DCHECK(source_.IsValid() || destination_.IsInvalid());
6164 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006165 }
6166
6167 // True if this blocks a move from the given location.
6168 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006169 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006170 }
6171
6172 // A move is redundant if it's been eliminated, if its source and
6173 // destination are the same, or if its destination is unneeded.
6174 bool IsRedundant() const {
6175 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6176 }
6177
6178 // We clear both operands to indicate move that's been eliminated.
6179 void Eliminate() {
6180 source_ = destination_ = Location::NoLocation();
6181 }
6182
6183 bool IsEliminated() const {
6184 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6185 return source_.IsInvalid();
6186 }
6187
Alexey Frunze4dda3372015-06-01 18:31:49 -07006188 Primitive::Type GetType() const { return type_; }
6189
Nicolas Geoffray90218252015-04-15 11:56:51 +01006190 bool Is64BitMove() const {
6191 return Primitive::Is64BitType(type_);
6192 }
6193
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006194 HInstruction* GetInstruction() const { return instruction_; }
6195
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006196 private:
6197 Location source_;
6198 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006199 // The type this move is for.
6200 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006201 // The instruction this move is assocatied with. Null when this move is
6202 // for moving an input in the expected locations of user (including a phi user).
6203 // This is only used in debug mode, to ensure we do not connect interval siblings
6204 // in the same parallel move.
6205 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006206};
6207
Roland Levillainc9285912015-12-18 10:38:42 +00006208std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6209
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006210static constexpr size_t kDefaultNumberOfMoves = 4;
6211
6212class HParallelMove : public HTemplateInstruction<0> {
6213 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006214 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006215 : HTemplateInstruction(SideEffects::None(), dex_pc),
6216 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6217 moves_.reserve(kDefaultNumberOfMoves);
6218 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006219
Nicolas Geoffray90218252015-04-15 11:56:51 +01006220 void AddMove(Location source,
6221 Location destination,
6222 Primitive::Type type,
6223 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006224 DCHECK(source.IsValid());
6225 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006226 if (kIsDebugBuild) {
6227 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006228 for (const MoveOperands& move : moves_) {
6229 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006230 // Special case the situation where the move is for the spill slot
6231 // of the instruction.
6232 if ((GetPrevious() == instruction)
6233 || ((GetPrevious() == nullptr)
6234 && instruction->IsPhi()
6235 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006236 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006237 << "Doing parallel moves for the same instruction.";
6238 } else {
6239 DCHECK(false) << "Doing parallel moves for the same instruction.";
6240 }
6241 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006242 }
6243 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006244 for (const MoveOperands& move : moves_) {
6245 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006246 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006247 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006248 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006249 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006250 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006251 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006252 }
6253
Vladimir Marko225b6462015-09-28 12:17:40 +01006254 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006255 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006256 }
6257
Vladimir Marko225b6462015-09-28 12:17:40 +01006258 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006259
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006260 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006261
6262 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006263 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006264
6265 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6266};
6267
Mark Mendell0616ae02015-04-17 12:49:27 -04006268} // namespace art
6269
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006270#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6271#include "nodes_shared.h"
6272#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006273#ifdef ART_ENABLE_CODEGEN_arm
6274#include "nodes_arm.h"
6275#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006276#ifdef ART_ENABLE_CODEGEN_arm64
6277#include "nodes_arm64.h"
6278#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006279#ifdef ART_ENABLE_CODEGEN_x86
6280#include "nodes_x86.h"
6281#endif
6282
6283namespace art {
6284
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006285class HGraphVisitor : public ValueObject {
6286 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006287 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6288 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006289
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006290 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006291 virtual void VisitBasicBlock(HBasicBlock* block);
6292
Roland Levillain633021e2014-10-01 14:12:25 +01006293 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006294 void VisitInsertionOrder();
6295
Roland Levillain633021e2014-10-01 14:12:25 +01006296 // Visit the graph following dominator tree reverse post-order.
6297 void VisitReversePostOrder();
6298
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006299 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006300
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006301 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006302#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006303 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6304
6305 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6306
6307#undef DECLARE_VISIT_INSTRUCTION
6308
6309 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006310 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006311
6312 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6313};
6314
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006315class HGraphDelegateVisitor : public HGraphVisitor {
6316 public:
6317 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6318 virtual ~HGraphDelegateVisitor() {}
6319
6320 // Visit functions that delegate to to super class.
6321#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006322 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006323
6324 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6325
6326#undef DECLARE_VISIT_INSTRUCTION
6327
6328 private:
6329 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6330};
6331
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006332class HInsertionOrderIterator : public ValueObject {
6333 public:
6334 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6335
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006336 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006337 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006338 void Advance() { ++index_; }
6339
6340 private:
6341 const HGraph& graph_;
6342 size_t index_;
6343
6344 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6345};
6346
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006347class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006348 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006349 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6350 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006351 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006352 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006353
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006354 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6355 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006356 void Advance() { ++index_; }
6357
6358 private:
6359 const HGraph& graph_;
6360 size_t index_;
6361
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006362 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006363};
6364
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006365class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006366 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006367 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006368 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006369 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006370 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006371 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006372
6373 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006374 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006375 void Advance() { --index_; }
6376
6377 private:
6378 const HGraph& graph_;
6379 size_t index_;
6380
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006381 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006382};
6383
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006384class HLinearPostOrderIterator : public ValueObject {
6385 public:
6386 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006387 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006388
6389 bool Done() const { return index_ == 0; }
6390
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006391 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006392
6393 void Advance() {
6394 --index_;
6395 DCHECK_GE(index_, 0U);
6396 }
6397
6398 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006399 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006400 size_t index_;
6401
6402 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6403};
6404
6405class HLinearOrderIterator : public ValueObject {
6406 public:
6407 explicit HLinearOrderIterator(const HGraph& graph)
6408 : order_(graph.GetLinearOrder()), index_(0) {}
6409
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006410 bool Done() const { return index_ == order_.size(); }
6411 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006412 void Advance() { ++index_; }
6413
6414 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006415 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006416 size_t index_;
6417
6418 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6419};
6420
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006421// Iterator over the blocks that art part of the loop. Includes blocks part
6422// of an inner loop. The order in which the blocks are iterated is on their
6423// block id.
6424class HBlocksInLoopIterator : public ValueObject {
6425 public:
6426 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6427 : blocks_in_loop_(info.GetBlocks()),
6428 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6429 index_(0) {
6430 if (!blocks_in_loop_.IsBitSet(index_)) {
6431 Advance();
6432 }
6433 }
6434
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006435 bool Done() const { return index_ == blocks_.size(); }
6436 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006437 void Advance() {
6438 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006439 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006440 if (blocks_in_loop_.IsBitSet(index_)) {
6441 break;
6442 }
6443 }
6444 }
6445
6446 private:
6447 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006448 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006449 size_t index_;
6450
6451 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6452};
6453
Mingyao Yang3584bce2015-05-19 16:01:59 -07006454// Iterator over the blocks that art part of the loop. Includes blocks part
6455// of an inner loop. The order in which the blocks are iterated is reverse
6456// post order.
6457class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6458 public:
6459 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6460 : blocks_in_loop_(info.GetBlocks()),
6461 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6462 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006463 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006464 Advance();
6465 }
6466 }
6467
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006468 bool Done() const { return index_ == blocks_.size(); }
6469 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006470 void Advance() {
6471 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006472 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6473 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006474 break;
6475 }
6476 }
6477 }
6478
6479 private:
6480 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006481 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006482 size_t index_;
6483
6484 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6485};
6486
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006487inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006488 if (constant->IsIntConstant()) {
6489 return constant->AsIntConstant()->GetValue();
6490 } else if (constant->IsLongConstant()) {
6491 return constant->AsLongConstant()->GetValue();
6492 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006493 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006494 return 0;
6495 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006496}
6497
Vladimir Marko58155012015-08-19 12:49:41 +00006498inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6499 // For the purposes of the compiler, the dex files must actually be the same object
6500 // if we want to safely treat them as the same. This is especially important for JIT
6501 // as custom class loaders can open the same underlying file (or memory) multiple
6502 // times and provide different class resolution but no two class loaders should ever
6503 // use the same DexFile object - doing so is an unsupported hack that can lead to
6504 // all sorts of weird failures.
6505 return &lhs == &rhs;
6506}
6507
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006508#define INSTRUCTION_TYPE_CHECK(type, super) \
6509 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6510 inline const H##type* HInstruction::As##type() const { \
6511 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6512 } \
6513 inline H##type* HInstruction::As##type() { \
6514 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6515 }
6516
6517 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6518#undef INSTRUCTION_TYPE_CHECK
6519
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006520class SwitchTable : public ValueObject {
6521 public:
6522 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6523 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6524 int32_t table_offset = instruction.VRegB_31t();
6525 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6526 if (sparse) {
6527 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6528 } else {
6529 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6530 }
6531 num_entries_ = table[1];
6532 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6533 }
6534
6535 uint16_t GetNumEntries() const {
6536 return num_entries_;
6537 }
6538
6539 void CheckIndex(size_t index) const {
6540 if (sparse_) {
6541 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6542 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6543 } else {
6544 // In a packed table, we have the starting key and num_entries_ values.
6545 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6546 }
6547 }
6548
6549 int32_t GetEntryAt(size_t index) const {
6550 CheckIndex(index);
6551 return values_[index];
6552 }
6553
6554 uint32_t GetDexPcForIndex(size_t index) const {
6555 CheckIndex(index);
6556 return dex_pc_ +
6557 (reinterpret_cast<const int16_t*>(values_ + index) -
6558 reinterpret_cast<const int16_t*>(&instruction_));
6559 }
6560
6561 // Index of the first value in the table.
6562 size_t GetFirstValueIndex() const {
6563 if (sparse_) {
6564 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6565 return num_entries_;
6566 } else {
6567 // In a packed table, we have the starting key and num_entries_ values.
6568 return 1;
6569 }
6570 }
6571
6572 private:
6573 const Instruction& instruction_;
6574 const uint32_t dex_pc_;
6575
6576 // Whether this is a sparse-switch table (or a packed-switch one).
6577 const bool sparse_;
6578
6579 // This can't be const as it needs to be computed off of the given instruction, and complicated
6580 // expressions in the initializer list seemed very ugly.
6581 uint16_t num_entries_;
6582
6583 const int32_t* values_;
6584
6585 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6586};
6587
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006588// Create space in `blocks` for adding `number_of_new_blocks` entries
6589// starting at location `at`. Blocks after `at` are moved accordingly.
6590inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6591 size_t number_of_new_blocks,
6592 size_t after) {
6593 DCHECK_LT(after, blocks->size());
6594 size_t old_size = blocks->size();
6595 size_t new_size = old_size + number_of_new_blocks;
6596 blocks->resize(new_size);
6597 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6598}
6599
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006600} // namespace art
6601
6602#endif // ART_COMPILER_OPTIMIZING_NODES_H_