blob: 0be75b5a2f3918a655f4ec2d8a4f350a3a927ef5 [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
Roland Levillain1a653882016-03-18 18:05:57 +00002432 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002433 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002434 // Is this constant 0 in the arithmetic sense?
2435 virtual bool IsArithmeticZero() const { return false; }
2436 // Is this constant a 0-bit pattern?
2437 virtual bool IsZeroBitPattern() const { return false; }
2438 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002439 virtual bool IsOne() const { return false; }
2440
David Brazdil77a48ae2015-09-15 12:34:04 +00002441 virtual uint64_t GetValueAsUint64() const = 0;
2442
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002443 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002444
2445 private:
2446 DISALLOW_COPY_AND_ASSIGN(HConstant);
2447};
2448
2449class HNullConstant : public HConstant {
2450 public:
2451 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2452 return true;
2453 }
2454
David Brazdil77a48ae2015-09-15 12:34:04 +00002455 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2456
Roland Levillain9867bc72015-08-05 10:21:34 +01002457 size_t ComputeHashCode() const OVERRIDE { return 0; }
2458
Roland Levillain1a653882016-03-18 18:05:57 +00002459 // The null constant representation is a 0-bit pattern.
2460 virtual bool IsZeroBitPattern() const { return true; }
2461
Roland Levillain9867bc72015-08-05 10:21:34 +01002462 DECLARE_INSTRUCTION(NullConstant);
2463
2464 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002466
2467 friend class HGraph;
2468 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2469};
2470
2471// Constants of the type int. Those can be from Dex instructions, or
2472// synthesized (for example with the if-eqz instruction).
2473class HIntConstant : public HConstant {
2474 public:
2475 int32_t GetValue() const { return value_; }
2476
David Brazdil9f389d42015-10-01 14:32:56 +01002477 uint64_t GetValueAsUint64() const OVERRIDE {
2478 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2479 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002480
Roland Levillain9867bc72015-08-05 10:21:34 +01002481 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002482 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002483 return other->AsIntConstant()->value_ == value_;
2484 }
2485
2486 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2487
2488 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002489 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2490 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002491 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2492
Roland Levillain1a653882016-03-18 18:05:57 +00002493 // Integer constants are used to encode Boolean values as well,
2494 // where 1 means true and 0 means false.
2495 bool IsTrue() const { return GetValue() == 1; }
2496 bool IsFalse() const { return GetValue() == 0; }
2497
Roland Levillain9867bc72015-08-05 10:21:34 +01002498 DECLARE_INSTRUCTION(IntConstant);
2499
2500 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002501 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2502 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2503 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2504 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002505
2506 const int32_t value_;
2507
2508 friend class HGraph;
2509 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2510 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2511 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2512};
2513
2514class HLongConstant : public HConstant {
2515 public:
2516 int64_t GetValue() const { return value_; }
2517
David Brazdil77a48ae2015-09-15 12:34:04 +00002518 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2519
Roland Levillain9867bc72015-08-05 10:21:34 +01002520 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002521 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002522 return other->AsLongConstant()->value_ == value_;
2523 }
2524
2525 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2526
2527 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002528 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2529 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002530 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2531
2532 DECLARE_INSTRUCTION(LongConstant);
2533
2534 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002535 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2536 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002537
2538 const int64_t value_;
2539
2540 friend class HGraph;
2541 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2542};
Dave Allison20dfc792014-06-16 20:44:29 -07002543
Roland Levillain31dd3d62016-02-16 12:21:02 +00002544class HFloatConstant : public HConstant {
2545 public:
2546 float GetValue() const { return value_; }
2547
2548 uint64_t GetValueAsUint64() const OVERRIDE {
2549 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2550 }
2551
2552 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2553 DCHECK(other->IsFloatConstant()) << other->DebugName();
2554 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2555 }
2556
2557 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2558
2559 bool IsMinusOne() const OVERRIDE {
2560 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2561 }
Roland Levillain1a653882016-03-18 18:05:57 +00002562 bool IsArithmeticZero() const OVERRIDE {
2563 return std::fpclassify(value_) == FP_ZERO;
2564 }
2565 bool IsArithmeticPositiveZero() const {
2566 return IsArithmeticZero() && !std::signbit(value_);
2567 }
2568 bool IsArithmeticNegativeZero() const {
2569 return IsArithmeticZero() && std::signbit(value_);
2570 }
2571 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002572 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002573 }
2574 bool IsOne() const OVERRIDE {
2575 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2576 }
2577 bool IsNaN() const {
2578 return std::isnan(value_);
2579 }
2580
2581 DECLARE_INSTRUCTION(FloatConstant);
2582
2583 private:
2584 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2585 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2586 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2587 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2588
2589 const float value_;
2590
2591 // Only the SsaBuilder and HGraph can create floating-point constants.
2592 friend class SsaBuilder;
2593 friend class HGraph;
2594 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2595};
2596
2597class HDoubleConstant : public HConstant {
2598 public:
2599 double GetValue() const { return value_; }
2600
2601 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2602
2603 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2604 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2605 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2606 }
2607
2608 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2609
2610 bool IsMinusOne() const OVERRIDE {
2611 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2612 }
Roland Levillain1a653882016-03-18 18:05:57 +00002613 bool IsArithmeticZero() const OVERRIDE {
2614 return std::fpclassify(value_) == FP_ZERO;
2615 }
2616 bool IsArithmeticPositiveZero() const {
2617 return IsArithmeticZero() && !std::signbit(value_);
2618 }
2619 bool IsArithmeticNegativeZero() const {
2620 return IsArithmeticZero() && std::signbit(value_);
2621 }
2622 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002623 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002624 }
2625 bool IsOne() const OVERRIDE {
2626 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2627 }
2628 bool IsNaN() const {
2629 return std::isnan(value_);
2630 }
2631
2632 DECLARE_INSTRUCTION(DoubleConstant);
2633
2634 private:
2635 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2636 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2637 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2638 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2639
2640 const double value_;
2641
2642 // Only the SsaBuilder and HGraph can create floating-point constants.
2643 friend class SsaBuilder;
2644 friend class HGraph;
2645 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2646};
2647
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002648// Conditional branch. A block ending with an HIf instruction must have
2649// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002650class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002652 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2653 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002654 SetRawInputAt(0, input);
2655 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002656
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002657 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002658
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002659 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002660 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002661 }
2662
2663 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002664 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002665 }
2666
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002667 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002668
2669 private:
2670 DISALLOW_COPY_AND_ASSIGN(HIf);
2671};
2672
David Brazdilfc6a86a2015-06-26 10:33:45 +00002673
2674// Abstract instruction which marks the beginning and/or end of a try block and
2675// links it to the respective exception handlers. Behaves the same as a Goto in
2676// non-exceptional control flow.
2677// Normal-flow successor is stored at index zero, exception handlers under
2678// higher indices in no particular order.
2679class HTryBoundary : public HTemplateInstruction<0> {
2680 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002681 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002682 kEntry,
2683 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002684 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002685 };
2686
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002687 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002688 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2689 SetPackedField<BoundaryKindField>(kind);
2690 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002691
2692 bool IsControlFlow() const OVERRIDE { return true; }
2693
2694 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002695 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002696
David Brazdild26a4112015-11-10 11:07:31 +00002697 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2698 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2699 }
2700
David Brazdilfc6a86a2015-06-26 10:33:45 +00002701 // Returns whether `handler` is among its exception handlers (non-zero index
2702 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002703 bool HasExceptionHandler(const HBasicBlock& handler) const {
2704 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002705 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002706 }
2707
2708 // If not present already, adds `handler` to its block's list of exception
2709 // handlers.
2710 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002711 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002712 GetBlock()->AddSuccessor(handler);
2713 }
2714 }
2715
Vladimir Markoa1de9182016-02-25 11:37:38 +00002716 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2717 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002718
David Brazdilffee3d32015-07-06 11:48:53 +01002719 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2720
David Brazdilfc6a86a2015-06-26 10:33:45 +00002721 DECLARE_INSTRUCTION(TryBoundary);
2722
2723 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002724 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2725 static constexpr size_t kFieldBoundaryKindSize =
2726 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2727 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2728 kFieldBoundaryKind + kFieldBoundaryKindSize;
2729 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2730 "Too many packed fields.");
2731 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002732
2733 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2734};
2735
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002736// Deoptimize to interpreter, upon checking a condition.
2737class HDeoptimize : public HTemplateInstruction<1> {
2738 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002739 // We set CanTriggerGC to prevent any intermediate address to be live
2740 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002741 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002742 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002743 SetRawInputAt(0, cond);
2744 }
2745
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002746 bool CanBeMoved() const OVERRIDE { return true; }
2747 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2748 return true;
2749 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002750 bool NeedsEnvironment() const OVERRIDE { return true; }
2751 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002752
2753 DECLARE_INSTRUCTION(Deoptimize);
2754
2755 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002756 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2757};
2758
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002759// Represents the ArtMethod that was passed as a first argument to
2760// the method. It is used by instructions that depend on it, like
2761// instructions that work with the dex cache.
2762class HCurrentMethod : public HExpression<0> {
2763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002764 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2765 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002766
2767 DECLARE_INSTRUCTION(CurrentMethod);
2768
2769 private:
2770 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2771};
2772
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002773// Fetches an ArtMethod from the virtual table or the interface method table
2774// of a class.
2775class HClassTableGet : public HExpression<1> {
2776 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002777 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002778 kVTable,
2779 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002780 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002781 };
2782 HClassTableGet(HInstruction* cls,
2783 Primitive::Type type,
2784 TableKind kind,
2785 size_t index,
2786 uint32_t dex_pc)
2787 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002788 index_(index) {
2789 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002790 SetRawInputAt(0, cls);
2791 }
2792
2793 bool CanBeMoved() const OVERRIDE { return true; }
2794 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2795 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002796 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002797 }
2798
Vladimir Markoa1de9182016-02-25 11:37:38 +00002799 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002800 size_t GetIndex() const { return index_; }
2801
2802 DECLARE_INSTRUCTION(ClassTableGet);
2803
2804 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002805 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2806 static constexpr size_t kFieldTableKindSize =
2807 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2808 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2809 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2810 "Too many packed fields.");
2811 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2812
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002813 // The index of the ArtMethod in the table.
2814 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002815
2816 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2817};
2818
Mark Mendellfe57faa2015-09-18 09:26:15 -04002819// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2820// have one successor for each entry in the switch table, and the final successor
2821// will be the block containing the next Dex opcode.
2822class HPackedSwitch : public HTemplateInstruction<1> {
2823 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002824 HPackedSwitch(int32_t start_value,
2825 uint32_t num_entries,
2826 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002827 uint32_t dex_pc = kNoDexPc)
2828 : HTemplateInstruction(SideEffects::None(), dex_pc),
2829 start_value_(start_value),
2830 num_entries_(num_entries) {
2831 SetRawInputAt(0, input);
2832 }
2833
2834 bool IsControlFlow() const OVERRIDE { return true; }
2835
2836 int32_t GetStartValue() const { return start_value_; }
2837
Vladimir Marko211c2112015-09-24 16:52:33 +01002838 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002839
2840 HBasicBlock* GetDefaultBlock() const {
2841 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002842 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002843 }
2844 DECLARE_INSTRUCTION(PackedSwitch);
2845
2846 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002847 const int32_t start_value_;
2848 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002849
2850 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2851};
2852
Roland Levillain88cb1752014-10-20 16:36:47 +01002853class HUnaryOperation : public HExpression<1> {
2854 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002855 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2856 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002857 SetRawInputAt(0, input);
2858 }
2859
2860 HInstruction* GetInput() const { return InputAt(0); }
2861 Primitive::Type GetResultType() const { return GetType(); }
2862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002863 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002864 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002865 return true;
2866 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002867
Roland Levillain31dd3d62016-02-16 12:21:02 +00002868 // Try to statically evaluate `this` and return a HConstant
2869 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002870 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002871 HConstant* TryStaticEvaluation() const;
2872
2873 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002874 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2875 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002876 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2877 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002878
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002879 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002880
2881 private:
2882 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2883};
2884
Dave Allison20dfc792014-06-16 20:44:29 -07002885class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002886 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002887 HBinaryOperation(Primitive::Type result_type,
2888 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002889 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002890 SideEffects side_effects = SideEffects::None(),
2891 uint32_t dex_pc = kNoDexPc)
2892 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002893 SetRawInputAt(0, left);
2894 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002895 }
2896
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002897 HInstruction* GetLeft() const { return InputAt(0); }
2898 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002899 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002900
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002901 virtual bool IsCommutative() const { return false; }
2902
2903 // Put constant on the right.
2904 // Returns whether order is changed.
2905 bool OrderInputsWithConstantOnTheRight() {
2906 HInstruction* left = InputAt(0);
2907 HInstruction* right = InputAt(1);
2908 if (left->IsConstant() && !right->IsConstant()) {
2909 ReplaceInput(right, 0);
2910 ReplaceInput(left, 1);
2911 return true;
2912 }
2913 return false;
2914 }
2915
2916 // Order inputs by instruction id, but favor constant on the right side.
2917 // This helps GVN for commutative ops.
2918 void OrderInputs() {
2919 DCHECK(IsCommutative());
2920 HInstruction* left = InputAt(0);
2921 HInstruction* right = InputAt(1);
2922 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2923 return;
2924 }
2925 if (OrderInputsWithConstantOnTheRight()) {
2926 return;
2927 }
2928 // Order according to instruction id.
2929 if (left->GetId() > right->GetId()) {
2930 ReplaceInput(right, 0);
2931 ReplaceInput(left, 1);
2932 }
2933 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002934
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002935 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002936 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002937 return true;
2938 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002939
Roland Levillain31dd3d62016-02-16 12:21:02 +00002940 // Try to statically evaluate `this` and return a HConstant
2941 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002942 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002943 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002944
2945 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002946 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2947 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002948 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2949 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002950 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002951 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2952 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002953 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2954 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002955 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2956 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002957 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002958 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2959 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002960
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002961 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002962 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002963 HConstant* GetConstantRight() const;
2964
2965 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002966 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002967 HInstruction* GetLeastConstantLeft() const;
2968
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002969 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002970
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002971 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002972 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2973};
2974
Mark Mendellc4701932015-04-10 13:18:51 -04002975// The comparison bias applies for floating point operations and indicates how NaN
2976// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002977enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002978 kNoBias, // bias is not applicable (i.e. for long operation)
2979 kGtBias, // return 1 for NaN comparisons
2980 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002981 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002982};
2983
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2985
Dave Allison20dfc792014-06-16 20:44:29 -07002986class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002987 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002988 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002989 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2990 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2991 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002992
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002993 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002994 // `instruction`, and disregard moves in between.
2995 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002996
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002997 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002998
2999 virtual IfCondition GetCondition() const = 0;
3000
Mark Mendellc4701932015-04-10 13:18:51 -04003001 virtual IfCondition GetOppositeCondition() const = 0;
3002
Vladimir Markoa1de9182016-02-25 11:37:38 +00003003 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003004 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3005
Vladimir Markoa1de9182016-02-25 11:37:38 +00003006 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3007 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003008
3009 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003010 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003011 }
3012
Roland Levillain4fa13f62015-07-06 18:11:54 +01003013 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003014 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003015 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003016 if (if_cond == kCondNE) {
3017 return true;
3018 } else if (if_cond == kCondEQ) {
3019 return false;
3020 }
3021 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003022 }
3023
3024 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003025 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003026 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003027 if (if_cond == kCondEQ) {
3028 return true;
3029 } else if (if_cond == kCondNE) {
3030 return false;
3031 }
3032 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003033 }
3034
Roland Levillain31dd3d62016-02-16 12:21:02 +00003035 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003036 // Needed if we merge a HCompare into a HCondition.
3037 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3038 static constexpr size_t kFieldComparisonBiasSize =
3039 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3040 static constexpr size_t kNumberOfConditionPackedBits =
3041 kFieldComparisonBias + kFieldComparisonBiasSize;
3042 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3043 using ComparisonBiasField =
3044 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3045
Roland Levillain31dd3d62016-02-16 12:21:02 +00003046 template <typename T>
3047 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3048
3049 template <typename T>
3050 int32_t CompareFP(T x, T y) const {
3051 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3052 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3053 // Handle the bias.
3054 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3055 }
3056
3057 // Return an integer constant containing the result of a condition evaluated at compile time.
3058 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3059 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3060 }
3061
Dave Allison20dfc792014-06-16 20:44:29 -07003062 private:
3063 DISALLOW_COPY_AND_ASSIGN(HCondition);
3064};
3065
3066// Instruction to check if two inputs are equal to each other.
3067class HEqual : public HCondition {
3068 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003069 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3070 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003071
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003072 bool IsCommutative() const OVERRIDE { return true; }
3073
Vladimir Marko9e23df52015-11-10 17:14:35 +00003074 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3075 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076 return MakeConstantCondition(true, GetDexPc());
3077 }
3078 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3079 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3080 }
3081 // In the following Evaluate methods, a HCompare instruction has
3082 // been merged into this HEqual instruction; evaluate it as
3083 // `Compare(x, y) == 0`.
3084 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3085 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3086 GetDexPc());
3087 }
3088 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3089 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3090 }
3091 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3092 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003093 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003094
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003095 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003096
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003097 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003098 return kCondEQ;
3099 }
3100
Mark Mendellc4701932015-04-10 13:18:51 -04003101 IfCondition GetOppositeCondition() const OVERRIDE {
3102 return kCondNE;
3103 }
3104
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003105 private:
Aart Bike9f37602015-10-09 11:15:55 -07003106 template <typename T> bool Compute(T x, T y) const { return x == y; }
3107
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003108 DISALLOW_COPY_AND_ASSIGN(HEqual);
3109};
3110
Dave Allison20dfc792014-06-16 20:44:29 -07003111class HNotEqual : public HCondition {
3112 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003113 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3114 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003115
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003116 bool IsCommutative() const OVERRIDE { return true; }
3117
Vladimir Marko9e23df52015-11-10 17:14:35 +00003118 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3119 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120 return MakeConstantCondition(false, GetDexPc());
3121 }
3122 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3123 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3124 }
3125 // In the following Evaluate methods, a HCompare instruction has
3126 // been merged into this HNotEqual instruction; evaluate it as
3127 // `Compare(x, y) != 0`.
3128 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3129 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3130 }
3131 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3132 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3133 }
3134 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3135 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003136 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003137
Dave Allison20dfc792014-06-16 20:44:29 -07003138 DECLARE_INSTRUCTION(NotEqual);
3139
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003140 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003141 return kCondNE;
3142 }
3143
Mark Mendellc4701932015-04-10 13:18:51 -04003144 IfCondition GetOppositeCondition() const OVERRIDE {
3145 return kCondEQ;
3146 }
3147
Dave Allison20dfc792014-06-16 20:44:29 -07003148 private:
Aart Bike9f37602015-10-09 11:15:55 -07003149 template <typename T> bool Compute(T x, T y) const { return x != y; }
3150
Dave Allison20dfc792014-06-16 20:44:29 -07003151 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3152};
3153
3154class HLessThan : public HCondition {
3155 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003156 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3157 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003158
Roland Levillain9867bc72015-08-05 10:21:34 +01003159 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003160 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003161 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162 // In the following Evaluate methods, a HCompare instruction has
3163 // been merged into this HLessThan instruction; evaluate it as
3164 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003165 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003166 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3167 }
3168 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3169 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3170 }
3171 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3172 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003173 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003174
Dave Allison20dfc792014-06-16 20:44:29 -07003175 DECLARE_INSTRUCTION(LessThan);
3176
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003177 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003178 return kCondLT;
3179 }
3180
Mark Mendellc4701932015-04-10 13:18:51 -04003181 IfCondition GetOppositeCondition() const OVERRIDE {
3182 return kCondGE;
3183 }
3184
Dave Allison20dfc792014-06-16 20:44:29 -07003185 private:
Aart Bike9f37602015-10-09 11:15:55 -07003186 template <typename T> bool Compute(T x, T y) const { return x < y; }
3187
Dave Allison20dfc792014-06-16 20:44:29 -07003188 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3189};
3190
3191class HLessThanOrEqual : public HCondition {
3192 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003193 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3194 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003195
Roland Levillain9867bc72015-08-05 10:21:34 +01003196 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003197 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003198 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 // In the following Evaluate methods, a HCompare instruction has
3200 // been merged into this HLessThanOrEqual instruction; evaluate it as
3201 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003202 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003203 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3204 }
3205 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3206 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3207 }
3208 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3209 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003210 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003211
Dave Allison20dfc792014-06-16 20:44:29 -07003212 DECLARE_INSTRUCTION(LessThanOrEqual);
3213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003214 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003215 return kCondLE;
3216 }
3217
Mark Mendellc4701932015-04-10 13:18:51 -04003218 IfCondition GetOppositeCondition() const OVERRIDE {
3219 return kCondGT;
3220 }
3221
Dave Allison20dfc792014-06-16 20:44:29 -07003222 private:
Aart Bike9f37602015-10-09 11:15:55 -07003223 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3224
Dave Allison20dfc792014-06-16 20:44:29 -07003225 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3226};
3227
3228class HGreaterThan : public HCondition {
3229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003230 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3231 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003232
Roland Levillain9867bc72015-08-05 10:21:34 +01003233 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003234 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003235 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 // In the following Evaluate methods, a HCompare instruction has
3237 // been merged into this HGreaterThan instruction; evaluate it as
3238 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003239 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3241 }
3242 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3243 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3244 }
3245 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3246 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003247 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003248
Dave Allison20dfc792014-06-16 20:44:29 -07003249 DECLARE_INSTRUCTION(GreaterThan);
3250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003251 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003252 return kCondGT;
3253 }
3254
Mark Mendellc4701932015-04-10 13:18:51 -04003255 IfCondition GetOppositeCondition() const OVERRIDE {
3256 return kCondLE;
3257 }
3258
Dave Allison20dfc792014-06-16 20:44:29 -07003259 private:
Aart Bike9f37602015-10-09 11:15:55 -07003260 template <typename T> bool Compute(T x, T y) const { return x > y; }
3261
Dave Allison20dfc792014-06-16 20:44:29 -07003262 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3263};
3264
3265class HGreaterThanOrEqual : public HCondition {
3266 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003267 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3268 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003269
Roland Levillain9867bc72015-08-05 10:21:34 +01003270 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003271 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003272 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 // In the following Evaluate methods, a HCompare instruction has
3274 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3275 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003276 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3278 }
3279 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3280 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3281 }
3282 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3283 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003284 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003285
Dave Allison20dfc792014-06-16 20:44:29 -07003286 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3287
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003288 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003289 return kCondGE;
3290 }
3291
Mark Mendellc4701932015-04-10 13:18:51 -04003292 IfCondition GetOppositeCondition() const OVERRIDE {
3293 return kCondLT;
3294 }
3295
Dave Allison20dfc792014-06-16 20:44:29 -07003296 private:
Aart Bike9f37602015-10-09 11:15:55 -07003297 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3298
Dave Allison20dfc792014-06-16 20:44:29 -07003299 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3300};
3301
Aart Bike9f37602015-10-09 11:15:55 -07003302class HBelow : public HCondition {
3303 public:
3304 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3305 : HCondition(first, second, dex_pc) {}
3306
3307 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003308 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003309 }
3310 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003311 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3312 }
3313 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3314 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3315 LOG(FATAL) << DebugName() << " is not defined for float values";
3316 UNREACHABLE();
3317 }
3318 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3319 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3320 LOG(FATAL) << DebugName() << " is not defined for double values";
3321 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003322 }
3323
3324 DECLARE_INSTRUCTION(Below);
3325
3326 IfCondition GetCondition() const OVERRIDE {
3327 return kCondB;
3328 }
3329
3330 IfCondition GetOppositeCondition() const OVERRIDE {
3331 return kCondAE;
3332 }
3333
3334 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003335 template <typename T> bool Compute(T x, T y) const {
3336 return MakeUnsigned(x) < MakeUnsigned(y);
3337 }
Aart Bike9f37602015-10-09 11:15:55 -07003338
3339 DISALLOW_COPY_AND_ASSIGN(HBelow);
3340};
3341
3342class HBelowOrEqual : public HCondition {
3343 public:
3344 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3345 : HCondition(first, second, dex_pc) {}
3346
3347 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003348 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003349 }
3350 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3352 }
3353 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3354 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3355 LOG(FATAL) << DebugName() << " is not defined for float values";
3356 UNREACHABLE();
3357 }
3358 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3359 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3360 LOG(FATAL) << DebugName() << " is not defined for double values";
3361 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003362 }
3363
3364 DECLARE_INSTRUCTION(BelowOrEqual);
3365
3366 IfCondition GetCondition() const OVERRIDE {
3367 return kCondBE;
3368 }
3369
3370 IfCondition GetOppositeCondition() const OVERRIDE {
3371 return kCondA;
3372 }
3373
3374 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003375 template <typename T> bool Compute(T x, T y) const {
3376 return MakeUnsigned(x) <= MakeUnsigned(y);
3377 }
Aart Bike9f37602015-10-09 11:15:55 -07003378
3379 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3380};
3381
3382class HAbove : public HCondition {
3383 public:
3384 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3385 : HCondition(first, second, dex_pc) {}
3386
3387 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003388 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003389 }
3390 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003391 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3392 }
3393 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3394 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3395 LOG(FATAL) << DebugName() << " is not defined for float values";
3396 UNREACHABLE();
3397 }
3398 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3399 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3400 LOG(FATAL) << DebugName() << " is not defined for double values";
3401 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003402 }
3403
3404 DECLARE_INSTRUCTION(Above);
3405
3406 IfCondition GetCondition() const OVERRIDE {
3407 return kCondA;
3408 }
3409
3410 IfCondition GetOppositeCondition() const OVERRIDE {
3411 return kCondBE;
3412 }
3413
3414 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003415 template <typename T> bool Compute(T x, T y) const {
3416 return MakeUnsigned(x) > MakeUnsigned(y);
3417 }
Aart Bike9f37602015-10-09 11:15:55 -07003418
3419 DISALLOW_COPY_AND_ASSIGN(HAbove);
3420};
3421
3422class HAboveOrEqual : public HCondition {
3423 public:
3424 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3425 : HCondition(first, second, dex_pc) {}
3426
3427 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003429 }
3430 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003431 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3432 }
3433 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3434 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3435 LOG(FATAL) << DebugName() << " is not defined for float values";
3436 UNREACHABLE();
3437 }
3438 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3439 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3440 LOG(FATAL) << DebugName() << " is not defined for double values";
3441 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003442 }
3443
3444 DECLARE_INSTRUCTION(AboveOrEqual);
3445
3446 IfCondition GetCondition() const OVERRIDE {
3447 return kCondAE;
3448 }
3449
3450 IfCondition GetOppositeCondition() const OVERRIDE {
3451 return kCondB;
3452 }
3453
3454 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003455 template <typename T> bool Compute(T x, T y) const {
3456 return MakeUnsigned(x) >= MakeUnsigned(y);
3457 }
Aart Bike9f37602015-10-09 11:15:55 -07003458
3459 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3460};
Dave Allison20dfc792014-06-16 20:44:29 -07003461
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003462// Instruction to check how two inputs compare to each other.
3463// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3464class HCompare : public HBinaryOperation {
3465 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003466 // Note that `comparison_type` is the type of comparison performed
3467 // between the comparison's inputs, not the type of the instantiated
3468 // HCompare instruction (which is always Primitive::kPrimInt).
3469 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003470 HInstruction* first,
3471 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003472 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003473 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003474 : HBinaryOperation(Primitive::kPrimInt,
3475 first,
3476 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003477 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003478 dex_pc) {
3479 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003480 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3481 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003482 }
3483
Roland Levillain9867bc72015-08-05 10:21:34 +01003484 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003485 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3486
3487 template <typename T>
3488 int32_t ComputeFP(T x, T y) const {
3489 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3490 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3491 // Handle the bias.
3492 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3493 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003494
Roland Levillain9867bc72015-08-05 10:21:34 +01003495 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3497 // reach this code path when processing a freshly built HIR
3498 // graph. However HCompare integer instructions can be synthesized
3499 // by the instruction simplifier to implement IntegerCompare and
3500 // IntegerSignum intrinsics, so we have to handle this case.
3501 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003502 }
3503 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3505 }
3506 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3507 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3508 }
3509 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3510 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003511 }
3512
Calin Juravleddb7df22014-11-25 20:56:51 +00003513 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003514 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003515 }
3516
Vladimir Markoa1de9182016-02-25 11:37:38 +00003517 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003518
Roland Levillain31dd3d62016-02-16 12:21:02 +00003519 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003520 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003521 bool IsGtBias() const {
3522 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003523 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003524 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003525
Roland Levillain1693a1f2016-03-15 14:57:31 +00003526 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3527 // Comparisons do not require a runtime call in any back end.
3528 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003529 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003530
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003531 DECLARE_INSTRUCTION(Compare);
3532
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003534 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3535 static constexpr size_t kFieldComparisonBiasSize =
3536 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3537 static constexpr size_t kNumberOfComparePackedBits =
3538 kFieldComparisonBias + kFieldComparisonBiasSize;
3539 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3540 using ComparisonBiasField =
3541 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3542
Roland Levillain31dd3d62016-02-16 12:21:02 +00003543 // Return an integer constant containing the result of a comparison evaluated at compile time.
3544 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3545 DCHECK(value == -1 || value == 0 || value == 1) << value;
3546 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3547 }
3548
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003549 private:
3550 DISALLOW_COPY_AND_ASSIGN(HCompare);
3551};
3552
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003553// A local in the graph. Corresponds to a Dex register.
3554class HLocal : public HTemplateInstruction<0> {
3555 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003556 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003557 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003558
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003559 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003560
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003561 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003562
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003563 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003564 // The Dex register number.
3565 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003566
3567 DISALLOW_COPY_AND_ASSIGN(HLocal);
3568};
3569
3570// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003571class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003572 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003573 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3574 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003575 SetRawInputAt(0, local);
3576 }
3577
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003578 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3579
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003580 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003581
3582 private:
3583 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3584};
3585
3586// Store a value in a given local. This instruction has two inputs: the value
3587// and the local.
3588class HStoreLocal : public HTemplateInstruction<2> {
3589 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003590 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3591 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003592 SetRawInputAt(0, local);
3593 SetRawInputAt(1, value);
3594 }
3595
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003596 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3597
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003598 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003599
3600 private:
3601 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3602};
3603
David Brazdil6de19382016-01-08 17:37:10 +00003604class HNewInstance : public HExpression<2> {
3605 public:
3606 HNewInstance(HInstruction* cls,
3607 HCurrentMethod* current_method,
3608 uint32_t dex_pc,
3609 uint16_t type_index,
3610 const DexFile& dex_file,
3611 bool can_throw,
3612 bool finalizable,
3613 QuickEntrypointEnum entrypoint)
3614 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3615 type_index_(type_index),
3616 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003617 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003618 SetPackedFlag<kFlagCanThrow>(can_throw);
3619 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003620 SetRawInputAt(0, cls);
3621 SetRawInputAt(1, current_method);
3622 }
3623
3624 uint16_t GetTypeIndex() const { return type_index_; }
3625 const DexFile& GetDexFile() const { return dex_file_; }
3626
3627 // Calls runtime so needs an environment.
3628 bool NeedsEnvironment() const OVERRIDE { return true; }
3629
3630 // It may throw when called on type that's not instantiable/accessible.
3631 // It can throw OOME.
3632 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003633 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003634
Vladimir Markoa1de9182016-02-25 11:37:38 +00003635 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003636
3637 bool CanBeNull() const OVERRIDE { return false; }
3638
3639 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3640
3641 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3642 entrypoint_ = entrypoint;
3643 }
3644
3645 bool IsStringAlloc() const;
3646
3647 DECLARE_INSTRUCTION(NewInstance);
3648
3649 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003650 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3651 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3652 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3653 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3654 "Too many packed fields.");
3655
David Brazdil6de19382016-01-08 17:37:10 +00003656 const uint16_t type_index_;
3657 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003658 QuickEntrypointEnum entrypoint_;
3659
3660 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3661};
3662
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003663enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003664#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3665 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003666#include "intrinsics_list.h"
3667 kNone,
3668 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3669#undef INTRINSICS_LIST
3670#undef OPTIMIZING_INTRINSICS
3671};
3672std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3673
Agi Csaki05f20562015-08-19 14:58:14 -07003674enum IntrinsicNeedsEnvironmentOrCache {
3675 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3676 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003677};
3678
Aart Bik5d75afe2015-12-14 11:57:01 -08003679enum IntrinsicSideEffects {
3680 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3681 kReadSideEffects, // Intrinsic may read heap memory.
3682 kWriteSideEffects, // Intrinsic may write heap memory.
3683 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3684};
3685
3686enum IntrinsicExceptions {
3687 kNoThrow, // Intrinsic does not throw any exceptions.
3688 kCanThrow // Intrinsic may throw exceptions.
3689};
3690
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003691class HInvoke : public HInstruction {
3692 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003693 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003694
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003695 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003696
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003697 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003698 SetRawInputAt(index, argument);
3699 }
3700
Roland Levillain3e3d7332015-04-28 11:00:54 +01003701 // Return the number of arguments. This number can be lower than
3702 // the number of inputs returned by InputCount(), as some invoke
3703 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3704 // inputs at the end of their list of inputs.
3705 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3706
Vladimir Markoa1de9182016-02-25 11:37:38 +00003707 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003708
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003709 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003710 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003711
Vladimir Markoa1de9182016-02-25 11:37:38 +00003712 InvokeType GetOriginalInvokeType() const {
3713 return GetPackedField<OriginalInvokeTypeField>();
3714 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003715
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003716 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003717 return intrinsic_;
3718 }
3719
Aart Bik5d75afe2015-12-14 11:57:01 -08003720 void SetIntrinsic(Intrinsics intrinsic,
3721 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3722 IntrinsicSideEffects side_effects,
3723 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003724
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003725 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003726 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003727 }
3728
Vladimir Markoa1de9182016-02-25 11:37:38 +00003729 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003730
3731 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3732
3733 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3734 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3735 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003736
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003737 uint32_t* GetIntrinsicOptimizations() {
3738 return &intrinsic_optimizations_;
3739 }
3740
3741 const uint32_t* GetIntrinsicOptimizations() const {
3742 return &intrinsic_optimizations_;
3743 }
3744
3745 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3746
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003747 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003748
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003749 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003750 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3751 static constexpr size_t kFieldOriginalInvokeTypeSize =
3752 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3753 static constexpr size_t kFieldReturnType =
3754 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3755 static constexpr size_t kFieldReturnTypeSize =
3756 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3757 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3758 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3759 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3760 using OriginalInvokeTypeField =
3761 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3762 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3763
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003764 HInvoke(ArenaAllocator* arena,
3765 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003766 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003767 Primitive::Type return_type,
3768 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003769 uint32_t dex_method_index,
3770 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003771 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003772 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003773 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003774 inputs_(number_of_arguments + number_of_other_inputs,
3775 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003776 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003777 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003778 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003779 SetPackedField<ReturnTypeField>(return_type);
3780 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3781 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003782 }
3783
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003784 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003785 return inputs_[index];
3786 }
3787
David Brazdil1abb4192015-02-17 18:33:36 +00003788 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003789 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003790 }
3791
Vladimir Markoa1de9182016-02-25 11:37:38 +00003792 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003793
Roland Levillain3e3d7332015-04-28 11:00:54 +01003794 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003795 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003796 const uint32_t dex_method_index_;
3797 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003798
3799 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3800 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003801
3802 private:
3803 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3804};
3805
Calin Juravle175dc732015-08-25 15:42:32 +01003806class HInvokeUnresolved : public HInvoke {
3807 public:
3808 HInvokeUnresolved(ArenaAllocator* arena,
3809 uint32_t number_of_arguments,
3810 Primitive::Type return_type,
3811 uint32_t dex_pc,
3812 uint32_t dex_method_index,
3813 InvokeType invoke_type)
3814 : HInvoke(arena,
3815 number_of_arguments,
3816 0u /* number_of_other_inputs */,
3817 return_type,
3818 dex_pc,
3819 dex_method_index,
3820 invoke_type) {
3821 }
3822
3823 DECLARE_INSTRUCTION(InvokeUnresolved);
3824
3825 private:
3826 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3827};
3828
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003829class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003830 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003831 // Requirements of this method call regarding the class
3832 // initialization (clinit) check of its declaring class.
3833 enum class ClinitCheckRequirement {
3834 kNone, // Class already initialized.
3835 kExplicit, // Static call having explicit clinit check as last input.
3836 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003837 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003838 };
3839
Vladimir Marko58155012015-08-19 12:49:41 +00003840 // Determines how to load the target ArtMethod*.
3841 enum class MethodLoadKind {
3842 // Use a String init ArtMethod* loaded from Thread entrypoints.
3843 kStringInit,
3844
3845 // Use the method's own ArtMethod* loaded by the register allocator.
3846 kRecursive,
3847
3848 // Use ArtMethod* at a known address, embed the direct address in the code.
3849 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3850 kDirectAddress,
3851
3852 // Use ArtMethod* at an address that will be known at link time, embed the direct
3853 // address in the code. If the image is relocatable, emit .patch_oat entry.
3854 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3855 // the image relocatable or not.
3856 kDirectAddressWithFixup,
3857
Vladimir Markoa1de9182016-02-25 11:37:38 +00003858 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003859 // Used when we need to use the dex cache, for example for invoke-static that
3860 // may cause class initialization (the entry may point to a resolution method),
3861 // and we know that we can access the dex cache arrays using a PC-relative load.
3862 kDexCachePcRelative,
3863
3864 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3865 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3866 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3867 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3868 kDexCacheViaMethod,
3869 };
3870
3871 // Determines the location of the code pointer.
3872 enum class CodePtrLocation {
3873 // Recursive call, use local PC-relative call instruction.
3874 kCallSelf,
3875
3876 // Use PC-relative call instruction patched at link time.
3877 // Used for calls within an oat file, boot->boot or app->app.
3878 kCallPCRelative,
3879
3880 // Call to a known target address, embed the direct address in code.
3881 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3882 kCallDirect,
3883
3884 // Call to a target address that will be known at link time, embed the direct
3885 // address in code. If the image is relocatable, emit .patch_oat entry.
3886 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3887 // the image relocatable or not.
3888 kCallDirectWithFixup,
3889
3890 // Use code pointer from the ArtMethod*.
3891 // Used when we don't know the target code. This is also the last-resort-kind used when
3892 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3893 kCallArtMethod,
3894 };
3895
3896 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003897 MethodLoadKind method_load_kind;
3898 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003899 // The method load data holds
3900 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3901 // Note that there are multiple string init methods, each having its own offset.
3902 // - the method address for kDirectAddress
3903 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003904 uint64_t method_load_data;
3905 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003906 };
3907
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003908 HInvokeStaticOrDirect(ArenaAllocator* arena,
3909 uint32_t number_of_arguments,
3910 Primitive::Type return_type,
3911 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003912 uint32_t method_index,
3913 MethodReference target_method,
3914 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003915 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003916 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003917 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003918 : HInvoke(arena,
3919 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003920 // There is potentially one extra argument for the HCurrentMethod node, and
3921 // potentially one other if the clinit check is explicit, and potentially
3922 // one other if the method is a string factory.
3923 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3924 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3925 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003926 return_type,
3927 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003928 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003929 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003930 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003931 dispatch_info_(dispatch_info) {
3932 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3933 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3934 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003935
Vladimir Markodc151b22015-10-15 18:02:30 +01003936 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003937 bool had_current_method_input = HasCurrentMethodInput();
3938 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3939
3940 // Using the current method is the default and once we find a better
3941 // method load kind, we should not go back to using the current method.
3942 DCHECK(had_current_method_input || !needs_current_method_input);
3943
3944 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003945 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3946 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003947 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003948 dispatch_info_ = dispatch_info;
3949 }
3950
Vladimir Markoc53c0792015-11-19 15:48:33 +00003951 void AddSpecialInput(HInstruction* input) {
3952 // We allow only one special input.
3953 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3954 DCHECK(InputCount() == GetSpecialInputIndex() ||
3955 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3956 InsertInputAt(GetSpecialInputIndex(), input);
3957 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003958
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003959 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003960 // We access the method via the dex cache so we can't do an implicit null check.
3961 // TODO: for intrinsics we can generate implicit null checks.
3962 return false;
3963 }
3964
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003965 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003966 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003967 }
3968
Vladimir Markoc53c0792015-11-19 15:48:33 +00003969 // Get the index of the special input, if any.
3970 //
David Brazdil6de19382016-01-08 17:37:10 +00003971 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3972 // method pointer; otherwise there may be one platform-specific special input,
3973 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003974 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003975 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003976
Vladimir Markoa1de9182016-02-25 11:37:38 +00003977 InvokeType GetOptimizedInvokeType() const {
3978 return GetPackedField<OptimizedInvokeTypeField>();
3979 }
3980
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003981 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003982 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003983 }
3984
Vladimir Marko58155012015-08-19 12:49:41 +00003985 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3986 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3987 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003988 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003989 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003990 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003991 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003992 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3993 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003994 bool HasCurrentMethodInput() const {
3995 // This function can be called only after the invoke has been fully initialized by the builder.
3996 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003997 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003998 return true;
3999 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004000 DCHECK(InputCount() == GetSpecialInputIndex() ||
4001 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004002 return false;
4003 }
4004 }
Vladimir Marko58155012015-08-19 12:49:41 +00004005 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4006 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004007 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004008
4009 int32_t GetStringInitOffset() const {
4010 DCHECK(IsStringInit());
4011 return dispatch_info_.method_load_data;
4012 }
4013
4014 uint64_t GetMethodAddress() const {
4015 DCHECK(HasMethodAddress());
4016 return dispatch_info_.method_load_data;
4017 }
4018
4019 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004020 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004021 return dispatch_info_.method_load_data;
4022 }
4023
4024 uint64_t GetDirectCodePtr() const {
4025 DCHECK(HasDirectCodePtr());
4026 return dispatch_info_.direct_code_ptr;
4027 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004028
Vladimir Markoa1de9182016-02-25 11:37:38 +00004029 ClinitCheckRequirement GetClinitCheckRequirement() const {
4030 return GetPackedField<ClinitCheckRequirementField>();
4031 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004032
Roland Levillain4c0eb422015-04-24 16:43:49 +01004033 // Is this instruction a call to a static method?
4034 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004035 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004036 }
4037
Vladimir Markofbb184a2015-11-13 14:47:00 +00004038 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4039 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4040 // instruction; only relevant for static calls with explicit clinit check.
4041 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004042 DCHECK(IsStaticWithExplicitClinitCheck());
4043 size_t last_input_index = InputCount() - 1;
4044 HInstruction* last_input = InputAt(last_input_index);
4045 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004046 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004047 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004048 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004049 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004050 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004051 }
4052
David Brazdilbc9ab162016-01-20 14:50:19 +00004053 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004054 DCHECK(IsStringInit());
4055 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004056 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004057 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004058 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004059 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004060 }
4061
Roland Levillain4c0eb422015-04-24 16:43:49 +01004062 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004063 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004064 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004065 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004066 }
4067
4068 // Is this a call to a static method whose declaring class has an
4069 // implicit intialization check requirement?
4070 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004071 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004072 }
4073
Vladimir Markob554b5a2015-11-06 12:57:55 +00004074 // Does this method load kind need the current method as an input?
4075 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4076 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4077 }
4078
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004079 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004080
Roland Levillain4c0eb422015-04-24 16:43:49 +01004081 protected:
4082 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4083 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4084 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4085 HInstruction* input = input_record.GetInstruction();
4086 // `input` is the last input of a static invoke marked as having
4087 // an explicit clinit check. It must either be:
4088 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4089 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4090 DCHECK(input != nullptr);
4091 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4092 }
4093 return input_record;
4094 }
4095
Vladimir Markoc53c0792015-11-19 15:48:33 +00004096 void InsertInputAt(size_t index, HInstruction* input);
4097 void RemoveInputAt(size_t index);
4098
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004099 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004100 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4101 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4102 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4103 static constexpr size_t kFieldClinitCheckRequirement =
4104 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4105 static constexpr size_t kFieldClinitCheckRequirementSize =
4106 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4107 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4108 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4109 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4110 "Too many packed fields.");
4111 using OptimizedInvokeTypeField =
4112 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4113 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4114 kFieldClinitCheckRequirement,
4115 kFieldClinitCheckRequirementSize>;
4116
Vladimir Marko58155012015-08-19 12:49:41 +00004117 // The target method may refer to different dex file or method index than the original
4118 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4119 // in derived class to increase visibility.
4120 MethodReference target_method_;
4121 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004122
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004123 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004124};
Vladimir Markof64242a2015-12-01 14:58:23 +00004125std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004126std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004127
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004128class HInvokeVirtual : public HInvoke {
4129 public:
4130 HInvokeVirtual(ArenaAllocator* arena,
4131 uint32_t number_of_arguments,
4132 Primitive::Type return_type,
4133 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004134 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004135 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004136 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004137 vtable_index_(vtable_index) {}
4138
Calin Juravle641547a2015-04-21 22:08:51 +01004139 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004140 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004141 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004142 }
4143
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004144 uint32_t GetVTableIndex() const { return vtable_index_; }
4145
4146 DECLARE_INSTRUCTION(InvokeVirtual);
4147
4148 private:
4149 const uint32_t vtable_index_;
4150
4151 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4152};
4153
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004154class HInvokeInterface : public HInvoke {
4155 public:
4156 HInvokeInterface(ArenaAllocator* arena,
4157 uint32_t number_of_arguments,
4158 Primitive::Type return_type,
4159 uint32_t dex_pc,
4160 uint32_t dex_method_index,
4161 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004162 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004163 imt_index_(imt_index) {}
4164
Calin Juravle641547a2015-04-21 22:08:51 +01004165 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004166 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004167 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004168 }
4169
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004170 uint32_t GetImtIndex() const { return imt_index_; }
4171 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4172
4173 DECLARE_INSTRUCTION(InvokeInterface);
4174
4175 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004176 const uint32_t imt_index_;
4177
4178 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4179};
4180
Roland Levillain88cb1752014-10-20 16:36:47 +01004181class HNeg : public HUnaryOperation {
4182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004183 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004184 : HUnaryOperation(result_type, input, dex_pc) {
4185 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4186 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004187
Roland Levillain9867bc72015-08-05 10:21:34 +01004188 template <typename T> T Compute(T x) const { return -x; }
4189
4190 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004191 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004192 }
4193 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004195 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004196 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4197 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4198 }
4199 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4200 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4201 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004202
Roland Levillain88cb1752014-10-20 16:36:47 +01004203 DECLARE_INSTRUCTION(Neg);
4204
4205 private:
4206 DISALLOW_COPY_AND_ASSIGN(HNeg);
4207};
4208
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004209class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004210 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004211 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004212 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004213 uint32_t dex_pc,
4214 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004215 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004216 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004217 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004218 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004219 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004220 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004221 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004222 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004223 }
4224
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004225 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004226 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004227
4228 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004229 bool NeedsEnvironment() const OVERRIDE { return true; }
4230
Mingyao Yang0c365e62015-03-31 15:09:29 -07004231 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4232 bool CanThrow() const OVERRIDE { return true; }
4233
Calin Juravle10e244f2015-01-26 18:54:32 +00004234 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004235
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004236 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4237
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004238 DECLARE_INSTRUCTION(NewArray);
4239
4240 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004241 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004242 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004243 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004244
4245 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4246};
4247
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004248class HAdd : public HBinaryOperation {
4249 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004250 HAdd(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 Geoffrayd8ee7372014-03-28 15:43:40 +00004255
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004256 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004257
Roland Levillain9867bc72015-08-05 10:21:34 +01004258 template <typename T> T Compute(T x, T y) const { return x + y; }
4259
4260 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004261 return GetBlock()->GetGraph()->GetIntConstant(
4262 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004263 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004264 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004265 return GetBlock()->GetGraph()->GetLongConstant(
4266 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004267 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004268 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4269 return GetBlock()->GetGraph()->GetFloatConstant(
4270 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4271 }
4272 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4273 return GetBlock()->GetGraph()->GetDoubleConstant(
4274 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4275 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004276
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004277 DECLARE_INSTRUCTION(Add);
4278
4279 private:
4280 DISALLOW_COPY_AND_ASSIGN(HAdd);
4281};
4282
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004283class HSub : public HBinaryOperation {
4284 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004285 HSub(Primitive::Type result_type,
4286 HInstruction* left,
4287 HInstruction* right,
4288 uint32_t dex_pc = kNoDexPc)
4289 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +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 Levillain93445682014-10-06 19:24:02 +01004296 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004297 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 Levillain93445682014-10-06 19:24:02 +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 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004309
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004310 DECLARE_INSTRUCTION(Sub);
4311
4312 private:
4313 DISALLOW_COPY_AND_ASSIGN(HSub);
4314};
4315
Calin Juravle34bacdf2014-10-07 20:23:36 +01004316class HMul : public HBinaryOperation {
4317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004318 HMul(Primitive::Type result_type,
4319 HInstruction* left,
4320 HInstruction* right,
4321 uint32_t dex_pc = kNoDexPc)
4322 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004324 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004325
Roland Levillain9867bc72015-08-05 10:21:34 +01004326 template <typename T> T Compute(T x, T y) const { return x * y; }
4327
4328 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004329 return GetBlock()->GetGraph()->GetIntConstant(
4330 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004331 }
4332 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004333 return GetBlock()->GetGraph()->GetLongConstant(
4334 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004335 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004336 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4337 return GetBlock()->GetGraph()->GetFloatConstant(
4338 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4339 }
4340 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4341 return GetBlock()->GetGraph()->GetDoubleConstant(
4342 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4343 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004344
4345 DECLARE_INSTRUCTION(Mul);
4346
4347 private:
4348 DISALLOW_COPY_AND_ASSIGN(HMul);
4349};
4350
Calin Juravle7c4954d2014-10-28 16:57:40 +00004351class HDiv : public HBinaryOperation {
4352 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004353 HDiv(Primitive::Type result_type,
4354 HInstruction* left,
4355 HInstruction* right,
4356 uint32_t dex_pc)
4357 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004358
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004360 T ComputeIntegral(T x, T y) const {
4361 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004362 // Our graph structure ensures we never have 0 for `y` during
4363 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004364 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004365 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004366 return (y == -1) ? -x : x / y;
4367 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004368
Roland Levillain31dd3d62016-02-16 12:21:02 +00004369 template <typename T>
4370 T ComputeFP(T x, T y) const {
4371 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4372 return x / y;
4373 }
4374
Roland Levillain9867bc72015-08-05 10:21:34 +01004375 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004376 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004377 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 }
4379 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004380 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004381 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4382 }
4383 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4384 return GetBlock()->GetGraph()->GetFloatConstant(
4385 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4386 }
4387 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4388 return GetBlock()->GetGraph()->GetDoubleConstant(
4389 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004390 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004391
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004392 static SideEffects SideEffectsForArchRuntimeCalls() {
4393 // The generated code can use a runtime call.
4394 return SideEffects::CanTriggerGC();
4395 }
4396
Calin Juravle7c4954d2014-10-28 16:57:40 +00004397 DECLARE_INSTRUCTION(Div);
4398
4399 private:
4400 DISALLOW_COPY_AND_ASSIGN(HDiv);
4401};
4402
Calin Juravlebacfec32014-11-14 15:54:36 +00004403class HRem : public HBinaryOperation {
4404 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 HRem(Primitive::Type result_type,
4406 HInstruction* left,
4407 HInstruction* right,
4408 uint32_t dex_pc)
4409 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004410
Roland Levillain9867bc72015-08-05 10:21:34 +01004411 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004412 T ComputeIntegral(T x, T y) const {
4413 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004414 // Our graph structure ensures we never have 0 for `y` during
4415 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004416 DCHECK_NE(y, 0);
4417 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4418 return (y == -1) ? 0 : x % y;
4419 }
4420
Roland Levillain31dd3d62016-02-16 12:21:02 +00004421 template <typename T>
4422 T ComputeFP(T x, T y) const {
4423 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4424 return std::fmod(x, y);
4425 }
4426
Roland Levillain9867bc72015-08-05 10:21:34 +01004427 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004428 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004429 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004430 }
4431 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004432 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004433 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004434 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004435 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4436 return GetBlock()->GetGraph()->GetFloatConstant(
4437 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4438 }
4439 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4440 return GetBlock()->GetGraph()->GetDoubleConstant(
4441 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4442 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004443
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004444 static SideEffects SideEffectsForArchRuntimeCalls() {
4445 return SideEffects::CanTriggerGC();
4446 }
4447
Calin Juravlebacfec32014-11-14 15:54:36 +00004448 DECLARE_INSTRUCTION(Rem);
4449
4450 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004451 DISALLOW_COPY_AND_ASSIGN(HRem);
4452};
4453
Calin Juravled0d48522014-11-04 16:40:20 +00004454class HDivZeroCheck : public HExpression<1> {
4455 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004456 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4457 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004458 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004459 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004460 SetRawInputAt(0, value);
4461 }
4462
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004463 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4464
Calin Juravled0d48522014-11-04 16:40:20 +00004465 bool CanBeMoved() const OVERRIDE { return true; }
4466
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004467 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004468 return true;
4469 }
4470
4471 bool NeedsEnvironment() const OVERRIDE { return true; }
4472 bool CanThrow() const OVERRIDE { return true; }
4473
Calin Juravled0d48522014-11-04 16:40:20 +00004474 DECLARE_INSTRUCTION(DivZeroCheck);
4475
4476 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004477 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4478};
4479
Calin Juravle9aec02f2014-11-18 23:06:35 +00004480class HShl : public HBinaryOperation {
4481 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004483 HInstruction* value,
4484 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004485 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004486 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4487 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4488 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004489 }
4490
Roland Levillain5b5b9312016-03-22 14:57:31 +00004491 template <typename T>
4492 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4493 return value << (distance & max_shift_distance);
4494 }
4495
4496 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004497 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004498 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004500 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004501 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004503 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004504 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4505 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4506 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4507 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004509 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4510 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004511 LOG(FATAL) << DebugName() << " is not defined for float values";
4512 UNREACHABLE();
4513 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004514 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4515 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004516 LOG(FATAL) << DebugName() << " is not defined for double values";
4517 UNREACHABLE();
4518 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004519
4520 DECLARE_INSTRUCTION(Shl);
4521
4522 private:
4523 DISALLOW_COPY_AND_ASSIGN(HShl);
4524};
4525
4526class HShr : public HBinaryOperation {
4527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004528 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004529 HInstruction* value,
4530 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004531 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004532 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4533 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4534 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004535 }
4536
Roland Levillain5b5b9312016-03-22 14:57:31 +00004537 template <typename T>
4538 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4539 return value >> (distance & max_shift_distance);
4540 }
4541
4542 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004543 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004544 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004545 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004549 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004550 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4551 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4552 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4553 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004554 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004555 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4556 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004557 LOG(FATAL) << DebugName() << " is not defined for float values";
4558 UNREACHABLE();
4559 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004560 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4561 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004562 LOG(FATAL) << DebugName() << " is not defined for double values";
4563 UNREACHABLE();
4564 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004565
4566 DECLARE_INSTRUCTION(Shr);
4567
4568 private:
4569 DISALLOW_COPY_AND_ASSIGN(HShr);
4570};
4571
4572class HUShr : public HBinaryOperation {
4573 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004574 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004575 HInstruction* value,
4576 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004577 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004578 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4579 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4580 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004581 }
4582
Roland Levillain5b5b9312016-03-22 14:57:31 +00004583 template <typename T>
4584 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4585 typedef typename std::make_unsigned<T>::type V;
4586 V ux = static_cast<V>(value);
4587 return static_cast<T>(ux >> (distance & max_shift_distance));
4588 }
4589
4590 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004591 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004593 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004595 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004597 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004598 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4599 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4600 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4601 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004602 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004603 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4604 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004605 LOG(FATAL) << DebugName() << " is not defined for float values";
4606 UNREACHABLE();
4607 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004608 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4609 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004610 LOG(FATAL) << DebugName() << " is not defined for double values";
4611 UNREACHABLE();
4612 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004613
4614 DECLARE_INSTRUCTION(UShr);
4615
4616 private:
4617 DISALLOW_COPY_AND_ASSIGN(HUShr);
4618};
4619
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004620class HAnd : public HBinaryOperation {
4621 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004622 HAnd(Primitive::Type result_type,
4623 HInstruction* left,
4624 HInstruction* right,
4625 uint32_t dex_pc = kNoDexPc)
4626 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004627
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004628 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004629
Roland Levillaine53bd812016-02-24 14:54:18 +00004630 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004631
4632 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004633 return GetBlock()->GetGraph()->GetIntConstant(
4634 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004635 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004636 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 return GetBlock()->GetGraph()->GetLongConstant(
4638 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004639 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004640 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4641 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4642 LOG(FATAL) << DebugName() << " is not defined for float values";
4643 UNREACHABLE();
4644 }
4645 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4646 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4647 LOG(FATAL) << DebugName() << " is not defined for double values";
4648 UNREACHABLE();
4649 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004650
4651 DECLARE_INSTRUCTION(And);
4652
4653 private:
4654 DISALLOW_COPY_AND_ASSIGN(HAnd);
4655};
4656
4657class HOr : public HBinaryOperation {
4658 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004659 HOr(Primitive::Type result_type,
4660 HInstruction* left,
4661 HInstruction* right,
4662 uint32_t dex_pc = kNoDexPc)
4663 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004664
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004665 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004666
Roland Levillaine53bd812016-02-24 14:54:18 +00004667 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004668
4669 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004670 return GetBlock()->GetGraph()->GetIntConstant(
4671 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004672 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004674 return GetBlock()->GetGraph()->GetLongConstant(
4675 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004676 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004677 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4678 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4679 LOG(FATAL) << DebugName() << " is not defined for float values";
4680 UNREACHABLE();
4681 }
4682 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4683 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4684 LOG(FATAL) << DebugName() << " is not defined for double values";
4685 UNREACHABLE();
4686 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004687
4688 DECLARE_INSTRUCTION(Or);
4689
4690 private:
4691 DISALLOW_COPY_AND_ASSIGN(HOr);
4692};
4693
4694class HXor : public HBinaryOperation {
4695 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004696 HXor(Primitive::Type result_type,
4697 HInstruction* left,
4698 HInstruction* right,
4699 uint32_t dex_pc = kNoDexPc)
4700 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004701
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004702 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004703
Roland Levillaine53bd812016-02-24 14:54:18 +00004704 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004705
4706 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004707 return GetBlock()->GetGraph()->GetIntConstant(
4708 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004709 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004710 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004711 return GetBlock()->GetGraph()->GetLongConstant(
4712 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004713 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004714 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4715 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4716 LOG(FATAL) << DebugName() << " is not defined for float values";
4717 UNREACHABLE();
4718 }
4719 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4720 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4721 LOG(FATAL) << DebugName() << " is not defined for double values";
4722 UNREACHABLE();
4723 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004724
4725 DECLARE_INSTRUCTION(Xor);
4726
4727 private:
4728 DISALLOW_COPY_AND_ASSIGN(HXor);
4729};
4730
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004731class HRor : public HBinaryOperation {
4732 public:
4733 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004734 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004735 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4736 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004737 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004738
Roland Levillain5b5b9312016-03-22 14:57:31 +00004739 template <typename T>
4740 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4741 typedef typename std::make_unsigned<T>::type V;
4742 V ux = static_cast<V>(value);
4743 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004744 return static_cast<T>(ux);
4745 } else {
4746 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004747 return static_cast<T>(ux >> (distance & max_shift_value)) |
4748 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004749 }
4750 }
4751
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004753 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004754 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004755 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004756 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004757 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004759 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4761 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4762 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4763 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004764 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4766 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004767 LOG(FATAL) << DebugName() << " is not defined for float values";
4768 UNREACHABLE();
4769 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4771 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004772 LOG(FATAL) << DebugName() << " is not defined for double values";
4773 UNREACHABLE();
4774 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004775
4776 DECLARE_INSTRUCTION(Ror);
4777
4778 private:
4779 DISALLOW_COPY_AND_ASSIGN(HRor);
4780};
4781
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004782// The value of a parameter in this method. Its location depends on
4783// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004784class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004785 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004786 HParameterValue(const DexFile& dex_file,
4787 uint16_t type_index,
4788 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004789 Primitive::Type parameter_type,
4790 bool is_this = false)
4791 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004792 dex_file_(dex_file),
4793 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004794 index_(index) {
4795 SetPackedFlag<kFlagIsThis>(is_this);
4796 SetPackedFlag<kFlagCanBeNull>(!is_this);
4797 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004798
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004799 const DexFile& GetDexFile() const { return dex_file_; }
4800 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004801 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004802 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004803
Vladimir Markoa1de9182016-02-25 11:37:38 +00004804 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4805 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004806
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004807 DECLARE_INSTRUCTION(ParameterValue);
4808
4809 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004810 // Whether or not the parameter value corresponds to 'this' argument.
4811 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4812 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4813 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4814 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4815 "Too many packed fields.");
4816
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004817 const DexFile& dex_file_;
4818 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004819 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004820 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004821 const uint8_t index_;
4822
4823 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4824};
4825
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004826class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004828 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4829 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004830
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004831 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004832 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004833 return true;
4834 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004835
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 template <typename T> T Compute(T x) const { return ~x; }
4837
4838 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004840 }
4841 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004842 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004843 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004844 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4845 LOG(FATAL) << DebugName() << " is not defined for float values";
4846 UNREACHABLE();
4847 }
4848 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4849 LOG(FATAL) << DebugName() << " is not defined for double values";
4850 UNREACHABLE();
4851 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004852
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004853 DECLARE_INSTRUCTION(Not);
4854
4855 private:
4856 DISALLOW_COPY_AND_ASSIGN(HNot);
4857};
4858
David Brazdil66d126e2015-04-03 16:02:44 +01004859class HBooleanNot : public HUnaryOperation {
4860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004861 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4862 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004863
4864 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004865 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004866 return true;
4867 }
4868
Roland Levillain9867bc72015-08-05 10:21:34 +01004869 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004870 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004871 return !x;
4872 }
4873
Roland Levillain9867bc72015-08-05 10:21:34 +01004874 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004875 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004876 }
4877 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4878 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004879 UNREACHABLE();
4880 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004881 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4882 LOG(FATAL) << DebugName() << " is not defined for float values";
4883 UNREACHABLE();
4884 }
4885 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4886 LOG(FATAL) << DebugName() << " is not defined for double values";
4887 UNREACHABLE();
4888 }
David Brazdil66d126e2015-04-03 16:02:44 +01004889
4890 DECLARE_INSTRUCTION(BooleanNot);
4891
4892 private:
4893 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4894};
4895
Roland Levillaindff1f282014-11-05 14:15:05 +00004896class HTypeConversion : public HExpression<1> {
4897 public:
4898 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004899 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004900 : HExpression(result_type,
4901 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4902 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004903 SetRawInputAt(0, input);
4904 DCHECK_NE(input->GetType(), result_type);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004905 // Invariant: We should never generate a conversion to a Boolean value.
4906 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004907 }
4908
4909 HInstruction* GetInput() const { return InputAt(0); }
4910 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4911 Primitive::Type GetResultType() const { return GetType(); }
4912
4913 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004914 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004915
Mark Mendelle82549b2015-05-06 10:55:34 -04004916 // Try to statically evaluate the conversion and return a HConstant
4917 // containing the result. If the input cannot be converted, return nullptr.
4918 HConstant* TryStaticEvaluation() const;
4919
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004920 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4921 Primitive::Type result_type) {
4922 // Some architectures may not require the 'GC' side effects, but at this point
4923 // in the compilation process we do not know what architecture we will
4924 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004925 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4926 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004927 return SideEffects::CanTriggerGC();
4928 }
4929 return SideEffects::None();
4930 }
4931
Roland Levillaindff1f282014-11-05 14:15:05 +00004932 DECLARE_INSTRUCTION(TypeConversion);
4933
4934 private:
4935 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4936};
4937
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004938static constexpr uint32_t kNoRegNumber = -1;
4939
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004940class HPhi : public HInstruction {
4941 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004942 HPhi(ArenaAllocator* arena,
4943 uint32_t reg_number,
4944 size_t number_of_inputs,
4945 Primitive::Type type,
4946 uint32_t dex_pc = kNoDexPc)
4947 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004948 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004949 reg_number_(reg_number) {
4950 SetPackedField<TypeField>(ToPhiType(type));
4951 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4952 // Phis are constructed live and marked dead if conflicting or unused.
4953 // Individual steps of SsaBuilder should assume that if a phi has been
4954 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4955 SetPackedFlag<kFlagIsLive>(true);
4956 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004957 }
4958
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004959 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4960 static Primitive::Type ToPhiType(Primitive::Type type) {
4961 switch (type) {
4962 case Primitive::kPrimBoolean:
4963 case Primitive::kPrimByte:
4964 case Primitive::kPrimShort:
4965 case Primitive::kPrimChar:
4966 return Primitive::kPrimInt;
4967 default:
4968 return type;
4969 }
4970 }
4971
David Brazdilffee3d32015-07-06 11:48:53 +01004972 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4973
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004974 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004975
4976 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004977 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004978
Vladimir Markoa1de9182016-02-25 11:37:38 +00004979 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004980 void SetType(Primitive::Type new_type) {
4981 // Make sure that only valid type changes occur. The following are allowed:
4982 // (1) int -> float/ref (primitive type propagation),
4983 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004984 DCHECK(GetType() == new_type ||
4985 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4986 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4987 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4988 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004989 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004990
Vladimir Markoa1de9182016-02-25 11:37:38 +00004991 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4992 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004993
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004994 uint32_t GetRegNumber() const { return reg_number_; }
4995
Vladimir Markoa1de9182016-02-25 11:37:38 +00004996 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4997 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4998 bool IsDead() const { return !IsLive(); }
4999 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01005000
David Brazdil77a48ae2015-09-15 12:34:04 +00005001 bool IsVRegEquivalentOf(HInstruction* other) const {
5002 return other != nullptr
5003 && other->IsPhi()
5004 && other->AsPhi()->GetBlock() == GetBlock()
5005 && other->AsPhi()->GetRegNumber() == GetRegNumber();
5006 }
5007
Calin Juravlea4f88312015-04-16 12:57:19 +01005008 // Returns the next equivalent phi (starting from the current one) or null if there is none.
5009 // An equivalent phi is a phi having the same dex register and type.
5010 // It assumes that phis with the same dex register are adjacent.
5011 HPhi* GetNextEquivalentPhiWithSameType() {
5012 HInstruction* next = GetNext();
5013 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
5014 if (next->GetType() == GetType()) {
5015 return next->AsPhi();
5016 }
5017 next = next->GetNext();
5018 }
5019 return nullptr;
5020 }
5021
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005022 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005023
David Brazdil1abb4192015-02-17 18:33:36 +00005024 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005025 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005026 return inputs_[index];
5027 }
David Brazdil1abb4192015-02-17 18:33:36 +00005028
5029 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005030 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00005031 }
5032
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005033 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005034 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
5035 static constexpr size_t kFieldTypeSize =
5036 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5037 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
5038 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
5039 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
5040 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5041 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
5042
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005043 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005044 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005045
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005046 DISALLOW_COPY_AND_ASSIGN(HPhi);
5047};
5048
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005049class HNullCheck : public HExpression<1> {
5050 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005051 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5052 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005054 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055 SetRawInputAt(0, value);
5056 }
5057
Calin Juravle10e244f2015-01-26 18:54:32 +00005058 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005059 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005060 return true;
5061 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005062
Calin Juravle10e244f2015-01-26 18:54:32 +00005063 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005064
Calin Juravle10e244f2015-01-26 18:54:32 +00005065 bool CanThrow() const OVERRIDE { return true; }
5066
5067 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005068
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005069
5070 DECLARE_INSTRUCTION(NullCheck);
5071
5072 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005073 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5074};
5075
5076class FieldInfo : public ValueObject {
5077 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005078 FieldInfo(MemberOffset field_offset,
5079 Primitive::Type field_type,
5080 bool is_volatile,
5081 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005082 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005083 const DexFile& dex_file,
5084 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005085 : field_offset_(field_offset),
5086 field_type_(field_type),
5087 is_volatile_(is_volatile),
5088 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005089 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005090 dex_file_(dex_file),
5091 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005092
5093 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005094 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005095 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005096 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005097 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005098 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005099 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005100
5101 private:
5102 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005103 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005104 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005105 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005106 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005107 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005108 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005109};
5110
5111class HInstanceFieldGet : public HExpression<1> {
5112 public:
5113 HInstanceFieldGet(HInstruction* value,
5114 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005115 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005116 bool is_volatile,
5117 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005118 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005119 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005120 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005121 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005122 : HExpression(field_type,
5123 SideEffects::FieldReadOfType(field_type, is_volatile),
5124 dex_pc),
5125 field_info_(field_offset,
5126 field_type,
5127 is_volatile,
5128 field_idx,
5129 declaring_class_def_index,
5130 dex_file,
5131 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132 SetRawInputAt(0, value);
5133 }
5134
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005135 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005136
5137 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5138 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5139 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005140 }
5141
Calin Juravle641547a2015-04-21 22:08:51 +01005142 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5143 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005144 }
5145
5146 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005147 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5148 }
5149
Calin Juravle52c48962014-12-16 17:02:57 +00005150 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005151 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005152 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005153 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154
5155 DECLARE_INSTRUCTION(InstanceFieldGet);
5156
5157 private:
5158 const FieldInfo field_info_;
5159
5160 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5161};
5162
5163class HInstanceFieldSet : public HTemplateInstruction<2> {
5164 public:
5165 HInstanceFieldSet(HInstruction* object,
5166 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005167 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005168 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005169 bool is_volatile,
5170 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005171 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005172 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005173 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005174 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005175 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5176 dex_pc),
5177 field_info_(field_offset,
5178 field_type,
5179 is_volatile,
5180 field_idx,
5181 declaring_class_def_index,
5182 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005183 dex_cache) {
5184 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185 SetRawInputAt(0, object);
5186 SetRawInputAt(1, value);
5187 }
5188
Calin Juravle641547a2015-04-21 22:08:51 +01005189 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5190 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005191 }
5192
Calin Juravle52c48962014-12-16 17:02:57 +00005193 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005194 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005195 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005196 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005197 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005198 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5199 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005200
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005201 DECLARE_INSTRUCTION(InstanceFieldSet);
5202
5203 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005204 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5205 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5206 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5207 "Too many packed fields.");
5208
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005209 const FieldInfo field_info_;
5210
5211 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5212};
5213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214class HArrayGet : public HExpression<2> {
5215 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005216 HArrayGet(HInstruction* array,
5217 HInstruction* index,
5218 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005219 uint32_t dex_pc,
5220 SideEffects additional_side_effects = SideEffects::None())
5221 : HExpression(type,
5222 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005223 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224 SetRawInputAt(0, array);
5225 SetRawInputAt(1, index);
5226 }
5227
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005228 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005229 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005230 return true;
5231 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005232 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005233 // TODO: We can be smarter here.
5234 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5235 // which generates the implicit null check. There are cases when these can be removed
5236 // to produce better code. If we ever add optimizations to do so we should allow an
5237 // implicit check here (as long as the address falls in the first page).
5238 return false;
5239 }
5240
David Brazdil4833f5a2015-12-16 10:37:39 +00005241 bool IsEquivalentOf(HArrayGet* other) const {
5242 bool result = (GetDexPc() == other->GetDexPc());
5243 if (kIsDebugBuild && result) {
5244 DCHECK_EQ(GetBlock(), other->GetBlock());
5245 DCHECK_EQ(GetArray(), other->GetArray());
5246 DCHECK_EQ(GetIndex(), other->GetIndex());
5247 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005248 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005249 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005250 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5251 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005252 }
5253 }
5254 return result;
5255 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005256
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005257 HInstruction* GetArray() const { return InputAt(0); }
5258 HInstruction* GetIndex() const { return InputAt(1); }
5259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 DECLARE_INSTRUCTION(ArrayGet);
5261
5262 private:
5263 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5264};
5265
5266class HArraySet : public HTemplateInstruction<3> {
5267 public:
5268 HArraySet(HInstruction* array,
5269 HInstruction* index,
5270 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005271 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005272 uint32_t dex_pc,
5273 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005274 : HTemplateInstruction(
5275 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005276 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5277 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005278 dex_pc) {
5279 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5280 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5281 SetPackedFlag<kFlagValueCanBeNull>(true);
5282 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 SetRawInputAt(0, array);
5284 SetRawInputAt(1, index);
5285 SetRawInputAt(2, value);
5286 }
5287
Calin Juravle77520bc2015-01-12 18:45:46 +00005288 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005289 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005290 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291 }
5292
Mingyao Yang81014cb2015-06-02 03:16:27 -07005293 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005294 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005295
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005296 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005297 // TODO: Same as for ArrayGet.
5298 return false;
5299 }
5300
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005301 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005302 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005303 }
5304
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005305 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005306 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005307 }
5308
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005309 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005310 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 }
5312
Vladimir Markoa1de9182016-02-25 11:37:38 +00005313 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5314 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5315 bool StaticTypeOfArrayIsObjectArray() const {
5316 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5317 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005318
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005319 HInstruction* GetArray() const { return InputAt(0); }
5320 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005321 HInstruction* GetValue() const { return InputAt(2); }
5322
5323 Primitive::Type GetComponentType() const {
5324 // The Dex format does not type floating point index operations. Since the
5325 // `expected_component_type_` is set during building and can therefore not
5326 // be correct, we also check what is the value type. If it is a floating
5327 // point type, we must use that type.
5328 Primitive::Type value_type = GetValue()->GetType();
5329 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5330 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005331 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005332 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005333
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005334 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005335 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005336 }
5337
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005338 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5339 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5340 }
5341
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342 DECLARE_INSTRUCTION(ArraySet);
5343
5344 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005345 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5346 static constexpr size_t kFieldExpectedComponentTypeSize =
5347 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5348 static constexpr size_t kFlagNeedsTypeCheck =
5349 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5350 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005351 // Cached information for the reference_type_info_ so that codegen
5352 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005353 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5354 static constexpr size_t kNumberOfArraySetPackedBits =
5355 kFlagStaticTypeOfArrayIsObjectArray + 1;
5356 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5357 using ExpectedComponentTypeField =
5358 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359
5360 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5361};
5362
5363class HArrayLength : public HExpression<1> {
5364 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005365 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005366 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005367 // Note that arrays do not change length, so the instruction does not
5368 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369 SetRawInputAt(0, array);
5370 }
5371
Calin Juravle77520bc2015-01-12 18:45:46 +00005372 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005373 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005374 return true;
5375 }
Calin Juravle641547a2015-04-21 22:08:51 +01005376 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5377 return obj == InputAt(0);
5378 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005379
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005380 DECLARE_INSTRUCTION(ArrayLength);
5381
5382 private:
5383 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5384};
5385
5386class HBoundsCheck : public HExpression<2> {
5387 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005388 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5389 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005391 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392 DCHECK(index->GetType() == Primitive::kPrimInt);
5393 SetRawInputAt(0, index);
5394 SetRawInputAt(1, length);
5395 }
5396
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005397 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005398 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005399 return true;
5400 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005402 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005404 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005405
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005406 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407
5408 DECLARE_INSTRUCTION(BoundsCheck);
5409
5410 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005411 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5412};
5413
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005414class HSuspendCheck : public HTemplateInstruction<0> {
5415 public:
5416 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005417 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005418
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005419 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005420 return true;
5421 }
5422
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005423 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5424 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005425
5426 DECLARE_INSTRUCTION(SuspendCheck);
5427
5428 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005429 // Only used for code generation, in order to share the same slow path between back edges
5430 // of a same loop.
5431 SlowPathCode* slow_path_;
5432
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005433 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5434};
5435
David Srbecky0cf44932015-12-09 14:09:59 +00005436// Pseudo-instruction which provides the native debugger with mapping information.
5437// It ensures that we can generate line number and local variables at this point.
5438class HNativeDebugInfo : public HTemplateInstruction<0> {
5439 public:
5440 explicit HNativeDebugInfo(uint32_t dex_pc)
5441 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5442
5443 bool NeedsEnvironment() const OVERRIDE {
5444 return true;
5445 }
5446
5447 DECLARE_INSTRUCTION(NativeDebugInfo);
5448
5449 private:
5450 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5451};
5452
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005453/**
5454 * Instruction to load a Class object.
5455 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005456class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005457 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005458 HLoadClass(HCurrentMethod* current_method,
5459 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005460 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005461 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005462 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005463 bool needs_access_check,
5464 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005465 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005466 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005467 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005468 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005469 // Referrers class should not need access check. We never inline unverified
5470 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005471 DCHECK(!is_referrers_class || !needs_access_check);
5472
5473 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5474 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5475 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5476 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005477 SetRawInputAt(0, current_method);
5478 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005479
5480 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005481
5482 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005483 // Note that we don't need to test for generate_clinit_check_.
5484 // Whether or not we need to generate the clinit check is processed in
5485 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005486 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005487 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005488 }
5489
5490 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5491
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005492 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005493 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005494
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005495 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005496 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005497 }
5498
Calin Juravle0ba218d2015-05-19 18:46:01 +01005499 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005500 // The entrypoint the code generator is going to call does not do
5501 // clinit of the class.
5502 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005503 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005504 }
5505
5506 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005507 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005508 (!IsReferrersClass() && !IsInDexCache()) ||
5509 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005510 }
5511
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005512
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005513 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005514 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005515 }
5516
Calin Juravleacf735c2015-02-12 15:25:22 +00005517 ReferenceTypeInfo GetLoadedClassRTI() {
5518 return loaded_class_rti_;
5519 }
5520
5521 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5522 // Make sure we only set exact types (the loaded class should never be merged).
5523 DCHECK(rti.IsExact());
5524 loaded_class_rti_ = rti;
5525 }
5526
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005527 const DexFile& GetDexFile() { return dex_file_; }
5528
Vladimir Markoa1de9182016-02-25 11:37:38 +00005529 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005530
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005531 static SideEffects SideEffectsForArchRuntimeCalls() {
5532 return SideEffects::CanTriggerGC();
5533 }
5534
Vladimir Markoa1de9182016-02-25 11:37:38 +00005535 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5536 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5537 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5538 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005539
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005540 DECLARE_INSTRUCTION(LoadClass);
5541
5542 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005543 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5544 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5545 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005546 // Whether this instruction must generate the initialization check.
5547 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005548 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5549 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5550 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5551
5552 const uint16_t type_index_;
5553 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005554
Calin Juravleacf735c2015-02-12 15:25:22 +00005555 ReferenceTypeInfo loaded_class_rti_;
5556
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005557 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5558};
5559
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005560class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005561 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005562 HLoadString(HCurrentMethod* current_method,
5563 uint32_t string_index,
5564 uint32_t dex_pc,
5565 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005566 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005567 string_index_(string_index) {
5568 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005569 SetRawInputAt(0, current_method);
5570 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005571
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005572 bool CanBeMoved() const OVERRIDE { return true; }
5573
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005574 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5575 return other->AsLoadString()->string_index_ == string_index_;
5576 }
5577
5578 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5579
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005580 uint32_t GetStringIndex() const { return string_index_; }
5581
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005582 // Will call the runtime if the string is not already in the dex cache.
5583 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5584
Vladimir Markodc151b22015-10-15 18:02:30 +01005585 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005586 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005587 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005588
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005589 static SideEffects SideEffectsForArchRuntimeCalls() {
5590 return SideEffects::CanTriggerGC();
5591 }
5592
Vladimir Markoa1de9182016-02-25 11:37:38 +00005593 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5594
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005595 DECLARE_INSTRUCTION(LoadString);
5596
5597 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005598 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5599 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5600 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5601
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005602 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005603
5604 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5605};
5606
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607/**
5608 * Performs an initialization check on its Class object input.
5609 */
5610class HClinitCheck : public HExpression<1> {
5611 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005612 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005613 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005614 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005615 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5616 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005617 SetRawInputAt(0, constant);
5618 }
5619
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005620 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005621 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005622 return true;
5623 }
5624
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005625 bool NeedsEnvironment() const OVERRIDE {
5626 // May call runtime to initialize the class.
5627 return true;
5628 }
5629
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005630 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005631
5632 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5633
5634 DECLARE_INSTRUCTION(ClinitCheck);
5635
5636 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005637 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5638};
5639
5640class HStaticFieldGet : public HExpression<1> {
5641 public:
5642 HStaticFieldGet(HInstruction* cls,
5643 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005644 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005645 bool is_volatile,
5646 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005647 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005648 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005649 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005650 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005651 : HExpression(field_type,
5652 SideEffects::FieldReadOfType(field_type, is_volatile),
5653 dex_pc),
5654 field_info_(field_offset,
5655 field_type,
5656 is_volatile,
5657 field_idx,
5658 declaring_class_def_index,
5659 dex_file,
5660 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005661 SetRawInputAt(0, cls);
5662 }
5663
Calin Juravle52c48962014-12-16 17:02:57 +00005664
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005665 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005666
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005667 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005668 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5669 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005670 }
5671
5672 size_t ComputeHashCode() const OVERRIDE {
5673 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5674 }
5675
Calin Juravle52c48962014-12-16 17:02:57 +00005676 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005677 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5678 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005679 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005680
5681 DECLARE_INSTRUCTION(StaticFieldGet);
5682
5683 private:
5684 const FieldInfo field_info_;
5685
5686 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5687};
5688
5689class HStaticFieldSet : public HTemplateInstruction<2> {
5690 public:
5691 HStaticFieldSet(HInstruction* cls,
5692 HInstruction* value,
5693 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005694 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005695 bool is_volatile,
5696 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005697 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005698 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005699 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005700 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005701 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5702 dex_pc),
5703 field_info_(field_offset,
5704 field_type,
5705 is_volatile,
5706 field_idx,
5707 declaring_class_def_index,
5708 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005709 dex_cache) {
5710 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711 SetRawInputAt(0, cls);
5712 SetRawInputAt(1, value);
5713 }
5714
Calin Juravle52c48962014-12-16 17:02:57 +00005715 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005716 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5717 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005718 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005719
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005720 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005721 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5722 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005723
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005724 DECLARE_INSTRUCTION(StaticFieldSet);
5725
5726 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005727 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5728 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5729 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5730 "Too many packed fields.");
5731
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005732 const FieldInfo field_info_;
5733
5734 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5735};
5736
Calin Juravlee460d1d2015-09-29 04:52:17 +01005737class HUnresolvedInstanceFieldGet : public HExpression<1> {
5738 public:
5739 HUnresolvedInstanceFieldGet(HInstruction* obj,
5740 Primitive::Type field_type,
5741 uint32_t field_index,
5742 uint32_t dex_pc)
5743 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5744 field_index_(field_index) {
5745 SetRawInputAt(0, obj);
5746 }
5747
5748 bool NeedsEnvironment() const OVERRIDE { return true; }
5749 bool CanThrow() const OVERRIDE { return true; }
5750
5751 Primitive::Type GetFieldType() const { return GetType(); }
5752 uint32_t GetFieldIndex() const { return field_index_; }
5753
5754 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5755
5756 private:
5757 const uint32_t field_index_;
5758
5759 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5760};
5761
5762class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5763 public:
5764 HUnresolvedInstanceFieldSet(HInstruction* obj,
5765 HInstruction* value,
5766 Primitive::Type field_type,
5767 uint32_t field_index,
5768 uint32_t dex_pc)
5769 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005770 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005771 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005772 DCHECK_EQ(field_type, value->GetType());
5773 SetRawInputAt(0, obj);
5774 SetRawInputAt(1, value);
5775 }
5776
5777 bool NeedsEnvironment() const OVERRIDE { return true; }
5778 bool CanThrow() const OVERRIDE { return true; }
5779
Vladimir Markoa1de9182016-02-25 11:37:38 +00005780 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005781 uint32_t GetFieldIndex() const { return field_index_; }
5782
5783 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5784
5785 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005786 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5787 static constexpr size_t kFieldFieldTypeSize =
5788 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5789 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5790 kFieldFieldType + kFieldFieldTypeSize;
5791 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5792 "Too many packed fields.");
5793 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5794
Calin Juravlee460d1d2015-09-29 04:52:17 +01005795 const uint32_t field_index_;
5796
5797 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5798};
5799
5800class HUnresolvedStaticFieldGet : public HExpression<0> {
5801 public:
5802 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5803 uint32_t field_index,
5804 uint32_t dex_pc)
5805 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5806 field_index_(field_index) {
5807 }
5808
5809 bool NeedsEnvironment() const OVERRIDE { return true; }
5810 bool CanThrow() const OVERRIDE { return true; }
5811
5812 Primitive::Type GetFieldType() const { return GetType(); }
5813 uint32_t GetFieldIndex() const { return field_index_; }
5814
5815 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5816
5817 private:
5818 const uint32_t field_index_;
5819
5820 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5821};
5822
5823class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5824 public:
5825 HUnresolvedStaticFieldSet(HInstruction* value,
5826 Primitive::Type field_type,
5827 uint32_t field_index,
5828 uint32_t dex_pc)
5829 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005830 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005831 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005832 DCHECK_EQ(field_type, value->GetType());
5833 SetRawInputAt(0, value);
5834 }
5835
5836 bool NeedsEnvironment() const OVERRIDE { return true; }
5837 bool CanThrow() const OVERRIDE { return true; }
5838
Vladimir Markoa1de9182016-02-25 11:37:38 +00005839 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005840 uint32_t GetFieldIndex() const { return field_index_; }
5841
5842 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5843
5844 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005845 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5846 static constexpr size_t kFieldFieldTypeSize =
5847 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5848 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5849 kFieldFieldType + kFieldFieldTypeSize;
5850 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5851 "Too many packed fields.");
5852 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5853
Calin Juravlee460d1d2015-09-29 04:52:17 +01005854 const uint32_t field_index_;
5855
5856 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5857};
5858
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005859// Implement the move-exception DEX instruction.
5860class HLoadException : public HExpression<0> {
5861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005862 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5863 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005864
David Brazdilbbd733e2015-08-18 17:48:17 +01005865 bool CanBeNull() const OVERRIDE { return false; }
5866
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005867 DECLARE_INSTRUCTION(LoadException);
5868
5869 private:
5870 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5871};
5872
David Brazdilcb1c0552015-08-04 16:22:25 +01005873// Implicit part of move-exception which clears thread-local exception storage.
5874// Must not be removed because the runtime expects the TLS to get cleared.
5875class HClearException : public HTemplateInstruction<0> {
5876 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005877 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5878 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005879
5880 DECLARE_INSTRUCTION(ClearException);
5881
5882 private:
5883 DISALLOW_COPY_AND_ASSIGN(HClearException);
5884};
5885
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005886class HThrow : public HTemplateInstruction<1> {
5887 public:
5888 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005889 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005890 SetRawInputAt(0, exception);
5891 }
5892
5893 bool IsControlFlow() const OVERRIDE { return true; }
5894
5895 bool NeedsEnvironment() const OVERRIDE { return true; }
5896
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005897 bool CanThrow() const OVERRIDE { return true; }
5898
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005899
5900 DECLARE_INSTRUCTION(Throw);
5901
5902 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005903 DISALLOW_COPY_AND_ASSIGN(HThrow);
5904};
5905
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906/**
5907 * Implementation strategies for the code generator of a HInstanceOf
5908 * or `HCheckCast`.
5909 */
5910enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005911 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 kExactCheck, // Can do a single class compare.
5913 kClassHierarchyCheck, // Can just walk the super class chain.
5914 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5915 kInterfaceCheck, // No optimization yet when checking against an interface.
5916 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005917 kArrayCheck, // No optimization yet when checking against a generic array.
5918 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005919};
5920
Roland Levillain86503782016-02-11 19:07:30 +00005921std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5922
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005923class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005924 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005925 HInstanceOf(HInstruction* object,
5926 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005927 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005928 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005929 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005931 dex_pc) {
5932 SetPackedField<TypeCheckKindField>(check_kind);
5933 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005934 SetRawInputAt(0, object);
5935 SetRawInputAt(1, constant);
5936 }
5937
5938 bool CanBeMoved() const OVERRIDE { return true; }
5939
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005940 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005941 return true;
5942 }
5943
5944 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005945 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005946 }
5947
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005948 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005949 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5950 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5951 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5952 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005953
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005954 static bool CanCallRuntime(TypeCheckKind check_kind) {
5955 // Mips currently does runtime calls for any other checks.
5956 return check_kind != TypeCheckKind::kExactCheck;
5957 }
5958
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005960 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005961 }
5962
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005963 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005964
5965 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005966 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5967 static constexpr size_t kFieldTypeCheckKindSize =
5968 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5969 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5970 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5971 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5972 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005973
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005974 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5975};
5976
Calin Juravleb1498f62015-02-16 13:13:29 +00005977class HBoundType : public HExpression<1> {
5978 public:
David Brazdilf5552582015-12-27 13:36:12 +00005979 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005980 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005981 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5982 SetPackedFlag<kFlagUpperCanBeNull>(true);
5983 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005984 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005985 SetRawInputAt(0, input);
5986 }
5987
David Brazdilf5552582015-12-27 13:36:12 +00005988 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005989 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005990 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005991 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005992
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005993 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005994 DCHECK(GetUpperCanBeNull() || !can_be_null);
5995 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005996 }
5997
Vladimir Markoa1de9182016-02-25 11:37:38 +00005998 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005999
Calin Juravleb1498f62015-02-16 13:13:29 +00006000 DECLARE_INSTRUCTION(BoundType);
6001
6002 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6004 // is false then CanBeNull() cannot be true).
6005 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6006 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6007 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6008 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6009
Calin Juravleb1498f62015-02-16 13:13:29 +00006010 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006011 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6012 // It is used to bound the type in cases like:
6013 // if (x instanceof ClassX) {
6014 // // uper_bound_ will be ClassX
6015 // }
David Brazdilf5552582015-12-27 13:36:12 +00006016 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006017
6018 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6019};
6020
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006021class HCheckCast : public HTemplateInstruction<2> {
6022 public:
6023 HCheckCast(HInstruction* object,
6024 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006026 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006027 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6028 SetPackedField<TypeCheckKindField>(check_kind);
6029 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006030 SetRawInputAt(0, object);
6031 SetRawInputAt(1, constant);
6032 }
6033
6034 bool CanBeMoved() const OVERRIDE { return true; }
6035
6036 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6037 return true;
6038 }
6039
6040 bool NeedsEnvironment() const OVERRIDE {
6041 // Instruction may throw a CheckCastError.
6042 return true;
6043 }
6044
6045 bool CanThrow() const OVERRIDE { return true; }
6046
Vladimir Markoa1de9182016-02-25 11:37:38 +00006047 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6048 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6049 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6050 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006051
6052 DECLARE_INSTRUCTION(CheckCast);
6053
6054 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006055 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6056 static constexpr size_t kFieldTypeCheckKindSize =
6057 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6058 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6059 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6060 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6061 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006062
6063 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006064};
6065
Calin Juravle27df7582015-04-17 19:12:31 +01006066class HMemoryBarrier : public HTemplateInstruction<0> {
6067 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006068 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006069 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6071 SetPackedField<BarrierKindField>(barrier_kind);
6072 }
Calin Juravle27df7582015-04-17 19:12:31 +01006073
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006075
6076 DECLARE_INSTRUCTION(MemoryBarrier);
6077
6078 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006079 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6080 static constexpr size_t kFieldBarrierKindSize =
6081 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6082 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6083 kFieldBarrierKind + kFieldBarrierKindSize;
6084 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6085 "Too many packed fields.");
6086 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006087
6088 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6089};
6090
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006091class HMonitorOperation : public HTemplateInstruction<1> {
6092 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006094 kEnter,
6095 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006096 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006097 };
6098
6099 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006100 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006101 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6102 dex_pc) {
6103 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006104 SetRawInputAt(0, object);
6105 }
6106
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006107 // Instruction may go into runtime, so we need an environment.
6108 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006109
6110 bool CanThrow() const OVERRIDE {
6111 // Verifier guarantees that monitor-exit cannot throw.
6112 // This is important because it allows the HGraphBuilder to remove
6113 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6114 return IsEnter();
6115 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006116
Vladimir Markoa1de9182016-02-25 11:37:38 +00006117 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6118 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006119
6120 DECLARE_INSTRUCTION(MonitorOperation);
6121
Calin Juravle52c48962014-12-16 17:02:57 +00006122 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006123 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6124 static constexpr size_t kFieldOperationKindSize =
6125 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6126 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6127 kFieldOperationKind + kFieldOperationKindSize;
6128 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6129 "Too many packed fields.");
6130 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006131
6132 private:
6133 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6134};
6135
David Brazdil74eb1b22015-12-14 11:44:01 +00006136class HSelect : public HExpression<3> {
6137 public:
6138 HSelect(HInstruction* condition,
6139 HInstruction* true_value,
6140 HInstruction* false_value,
6141 uint32_t dex_pc)
6142 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6143 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6144
6145 // First input must be `true_value` or `false_value` to allow codegens to
6146 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6147 // that architectures which implement HSelect as a conditional move also
6148 // will not need to invert the condition.
6149 SetRawInputAt(0, false_value);
6150 SetRawInputAt(1, true_value);
6151 SetRawInputAt(2, condition);
6152 }
6153
6154 HInstruction* GetFalseValue() const { return InputAt(0); }
6155 HInstruction* GetTrueValue() const { return InputAt(1); }
6156 HInstruction* GetCondition() const { return InputAt(2); }
6157
6158 bool CanBeMoved() const OVERRIDE { return true; }
6159 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6160
6161 bool CanBeNull() const OVERRIDE {
6162 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6163 }
6164
6165 DECLARE_INSTRUCTION(Select);
6166
6167 private:
6168 DISALLOW_COPY_AND_ASSIGN(HSelect);
6169};
6170
Vladimir Markof9f64412015-09-02 14:05:49 +01006171class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006172 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006173 MoveOperands(Location source,
6174 Location destination,
6175 Primitive::Type type,
6176 HInstruction* instruction)
6177 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006178
6179 Location GetSource() const { return source_; }
6180 Location GetDestination() const { return destination_; }
6181
6182 void SetSource(Location value) { source_ = value; }
6183 void SetDestination(Location value) { destination_ = value; }
6184
6185 // The parallel move resolver marks moves as "in-progress" by clearing the
6186 // destination (but not the source).
6187 Location MarkPending() {
6188 DCHECK(!IsPending());
6189 Location dest = destination_;
6190 destination_ = Location::NoLocation();
6191 return dest;
6192 }
6193
6194 void ClearPending(Location dest) {
6195 DCHECK(IsPending());
6196 destination_ = dest;
6197 }
6198
6199 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006200 DCHECK(source_.IsValid() || destination_.IsInvalid());
6201 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006202 }
6203
6204 // True if this blocks a move from the given location.
6205 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006206 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006207 }
6208
6209 // A move is redundant if it's been eliminated, if its source and
6210 // destination are the same, or if its destination is unneeded.
6211 bool IsRedundant() const {
6212 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6213 }
6214
6215 // We clear both operands to indicate move that's been eliminated.
6216 void Eliminate() {
6217 source_ = destination_ = Location::NoLocation();
6218 }
6219
6220 bool IsEliminated() const {
6221 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6222 return source_.IsInvalid();
6223 }
6224
Alexey Frunze4dda3372015-06-01 18:31:49 -07006225 Primitive::Type GetType() const { return type_; }
6226
Nicolas Geoffray90218252015-04-15 11:56:51 +01006227 bool Is64BitMove() const {
6228 return Primitive::Is64BitType(type_);
6229 }
6230
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006231 HInstruction* GetInstruction() const { return instruction_; }
6232
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006233 private:
6234 Location source_;
6235 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006236 // The type this move is for.
6237 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006238 // The instruction this move is assocatied with. Null when this move is
6239 // for moving an input in the expected locations of user (including a phi user).
6240 // This is only used in debug mode, to ensure we do not connect interval siblings
6241 // in the same parallel move.
6242 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006243};
6244
Roland Levillainc9285912015-12-18 10:38:42 +00006245std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6246
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006247static constexpr size_t kDefaultNumberOfMoves = 4;
6248
6249class HParallelMove : public HTemplateInstruction<0> {
6250 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006251 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006252 : HTemplateInstruction(SideEffects::None(), dex_pc),
6253 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6254 moves_.reserve(kDefaultNumberOfMoves);
6255 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006256
Nicolas Geoffray90218252015-04-15 11:56:51 +01006257 void AddMove(Location source,
6258 Location destination,
6259 Primitive::Type type,
6260 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006261 DCHECK(source.IsValid());
6262 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006263 if (kIsDebugBuild) {
6264 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006265 for (const MoveOperands& move : moves_) {
6266 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006267 // Special case the situation where the move is for the spill slot
6268 // of the instruction.
6269 if ((GetPrevious() == instruction)
6270 || ((GetPrevious() == nullptr)
6271 && instruction->IsPhi()
6272 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006273 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006274 << "Doing parallel moves for the same instruction.";
6275 } else {
6276 DCHECK(false) << "Doing parallel moves for the same instruction.";
6277 }
6278 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006279 }
6280 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006281 for (const MoveOperands& move : moves_) {
6282 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006283 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006284 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006285 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006286 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006287 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006288 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006289 }
6290
Vladimir Marko225b6462015-09-28 12:17:40 +01006291 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006292 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006293 }
6294
Vladimir Marko225b6462015-09-28 12:17:40 +01006295 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006296
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006297 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006298
6299 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006300 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006301
6302 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6303};
6304
Mark Mendell0616ae02015-04-17 12:49:27 -04006305} // namespace art
6306
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006307#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6308#include "nodes_shared.h"
6309#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006310#ifdef ART_ENABLE_CODEGEN_arm
6311#include "nodes_arm.h"
6312#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006313#ifdef ART_ENABLE_CODEGEN_arm64
6314#include "nodes_arm64.h"
6315#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006316#ifdef ART_ENABLE_CODEGEN_x86
6317#include "nodes_x86.h"
6318#endif
6319
6320namespace art {
6321
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006322class HGraphVisitor : public ValueObject {
6323 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006324 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6325 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006326
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006327 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006328 virtual void VisitBasicBlock(HBasicBlock* block);
6329
Roland Levillain633021e2014-10-01 14:12:25 +01006330 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006331 void VisitInsertionOrder();
6332
Roland Levillain633021e2014-10-01 14:12:25 +01006333 // Visit the graph following dominator tree reverse post-order.
6334 void VisitReversePostOrder();
6335
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006336 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006337
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006338 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006339#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006340 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6341
6342 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6343
6344#undef DECLARE_VISIT_INSTRUCTION
6345
6346 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006347 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006348
6349 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6350};
6351
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006352class HGraphDelegateVisitor : public HGraphVisitor {
6353 public:
6354 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6355 virtual ~HGraphDelegateVisitor() {}
6356
6357 // Visit functions that delegate to to super class.
6358#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006359 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006360
6361 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6362
6363#undef DECLARE_VISIT_INSTRUCTION
6364
6365 private:
6366 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6367};
6368
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006369class HInsertionOrderIterator : public ValueObject {
6370 public:
6371 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6372
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006373 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006374 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006375 void Advance() { ++index_; }
6376
6377 private:
6378 const HGraph& graph_;
6379 size_t index_;
6380
6381 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6382};
6383
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006384class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006385 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006386 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6387 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006388 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006389 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006390
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006391 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6392 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006393 void Advance() { ++index_; }
6394
6395 private:
6396 const HGraph& graph_;
6397 size_t index_;
6398
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006399 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006400};
6401
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006402class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006403 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006404 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006405 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006406 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006407 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006408 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006409
6410 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006411 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006412 void Advance() { --index_; }
6413
6414 private:
6415 const HGraph& graph_;
6416 size_t index_;
6417
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006418 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006419};
6420
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006421class HLinearPostOrderIterator : public ValueObject {
6422 public:
6423 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006424 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006425
6426 bool Done() const { return index_ == 0; }
6427
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006428 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006429
6430 void Advance() {
6431 --index_;
6432 DCHECK_GE(index_, 0U);
6433 }
6434
6435 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006436 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006437 size_t index_;
6438
6439 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6440};
6441
6442class HLinearOrderIterator : public ValueObject {
6443 public:
6444 explicit HLinearOrderIterator(const HGraph& graph)
6445 : order_(graph.GetLinearOrder()), index_(0) {}
6446
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006447 bool Done() const { return index_ == order_.size(); }
6448 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006449 void Advance() { ++index_; }
6450
6451 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006452 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006453 size_t index_;
6454
6455 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6456};
6457
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006458// Iterator over the blocks that art part of the loop. Includes blocks part
6459// of an inner loop. The order in which the blocks are iterated is on their
6460// block id.
6461class HBlocksInLoopIterator : public ValueObject {
6462 public:
6463 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6464 : blocks_in_loop_(info.GetBlocks()),
6465 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6466 index_(0) {
6467 if (!blocks_in_loop_.IsBitSet(index_)) {
6468 Advance();
6469 }
6470 }
6471
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006472 bool Done() const { return index_ == blocks_.size(); }
6473 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006474 void Advance() {
6475 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006476 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006477 if (blocks_in_loop_.IsBitSet(index_)) {
6478 break;
6479 }
6480 }
6481 }
6482
6483 private:
6484 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006485 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006486 size_t index_;
6487
6488 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6489};
6490
Mingyao Yang3584bce2015-05-19 16:01:59 -07006491// Iterator over the blocks that art part of the loop. Includes blocks part
6492// of an inner loop. The order in which the blocks are iterated is reverse
6493// post order.
6494class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6495 public:
6496 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6497 : blocks_in_loop_(info.GetBlocks()),
6498 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6499 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006500 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006501 Advance();
6502 }
6503 }
6504
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006505 bool Done() const { return index_ == blocks_.size(); }
6506 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006507 void Advance() {
6508 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006509 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6510 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006511 break;
6512 }
6513 }
6514 }
6515
6516 private:
6517 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006518 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006519 size_t index_;
6520
6521 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6522};
6523
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006524inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006525 if (constant->IsIntConstant()) {
6526 return constant->AsIntConstant()->GetValue();
6527 } else if (constant->IsLongConstant()) {
6528 return constant->AsLongConstant()->GetValue();
6529 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006530 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006531 return 0;
6532 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006533}
6534
Vladimir Marko58155012015-08-19 12:49:41 +00006535inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6536 // For the purposes of the compiler, the dex files must actually be the same object
6537 // if we want to safely treat them as the same. This is especially important for JIT
6538 // as custom class loaders can open the same underlying file (or memory) multiple
6539 // times and provide different class resolution but no two class loaders should ever
6540 // use the same DexFile object - doing so is an unsupported hack that can lead to
6541 // all sorts of weird failures.
6542 return &lhs == &rhs;
6543}
6544
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006545#define INSTRUCTION_TYPE_CHECK(type, super) \
6546 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6547 inline const H##type* HInstruction::As##type() const { \
6548 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6549 } \
6550 inline H##type* HInstruction::As##type() { \
6551 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6552 }
6553
6554 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6555#undef INSTRUCTION_TYPE_CHECK
6556
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006557class SwitchTable : public ValueObject {
6558 public:
6559 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6560 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6561 int32_t table_offset = instruction.VRegB_31t();
6562 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6563 if (sparse) {
6564 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6565 } else {
6566 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6567 }
6568 num_entries_ = table[1];
6569 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6570 }
6571
6572 uint16_t GetNumEntries() const {
6573 return num_entries_;
6574 }
6575
6576 void CheckIndex(size_t index) const {
6577 if (sparse_) {
6578 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6579 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6580 } else {
6581 // In a packed table, we have the starting key and num_entries_ values.
6582 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6583 }
6584 }
6585
6586 int32_t GetEntryAt(size_t index) const {
6587 CheckIndex(index);
6588 return values_[index];
6589 }
6590
6591 uint32_t GetDexPcForIndex(size_t index) const {
6592 CheckIndex(index);
6593 return dex_pc_ +
6594 (reinterpret_cast<const int16_t*>(values_ + index) -
6595 reinterpret_cast<const int16_t*>(&instruction_));
6596 }
6597
6598 // Index of the first value in the table.
6599 size_t GetFirstValueIndex() const {
6600 if (sparse_) {
6601 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6602 return num_entries_;
6603 } else {
6604 // In a packed table, we have the starting key and num_entries_ values.
6605 return 1;
6606 }
6607 }
6608
6609 private:
6610 const Instruction& instruction_;
6611 const uint32_t dex_pc_;
6612
6613 // Whether this is a sparse-switch table (or a packed-switch one).
6614 const bool sparse_;
6615
6616 // This can't be const as it needs to be computed off of the given instruction, and complicated
6617 // expressions in the initializer list seemed very ugly.
6618 uint16_t num_entries_;
6619
6620 const int32_t* values_;
6621
6622 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6623};
6624
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006625// Create space in `blocks` for adding `number_of_new_blocks` entries
6626// starting at location `at`. Blocks after `at` are moved accordingly.
6627inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6628 size_t number_of_new_blocks,
6629 size_t after) {
6630 DCHECK_LT(after, blocks->size());
6631 size_t old_size = blocks->size();
6632 size_t new_size = old_size + number_of_new_blocks;
6633 blocks->resize(new_size);
6634 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6635}
6636
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006637} // namespace art
6638
6639#endif // ART_COMPILER_OPTIMIZING_NODES_H_